-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic-tac-toe-human.cpp
125 lines (107 loc) · 2.98 KB
/
tic-tac-toe-human.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
#include <iostream>
#include <string>
using namespace std;
char space[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
int row, col;
char token = 'X';
string n1 = "";
string n2 = "";
bool tie = false;
void design() {
cout << " | | \n";
cout << " " << space[0][0] << " | " << space[0][1] << " | " << space[0][2] << " \n";
cout << "_____|_____|_____\n";
cout << " | | \n";
cout << " " << space[1][0] << " | " << space[1][1] << " | " << space[1][2] << " \n";
cout << "_____|_____|_____\n";
cout << " | | \n";
cout << " " << space[2][0] << " | " << space[2][1] << " | " << space[2][2] << " \n";
cout << " | | \n";
}
void check() {
int digit;
if (token == 'X') {
cout << n1 << ", please enter a number: ";
} else {
cout << n2 << ", please enter a number: ";
}
cin >> digit;
row = (digit - 1) / 3;
col = (digit - 1) % 3;
if (digit < 1 || digit > 9 || space[row][col] == 'X' || space[row][col] == '0') {
cout << "Invalid move. Try again.\n";
check();
}
else{
space[row][col] = token;
if (token == 'X') {
token = '0';
}
else{
token = 'X';
}
}
design();
}
bool result() {
for (int i = 0; i < 3; i++) {
if (space[i][0] == space[i][1] && space[i][1] == space[i][2]) {
return true;
}
if (space[0][i] == space[1][i] && space[1][i] == space[2][i]) {
return true;
}
}
if (space[0][0] == space[1][1] && space[1][1] == space[2][2]) {
return true;
}
if (space[0][2] == space[1][1] && space[1][1] == space[2][0]) {
return true;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (space[i][j] != 'X' && space[i][j] != '0') {
return false;
}
}
}
tie = true;
return false;
}
void resetBoard() {
char initial = '1';
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
space[i][j] = initial++;
}
}
token = 'X';
tie = false;
}
int main2() {
char playAgain;
do {
cout << "Enter the name of the first player: ";
cin >> n1;
cout << "Enter the name of the second player: ";
cin >> n2;
cout << n1 << " is player 1, so they will play first.\n";
cout << n2 << " is player 2, so they will play second.\n";
resetBoard();
design();
while (!result()) {
check();
}
if (token == 'X' && !tie) {
cout << n2 << " wins!\n";
} else if (token == '0' && !tie) {
cout << n1 << " wins!\n";
} else {
cout << "It's a draw!\n";
}
cout << "Do you want to play again? (y/n): ";
cin >> playAgain;
} while (playAgain == 'y' || playAgain == 'Y');
cout << "Thank you for playing!\n";
return 0;
}