-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.cpp
More file actions
146 lines (120 loc) · 3.86 KB
/
TicTacToe.cpp
File metadata and controls
146 lines (120 loc) · 3.86 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
#include <iostream>
#include <vector>
using namespace std;
class TicTacToe {
private:
int board[3][3];
int side;
public:
TicTacToe() {
for(int x = 0; x < 3; x++) {
for(int y = 0; y < 3; y++) {
board[x][y] = -1;
}
}
side = 0;
}
/*
void testBoard() {
board[0][0] = 0;
board[0][1] = 1;
board[0][2] = 0;
board[1][0] = 1;
board[1][1] = 0;
board[1][2] = 1;
board[2][0] = 0;
board[2][1] = 1;
board[2][2] = 0;
}*/
void printBoard() {
for(int x = 0; x < 3; x++) {
for(int y = 0; y < 3; y++) {
switch(board[x][y]) {
case 0:
cout << "X";
break;
case 1:
cout << "O";
break;
default:
cout << " ";
break;
}
if(y < 2) {cout << " | ";}
}
cout << endl;
if(x < 2) {cout << "--+---+--" << endl;}
}
}
bool placeMarker(int side, int x, int y) {
if(board[x][y] != -1) {
if(board[x][y] == side) {
cout << "You already placed the marker there, please enter another coordinates";
} else {
cout << "The enemy already placed the marker there, please enter another coordinates";
}
return false;
} else {
board[x][y] = side;
return true;
}
}
void GetPlayerChoice() {
bool go = true;
bool placeable = false;
string x;
string y;
while(!placeable) {
if(side == 0) {cout << "X's turn";}
else {cout << "O's turn";}
while(go) {
x = "";
cout << endl << "Enter a row to move to 0-2: ";
cin >> x;
if (x == "0" || x == "1" || x == "2") {
go = false;
}
}
go = true;
while(go) {
y = "";
cout << endl << "Enter a column to move to 0-2: ";
cin >> y;
if (y == "0" || y == "1" || y == "2") {
go = false;
}
}
placeable = placeMarker(side, stoi(x), stoi(y));
}
side = (side+1)%2;
}
int checkGameEnd() {
int winner = 0;
for(int x = 0; x < 3; x++) {
if(board[x][0] == board[x][1] && board[x][1] == board[x][2]) {
winner = board[x][0] + 1;
}
}
for(int y = 0; y < 3; y++) {
if(board[0][y] == board[1][y] && board[1][y] == board[2][y]) {
winner = board[0][y] + 1;
}
}
if(board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
winner = board[1][1] + 1;
}
if(board[2][0] == board[1][1] && board[1][1] == board[0][2]) {
winner = board[1][1] + 1;
}
return winner;
}
};
int main() {
TicTacToe game = TicTacToe();
while(!game.checkGameEnd()) {
game.GetPlayerChoice();
game.printBoard();
}
if(game.checkGameEnd() == 1) {cout << "X wins the game" << endl;}
else {cout << "O wins the game" << endl;}
}