-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.cpp
More file actions
112 lines (102 loc) · 3.01 KB
/
tictactoe.cpp
File metadata and controls
112 lines (102 loc) · 3.01 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
#include <iostream>
using namespace std;
void drawBoard(char *spaces);
void playerMove(char * spaces, char player);
void computerMove(char * spaces, char computer);
bool checkWinner(char *spaces, char mark);
bool checkTie(char *spaces);
int main() {
char spaces[9] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
char player = 'X';
char computer = 'O';
bool running = true;
drawBoard(spaces);
while(running) {
computerMove(spaces, computer);
drawBoard(spaces);
if(checkWinner(spaces, computer)) {
running = false;
cout << "You lose! Computer wins!" << '\n';
return 0;
} else if(checkTie(spaces)) {
running = false;
cout << "It's a tie!" << '\n';
return 0;
}
playerMove(spaces, player);
drawBoard(spaces);
if(checkWinner(spaces, player)) {
running = false;
cout << "You win! You beat the computer!" << '\n';
return 0;
} else if(checkTie(spaces)) {
running = false;
cout << "It's a tie!" << '\n';
return 0;
}
}
return 0;
}
void drawBoard(char *spaces) {
cout << '\n';
cout << " | | " << '\n';
cout << " " << spaces[0] << " | " << spaces[1] << " | " << spaces[2] << " " << '\n';
cout << "_____|_____|_____" << '\n';
cout << " | | " << '\n';
cout << " " << spaces[3] << " | " << spaces[4] << " | " << spaces[5] << " " << '\n';
cout << "_____|_____|_____" << '\n';
cout << " | | " << '\n';
cout << " " << spaces[6] << " | " << spaces[7] << " | " << spaces[8] << " " << '\n';
cout << " | | " << '\n';
cout << '\n';
}
void playerMove(char * spaces, char player) {
int number;
do {
cout << "Enter a spot to place a marker (1-9): " << '\n';
cin >> number;
number--;
if(spaces[number] == ' ') {
spaces[number] = player;
break;
}
} while(!number > 0 || !number < 8);
}
void computerMove(char * spaces, char computer) {
int number;
srand(time(0));
while(true) {
number = rand() % 9;
if(spaces[number] == ' ') {
spaces[number] = computer;
break;
}
}
}
bool checkWinner(char *spaces, char mark) {
for(int i = 0; i < 9; i += 3) {
if(spaces[i] == mark && spaces[i+1] == mark && spaces[i+2] == mark) {
return true;
}
}
for(int i = 0; i < 3; i++) {
if(spaces[i] == mark && spaces[i+3] == mark && spaces[i+6] == mark) {
return true;
}
}
if(spaces[0] == mark && spaces[4] == mark && spaces[8] == mark) {
return true;
}
if(spaces[2] == mark && spaces[4] == mark && spaces[6] == mark) {
return true;
}
return false;
}
bool checkTie(char *spaces) {
for(int i = 0; i < 9; i++) {
if(spaces[i] == ' ') {
return false;
}
}
return true;
}