-
Notifications
You must be signed in to change notification settings - Fork 1
/
TicTacToeClasses.cpp
185 lines (167 loc) · 4.62 KB
/
TicTacToeClasses.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
/*The Class header file for main.cpp*/
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
using namespace std;
class Board
{//the class tracks the game and the winner
char positionsSelected[16];
char winner;
public:
/*This is another way of doing methods in classes.
Instead of listing the methods in the class,
Then defining them afterwards, the methods are
defined within the class definition.
The downside of this method is: it is not immediately
clear what methods are in the class.
*/
Board()
{//sets the board to blanks and the winner to 'z'
//std::cout<<"Creating a board\n";
winner = 'z'; //not determined or tie
for(int i = 0; i < 16; i++)
{
positionsSelected[i] = '_';
}
}
char* getPositions(void)
{//return all the positions on the board
return positionsSelected;
}
int setPosition(int gridNumber, char user)
{//set a given position to x or o
{
if(positionsSelected[gridNumber] == '_')
{
positionsSelected[gridNumber] = user;
return 0;
}
else
{
//cout<<"That position is taken\n";
return -1;
}
}
return 0;
}
char checkRows()
{//check the rows for a winner
int rows = 0;
int columns = 0;
int fourInRowX = 0;
int fourInRowO = 0;
//Check rows for a winner
for(int rows = 0; rows<16; rows=rows+4)
{
for(int i = 0; i < 4; i++)
{
if(positionsSelected[i + rows] == 'x')
fourInRowX++;
if(positionsSelected[i + rows] == 'o')
fourInRowO++;
}
if(fourInRowX == 4)
{
//cout<<"ROWS X won\n";
winner = 'x';
return 'x';
}
if(fourInRowO == 4)
{
//cout<<"ROWS O won\n";
winner = 'o';
return 'o';
}
fourInRowX = 0;
fourInRowO = 0;
}
return 'z';
}
char checkColumns()
{//check the columns for a winner
int rows = 0;
int columns = 0;
int fourInRowX = 0;
int fourInRowO = 0;
//Check columns for a winner
for(int columns = 0; columns<4; columns++)
{
for(int i = 0; i < 16; i= i + 4)
{
if(positionsSelected[i + columns] == 'x')
fourInRowX++;
if(positionsSelected[i + columns] == 'o')
fourInRowO++;
}
if(fourInRowX == 4)
{
//cout<<"COLUMNS X won\n";
winner = 'x';
return 'x';
}
if(fourInRowO == 4)
{
//cout<<"COLUMNs O won\n";
winner = 'o';
return 'o';
}
fourInRowX = 0;
fourInRowO = 0;
}
return 'z';
}
char checkDiagonals()
{//check the diagonals for a winner
char winner = 'z';
int fourInRowX = 0;
int fourInRowO = 0;
//check left to right diagonal
for(int i = 0; i < 16; i=i+5)
{
if(positionsSelected[i] == 'x')
fourInRowX++;
if(positionsSelected[i] == 'o')
fourInRowO++;
}
//check right to left diagonal
if(fourInRowO != 4 and fourInRowX != 4)
{
fourInRowX = 0;
fourInRowO = 0;
for(int i = 3; i < 15; i= i+3)
{
if(positionsSelected[i] == 'x')
fourInRowX++;
if(positionsSelected[i] == 'o')
fourInRowO++;
}
}
if(fourInRowX == 4)
{
//cout<<"DIAGONALS X won\n";
winner = 'x';
return winner;
}
if(fourInRowO == 4)
{
//cout<<"DIAGONALS O won\n";
winner = 'o';
return winner;
}
fourInRowX = 0;
fourInRowO = 0;
return winner;
}
char determineWinner()
{//if 4 in a row, then there is a winner
char winner = 'z';
winner = checkRows();
if(winner == 'z')
winner = checkColumns();
if(winner =='z')
winner = checkDiagonals();
return winner;
}
};