-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy path35_NoughtsDeterminer.cpp
171 lines (157 loc) · 4.48 KB
/
35_NoughtsDeterminer.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
// For this challenge you will be determining the winning position of a Tic-tac-toe board.
/*
have the function NoughtsDeterminer(strArr) take the strArr parameter being passed which will be an array of size eleven. The array will take the shape of a Tic-tac-toe board with spaces strArr[3] and strArr[7] being the separators ("<>") between the rows, and the rest of the spaces will be either "X", "O", or "-" which signifies an empty space. So for example strArr may be ["X","O","-","<>","-","O","-","<>","O","X","-"]. This is a Tic-tac-toe board with each row separated double arrows ("<>"). Your program should output the space in the array by which any player could win by putting down either an "X" or "O". In the array above, the output should be 2 because if an "O" is placed in strArr[2] then one of the players wins. Each board will only have one solution for a win, not multiple wins. You output should never be 3 or 7 because those are the separator spaces.
*/
#include <iostream>
#include <string>
using namespace std;
bool checkHorizontal(string[],int,string);
bool checkVertical(string[],int,string);
bool checkDiagonal(string[],int, string);
/*
Solution involves traversing the input array
at each index equal to "-" we check for possible solutions
we call on helper functions to check for horizontal,vertical and diagonal solution
*/
int NoughtsDeterminer(string strArr[])
{
// traverse the array
for (int x = 0; x < 11; x++)
{
// condition to check if current spot has a winning condition for either X or O
if (strArr[x] == "-")
{
if (checkHorizontal(strArr, x, "O") || checkVertical(strArr, x, "O") || checkDiagonal(strArr,x, "O"))
{
return x;
}
else if (checkHorizontal(strArr, x, "X") || checkVertical(strArr, x, "X") || checkDiagonal(strArr,x, "X"))
{
return x;
}
}
}
}
// method to check for winning solution going horizontal
bool checkHorizontal(string arr[], int index, string symbol)
{
// checking which row to analyze
if (index < 3)
{
for (int x = 0; x < 3; x++)
{
if (x == index)
{
continue;
}
if (arr[x] != symbol)
{
return false;
}
}
return true;
}
else if (index > 3 && index < 7)
{
for (int x = 4; x < 7; x++)
{
if (x == index)
{
continue;
}
if (arr[x] != symbol)
{
return false;
}
}
return true;
}
else if (index > 7)
{
for (int x = 8; x < 11; x++)
{
if (x == index)
{
continue;
}
if (arr[x] != symbol)
{
return false;
}
}
return true;
}
return false;
}
// method to check for a winning solution going vertical
bool checkVertical(string arr[], int index, string symbol)
{
// locating index location
if (index < 3)
{
if (arr[index+4] == symbol && arr[index+8] == symbol)
{
return true;
}
}
else if (index > 3 && index < 7)
{
if (arr[index+4] == symbol && arr[index-4] == symbol)
{
return true;
}
}
else if (index > 7)
{
if (arr[index-4] == symbol && arr[index-8] == symbol)
{
return true;
}
}
return false;
}
// method to check for a winning solution going diagonal
bool checkDiagonal(string arr[], int index, string symbol)
{
// checking which row to analyze
if (index < 3)
{
if ((arr[5] == symbol && arr[8] == symbol && index == 2) || (arr[5] == symbol && arr[10] == symbol && index == 0))
{
return true;
}
}
else if (index > 3 && index < 7)
{
if ((arr[2] == symbol && arr[8] == symbol && index == 5) || (arr[0] == symbol && arr[10] == symbol && index == 5))
{
return true;
}
}
else if (index > 7)
{
if ((arr[5] == symbol && arr[0] == symbol && index == 10) || (arr[5] == symbol && arr[2] == symbol && index == 8))
{
return true;
}
}
return false;
}
int main()
{
string A[] = { "X", "O", "-", "<>", "-", "O", "-", "<>", "O", "X", "-" };
string B[] = { "X", "-", "O", "<>", "-", "-", "O", "<>", "-", "-", "X" };
string C[] = { "X", "O", "X", "<>", "-", "O", "O", "<>", "X", "X", "O" };
string D[] = { "O", "-", "O", "<>", "-", "X", "-", "<>", "-", "-", "X" };
string E[] = { "X", "-", "X", "<>", "-", "O", "-", "<>", "-", "-", "O" };
string F[] = { "X", "-", "X", "<>", "-", "-", "O", "<>", "O", "-", "-" };
string G[] = { "X", "O", "X", "<>", "-", "O", "-", "<>", "-", "-", "-" };
cout << NoughtsDeterminer(A) << endl; // 2
cout << NoughtsDeterminer(B) << endl; // 5
cout << NoughtsDeterminer(C) << endl; // 4
cout << NoughtsDeterminer(D) << endl; // 1
cout << NoughtsDeterminer(E) << endl; // 1
cout << NoughtsDeterminer(F) << endl; // 1
cout << NoughtsDeterminer(G) << endl; // 9
return 0;
}