-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttt.cpp
205 lines (187 loc) · 6.05 KB
/
ttt.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
// Welcome to my Tic Tac Toe program
// Global Variable Declarations
std::vector<char> boardrow1 = {' ','a',' ','|',' ','b',' ','|',' ','c',' ','\n'};
std::vector<char> boardrow2 = {' ','d',' ','|',' ','e',' ','|',' ','f',' ','\n'};
std::vector<char> boardrow3 = {' ','g',' ','|',' ','h',' ','|',' ','i',' ','\n'};
std::string boardrowX = "---+---+---\n";
std::string player1 = "Play1", player2 = "Play2";
std::string first = "Elon", second = "Musk";
char move;
int moves = 0;
bool choice = false, gameover = false;
std::string winner;
// function declarations
void intro();
void gameboard (std::vector<char> boardrow1, std::vector<char> boardrow2, std::vector<char> boardrow3, std::string boardrowX);
void checkwinner();
void announcewinner(char XO);
int main () {
intro();
/* Removed for texting purposes
std::cout << "Greetings! First, let's get your names!\nPlayer 1, what is your name?\n";
std::cin >> player1;
std::cout << "Thanks, " << player1 << "!\nNow Player 2, what is your name?\n";
std::cin >> player2;
std::cout << "Awesome, " << player2 << "!\nNow let's play a game of Tic-Tac-Toe!\n\nNow let's find out who goes first!\n\n";
*/
// get order of players
srand(time(0));
if (rand() % 2 == 0) {
first = player1;
second = player2;
}
else {
first = player2;
second = player1;
}
std::cout << "The computer has randomly decided that " << first << " will go first as the Xs and " << second << " will go second as the Os!\n\n";
// provide instruction and initial board layout
std::cout << "This is what the gameboard looks like:\n\n";
gameboard(boardrow1, boardrow2, boardrow3, boardrowX);
std::cout << "When it is your turn, enter the letter of the space you want to go in.\n\n";
while (gameover == false) {
// first turn
std::cout << "Ok, " << first << " it is your turn.\n";
//check to make sure it is a valid move
choice = false;
while (choice == false) {
std::cout << "Enter the letter of the space you want to go in.\n\nHere is the current gameboard for reference:\n\n";
gameboard(boardrow1, boardrow2, boardrow3, boardrowX);
std::cin >> move;
for (int i=0; i < boardrow1.size(); i++) {
if (move == boardrow1[i] && move != '|' && move != ' ') {
boardrow1[i] = 'X';
moves++;
choice = true;
}
}
for (int i=0; i < boardrow2.size(); i++) {
if (move == boardrow2[i] && move != '|' && move != ' ') {
boardrow2[i] = 'X';
moves++;
choice = true;
}
}
for (int i=0; i < boardrow3.size(); i++) {
if (move == boardrow3[i] && move != '|' && move != ' ' ) {
boardrow3[i] = 'X';
moves++;
choice = true;
}
}
if (choice == false) {
std::cout << "---->>>>That is not a valid choice.\n Please try again.<<<<----\n\n";
}
} // end first turn while
// check to see if there is a winner yet
checkwinner();
// second turn
if (gameover == false) {
std::cout << "Ok, " << second << " it is your turn.\n";
//check to make sure it is a valid move
choice = false;
while (choice == false) {
std::cout << "Enter the letter of the space you want to go in.\n\nHere is the current gameboard for reference:\n\n";
gameboard(boardrow1, boardrow2, boardrow3, boardrowX);
std::cin >> move;
for (int i=0; i < boardrow1.size(); i++) {
if (move == boardrow1[i] && move != '|' && move != ' ') {
boardrow1[i] = 'O';
moves++;
choice = true;
}
}
for (int i=0; i < boardrow2.size(); i++) {
if (move == boardrow2[i] && move != '|' && move != ' ') {
boardrow2[i] = 'O';
moves++;
choice = true;
}
}
for (int i=0; i < boardrow3.size(); i++) {
if (move == boardrow3[i] && move != '|' && move != ' ' ) {
boardrow3[i] = 'O';
moves++;
choice = true;
}
}
if (choice == false) {
std::cout << "---->>>>That is not a valid choice.\n Please try again.<<<<----\n\n";
}
} // end second turn while
} // end if
// check to see if there is a winner yet
checkwinner();
} // end gameover while
} // end main
// functions
void intro() {
std::cout << "-------------------------------\nWelcome to the World Championships\nof Tic-Tac-Toe\n-------------------------------\n";
}
void gameboard (std::vector<char> boardrow1, std::vector<char> boardrow2, std::vector<char> boardrow3, std::string boardrowX) {
for (int i=0; i < boardrow1.size(); i++) {
std::cout << boardrow1[i];
}
std::cout << boardrowX;
for (int i=0; i < boardrow2.size(); i++) {
std::cout << boardrow2[i];
}
std::cout << boardrowX;
for (int i=0; i < boardrow3.size(); i++) {
std::cout << boardrow3[i];
}
}
void checkwinner() {
std::cout << "Here is the current gameboard:\n\n";
gameboard(boardrow1, boardrow2, boardrow3, boardrowX);
//check top row
if (boardrow1[1] == boardrow1[5] && boardrow1[1] == boardrow1[9]) {
announcewinner(boardrow1[1]);
}
//check middle row
else if (boardrow2[1] == boardrow2[5] && boardrow2[1] == boardrow2[9]) {
announcewinner(boardrow2[1]);
}
//check bottom row
else if (boardrow3[1] == boardrow3[5] && boardrow3[1] == boardrow3[9]) {
announcewinner(boardrow3[1]);
}
//check left column
else if (boardrow1[1] == boardrow2[1] && boardrow1[1] == boardrow3[1]) {
announcewinner(boardrow1[1]);
}
//check middle column
else if (boardrow1[5] == boardrow2[5] && boardrow1[5] == boardrow3[5]) {
announcewinner(boardrow1[5]);
}
//check right column
else if (boardrow1[9] == boardrow2[9] && boardrow1[9] == boardrow3[9]) {
announcewinner(boardrow1[9]);
}
//check \ diagonal
else if (boardrow1[1] == boardrow2[5] && boardrow1[1] == boardrow3[9]) {
announcewinner(boardrow1[1]);
}
//check / diagonal
else if (boardrow1[9] == boardrow2[5] && boardrow1[9] == boardrow3[1]) {
announcewinner(boardrow1[9]);
}
else if (moves == 9) {
std::cout << "Cats Eye! It's a draw! Nobody won.\n";
gameover = true;
}
}
void announcewinner(char XO) {
if (XO == 'X') {
std::cout << first << " is the winner!\n\n";
gameover = true;
}
else {
std::cout << second << " is the winner!\n\n";
gameover = true;
}
}