-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
128 lines (114 loc) · 3.2 KB
/
Board.cpp
File metadata and controls
128 lines (114 loc) · 3.2 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 "Board.h"
int random_int(int min, int max) {
std::random_device rd;
std::mt19937 rand_gen(rd());
std::uniform_int_distribution dist(min, max);
return dist(rand_gen);
}
Cell::Cell(int x, int y, int x_bound, int y_bound, char type, std::mutex* board_mutex, CellMatrix* board_grid)
: x(x), y(y), x_bound(x_bound), y_bound(y_bound), type(type), board_mutex(board_mutex), board_grid(board_grid), running(true) {
next_x = x;
next_y = y;
}
Cell::~Cell() {
stop();
if (cell_thread.joinable()) {
cell_thread.join();
}
}
void Cell::random_move() {
int rng = random_int(0, 3);
next_x = x;
next_y = y; // Reiniciar para valores atuais
switch (rng) {
case 0:
if (x > 0) next_x = x - 1;
break;
case 1:
if (x < x_bound) next_x = x + 1;
break;
case 2:
if (y > 0) next_y = y - 1;
break;
case 3:
if (y < y_bound) next_y = y + 1;
break;
default:
break;
}
}
void Cell::thread_safe_move() {
while (running.load()) { // Verifica a condição de término
random_move();
std::lock_guard<std::mutex> lock(*board_mutex);
if (next_x >= 0 && next_x <= x_bound && next_y >= 0 && next_y <= y_bound) {
if ((*board_grid)[next_x][next_y] == nullptr) {
(*board_grid)[x][y] = nullptr;
x = next_x;
y = next_y;
(*board_grid)[x][y] = this;
// Move a célula para a nova posição
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
void Cell::start() {
cell_thread = std::thread(&Cell::thread_safe_move, this);
}
void Cell::stop() {
running.store(false);
}
Board::Board(int rows, int cols) : rows(rows), cols(cols) {
grid = CellMatrix(rows, std::vector<Cell*>(cols, nullptr));
// Preenchendo a primeira linha
for (int j = 0; j < cols; ++j) {
grid[0][j] = new Cell(0, j, rows - 1, cols - 1, 'b', &board_mutex, &grid);
}
// Preenchendo a última linha
for (int j = 0; j < cols; ++j) {
grid[rows - 1][j] = new Cell(rows - 1, j, rows - 1, cols - 1, 'r', &board_mutex, &grid);
}
}
Board::~Board() {
join_threads();
for (auto& row : grid) {
for (auto& elem : row) {
delete elem;
}
}
}
void Board::print_grid() {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (grid[i][j] != nullptr) {
std::cout << grid[i][j]->type << " ";
} else {
std::cout << "0 ";
}
}
std::cout << "\n";
}
std::cout << "\n";
}
void Board::start_threads() {
for (auto& row : grid) {
for (auto& elem : row) {
if (elem != nullptr) {
elem->start();
}
}
}
}
void Board::join_threads() {
for (auto& row : grid) {
for (auto& elem : row) {
if (elem != nullptr) {
elem->stop();
if (elem->cell_thread.joinable()) {
elem->cell_thread.join();
}
}
}
}
}