-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReversiBoard.cpp
More file actions
170 lines (149 loc) · 4.1 KB
/
Copy pathReversiBoard.cpp
File metadata and controls
170 lines (149 loc) · 4.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* ReversiBoard.cpp
*
* Created on: 07-Nov-2023
* Author: Sai Swaroop Maram
*/
#include "ReversiBoard.h"
#include<iostream>
#include <vector>
using namespace std;
/**
* Constructor, initializes the board with center pieces
*/
ReversiBoard::ReversiBoard()
{
row = 8;
col = 8;
for (int row_iterator = 0; row_iterator < 8; row_iterator++)
{
for (int column_iterator = 0; column_iterator < 8; column_iterator++)
{
if ((row_iterator == 4 && column_iterator == 4)
|| (row_iterator == 3 && column_iterator == 3))
{
Board[row_iterator][column_iterator] = P1_DISK;
}
else if ((row_iterator == 3 && column_iterator == 4)
|| (row_iterator == 4 && column_iterator == 3))
{
Board[row_iterator][column_iterator] = P2_DISK;
}
else
Board[row_iterator][column_iterator] = EMPTY;
}
}
cout << "ReversiBoard object is created at " << this << endl;
}
/**
* Destructor for ReversiBoard
*/
ReversiBoard::~ReversiBoard()
{
cout << "ReversiBoard object is destructed at " << this << endl;
}
/**
* Used to return the state of a position
* \param int row : [IN] the row from user move
* \param int col : [IN] the column from user move
* \return state : [OUT] returns the state of the passed position
*/
ReversiBoard::state ReversiBoard::stateOfSquare(int row, int col)
{
return Board[row][col];
}
/**
* Used to set the players state only if its valid
* \param state s : [IN] Player state
* \param int row : [IN] the row from user move
* \param int col : [IN] the column from user move
* \return void
*/
void ReversiBoard::setState(state s, int row, int col)
{
if (isValidThenFlip(s, row, col))
{
Board[row][col] = s;
}
else
cout << "Wrong move" << endl;
}
/**
* Used to check if players move is valid and flips if valid
* \param state s : [IN] Player state
* \param int row : [IN] the row from user move
* \param int col : [IN] the column from user move
* \return bool : [OUT] returns true if its valid move else False
*/
bool ReversiBoard::isValidThenFlip(state s, int row, int col)
{
bool flag = false;
ReversiBoard::state SAME = (s == P1_DISK) ? P1_DISK : P2_DISK;
ReversiBoard::state OPPONENT = (s == P1_DISK) ? P2_DISK : P1_DISK;
int dir_row[8] =
{ -1, 0, -1, -1, 0, 1, 1, 1 };
int dir_col[8] =
{ 0, -1, -1, 1, 1, 1, 0, -1 };
// U, L, UL, UR, R, DR, D, DL
if (stateOfSquare(row, col) != EMPTY)
return false;
vector<int> opponent_row =
{ };
vector<int> opponent_col =
{ };
for (int direction_itr = 0; direction_itr < 8; direction_itr++)
//directions traversal loop
{
int next_row = row + dir_row[direction_itr];
int next_col = col + dir_col[direction_itr];
if (next_col >= 0 && next_row >= 0 && next_row < 8 && next_col < 8)
{
if (stateOfSquare(next_row, next_col) != EMPTY
&& stateOfSquare(next_row, next_col) != SAME)
{
if (stateOfSquare(next_row, next_col) == OPPONENT)
{
while (stateOfSquare(next_row, next_col) == OPPONENT)
//_,W,W,W
{
opponent_row.push_back(next_row);
opponent_col.push_back(next_col);
if (next_col >= 0 && next_row >= 0 && next_row < 8
&& next_col < 8)
{
next_row = next_row + dir_row[direction_itr];
next_col = next_col + dir_col[direction_itr];
}
else
break;
}
if (stateOfSquare(next_row, next_col) == SAME
&& next_col >= 0 && next_row >= 0 && next_row < 8
&& next_col < 8) //_,W,W,W,B
{
for (size_t k = 0; k < opponent_col.size(); k++)
{
Board[opponent_row[k]][opponent_col[k]] = SAME;
}
flag = true;
}
else if (stateOfSquare(next_row, next_col) == EMPTY
|| next_row < 0 || next_col < 0 || next_row >= 8
|| next_col >= 8)
//_,W,W;W;E
{
opponent_col.clear();
opponent_row.clear();
}
}
else if (stateOfSquare(next_row, next_col) == SAME) //_,W,W,W,B
{
flag = false;
}
else
flag = false;
}
}
}
return flag;
}