-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
82 lines (77 loc) · 2.79 KB
/
Copy pathBoard.cpp
File metadata and controls
82 lines (77 loc) · 2.79 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
#include "Errors.h"
#include "Board.h"
#include <iostream>
Board::Board() {
player = 'a';
current_state = "";
}
std::string Board::get_current_state() {
return current_state;
}
void Board::enter_move(char player, int row, int col) {
row--;
col--;
if (game_state[row][col] == 0) {
game_state[row][col] = player;
this->player = (player == 'X' ? 'O' : 'X');
} else {
throw InvalidMove("invalid location");
}
}
//79-O 88-X
bool Board::checkgame() {
if (player == 'a') {
current_state = "Game in progress: New game.\n";
return true;
}
//horizontal O
if ((game_state[0][0] + game_state[0][1] + game_state[0][2]) == 79*3 || (game_state[1][0] + game_state[1][1] + game_state[1][2]) == 79*3 || (game_state[2][0] + game_state[2][1] + game_state[2][2]) == 79*3) {
current_state = "Game over: O wins.\n";
return false;
}
//vertical O
if ((game_state[0][0] + game_state[1][0] + game_state[2][0]) == 79*3 || (game_state[0][1] + game_state[1][1] + game_state[2][1]) == 79*3 || (game_state[0][2] + game_state[1][2] + game_state[2][2]) == 79*3) {
current_state = "Game over: O wins.\n";
return false;
}
//diagonal O
if ((game_state[0][0] + game_state[1][1] + game_state[2][2]) == 3*79 || (game_state[0][2] + game_state[1][1] + game_state[2][0]) == 79*3) {
current_state = "Game over: O wins.\n";
return false;
}
//horizontal X
if ((game_state[0][0] + game_state[0][1] + game_state[0][2]) == 88*3 || (game_state[1][0] + game_state[1][1] + game_state[1][2]) == 88*3 || (game_state[2][0] + game_state[2][1] + game_state[2][2]) == 88*3) {
current_state = "Game over: X wins.\n";
return false;
}
//vertical X
if ((game_state[0][0] + game_state[1][0] + game_state[2][0]) == 88*3 || (game_state[0][1] + game_state[1][1] + game_state[2][1]) == 88*3 || (game_state[0][2] + game_state[1][2] + game_state[2][2]) == 88*3) {
current_state = "Game over: X wins.\n";
return false;
}
//diagonal X
if ((game_state[0][0] + game_state[1][1] + game_state[2][2]) == 3*88 || (game_state[0][2] + game_state[1][1] + game_state[2][0]) == 88*3) {
current_state = "Game over: X wins.\n";
return false;
}
bool is_full = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (game_state[i][j] == 0) {
is_full = false;
break;
}
}
}
if (is_full) {
current_state = "Game over: Draw.\n";
return false;
}
if (player == 'O') {
current_state = "Game in progress: O's turn.\n";
return true;
} else {
current_state = "Game in progress: X's turn.\n";
return true;
}
}