-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe
154 lines (137 loc) · 5.27 KB
/
TicTacToe
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
#include <iostream>
#include <stdlib.h>
void intro();
void displayBoard(char board[3][3]);
int makePlayerChoice();
bool isEmptySpace(int playerChoice, char board[3][3]);
bool isAWin(char board[3][3]);
void playAgainChoice(char &playAgain, bool &restart, int &drawCounter, char board[3][3]);
int main() {
char board[3][3] = {{' ',' ',' '},
{' ',' ',' '},
{' ',' ',' '} };
int playerChoice;
bool restart = true;
bool gameOver = false;
int drawCounter = 0;
while (restart == true) {
intro();
while (isAWin(board) == false && drawCounter != 9) {
//O's turn
std::cout << "\nO's turn, pick a spot\n" << std::endl;
displayBoard(board);
playerChoice = makePlayerChoice();
while (isEmptySpace(playerChoice, board) == false) {
std::cout << "Not empty, please pick another spot!" << std::endl;
playerChoice = makePlayerChoice();
}
board[playerChoice / 10][playerChoice % 10] = 'O';
drawCounter += 1;
system("CLS");
if (isAWin(board) == true) {
std::cout << "\nO wins!\n" << std::endl;
}
// X's Turn check to see if 0 won
if (isAWin(board) == false && drawCounter != 9) {
std::cout << "\nX's turn, pick a spot\n" << std::endl;
displayBoard(board);
playerChoice = makePlayerChoice();
while (isEmptySpace(playerChoice, board) == false) {
std::cout << "Not a valid move, please pick another spot!" << std::endl;
playerChoice = makePlayerChoice();
}
board[playerChoice / 10][playerChoice % 10] = 'X';
drawCounter += 1;
system("CLS");
if (isAWin(board) == true) {
std::cout << "\nX wins!\n" << std::endl;
}
}
}
displayBoard(board);
if (drawCounter == 9 && isAWin(board) == false) {
std::cout << "Game ended in a draw.\n" << std::endl;
}
char playAgain;
std::cout << "Do you want to play again?\n Type \"Y\" to play again or anything else to terminate" << std::endl;
std::cin >> playAgain;
playAgainChoice(playAgain, restart, drawCounter, board);
}
return 0;
}
void intro() {
std::cout << " _____ _ _____ _____ " << std::endl;
std::cout << " |_ _(_) ___ |_ ___ _ ___ |_ ____ ___ " << std::endl;
std::cout << " | | | |/ _______| |/ _` |/ _______| |/ _ \\ / _ \\" << std::endl;
std::cout << " | | | | (_|_____| | (_| | (_|_____| | (_) | __/" << std::endl;
std::cout << " |_| |_|\\___| |_|\\__,_|\\___| |_|\\___/ \\___| " << std::endl;
std::cout << "=========================================================\n" << std::endl;
}
//shows the board
void displayBoard(char board[3][3]) {
std::cout << " 1C 2C 3C\n" << std::endl;
std::cout << "1R " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << std::endl;
std::cout << " ------------" << std::endl;
std::cout << "2R " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << std::endl;
std::cout << " ------------" << std::endl;
std::cout <<"3R " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << std::endl;
std::cout << "\nR = Row\n" << "C = Column\n" << std::endl;
}
//turns the row &_ column into one number for use
int makePlayerChoice() {
int playerRow;
int playerColumn;
std::cout << "Please enter the row to place in (1-3)" << std::endl;
std::cin >> playerRow;
playerRow -= 1;
std::cout << "Please enter the column (1-3)" << std::endl;
std::cin >> playerColumn;
playerColumn -= 1;
playerRow *= 10;
return playerRow + playerColumn;
}
//checks for empty board space using /10 and %10
bool isEmptySpace(int playerChoice, char board[3][3]) {
if (board[playerChoice / 10][playerChoice % 10] == ' ') {
return true;
}
else return false;
}
//Win Logic
bool isAWin(char board[3][3]) {
if ((board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O') ||
(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')) return true;
if ((board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O') ||
(board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')) return true;
if ((board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O') ||
(board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')) return true;
if ((board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O') ||
(board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')) return true;
if ((board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O') ||
(board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')) return true;
if ((board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O') ||
(board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')) return true;
if ((board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O') ||
(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')) return true;
if ((board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O') ||
(board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X')) return true;
else return false;
}
//Play again Y clears the game to beginning.
void playAgainChoice(char &playAgain, bool &restart, int &drawCounter, char board[3][3]) {
if (playAgain == 'Y') {
restart = true;
system("CLS");
drawCounter = 0;
for (int i = 0; i < 3; ++i) {
for (int x = 0; x < 3; ++x) {
board[i][x] = ' ';
}
}
isAWin(board);
}
else {
restart = false;
std::cout << "Thank you for playing!" << std::endl;
}
}