removed clock pref widget. clock prefs are definitively part of the lua theme.
[kboard.git] / src / variants / xchess / piece.cpp
bloba68aec069b57c4b0e3faa35beac23395cb36ff68
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 return equals(other);
46 bool ChessPiece::sameColor(const ChessPiece& other) const {
47 return other && (m_color == other.color());
50 int ChessPiece::id() const {
51 int res = static_cast<int>(type());
52 if (color() == BLACK) res = -res - 1;
53 return res;
56 ChessPiece::Color ChessPiece::colorFromId(int x) {
57 return x < 0 ? BLACK : WHITE;
60 ChessPiece::Type ChessPiece::typeFromId(int x) {
61 return static_cast<Type>(x<0 ? (-1-x) : x);
64 QString ChessPiece::typeName() const {
65 switch (m_type) {
66 case ROOK:
67 return "rook";
68 case BISHOP:
69 return "bishop";
70 case KNIGHT:
71 return "knight";
72 case QUEEN:
73 return "queen";
74 case KING:
75 return "king";
76 case PAWN:
77 return "pawn";
78 default:
79 return "unknown";
83 QString ChessPiece::name() const {
84 switch(m_color) {
85 case WHITE:
86 switch (m_type) {
87 case ROOK:
88 return "white_rook";
89 case BISHOP:
90 return "white_bishop";
91 case KNIGHT:
92 return "white_knight";
93 case QUEEN:
94 return "white_queen";
95 case KING:
96 return "white_king";
97 case PAWN:
98 return "white_pawn";
99 default:
100 return "unknown";
102 case BLACK:
103 switch (m_type) {
104 case ROOK:
105 return "black_rook";
106 case BISHOP:
107 return "black_bishop";
108 case KNIGHT:
109 return "black_knight";
110 case QUEEN:
111 return "black_queen";
112 case KING:
113 return "black_king";
114 case PAWN:
115 return "black_pawn";
116 default:
117 return "unknown";
119 default:
120 return "unknown";
124 ChessPiece::Type ChessPiece::getType(const QString& str) {
125 // assume the string is well formatted
126 if (str.isEmpty())
127 return PAWN;
129 char c = str[0].toAscii();
130 switch (c) {
131 case 'Q':
132 case 'q':
133 return QUEEN;
134 case 'N':
135 case 'n':
136 return KNIGHT;
137 case 'B':
138 case 'b':
139 return BISHOP;
140 case 'K':
141 case 'k':
142 return KING;
143 case 'R':
144 case 'r':
145 return ROOK;
146 case 'P':
147 case 'p':
148 return PAWN;
149 default:
150 return INVALID_TYPE;
154 QString ChessPiece::typeSymbol(ChessPiece::Type type) {
155 switch (type) {
156 case QUEEN:
157 return "Q";
158 case KING:
159 return "K";
160 case KNIGHT:
161 return "N";
162 case BISHOP:
163 return "B";
164 case ROOK:
165 return "R";
166 case PAWN:
167 return "P";
168 default:
169 return "?";
173 std::ostream& operator<<(std::ostream& os, const ChessPiece& p) {
174 return os << (p.color() == WHITE? "white" : p.color() == BLACK? "black" : "unknown")
175 << " " << p.typeName();