forked from InfiniBrains/SDL2-CPM-CMake-Example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
107 lines (94 loc) · 2.4 KB
/
Board.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
//
// Created by Alexandre Tolstenko Nogueira on 2024.03.21.
//
#include "Board.h"
#include <iostream>
void Board::print_board() const {
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(get_square(i,j)==Square::X){
std::cout<<"X ";
} else if(get_square(i,j)==Square::O){
std::cout<<"O ";
} else {
std::cout<<". ";
}
}
std::cout<<std::endl;
}
std::cout<<std::endl;
}
Square Board::get_square(int x, int y) const {
return static_cast<Square>((board >> (x * 2 + y * 6)) & 0b11);
}
void Board::set_square(int x, int y, Square square) {
board &= ~(0b11 << (x * 2 + y * 6));
board |= (static_cast<uint32_t>(square) << (x * 2 + y * 6));
}
Square Board::get_turn() const {
return turn;
}
void Board::set_turn(Square turn) {
this->turn = turn;
}
const uint32_t Board::get_board() const {
return board;
}
bool Board::operator==(const Board &other) const {
return board == other.board;
}
std::unordered_set<Board> Board::generate_children() const {
std::unordered_set<Board> children;
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
if (get_square(x, y) == Square::EMPTY) {
Board child = *this;
child.set_square(x, y, turn);
child.set_turn((Square) (!(bool) turn));
children.emplace(child);
}
}
}
return children;
}
bool Board::have_winner(Square square) const {
// vertical and horizontal
for (int i = 0; i < 3; i++) {
if (get_square(i, 0) == square && get_square(i, 1) == square && get_square(i, 2) == square) {
return true;
}
if (get_square(0, i) == square && get_square(1, i) == square && get_square(2, i) == square) {
return true;
}
}
// diagonal
if (get_square(0, 0) == square && get_square(1, 1) == square && get_square(2, 2) == square) {
return true;
}
if (get_square(0, 2) == square && get_square(1, 1) == square && get_square(2, 0) == square) {
return true;
}
return false;
}
Square Board::get_winner() const {
if (have_winner(Square::X)) {
return Square::X;
}
if (have_winner(Square::O)) {
return Square::O;
}
return Square::EMPTY;
}
bool Board::is_full() const {
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(get_square(i,j)==Square::EMPTY){
return false;
}
}
}
return true;
}
bool Board::is_terminal() const {
return have_winner(Square::X) || have_winner(Square::O) || is_full();
}