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.
13 ChessPiece::ChessPiece(ChessPiece::Color color
, ChessPiece::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
;
27 ChessPiece
* ChessPiece::clone() const {
28 return new ChessPiece(*this);
31 bool ChessPiece::equals(const ChessPiece
& other
) const {
36 return m_color
== other
.color() && m_type
== other
.type();
42 bool ChessPiece::operator==(const ChessPiece
& other
) const {
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;
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 {
83 QString
ChessPiece::name() const {
90 return "white_bishop";
92 return "white_knight";
107 return "black_bishop";
109 return "black_knight";
111 return "black_queen";
124 ChessPiece::Type
ChessPiece::getType(const QString
& str
) {
125 // assume the string is well formatted
129 char c
= str
[0].toAscii();
154 QString
ChessPiece::typeSymbol(ChessPiece::Type type
) {
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();