-
Notifications
You must be signed in to change notification settings - Fork 0
/
Piezas.cpp
200 lines (190 loc) · 6.05 KB
/
Piezas.cpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "Piezas.h"
#include <vector>
/** CLASS Piezas
* Class for representing a Piezas vertical board, which is roughly based
* on the game "Connect Four" where pieces are placed in a column and
* fall to the bottom of the column, or on top of other pieces already in
* that column. For an illustration of the board, see:
* https://en.wikipedia.org/wiki/Connect_Four
*
* Board coordinates [row,col] should match with:
* [2,0][2,1][2,2][2,3]
* [1,0][1,1][1,2][1,3]
* [0,0][0,1][0,2][0,3]
* So that a piece dropped in column 2 should take [0,2] and the next one
* dropped in column 2 should take [1,2].
**/
/**
* Constructor sets an empty board (default 3 rows, 4 columns) and
* specifies it is X's turn first
**/
Piezas::Piezas()
{
std::vector<Piece> row;
turn = X;
for(int i=0; i<BOARD_ROWS; i++){
board.push_back(row);
for(int j=0; j<BOARD_COLS; j++){
board[i].push_back(Blank);
}
}
}
/**
* Resets each board location to the Blank Piece value, with a board of the
* same size as previously specified
**/
void Piezas::reset()
{
for(int i=0; i<BOARD_ROWS; i++){
for(int j=0; j<BOARD_COLS; j++){
board[i][j]=Blank;
}
}
}
/**
* Places a piece of the current turn on the board, returns what
* piece is placed, and toggles which Piece's turn it is. dropPiece does
* NOT allow to place a piece in a location where a column is full.
* In that case, placePiece returns Piece Blank value
* Out of bounds coordinates return the Piece Invalid value
* Trying to drop a piece where it cannot be placed loses the player's turn
**/
Piece Piezas::dropPiece(int column)
{
if(pieceAt(0,column)==Blank){
board[0][column]=turn;
if(turn==X){
turn=O;
}else if(turn==O){
turn=O;
}
return board[0][column];
}else if(pieceAt(1,column)==Blank){
board[1][column]=turn;
if(turn==X){
turn=O;
}else if(turn==O){
turn=O;
}
return board[1][column];
}else if(pieceAt(2,column)==Blank){
board[2][column]=turn;
if(turn==X){
turn=O;
}else if(turn==O){
turn=O;
}
return board[2][column];
}else if(pieceAt(2,column)==Invalid){
if(turn==X){
turn=O;
}else if(turn==O){
turn=O;
}
return Invalid;
}else{
if(turn==X){
turn=O;
}else if(turn==O){
turn=O;
}
return Blank;
}
}
/**
* Returns what piece is at the provided coordinates, or Blank if there
* are no pieces there, or Invalid if the coordinates are out of bounds
**/
Piece Piezas::pieceAt(int row, int column)
{
if((row>=BOARD_ROWS||column>=BOARD_COLS)||(row<0||column<0)){
return Invalid;
}else if(board[row][column]!=X||board[row][column]!=O){
return Blank;
}else{
return board[row][column];
}
}
/**
* Returns which Piece has won, if there is a winner, Invalid if the game
* is not over, or Blank if the board is filled and no one has won ("tie").
* For a game to be over, all locations on the board must be filled with X's
* and O's (i.e. no remaining Blank spaces). The winner is which player has
* the most adjacent pieces in a single line. Lines can go either vertically
* or horizontally. If both X's and O's have the same max number of pieces in a
* line, it is a tie.
**/
Piece Piezas::gameState()
{
//Piece curWinner=Blank;
Piece lastPiece=Blank;
int highCountX=1;
int highCountO=1;
int numConsec=1;
if(board[0][0]==' '||board[0][1]==' '||board[0][2]==' '||board[0][3]==' '||board[1][0]==' '||board[1][1]==' '||board[1][2]==' '||board[1][3]==' '||board[2][0]==' '||board[2][1]==' '||board[2][2]==' '||board[2][3]==' '){//empty cell
return Invalid;
}else{
for(int i=0;i<BOARD_ROWS;i++){//checks each row for consecutive pieces
lastPiece=Blank;
numConsec=1;
for(int j=0;j<BOARD_COLS;j++){
if(lastPiece==board[i][j]){
numConsec++;
if(lastPiece==X && numConsec>highCountX){
highCountX=numConsec;
}else if(lastPiece==O && numConsec>highCountO){
highCountO=numConsec;
}
}else{
lastPiece=board[i][j];
numConsec=1;
}
}
}
for(int i=0;i<BOARD_COLS;i++){//checks each col for consecutive pieces
lastPiece=Blank;
numConsec=1;
for(int j=0;j<BOARD_ROWS;j++){
if(lastPiece==board[j][i]){
numConsec++;
if(lastPiece==X && numConsec>highCountX){
highCountX=numConsec;
}else if(lastPiece==O && numConsec>highCountO){
highCountO=numConsec;
}
}else{
lastPiece=board[j][i];
numConsec=1;
}
}
}
/*if((board[0][0]==board[0][1]&&board[0][1]==board[0][2]&&board[0][2]==board[0][3])){//row 1
if(board[0][0]==X){
highCountX=4;
}else if(board[0][0]==O){
highCountO=4;
}
}
if((board[1][0]==board[1][1]&&board[1][1]==board[1][2]&&board[1][2]==board[1][3])){//row 2
if(board[1][0]==X){
highCountX=4;
}else if(board[1][0]==O){
highCountO=4;
}
}
if((board[2][0]==board[2][1]&&board[2][1]==board[2][2]&&board[2][2]==board[2][3])){//row 3
if(board[2][0]==X){
highCountX=4;
}else if(board[2][0]==O){
highCountO=4;
}
}*/
if(highCountX==highCountO){
return Blank;
}else if(highCountX>highCountX){
return X;
}else if(highCountX<highCountO){
return O;
}
}
}