-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.cpp
188 lines (164 loc) · 4.39 KB
/
console.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
#include "Console.h"
void gameManager::start()
{
bool leave = false, checkifWon = false;
int i = 0;//this keeps track of move, (i+1)th move , so it mustn't exceed (sidelength)^2
while (!leave) {
system("CLS");//fresh start
cout << "welcome to the game!\n\n\n\n";
cout << "NOTE: To mark your symbol please use coordinate system\n";
init();
print(); //inital map ready
while (!checkifWon && i < sideLength*sideLength) {
// taking input for both players one after another
if (i % 2 == 0) {
inputSymbol(sPlayer1);
}
else {
inputSymbol(sPlayer2);
}
print();
//checking to see if somebody wins only after move 4 (small optimization)
if (i > 3) {
checkifWon = somebodyWon();
}
i++;
}
//setting the things back to inital as game is over
map.clear();
checkifWon = false;
cout << "\nDo you want to leave ? 1(True) or 0(False)";
cin >> leave;
}
}
void gameManager::init()
{
//setting the board
cout << "\n\n\nplease set the side length of the board (square board): ";
cin >> sideLength;
//error check
while (sideLength < 3) {
cout << "\nThe side Length can't be less the 3, please set the sideLength again: ";
cin >> sideLength;
cout << endl;
}
cout << "\nPlayer 1, please select the symbol you'd like to have: ";
cin >> sPlayer1;
cout << "\nPlayer 2, please select the symbol you'd like to have: ";
cin >> sPlayer2;
//filling the vector
string row;
//making one row
for (int i = 0; i < sideLength; i++)
{
row = row + ".";
}
//filling the vector
for (int i = 0; i < sideLength; i++)
{
map.push_back(row);
}
}
void gameManager::inputSymbol(char input_symbol)
{
cout << "x coordinate: ";
cin >> xPos;
cout << "y coordinate: ";
cin >> yPos;
//no idea what to do with the green squigly line
if(map[yPos - 1][xPos - 1] != sPlayer1 && map[yPos - 1][xPos - 1] != sPlayer2)
map[yPos - 1][xPos - 1] = input_symbol;
else {
inputSymbol(input_symbol); //loop to ensure that correct coordinates are given
}
}
void gameManager::print()
{
system("CLS");
//side length + 1 for printing out the numbers at the side (kind of x axis and y axis)
for (int i = 0; i < sideLength + 1; i++)
{
for (int j = 0; j < sideLength + 1; j++)
{
if (i == 0 && j == 0)
cout << "0";
else if (i == 0 && j != 0)
cout << "| " << j;
else if (j == 0 && i != 0)
cout << i;
else
cout << "| " << map[i - 1][j - 1];
}
cout << endl;
}
}
bool gameManager::somebodyWon()
{
//the repetitive checking bugs me a lot, tho no idea if it is possible to avoid them
if (checkColumn(sPlayer1) || checkRow(sPlayer1) || checkDiagonal_LeftRight(sPlayer1) || checkDiagonal_RightLeft(sPlayer1)) {
system("CLS");
cout << "Congrats Player 1 you've won !!!";
return true;
}
else if (checkColumn(sPlayer2) || checkRow(sPlayer2) || checkDiagonal_LeftRight(sPlayer2) || checkDiagonal_RightLeft(sPlayer2)) {
system("CLS");
cout << "Congrats Player 2 you've won !!!";
return true;
}
else
return false;
}
bool gameManager::checkColumn(char symbol)
{
//compares three things ( 3 vertical symbols ) at a time, moves through the vector
// -2 because the (number side length - 3) needs to be checked and also because up to down movemnt is limited
for (int i = 0; i < sideLength; i++)
{
for (int j = 0; j < sideLength - 2; j++)
{
if (map[j][i] == symbol && map[j + 1][i] == symbol && map[j + 2][i] == symbol)
return true;
}
}
return false;
}
bool gameManager::checkRow(char symbol)
{
//same as check coloumn, but this time the left to right movement is limited so - 2
for (int i = 0; i < sideLength - 2; i++)
{
for (int j = 0; j < sideLength; j++)
{
if (map[j][i] == symbol && map[j][i + 1] == symbol && map[j][i + 2] == symbol)
return true;
}
}
return false;
}
bool gameManager::checkDiagonal_LeftRight(char symbol)
{
//the whole thing is limited by how many 3 box diagonals you can draw
// 1 in 3*3, 4 in 4*4 , 9 in 5*5
for (int i = 0; i < sideLength - 1; i++)
{
for (int j = 0; j < sideLength - 1; j++)
{
if (map[i][j] == symbol && map[i + 1][j + 1] == symbol && map[i + 2][j + 2] == symbol)
return true;
}
}
return false;
}
bool gameManager::checkDiagonal_RightLeft(char symbol)
{
//same as above
for (int i = 0; i < sideLength - 2; i++)
{
for (int j = sideLength - 1; j >= 2; j--)
{
if (map[i][j] == symbol && map[i + 1][j - 1] == symbol && map[i + 2][j - 2] == symbol)
return true;
}
}
return false;
}