-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cpp
119 lines (103 loc) · 3 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
#include "board.h"
#include <QTimer>
#include <QPainter>
#include <iostream>
#include <QRect>
Board::Board(QWidget *parent) : QFrame(parent)
{
is_game_on = false;
clear_arrays();
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update_arrays()));
}
void Board::start(){
timer->start(1000);
is_game_on = true;
}
void Board::clear(){
clear_arrays();
timer->stop();
is_game_on = false;
repaint();
}
void Board::mousePressEvent(QMouseEvent *event)
{
if (is_game_on == false) {
if(event->y() < board_height && event->x() < board_width){
int i = event->x()/outer_border+1, j = event->y()/outer_border+1;
old_cells[i][j] = old_cells[i][j] == DEAD?ALIVE:DEAD;
repaint();
}
}
}
void Board::paintEvent(QPaintEvent *){
QPainter painter(this);
QColor alive_col(255, 230, 91, 255);
QColor dead_col(255, 255, 255, 50);
for (int i = 0; i < width_count-2; ++i){
for (int j = 0; j < height_count-2; ++j){
QRect *r1 = new QRect(i*outer_border, j*outer_border,inner_border, inner_border);
if(old_cells[i+1][j+1] == ALIVE)
painter.fillRect(*r1, alive_col);
else
painter.fillRect(*r1, dead_col);
}
}
}
void Board::clear_arrays(){
for(int i = 1; i < width_count-1; ++i){
for(int j = 1; j < height_count-1; ++j){
old_cells[i][j] = new_cells[i][j] = DEAD;
}
}
}
int Board::count_neighbours(int i, int j){
int counter=0;
if(old_cells[i-1][j-1]==ALIVE) ++counter;
if(old_cells[i-1][j]==ALIVE) ++counter;
if(old_cells[i-1][j+1]==ALIVE) ++counter;
if(old_cells[i][j-1]==ALIVE) ++counter;
if(old_cells[i][j+1]==ALIVE) ++counter;
if(old_cells[i+1][j-1]==ALIVE) ++counter;
if(old_cells[i+1][j]==ALIVE) ++counter;
if(old_cells[i+1][j+1]==ALIVE) ++counter;
return counter;
}
void Board::update_arrays(){
is_changed = false;
int num_of_neighbours;
for(int i = 1; i < width_count-1; ++i){
for(int j = 1; j < height_count-1; ++j){
num_of_neighbours = count_neighbours(i, j);
if(old_cells[i][j] == DEAD ){
if( num_of_neighbours == 3){
new_cells[i][j] = ALIVE;
is_changed = true;
}
else{
new_cells[i][j] = DEAD;
}
}
else if(old_cells[i][j] == ALIVE){
if (num_of_neighbours == 2 || num_of_neighbours==3){
new_cells[i][j] = ALIVE;
}
else{
new_cells[i][j] = DEAD;
is_changed = true;
}
}
}
}
if(is_changed == false) stop_game();
for(int i = 1; i < width_count-1; ++i){
for(int j = 1; j < height_count-1; ++j){
old_cells[i][j]=new_cells[i][j];
}
}
repaint();
}
void Board::stop_game(){
timer->stop();
is_game_on = false;
}