forked from ChicoState/TicTacToeBoard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToeBoard.cpp
More file actions
165 lines (140 loc) · 3.33 KB
/
TicTacToeBoard.cpp
File metadata and controls
165 lines (140 loc) · 3.33 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
#include "TicTacToeBoard.h"
/**
* Class for representing a 3x3 Tic-Tac-Toe game board, using the Piece enum
* to represent the spaces on the board.
**/
//Constructor sets an empty board and specifies it is X's turn first
TicTacToeBoard::TicTacToeBoard()
{
turn = X;
for(int i=0; i<BOARDSIZE; i++)
for(int j=0; j<BOARDSIZE; j++)
board[i][j] = Blank;
}
/**
* Switches turn member variable to represent whether it's X's or O's turn
* and returns whose turn it is
**/
Piece TicTacToeBoard::toggleTurn()
{
if(turn == X)
{
turn = O;
return turn;
}
if(turn == O)
{
turn = X;
return turn;
}
return Invalid;
}
/**
* Places the piece of the current turn on the board, returns what
* piece is placed, and toggles which Piece's turn it is. placePiece does
* NOT allow to place a piece in a location where there is already a piece.
* In that case, placePiece just returns what is already at that location.
* Out of bounds coordinates return the Piece Invalid value. When the game
* is over, no more pieces can be placed so attempting to place a piece
* should neither change the board nor change whose turn it is.
**/
Piece TicTacToeBoard::placePiece(int row, int column)
{
/*
BUG: Switched the OR's for AND's in this for loop, normally wouldn't allow and of these things when placing
a pice but will allow with bug if one holds true.
*/
if(row < 0 || column < 0 || row > BOARDSIZE-1 || column > BOARDSIZE-1)
{
toggleTurn();
return Invalid;
}
if(board[row][column] == Blank)
{
board[row][column] = turn;
toggleTurn();
return board[row][column];
}
if(board[row][column] != Blank)
{
toggleTurn();
return board[row][column];
}
return Invalid;
}
/**
* 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 TicTacToeBoard::getPiece(int row, int column)
{
if(row < 0 || column < 0 || row > BOARDSIZE-1 || column > BOARDSIZE-1)
{
return Invalid;
}
else if(board[row][column] != O && board[row][column] != X)
{
return Blank;
}
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.
**/
Piece TicTacToeBoard::getWinner()
{
for(int i = 0; i<BOARDSIZE; i++)
{
for(int j = 0; j<BOARDSIZE; j++)
{
if(board[i][j] != X && board[i][j] != O)
{
return Invalid;
}
}
}
int i = 0;
int j = 0;
for(i = 0; i<BOARDSIZE; i++)
{
if(board[i][j] == O && board[i][j+1] == O && board[i][j+2] == O)
{
return O;
}
else if(board[i][j] == X && board[i][j+1] == X && board[i][j+2] == X)
{
return X;
}
}
i = 0;
j = 0;
for(j = 0; j<BOARDSIZE; j++)
{
if(board[i][j] == X && board[i+1][j] == X && board[i+2][j] == X)
{
return X;
}
else if (board[i][j] == O && board[i+1][j] == O && board[i+2][j] == O)
{
return O;
}
}
if(board[0][0] == X && board[1][1] == X && board[2][2] == X)
{
return X;
}
else if(board[2][2] == X && board[1][1] == X && board[0][0] == X)
{
return X;
}
if(board[0][0] == O && board[1][1] == O && board[2][2] == O)
{
return O;
}
else if(board[2][2] == O && board[1][1] == O && board[0][0] == O)
{
return O;
}
return Blank;
}