Initial implementation of the new abstraction architecture.
[tagua/yd.git] / src / variants / xchess / piece.cpp
blobefde50fae89d11af695d7e1f6c65db405e5b3b5f
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #include "piece.h"
13 ChessPiece::ChessPiece(ChessPiece::Color color, ChessPiece::Type type)
14 : m_color(color)
15 , m_type(type) { }
17 ChessPiece::ChessPiece(const ChessPiece& other)
18 : m_color(other.color())
19 , m_type(other.type()) { }
21 ChessPiece& ChessPiece::operator=(const ChessPiece& other) {
22 m_color = other.m_color;
23 m_type = other.m_type;
24 return *this;
27 ChessPiece* ChessPiece::clone() const {
28 return new ChessPiece(*this);
31 bool ChessPiece::equals(const ChessPiece* other) const {
32 if (other) {
33 if (!valid())
34 return false;
35 else
36 return m_color == other->color() && m_type == other->type();
38 else
39 return !valid();
42 bool ChessPiece::operator==(const ChessPiece& other) const {
43 if (other.valid())
44 return equals(&other);
45 else
46 return equals(0);
49 bool ChessPiece::sameColor(const ChessPiece* other) const {
50 return other && (m_color == other->color());
53 int ChessPiece::id() const {
54 int res = static_cast<int>(type());
55 if (color() == BLACK) res = -res - 1;
56 return res;
59 ChessPiece::Color ChessPiece::colorFromId(int x) {
60 return x < 0 ? BLACK : WHITE;
63 ChessPiece::Type ChessPiece::typeFromId(int x) {
64 return static_cast<Type>(x<0 ? (-1-x) : x);
67 QString ChessPiece::typeName() const {
68 switch (m_type) {
69 case ROOK:
70 return "rook";
71 case BISHOP:
72 return "bishop";
73 case KNIGHT:
74 return "knight";
75 case QUEEN:
76 return "queen";
77 case KING:
78 return "king";
79 case PAWN:
80 return "pawn";
81 default:
82 return "unknown";
86 QString ChessPiece::name() const {
87 switch(m_color) {
88 case WHITE:
89 switch (m_type) {
90 case ROOK:
91 return "white_rook";
92 case BISHOP:
93 return "white_bishop";
94 case KNIGHT:
95 return "white_knight";
96 case QUEEN:
97 return "white_queen";
98 case KING:
99 return "white_king";
100 case PAWN:
101 return "white_pawn";
102 default:
103 return "unknown";
105 case BLACK:
106 switch (m_type) {
107 case ROOK:
108 return "black_rook";
109 case BISHOP:
110 return "black_bishop";
111 case KNIGHT:
112 return "black_knight";
113 case QUEEN:
114 return "black_queen";
115 case KING:
116 return "black_king";
117 case PAWN:
118 return "black_pawn";
119 default:
120 return "unknown";
122 default:
123 return "unknown";
127 ChessPiece::Type ChessPiece::getType(const QString& str) {
128 // assume the string is well formatted
129 if (str.isEmpty())
130 return PAWN;
132 char c = str[0].toAscii();
133 switch (c) {
134 case 'Q':
135 case 'q':
136 return QUEEN;
137 case 'N':
138 case 'n':
139 return KNIGHT;
140 case 'B':
141 case 'b':
142 return BISHOP;
143 case 'K':
144 case 'k':
145 return KING;
146 case 'R':
147 case 'r':
148 return ROOK;
149 case 'P':
150 case 'p':
151 return PAWN;
152 default:
153 return INVALID_TYPE;
157 QString ChessPiece::typeSymbol(ChessPiece::Type type) {
158 switch (type) {
159 case QUEEN:
160 return "Q";
161 case KING:
162 return "K";
163 case KNIGHT:
164 return "N";
165 case BISHOP:
166 return "B";
167 case ROOK:
168 return "R";
169 case PAWN:
170 return "P";
171 default:
172 return "?";
176 std::ostream& operator<<(std::ostream& os, const ChessPiece& p) {
177 return os << (p.color() == WHITE? "white" : p.color() == BLACK? "black" : "unknown")
178 << " " << p.typeName();