-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfinity_XO.cpp
More file actions
121 lines (93 loc) · 3.04 KB
/
Infinity_XO.cpp
File metadata and controls
121 lines (93 loc) · 3.04 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
#include "Infinity_XO.h"
#include <iostream>
#include <cctype>
#include <cstdlib>
using namespace std;
//==================== Board ====================
Infinity_XO_Board::Infinity_XO_Board() : Board<char>(3, 3)
{
for (auto& row : board)
for (auto& cell : row)
cell = blank_symbol;
}
bool Infinity_XO_Board::update_board(Move<char>* move)
{
int x = move->get_x();
int y = move->get_y();
char mark = toupper(move->get_symbol());
if (x < 0 || x >= rows || y < 0 || y >= columns)
return false;
if (board[x][y] != blank_symbol)
return false;
board[x][y] = mark;
n_moves++;
history.push_back(*move);
if (history.size() >= 3)
remove_oldest();
return true;
}
bool Infinity_XO_Board::is_win(Player<char>* player)
{
char sym = player->get_symbol();
auto eq = [&](char a, char b, char c) {
return a == b && b == c && a == sym;
};
for (int i = 0; i < 3; i++) {
if (eq(board[i][0], board[i][1], board[i][2])) return true;
if (eq(board[0][i], board[1][i], board[2][i])) return true;
}
if (eq(board[0][0], board[1][1], board[2][2])) return true;
if (eq(board[0][2], board[1][1], board[2][0])) return true;
return false;
}
bool Infinity_XO_Board::is_draw(Player<char>* player)
{
return (n_moves >= 9 && !is_win(player));
}
void Infinity_XO_Board::remove_oldest()
{
if (n_moves > 3 && (n_moves - 3) % 3 == 0 && history.size() >= 3)
{
Move<char> oldest = history.front();
history.pop_front();
int x = oldest.get_x();
int y = oldest.get_y();
board[x][y] = blank_symbol;
cout << "\n>> Removed oldest mark at (" << x << "," << y << ")\n";
}
}
void Infinity_XO_Board::remove_oldest_after_win()
{
if (!history.empty())
{
Move<char> oldest = history.front();
history.pop_front();
int x = oldest.get_x();
int y = oldest.get_y();
board[x][y] = blank_symbol;
cout << "\n>> Removed oldest mark after win/draw at (" << x << "," << y << ")\n";
}
}
//==================== UI ====================
Infinity_XO_UI::Infinity_XO_UI() : UI<char>("=== Infinity Tic-Tac-Toe ===", 3) {}
Player<char>* Infinity_XO_UI::create_player(string& name, char symbol, PlayerType type)
{
cout << "Creating " << (type == PlayerType::HUMAN ? "Human" : "Computer")
<< " player: " << name << " (" << symbol << ")\n";
return new Player<char>(name, symbol, type);
}
Move<char>* Infinity_XO_UI::get_move(Player<char>* player)
{
int x, y;
if (player->get_type() == PlayerType::HUMAN)
{
cout << "\nPlayer " << player->get_name() << " (" << player->get_symbol() << ") enter x y (0-2): ";
cin >> x >> y;
}
else
{
x = rand() % player->get_board_ptr()->get_rows();
y = rand() % player->get_board_ptr()->get_columns();
}
return new Move<char>(x, y, player->get_symbol());
}