-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodsoft_Task3.cpp
More file actions
126 lines (112 loc) · 3.47 KB
/
Copy pathCodsoft_Task3.cpp
File metadata and controls
126 lines (112 loc) · 3.47 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
void display_board(const vector<vector<char>>& board) {
cout << "+-------+-------+-------+" << endl;
for (int row = 0; row < 3; row++) {
cout << "| | | |" << endl;
for (int col = 0; col < 3; col++) {
cout << "| " << board[row][col] << " ";
}
cout << "|" << endl;
cout << "| | | |" << endl;
cout << "+-------+-------+-------+" << endl;
}
}
char check_win(const vector<vector<char>>& board) {
int win_conditions[8][3] = {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8},
{0, 3, 6}, {1, 4, 7}, {2, 5, 8},
{0, 4, 8}, {2, 4, 6}
};
for (const auto& condition : win_conditions) {
if (board[condition[0] / 3][condition[0] % 3] == board[condition[1] / 3][condition[1] % 3] &&
board[condition[1] / 3][condition[1] % 3] == board[condition[2] / 3][condition[2] % 3] &&
board[condition[0] / 3][condition[0] % 3] != ' ') {
return board[condition[0] / 3][condition[0] % 3];
}
}
return ' ';
}
bool is_full(const vector<vector<char>>& board) {
for (const auto& row : board) {
for (const auto& cell : row) {
if (cell >= '1' && cell <= '9') {
return false;
}
}
}
return true;
}
bool check_move(const vector<vector<char>>& board, int move) {
if (move < 1 || move > 9) {
return false;
}
int row = (move - 1) / 3;
int col = (move - 1) % 3;
return board[row][col] >= '1' && board[row][col] <= '9';
}
void make_move(vector<vector<char>>& board, int move, char player) {
int row = (move - 1) / 3;
int col = (move - 1) % 3;
board[row][col] = player;
}
void computer_move(vector<vector<char>>& board) {
while (true) {
int move = rand() % 9 + 1;
if (check_move(board, move)) {
make_move(board, move, 'X');
break;
}
}
}
void game() {
vector<vector<char>> board = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
while (true) {
display_board(board);
int move;
cout << "Enter your move (1-9) or 0 to quit: ";
cin >> move;
if (move == 0) {
cout << "Goodbye!" << endl;
break;
}
if (check_move(board, move)) {
make_move(board, move, 'O');
char winner = check_win(board);
if (winner != ' ') {
display_board(board);
cout << "You won!" << endl;
break;
} else if (is_full(board)) {
display_board(board);
cout << "It's a tie!" << endl;
break;
}
computer_move(board);
winner = check_win(board);
if (winner != ' ') {
display_board(board);
cout << "Computer won!" << endl;
break;
} else if (is_full(board)) {
display_board(board);
cout << "It's a tie!" << endl;
break;
}
} else {
cout << "Invalid move, try again." << endl;
}
}
}
int main() {
srand(static_cast<unsigned int>(time(0)));
game();
return 0;
}