Repository navigation
Expand file tree
/
Copy pathTState.cpp
More file actions
124 lines (108 loc) · 2.39 KB
/
Copy pathTState.cpp
File metadata and controls
124 lines (108 loc) · 2.39 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
//---------------------------------------------------------------------------
#pragma hdrstop
#include "TState.h"
#include "fen.h"
#include "move_do.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
TState::TState()
{
SetNewGame();
Inited = false;
}
TState::~TState()
{
}
bool TState::IsWhite()
{
int t = MoveHistoryLen;
if (LastMove)
t++;
return (t%2) == 0;
}
void TState::AddMove(mv_t move)
{
if (LastMove) {
MoveHistory[MoveHistoryLen++] = LastMove;
undo_t undo;
move_do(&Board,LastMove,&undo);
}
LastMove = move;
}
void TState::SetNewGame()
{
MoveHistoryLen = 0;
board_from_fen(&Board,StartFen);
LastMove = 0;
}
bool TState::IsEqual(TState * State)
{
if (Inited != State->Inited)
return false;
int this_moves_len = MoveHistoryLen;
mv_t this_moves[max_game_len];
for (int i=0; i<this_moves_len; i++)
this_moves[i] = MoveHistory[i];
if (LastMove)
this_moves[this_moves_len++] = LastMove;
int others_moves_len = State->MoveHistoryLen;
mv_t others_moves[max_game_len];
for (int i=0; i<others_moves_len; i++)
others_moves[i] = State->MoveHistory[i];
if (State->LastMove)
others_moves[others_moves_len++] = State->LastMove;
if (this_moves_len != others_moves_len)
return false;
for (int i=0; i<this_moves_len; i++)
if (this_moves[i]!=others_moves[i])
return false;
return true;
}
void TState::CopyTo(TState * State)
{
State->Inited = Inited;
State->LastMove = LastMove;
State->MoveHistoryLen = MoveHistoryLen;
for (int i=0; i<MoveHistoryLen; i++)
State->MoveHistory[i] = MoveHistory[i];
State->Board = Board;
}
int TState::Len()
{
int len = MoveHistoryLen;
if (LastMove)
len++;
return len;
}
void TState::SaveToStr(char *s)
{
sprintf(s,"len:%i, inited:%i",Len(),Inited);
char move_str[6];
for (int i=0; i<MoveHistoryLen; i++) {
move_to_string(MoveHistory[i],move_str,6);
strcat(s,move_str);
strcat(s,",");
}
if (LastMove) {
strcat(s,"_");
move_to_string(LastMove,move_str,6);
strcat(s,move_str);
}
}
mv_t TState::GetLastMove()
{
if (LastMove)
return LastMove;
if (!MoveHistoryLen)
return 0;
return MoveHistory[MoveHistoryLen-1];
}
void TState::CommitLastMove()
{
if (LastMove) {
MoveHistory[MoveHistoryLen++] = LastMove;
undo_t undo;
move_do(&Board,LastMove,&undo);
}
LastMove = 0;
}