Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "sys.h"
00025 #include <iostream>
00026 #include <vector>
00027 #include <cassert>
00028 #include "ChessPosition.h"
00029 #include "MoveIterator.h"
00030 #include "ChessNotation.h"
00031 #include "debug.h"
00032
00033 namespace test {
00034 using cwchess::ArrayCode;
00035 using cwchess::BitBoard;
00036 using cwchess::ArrayIndex;
00037 using cwchess::Piece;
00038 using cwchess::ArrayColor;
00039 using cwchess::CountBoard;
00040 using cwchess::CastleFlags;
00041 using cwchess::Color;
00042 using cwchess::EnPassant;
00043
00044 struct ChessPosition {
00045 ArrayCode<BitBoard> M_bitboards;
00046 ArrayIndex<Piece> M_pieces;
00047 ArrayColor<BitBoard> M_attackers;
00048 ArrayColor<BitBoard> M_pinning;
00049 ArrayColor<CountBoard> M_defended;
00050 ArrayColor<uint8_t> M_king_battery_attack_count;
00051 uint16_t M_full_move_number;
00052 uint8_t M_half_move_clock;
00053 CastleFlags M_castle_flags;
00054 Color M_to_move;
00055 EnPassant M_en_passant;
00056 bool M_double_check;
00057 };
00058 }
00059
00060 int main(int argc, char* argv[])
00061 {
00062 Debug(debug::init());
00063
00064 using namespace cwchess;
00065
00066
00067
00068 std::cout << "sizeof(Color) = " << sizeof(Color) << '\n';
00069 assert(sizeof(Color) == 1);
00070 std::cout << "sizeof(Index) = " << sizeof(Index) << '\n';
00071 assert(sizeof(Index) == 1);
00072 std::cout << "sizeof(Code) = " << sizeof(Code) << '\n';
00073 assert(sizeof(Code) == 1);
00074 std::cout << "sizeof(Flags) = " << sizeof(Flags) << '\n';
00075 assert(sizeof(Flags) == 1);
00076 std::cout << "sizeof(Piece) = " << sizeof(Piece) << '\n';
00077 assert(sizeof(Piece) == sizeof(Code) + sizeof(Flags));
00078 std::cout << "sizeof(BitBoard) = " << sizeof(BitBoard) << '\n';
00079 assert(sizeof(BitBoard) == 8);
00080 std::cout << "sizeof(CountBoard) = " << sizeof(CountBoard) << '\n';
00081 assert(sizeof(CountBoard) == 5 * sizeof(BitBoard));
00082 std::cout << "sizeof(CastleFlags) = " << sizeof(CastleFlags) << '\n';
00083 assert(sizeof(CastleFlags) == 1);
00084 std::cout << "sizeof(EnPassant) = " << sizeof(EnPassant) << '\n';
00085 assert(sizeof(EnPassant) == 1);
00086 std::cout << "sizeof(ArrayCode<BitBoard>) = " << sizeof(ArrayCode<BitBoard>) << '\n';
00087 assert(sizeof(ArrayCode<BitBoard>) == 16 * sizeof(BitBoard));
00088 std::cout << "sizeof(ArrayIndex<Piece>) = " << sizeof(ArrayIndex<Piece>) << '\n';
00089 assert(sizeof(ArrayIndex<Piece>) == 64 * sizeof(Piece));
00090 std::cout << "sizeof(ArrayColor<BitBoard>) = " << sizeof(ArrayColor<BitBoard>) << '\n';
00091 assert(sizeof(ArrayColor<BitBoard>) == 2 * sizeof(BitBoard));
00092 std::cout << "sizeof(ArrayColor<CountBoard>) = " << sizeof(ArrayColor<CountBoard>) << '\n';
00093 assert(sizeof(ArrayColor<CountBoard>) == 2 * sizeof(CountBoard));
00094 std::cout << "sizeof(ArrayColor<uint8_t>) = " << sizeof(ArrayColor<uint8_t>) << '\n';
00095 assert(sizeof(ArrayColor<uint8_t>) == 2);
00096 size_t sum = sizeof(ArrayCode<BitBoard>) + sizeof(ArrayIndex<Piece>) +
00097 sizeof(ArrayColor<BitBoard>) + sizeof(ArrayColor<BitBoard>) + sizeof(ArrayColor<CountBoard>) + sizeof(ArrayColor<uint8_t>) +
00098 sizeof(uint16_t) + sizeof(uint8_t) + sizeof(CastleFlags) + sizeof(Color) + sizeof(EnPassant) + sizeof(bool);
00099 std::cout << "Sum is " << sum << '\n';
00100 std::cout << "sizeof(test::ChessPosition) = " << sizeof(test::ChessPosition) << '\n';
00101 std::cout << "sizeof(ChessPosition) = " << sizeof(ChessPosition) << '\n';
00102 #ifdef CWDEBUG
00103 if (sizeof(ChessPosition) != sum)
00104 Dout(dc::warning, "ChessPosition is not compact!?!");
00105 #endif
00106
00107
00108
00109
00110
00111 ChessPosition chess_position;
00112
00113 if (argc > 1)
00114 {
00115 std::string str(argv[1]);
00116 std::cout << "Loading \"" << str << "\".\n";
00117
00118 if (chess_position.load_FEN(str))
00119 std::cout << "Loading successful\n";
00120 else
00121 std::cout << "Loading failed!\n";
00122 }
00123 else
00124 chess_position.initial_position();
00125
00126 std::cout << "FEN code is: \"" << chess_position.FEN() << "\".\n";
00127
00128 std::vector<Move> moves;
00129 Color color(chess_position.to_move());
00130 PieceIterator piece_end(chess_position.piece_end());
00131 for (PieceIterator piece_iter = chess_position.piece_begin(color); piece_iter != piece_end; ++piece_iter)
00132 {
00133 MoveIterator move_end(chess_position.move_end());
00134 for (MoveIterator iter = chess_position.move_begin(piece_iter.index()); iter != move_end; ++iter)
00135 moves.push_back(*iter);
00136 }
00137 std::cout << "There are " << moves.size() << " moves: ";
00138 bool first = true;
00139 for (std::vector<Move>::iterator iter = moves.begin(); iter != moves.end(); ++iter)
00140 {
00141 if (first)
00142 first = false;
00143 else
00144 std::cout << ", ";
00145 std::cout << ChessNotation(chess_position,* iter);
00146 }
00147 std::cout << std::endl;
00148 }