-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
132 lines (109 loc) · 3.1 KB
/
main.c
File metadata and controls
132 lines (109 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "main.h"
#include "fen.h"
#include "utils.h"
#include "moveHandler.h"
#include "search.h"
#include "hashing.h"
#include "eval.h"
#include "book.h"
#include "sorting.h"
#include "test.h"
void m_playComputerTurn(Board *p_board, Book *book, uint8_t ply);
void m_playUserTurn(Board *p_board, Book *book);
int main(void)
{
//__test__FEN();
//__test__pawnMovement();
//__test__slidingMovement();
//__test__kingMovement();
//__test__knightMovement();
//__test__moveTree();
//__test__hashmap();
//__test__checkmate();
//__test__evaluation();
Book book;
printf("Generating book...\n");
clock_t start = clock(), diff;
generateBook(&book, 25, "uci.txt");
book.status = BOOK_ENDED;
diff = clock() - start;
int msec = diff * 1000 / CLOCKS_PER_SEC;
printf("Book generation time: %d sec, %d ms\n", msec / 1000, msec % 1000);
Board board;
Board *p_board = &board;
createBoardFormFEN("rnb2b1r/ppk2ppp/8/2p2qB1/1P4n1/2PB1N2/P3Q1P1/4RKR1 b - - 0 1", p_board);
initEvalTables();
int8_t boardStatus;
printBoard(p_board);
for (uint8_t i = 0; i < 100; i++)
{
printf("Move number: %d\n", i + 1);
m_playComputerTurn(p_board, &book, 6);
//m_playUserTurn(p_board, &book);
printf("Book status: %s\n", book.status == BOOK_READY ? "in" : "out");
boardStatus = isCheckmate(p_board);
if (boardStatus)
break;
m_playComputerTurn(p_board, &book, 6);
//m_playUserTurn(p_board, &book);
printf("Book status: %s\n", book.status == BOOK_READY ? "in" : "out");
boardStatus = isCheckmate(p_board);
if (boardStatus)
break;
}
if (boardStatus == 1)
{
printf("Stalemate!\n");
}
else if (boardStatus == WHITE)
{
printf("White won!\n");
}
else if (boardStatus == BLACK)
{
printf("Black won!\n");
}
else
{
printf("Game terminated!");
}
freeBook(&book);
extern Hashmap *p_tt;
freehashmap(p_tt);
printf("Exit success\n");
return 0;
}
void m_playComputerTurn(Board *p_board, Book *book, uint8_t ply)
{
static clock_t totalTime;
static uint16_t numComputerMoves;
Move bestMove;
if (book->status == BOOK_READY)
{
getNextMove(book, &bestMove);
}
else
{
clock_t start = clock(), diff;
bestMove = findBestMove(p_board, ply);
diff = clock() - start;
totalTime += diff;
numComputerMoves++;
int msec = diff * 1000 / CLOCKS_PER_SEC;
int totalMS = totalTime * 1000 / (CLOCKS_PER_SEC * numComputerMoves);
printf("Move calculation time: %d sec, %d ms\n", msec / 1000, msec % 1000);
printf("Average time: %d sec, %d ms\n", totalMS / 1000, totalMS % 1000);
}
performMove(&bestMove, p_board);
printBoard(p_board);
}
void m_playUserTurn(Board *p_board, Book *book)
{
Move selectedMove = selectMove(p_board);
advanceInBook(book, selectedMove);
performMove(&selectedMove, p_board);
printBoard(p_board);
}