Changed my email in the copyright statements.
[tagua/yd.git] / src / hlvariant / chess / piece.cpp
blobbed3804071de3fff5d1e5a0332e7b69a7c3f7d8c
1 /*
2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2007 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"
12 #include "san.h"
14 namespace HLVariant {
15 namespace Chess {
17 Piece::~Piece() { }
19 Piece::Piece(Color color, Type type)
20 : m_color(color)
21 , m_type(type) { }
23 Piece::Color Piece::color() const { return m_color; }
25 Piece::Type Piece::type() const { return m_type; }
27 QString Piece::colorName() const { return colorName(m_color); }
29 QString Piece::colorName(Color color) {
30 switch (color) {
31 case WHITE:
32 return "white";
33 case BLACK:
34 return "black";
35 default:
36 return "unknown";
40 QString Piece::typeName() const { return typeName(m_type); }
42 QString Piece::typeName(Type type) {
43 switch (type) {
44 case ROOK:
45 return "rook";
46 case BISHOP:
47 return "bishop";
48 case KNIGHT:
49 return "knight";
50 case QUEEN:
51 return "queen";
52 case KING:
53 return "king";
54 case PAWN:
55 return "pawn";
56 default:
57 return "unknown";
61 QString Piece::name() const {
62 return colorName() + '_' + typeName();
65 Piece::Color Piece::oppositeColor(Color color) {
66 switch (color) {
67 case WHITE:
68 return BLACK;
69 case BLACK:
70 return WHITE;
71 default:
72 return INVALID_COLOR;
76 bool Piece::operator==(const Piece& other) const {
77 return m_color == other.m_color &&
78 m_type == other.m_type;
81 bool Piece::operator!=(const Piece& other) const {
82 return !((*this) == other);
85 Piece Piece::fromDescription(const QString& description) {
86 if (description.size() == 1) {
87 QChar c = description[0];
88 Color color;
90 if (c.category() == QChar::Letter_Uppercase) {
91 color = WHITE;
93 else if (c.category() == QChar::Letter_Lowercase) {
94 color = BLACK;
96 else {
97 return Piece();
100 Type type = static_cast<Type>(SAN::getType(c));
102 if (type != INVALID_TYPE) {
103 return Piece(color, type);
107 return Piece();
110 } // namespace HLVariant
111 } // namespace Chess