ChessNotation.cc
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set
2 //
3 //! @file ChessNotation.cc This file contains the implementation of class ChessNotation.
4 //
5 // Copyright (C) 2008, by
6 //
7 // Carlo Wood, Run on IRC <carlo@alinoe.com>
8 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
9 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef USE_PCH
25 #include "sys.h"
26 #include <iostream>
27 #endif
28 
29 #include "ChessNotation.h"
30 
31 namespace cwchess {
32 
33 std::ostream& operator<<(std::ostream& os, ChessNotation const& chess_notation)
34 {
35  if (chess_notation.M_type)
36  chess_notation.print_on(os,* chess_notation.M_type);
37  if (chess_notation.M_piece)
38  chess_notation.print_on(os,* chess_notation.M_piece);
39  if (chess_notation.M_index)
40  chess_notation.print_on(os,* chess_notation.M_index);
41  if (chess_notation.M_move)
42  chess_notation.print_on(os,* chess_notation.M_move);
43  return os;
44 }
45 
46 void ChessNotation::print_on(std::ostream& os, Piece const& piece) const
47 {
48  print_on(os, piece.type());
49 }
50 
51 void ChessNotation::print_on(std::ostream& os, Type const& type) const
52 {
53  switch(type())
54  {
55  case knight_bits:
56  os << 'N';
57  break;
58  case king_bits:
59  os << 'K';
60  break;
61  case bishop_bits:
62  os << 'B';
63  break;
64  case rook_bits:
65  os << 'R';
66  break;
67  case queen_bits:
68  os << 'Q';
69  break;
70  }
71 }
72 
73 void ChessNotation::print_on(std::ostream& os, Index const& index) const
74 {
75  char column = 'a' + index.col();
76  char rank = '1' + index.row();
77  os << column << rank;
78 }
79 
80 void ChessNotation::print_on(std::ostream& os, Move const& move) const
81 {
82  Piece const& piece(M_chess_position.piece_at(move.from()));
83  int col_diff = move.from().col() - move.to().col();
84  if (piece == king && (col_diff == 2 || col_diff == -2))
85  {
86  if (col_diff == 2)
87  os << "0-0-0";
88  else
89  os << "0-0";
90  }
91  else
92  {
93  print_on(os, piece);
94  print_on(os, move.from());
95  bool target_square_empty = M_chess_position.piece_at(move.to()) == nothing;
96  bool en_passant = col_diff != 0 && piece == pawn && target_square_empty;
97  if (target_square_empty && !en_passant)
98  os << '-';
99  else
100  os << 'x';
101  print_on(os, move.to());
102  if (en_passant)
103  os << " e.p.";
104  if (move.is_promotion())
105  {
106  os << '(';
107  print_on(os, move.promotion_type());
108  os << ')';
109  }
110  }
111  Debug(dc::place.off());
112  ChessPosition tmp(M_chess_position);
113  if (!tmp.legal(move))
114  os << " illegal move!";
115  else
116  {
117  bool draw = tmp.execute(move);
118  // Find the number of remaining possible moves.
119  int moves = 0;
120  for (PieceIterator piece_iter = tmp.piece_begin(tmp.to_move()); piece_iter != tmp.piece_end(); ++piece_iter)
121  {
122  MoveIterator move_end(tmp.move_end());
123  for (MoveIterator iter = tmp.move_begin(piece_iter.index()); iter != move_end; ++iter)
124  ++moves;
125  }
126  bool check = tmp.check();
127  bool check_mate = false;
128  bool stale_mate = false;
129  if (moves == 0)
130  {
131  if (check)
132  {
133  check_mate = true;
134  draw = false;
135  }
136  else
137  {
138  stale_mate = true;
139  draw = true;
140  }
141  }
142  if (check_mate)
143  os << '#';
144  else if (stale_mate)
145  os << " stale mate";
146  if (check_mate)
147  {
148  if (tmp.to_move() == black)
149  os << " 1-0";
150  else
151  os << " 0-1";
152  }
153  else if (check)
154  os << '+';
155  if (draw)
156  os << " 1/2-1/2";
157  }
158  Debug(dc::place.on());
159 }
160 
161 } // namespace cwchess
A namespace for all chess related objects that are not related to the GUI.
Definition: Array.h:39
TypeData const pawn
A constant representing a pawn.
Definition: Type.h:65
TypeData const king
A constant representing a king.
Definition: Type.h:69
uint8_t const bishop_bits
The underlaying integral value of type& #39;bishop&#39;.
Definition: Type.h:57
Piece piece_at(Index const& index) const
Return the Piece on the square index.
uint8_t const king_bits
The underlaying integral value of type& #39;king&#39;.
Definition: Type.h:56
TypeData const nothing
A constant representing the absence of a piece.
Definition: Type.h:63
uint8_t const knight_bits
The underlaying integral value of type& #39;knight&#39;.
Definition: Type.h:55
uint8_t const queen_bits
The underlaying integral value of type& #39;queen&#39;.
Definition: Type.h:59
This file contains the declaration of class ChessNotation.
uint8_t const rook_bits
The underlaying integral value of type& #39;rook&#39;.
Definition: Type.h:58
ColorData const black
A constant representing the color black.
Definition: Color.h:53

Copyright © 2006 - 2010 Carlo Wood.  All rights reserved.