-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObstacle.cpp
More file actions
128 lines (99 loc) · 3.36 KB
/
Obstacle.cpp
File metadata and controls
128 lines (99 loc) · 3.36 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
127
128
#include <iostream>
#include <iomanip>
#include <cctype> // for toupper()
#include "Obstacle.h"
using namespace std;
Obstacle_Board::Obstacle_Board() : Board(6, 6) {
for (auto& row : board)
for (auto& cell : row)
cell = blank_symbol;
}
bool Obstacle_Board::update_board(Move<char>* move) {
int x = move->get_x();
int y = move->get_y();
char mark = move->get_symbol();
// Validate move
if (x < 0 || x >= rows || y < 0 || y >= columns)
return false;
if (board[x][y] != blank_symbol)
return false;
// Apply move
board[x][y] = toupper(mark);
n_moves++;
// After every 2 moves add obstacles
if (n_moves % 2 == 0) {
add_obstacles(2);
}
return true;
}
bool Obstacle_Board::is_win(Player<char>* player) {
const char sym = player->get_symbol();
int directions[4][2] = {
{1, 0},
{0, 1},
{1, 1},
{1,-1}
};
for (int r = 0; r < rows; r++) {
for (int c = 0; c < columns; c++) {
if (board[r][c] != sym) continue;
for (auto& d : directions) {
int dr = d[0], dc = d[1];
int count = 1;
for (int k = 1; k < 4; k++) {
int nr = r + dr * k;
int nc = c + dc * k;
if (nr < 0 || nr >= rows || nc < 0 || nc >= columns)
break;
if (board[nr][nc] == sym)
count++;
else
break;
}
if (count == 4)
return true;
}
}
}
return false;
}
void Obstacle_Board::add_obstacles(int count) {
vector<pair<int, int>> empty_cells;
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++)
if (board[r][c] == blank_symbol)
empty_cells.emplace_back(r, c);
if (empty_cells.empty()) return;
for (int i = 0; i < count && !empty_cells.empty(); i++) {
int idx = rand() % empty_cells.size();
auto [r, c] = empty_cells[idx];
board[r][c] = '#';
empty_cells.erase(empty_cells.begin() + idx);
}
}
bool Obstacle_Board::is_draw(Player<char>* player) {
return (n_moves == rows * columns && !is_win(player));
}
bool Obstacle_Board::game_is_over(Player<char>* player) {
return is_win(player) || is_draw(player);
}
Obstacle_UI::Obstacle_UI() : UI<char>("Weclome to OBSTACLE!", 3) {}
Player<char>* Obstacle_UI::create_player(string& name, char symbol, PlayerType type) {
// Create player based on type
cout << "Creating " << (type == PlayerType::HUMAN ? "human" : "computer")
<< " player: " << name << " (" << symbol << ")\n";
return new Player<char>(name, symbol, type);
}
Move<char>* Obstacle_UI::get_move(Player<char>* player) {
int x, y;
if (player->get_type() == PlayerType::HUMAN) {
cout << "\nPlease enter your move x and y (0 to 5): ";
cin >> x >> y;
}
else if (player->get_type() == PlayerType::COMPUTER) {
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());
}