tstchessposition.cc
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set for gtkmm
2 //
3 //! @file tstchessposition.cc A test application that prints all legal moves of a given position.
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 #include "sys.h"
25 #include <iostream>
26 #include <vector>
27 #include <cassert>
28 #include "ChessPosition.h"
29 #include "MoveIterator.h"
30 #include "ChessNotation.h"
31 #include "debug.h"
32 
33 namespace test {
34  using cwchess::ArrayCode;
35  using cwchess::BitBoard;
36  using cwchess::ArrayIndex;
37  using cwchess::Piece;
38  using cwchess::ArrayColor;
39  using cwchess::CountBoard;
41  using cwchess::Color;
42  using cwchess::EnPassant;
43 
44  struct ChessPosition {
45  ArrayCode<BitBoard> M_bitboards; //!< Bitboards reflecting the current position.
46  ArrayIndex<Piece> M_pieces; //!< A piece per square. The index of the array is an Index.
47  ArrayColor<BitBoard> M_attackers; //!< Bitboards for squares of enemy pieces on the same line as the king and all squares in between.
48  ArrayColor<BitBoard> M_pinning; //!< Squares between attacker and king for actually pinned pieces (including attacker).
49  ArrayColor<CountBoard> M_defended; //!< The number times a square is defended.
50  ArrayColor<uint8_t> M_king_battery_attack_count; //!< The number of times that a king is 'attacked' by pieces behind another attacker.
51  uint16_t M_full_move_number; //!< The number of the full move. It starts at 1, and is incremented after Black's move.
52  uint8_t M_half_move_clock; //!< Number of half moves since the last pawn advance or capture.
53  CastleFlags M_castle_flags; //!< Whether black and white may castle long or short.
54  Color M_to_move; //!< The active color.
55  EnPassant M_en_passant; //!< A pawn that can be taken en passant, or zeroed if none such pawn exists.
56  bool M_double_check; //!< Cached value of wether or not M_to_move is in double check.
57  };
58 }
59 
60 int main(int argc, char* argv[])
61 {
62  Debug(NAMESPACE_DEBUG::init());
63 
64  using namespace cwchess;
65 
66  //-------------------------------------------------------------------------
67  // Print the sizes of classes.
68  std::cout << "sizeof(Color) = " << sizeof(Color) << '\n';
69  assert(sizeof(Color) == 1);
70  std::cout << "sizeof(Index) = " << sizeof(Index) << '\n';
71  assert(sizeof(Index) == 1);
72  std::cout << "sizeof(Code) = " << sizeof(Code) << '\n';
73  assert(sizeof(Code) == 1);
74  std::cout << "sizeof(Flags) = " << sizeof(Flags) << '\n';
75  assert(sizeof(Flags) == 1);
76  std::cout << "sizeof(Piece) = " << sizeof(Piece) << '\n';
77  assert(sizeof(Piece) == sizeof(Code) + sizeof(Flags));
78  std::cout << "sizeof(BitBoard) = " << sizeof(BitBoard) << '\n';
79  assert(sizeof(BitBoard) == 8);
80  std::cout << "sizeof(CountBoard) = " << sizeof(CountBoard) << '\n';
81  assert(sizeof(CountBoard) == 5 * sizeof(BitBoard));
82  std::cout << "sizeof(CastleFlags) = " << sizeof(CastleFlags) << '\n';
83  assert(sizeof(CastleFlags) == 1);
84  std::cout << "sizeof(EnPassant) = " << sizeof(EnPassant) << '\n';
85  assert(sizeof(EnPassant) == 1);
86  std::cout << "sizeof(ArrayCode<BitBoard>) = " << sizeof(ArrayCode<BitBoard>) << '\n';
87  assert(sizeof(ArrayCode<BitBoard>) == 16 * sizeof(BitBoard));
88  std::cout << "sizeof(ArrayIndex<Piece>) = " << sizeof(ArrayIndex<Piece>) << '\n';
89  assert(sizeof(ArrayIndex<Piece>) == 64 * sizeof(Piece));
90  std::cout << "sizeof(ArrayColor<BitBoard>) = " << sizeof(ArrayColor<BitBoard>) << '\n';
91  assert(sizeof(ArrayColor<BitBoard>) == 2 * sizeof(BitBoard));
92  std::cout << "sizeof(ArrayColor<CountBoard>) = " << sizeof(ArrayColor<CountBoard>) << '\n';
93  assert(sizeof(ArrayColor<CountBoard>) == 2 * sizeof(CountBoard));
94  std::cout << "sizeof(ArrayColor<uint8_t>) = " << sizeof(ArrayColor<uint8_t>) << '\n';
95  assert(sizeof(ArrayColor<uint8_t>) == 2);
96  size_t sum = sizeof(ArrayCode<BitBoard>) + sizeof(ArrayIndex<Piece>) +
98  sizeof(uint16_t) + sizeof(uint8_t) + sizeof(CastleFlags) + sizeof(Color) + sizeof(EnPassant) + sizeof(bool);
99  std::cout << "Sum is " << sum << '\n';
100  std::cout << "sizeof(test::ChessPosition) = " << sizeof(test::ChessPosition) << '\n';
101  std::cout << "sizeof(ChessPosition) = " << sizeof(ChessPosition) << '\n';
102 #ifdef CWDEBUG
103  if (sizeof(ChessPosition) != sum)
104  Dout(dc::warning, "ChessPosition is not compact!?!");
105 #endif
106 
107  //-------------------------------------------------------------------------
108  // Load FEN code from command line and print all legal moves.
109  //
110 
111  ChessPosition chess_position;
112 
113  if (argc > 1)
114  {
115  std::string str(argv[1]);
116  std::cout << "Loading \"" << str << "\".\n";
117 
118  if (chess_position.load_FEN(str))
119  std::cout << "Loading successful\n";
120  else
121  std::cout << "Loading failed!\n";
122  }
123  else
124  chess_position.initial_position();
125 
126  std::cout << "FEN code is: \"" << chess_position.FEN() << "\".\n";
127 
128  std::vector<Move> moves;
129  Color color(chess_position.to_move());
130  PieceIterator piece_end(chess_position.piece_end());
131  for (PieceIterator piece_iter = chess_position.piece_begin(color); piece_iter != piece_end; ++piece_iter)
132  {
133  MoveIterator move_end(chess_position.move_end());
134  for (MoveIterator iter = chess_position.move_begin(piece_iter.index()); iter != move_end; ++iter)
135  moves.push_back(*iter);
136  }
137  std::cout << "There are " << moves.size() << " moves: ";
138  bool first = true;
139  for (std::vector<Move>::iterator iter = moves.begin(); iter != moves.end(); ++iter)
140  {
141  if (first)
142  first = false;
143  else
144  std::cout << ", ";
145  std::cout << ChessNotation(chess_position,* iter);
146  }
147  std::cout << std::endl;
148 }
ArrayColor< BitBoard > M_pinning
Squares between attacker and king for actually pinned pieces (including attacker).
A namespace for all chess related objects that are not related to the GUI.
Definition: Array.h:39
EnPassant M_en_passant
A pawn that can be taken en passant, or zeroed if none such pawn exists.
uint8_t M_half_move_clock
Number of half moves since the last pawn advance or capture.
Flags representing the state of a piece on the chessboard.
Definition: Flags.h:85
Non-mutable iterator over all moves of a given chess piece.
Definition: MoveIterator.h:58
A helper class to write other objects to an ostream.
Definition: ChessNotation.h:51
ArrayIndex< Piece > M_pieces
A piece per square. The index of the array is an Index.
ArrayColor< CountBoard > M_defended
The number times a square is defended.
ArrayColor< BitBoard > M_attackers
Bitboards for squares of enemy pieces on the same line as the king and all squares in between...
An object representing en passant information.
Definition: EnPassant.h:49
A one-boolean-per-square chessboard.
Definition: BitBoard.h:266
The index of a chess square.
Definition: Index.h:211
CastleFlags M_castle_flags
Whether black and white may castle long or short.
uint16_t M_full_move_number
The number of the full move. It starts at 1, and is incremented after Black&#39;s move.
ArrayCode< BitBoard > M_bitboards
Bitboards reflecting the current position.
A class to keep track of castling rights.
Definition: CastleFlags.h:63
A particular piece on the board.
Definition: Piece.h:45
bool M_double_check
Cached value of wether or not M_to_move is in double check.
A chess piece type including color.
Definition: Code.h:92
This file contains the declaration of class ChessPosition.
A color (black or white).
Definition: Color.h:67
Color M_to_move
The active color.
This file contains the declaration of class MoveIterator.
Non-mutable iterator over selective chess pieces in a chess position.
Definition: PieceIterator.h:42
This file contains the declaration of class ChessNotation.
ArrayColor< uint8_t > M_king_battery_attack_count
The number of times that a king is& #39;attacked&#39; by pieces behind another attacker.

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