-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboard.cpp
150 lines (121 loc) · 3.25 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <cstdlib>
#include <iostream>
#include "board.h"
#include "ucrc_t.h"
using namespace std;
Board::Board(int f, int c){
reset(f, c);
}
void Board::reset(int f, int c){
rows = f;
columns = c;
board.resize(rows);
for(int i = 0; i < board.size(); i++){
board[i].resize(columns);
}
for(int f = 0; f < rows; f++){
for(int c = 0; c < columns; c++){
if(rand() % 2){
board[f][c] = 1;
}
else
board[f][c] = 0;
}
}
uint64_t localCrc = getCrc();
for (int i = 0; i < PERIOD; i++){
crc[i] = localCrc;
}
}
uint64_t Board::getCrc(){
char msg[rows * columns];
for (int i = 0; i < rows; i++){
for (int j = 0; j < columns; j++){
msg[columns * i + j] = board[i][j];
}
}
uCRC_t ucrc;
uint64_t localCrc;
localCrc = ucrc.get_crc_init();
localCrc = ucrc.get_raw_crc(msg, sizeof(msg), localCrc);
localCrc = ucrc.get_final_crc(localCrc);
return localCrc;
}
int Board::compareCrc(){
uint64_t localCrc = getCrc();
uint64_t currentCrc = localCrc;
uint64_t tempCrc;
for (int i = 0; i < PERIOD; i++){
if (crc[i] == localCrc)
return 0;
tempCrc = crc[i];
crc[i] = currentCrc;
currentCrc = tempCrc;
}
return 1;
}
void Board::draw(){
for(int f = 0; f < rows; f++){
for(int c = 0; c < columns; c++){
if(board[f][c] == 1)
cout << "* ";
else
cout << ". ";
}
cout << "\n";
}
}
int Board::countNeighbors(int row, int column){
int vecinos = 0;
if(row - 1 >= 0 and column - 1 >= 0)
if(board[row - 1][column - 1] == 1)
vecinos++;
if(row - 1 >= 0)
if(board[row - 1][column] == 1)
vecinos++;
if(row - 1 >= 0 and column + 1 <= columns - 1)
if(board[row - 1][column + 1] == 1)
vecinos++;
if(column - 1 >= 0)
if(board[row][column - 1] == 1)
vecinos++;
if(column + 1 <= columns - 1)
if(board[row][column + 1] == 1)
vecinos++;
if(row + 1 <= rows - 1 and column - 1 >= 0)
if(board[row + 1][column - 1] == 1)
vecinos++;
if(row + 1 <= rows - 1)
if(board[row + 1][column] == 1)
vecinos++;
if(row + 1 <= rows - 1 and column + 1 <= columns - 1)
if(board[row + 1][column + 1] == 1)
vecinos++;
return vecinos;
}
int Board::loop(){
vector<vector<int> > nueva_conf = board;
for (int f = 0; f < rows; f++){
for(int c = 0; c < columns; c++){
int n_vecinos = countNeighbors(f, c);
if(board[f][c] == 0){
if(n_vecinos == 3){
nueva_conf[f][c] = 1;
}else
nueva_conf[f][c] = 0;
}
if (board[f][c] == 1){
if(n_vecinos == 2 || n_vecinos == 3){
nueva_conf[f][c] = 1;
}
else
nueva_conf[f][c] = 0;
}
}
}
board = nueva_conf;
// si el crc es distinto a los de la lista es porque no hay ciclo
if(compareCrc())
return 1;
return 0;
}