-
Notifications
You must be signed in to change notification settings - Fork 0
/
TIC_TACTOE_game.cpp
120 lines (100 loc) · 3.78 KB
/
TIC_TACTOE_game.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
#include <iostream>
#include <ctime>
void drawBoard(char *spaces);
void playerMove(char *spaces, char player);
void computerMove(char *spaces, char computer);
bool checkWinner(char *spaces, char player, char computer);
bool checkTie(char *spaces);
int main() {
char spaces[9] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
char player = 'X';
char computer = 'O';
bool running = true;
drawBoard(spaces);
while (running) {
playerMove(spaces, player);
drawBoard(spaces);
if (checkWinner(spaces, player, computer)) {
running = false;
break;
} else if (checkTie(spaces)) {
running = false;
break;
}
computerMove(spaces, computer);
drawBoard(spaces);
if (checkWinner(spaces, player, computer)) {
running = false;
break;
} else if (checkTie(spaces)) {
running = false;
break;
}
}
std::cout << "Thanks for playing!\n";
return 0;
}
void drawBoard(char *spaces) {
std::cout << " | | " << '\n';
std::cout << " " << spaces[0] << " | " << spaces[1] << " | " << spaces[2] << '\n';
std::cout << "_____|_____|_____" << '\n';
std::cout << " | | " << '\n';
std::cout << " " << spaces[3] << " | " << spaces[4] << " | " << spaces[5] << '\n';
std::cout << "_____|_____|_____" << '\n';
std::cout << " | | " << '\n';
std::cout << " " << spaces[6] << " | " << spaces[7] << " | " << spaces[8] << '\n';
}
void playerMove(char *spaces, char player) {
int number;
do {
std::cout << "Enter a spot to place a marker (1 ~ 9) ";
std::cin >> number;
number--;
if (spaces[number] == ' ') {
spaces[number] = player;
break;
}
} while (!number > 0 || !number < 9);
}
void computerMove(char *spaces, char computer) {
int number;
srand(time(NULL));
while (true) {
number = rand() % 9;
if (spaces[number] == ' ') {
spaces[number] = computer;
break;
}
}
}
bool checkWinner(char *spaces, char player, char computer) {
if (spaces[0] == spaces[1] && spaces[1] == spaces[2] && spaces[0] != ' ') {
spaces[0] == player ? std::cout << "YOU WIN!\n" : std::cout << "YOU LOSE!\n";
} else if (spaces[3] == spaces[4] && spaces[4] == spaces[5] && spaces[3] != ' ') {
spaces[3] == player ? std::cout << "YOU WIN!\n" : std::cout << "YOU LOSE!\n";
} else if (spaces[6] == spaces[7] && spaces[7] == spaces[8] && spaces[6] != ' ') {
spaces[6] == player ? std::cout << "YOU WIN!\n" : std::cout << "YOU LOSE!\n";
} else if (spaces[0] == spaces[3] && spaces[3] == spaces[6] && spaces[0] != ' ') {
spaces[0] == player ? std::cout << "YOU WIN!\n" : std::cout << "YOU LOSE!\n";
} else if (spaces[1] == spaces[4] && spaces[4] == spaces[7] && spaces[1] != ' ') {
spaces[1] == player ? std::cout << "YOU WIN!\n" : std::cout << "YOU LOSE!\n";
} else if (spaces[2] == spaces[5] && spaces[5] == spaces[8] && spaces[2] != ' ') {
spaces[2] == player ? std::cout << "YOU WIN!\n" : std::cout << "YOU LOSE!\n";
} else if (spaces[0] == spaces[4] && spaces[4] == spaces[8] && spaces[0] != ' ') {
spaces[0] == player ? std::cout << "YOU WIN!\n" : std::cout << "YOU LOSE!\n";
} else if (spaces[2] == spaces[4] && spaces[4] == spaces[6] && spaces[2] != ' ') {
spaces[2] == player ? std::cout << "YOU WIN!\n" : std::cout << "YOU LOSE!\n";
} else {
return false;
}
return true;
}
bool checkTie(char *spaces) {
for (int i = 0; i < 9; i++) {
if (spaces[i] == ' ') {
return false;
}
}
std::cout << "It's a Tie!\n";
return true;
}