forked from adhip12/ChessGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChessGame.cc
145 lines (112 loc) · 2.97 KB
/
ChessGame.cc
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
133
134
135
136
137
138
139
140
141
142
#include "ChessState.hh"
Position::Position(const int row, const int col) : x(row), y(col) {}
Piece:: Piece (const PieceType type, const Position &posn) : pieceType(type), pos(posn) {}
Position::Position() : x(-1), y(-1) {}
Piece::Piece()
{
setPieceType(INVALID_PIECE_TYPE);
setPosition(Position(-1, -1));
}
void Piece :: initPiece (const PieceType type, Position &pos)
{
setPieceType (type);
setPosition (pos);
//piece_num = num;
//this->pos.SetX(pos.x);
//this->pos.SetY(pos.y);
}
Player:: Player(Color color)
{
setColor(color);
for (int type = 0; type < NUM_PIECES; type++) {
Position pos = GetInitialPositionOfPiece ((PieceType)type, color);
piece[type].initPiece((PieceType)type, pos);
//newPiece.initPiece (num, Position(pos.i, pos.j));
//piece[num] = *newPiece;
}
}
ChessGame::ChessGame() : head (0), tail(0), currMove(0)
{
int x = 0, y = 0;
player1 = new Player (White);
player2 = new Player (Black);
Color color = White;
// Initialize the Chess board, and mark the initial positions with the Piece info.
memset (chess_board, 0, sizeof(chess_board[0][0]) * 8 * 8);
for (int type = 0; type < NUM_PIECES; type++) {
color = White;
Position pos = GetInitialPositionOfPiece ((PieceType)type, color);
x = pos.GetX();
y = pos.GetY();
chess_board[x][y] = type;
color = Black;
pos = GetInitialPositionOfPiece ((PieceType)type, color);
x = pos.GetX();
y = pos.GetY();
chess_board[x][y] = 2 * type;
}
}
ChessGame::~ChessGame ()
{
delete player1;
delete player2;
}
Position GetInitialPositionOfPiece (PieceType type, Color color)
{
int position;
if (color == White) {
if (type < PAWN8) {
return (Position(1, type - 1));
} else {
return (Position(0, type - PAWN8 - 1));
}
} else {
if (type < PAWN8) {
return (Position (6, type - 1));
} else {
return (Position (7, type - PAWN8 - 1));
}
}
}
#if 0
ChessGame::int RestoreToMove(int move)
{
int count = move;
// Go back to initial positions.
ChessGame();
if ((move > currMove) || (move < 0)) {
return NO_SUCH_MOVE_ERROR;
}
// Traverse the list, until we reach the move number
while (count != 0) {
Node *node = head;
RestorePiece(node->type, )
}
}
int Piece::MovePiece (PieceType piece, Position &pos)
{
//Check Position, if its already taken.
if (chess_board[pos->i, pos->j] == TAKEN) {
return SLOT_TAKEN_ERROR;
}
/*Use either the vector of vector method or the linked list node method
to keep track of the move */
//Also have to keep track of the current position of the piece.
//Not ading code for the above right now. Only checking the code to add moves, irrespective of it being an invalid move.
Node *move = new Node();
Piece *piece = this->piece[piece_num];
move->pieceInfo = piece->setPieceInfo (piece_num, pos);
move->playerType = player->type;
if (head == NULL) {
head = move;
move->next = NULL;
move->prev = NULL;
tail = move;
} else {
tail->next = move;
move->prev = tail;
tail = move;
}
currMove ++;
}
#endif