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 #ifndef USE_PCH
00025 #include "sys.h"
00026 #include <iostream>
00027 #include <cstdlib>
00028 #include <ctime>
00029 #include <iomanip>
00030 #include <sstream>
00031 #include <vector>
00032 #include <sys/time.h>
00033 #include "debug.h"
00034 #endif
00035
00036 #include "ChessPosition.h"
00037 #include "PieceIterator.h"
00038 #include "MoveIterator.h"
00039 #include "ChessNotation.h"
00040
00041 using namespace cwchess;
00042
00043 void print_move(ChessPosition const& chess_position, Move const& move)
00044 {
00045 if (chess_position.to_move() == white)
00046 std::cout << chess_position.full_move_number() << ". ";
00047 std::ostringstream str;
00048 str << ChessNotation(chess_position, move);
00049 std::cout << std::setfill(' ') << std::setw(10) << std::left << str.str();
00050 if (chess_position.to_move() == black)
00051 std::cout << '\n';
00052 }
00053
00054 int main()
00055 {
00056 Debug(libcw_do.off());
00057 Debug(debug::init());
00058
00059 time_t seed = 1220638382;
00060
00061 std::srand(seed);
00062
00063 ChessPosition chess_position;
00064 std::vector<Move> game;
00065 Move moves[2048];
00066 Move* move_ptr;
00067 static int random_numbers[5000000];
00068
00069
00070 for (int i = 0; i < 5000000; ++i)
00071 random_numbers[i] = std::rand();
00072
00073 int games = 0;
00074 int total_moves = 0;
00075 int Move_count = 0;
00076
00077 struct timeval before, after;
00078 gettimeofday(&before, NULL);
00079
00080 do
00081 {
00082 chess_position.initial_position();
00083
00084 for(;;)
00085 {
00086 move_ptr = moves;
00087 for (PieceIterator piece_iter(chess_position.piece_begin(chess_position.to_move()));
00088 piece_iter != chess_position.piece_end();
00089 ++piece_iter)
00090 {
00091 MoveIterator const move_end;
00092 #if 0
00093 Move* move_save = move_ptr;
00094 for (int i = 0; i < 100; ++i)
00095 {
00096 move_ptr = move_save;
00097 #endif
00098 for (MoveIterator move_iter(chess_position.move_begin(piece_iter.index()));
00099 move_iter != move_end;
00100 ++move_iter)
00101 {
00102 * move_ptr++ =* move_iter;
00103 }
00104 #if 0
00105 }
00106 #endif
00107 }
00108 int number_of_moves = move_ptr - moves;
00109 Move_count += number_of_moves;
00110 if (number_of_moves == 0)
00111 break;
00112 int mn = random_numbers[total_moves] % number_of_moves;
00113 ++total_moves;
00114
00115 if (chess_position.execute(moves[mn]))
00116 break;
00117 }
00118 ++games;
00119 }
00120 while (games < 10000);
00121
00122 gettimeofday(&after, NULL);
00123
00124 #if 0
00125 chess_position.initial_position();
00126 for (std::vector<Move>::iterator iter = game.begin(); iter != game.end(); ++iter)
00127 {
00128
00129 Move const& move(*iter);
00130 print_move(chess_position, move);
00131 chess_position.execute(move);
00132 }
00133 if (chess_position.to_move() == black)
00134 std::cout << std::endl;
00135 #endif
00136
00137 std::cout << "Number of games played: " << games << "\nTotal number of moves played: " << total_moves << std::endl;
00138
00139 timersub(&after,& before,& after);
00140 uint64_t t = after.tv_sec;
00141 t* = 1000000;
00142 t += after.tv_usec;
00143 std::cout << "Generated Move objects: " << (unsigned long)(Move_count / (t / 1000000.0) + 0.5) << " Moves/second." << std::endl;
00144 std::cout << "Executed ply: " << (unsigned long)(total_moves / (t / 1000000.0) + 0.5) << " ply/second." << std::endl;
00145 std::cout << "Computing time: " << ((double)t / total_moves) << " microseconds per executed move (ply)." << std::endl;
00146 }