-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseGame.cpp
146 lines (133 loc) · 3.73 KB
/
BaseGame.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
#include "BaseGame.h"
#include <stdlib.h>
#include <time.h>
BaseGame::BaseGame(){}
BaseGame::BaseGame(bool **board, int h, int w){
// sets variables, created board (pointer to pointer), and copies input board to current and next boards
height = h;
width = w;
currentBoard = new bool*[height];
nextBoard = new bool*[height];
for(int i = 0; i < height; ++i){
currentBoard[i] = new bool[width];
nextBoard[i] = new bool[width];
}
copyInput(board);
}
BaseGame::BaseGame(int h, int w, double p){
// sets variables, creates board (point to pointer), and generates random board
height = h;
width = w;
population = p;
currentBoard = new bool*[height];
nextBoard = new bool*[height];
for(int i = 0; i < height; ++i){
currentBoard[i] = new bool[width];
nextBoard[i] = new bool[width];
}
generateRandomBoard();
}
BaseGame::~BaseGame(){
for(int i = 0; i < height; ++i){
delete[] currentBoard[i];
delete[] nextBoard[i];
}
delete[] currentBoard;
delete[] nextBoard;
}
bool** BaseGame::getBoard(){
return currentBoard;
}
void BaseGame::copyInput(bool **board){
// copies board
for(int i = 0; i < height; ++i){
for(int j = 0; j < width; ++j){
currentBoard[i][j] = board[i][j];
nextBoard[i][j] = board[i][j];
}
}
}
void BaseGame::currentToNext(){
// copies board from current to next
for(int i = 0; i < height; ++i){
for(int j = 0; j < width; ++j){
nextBoard[i][j] = currentBoard[i][j];
}
}
}
void BaseGame::generateRandomBoard(){
int random = 0; // number between 1 and 10
int totalCells = height * width;
int cellsToOccupy = totalCells * population; // gets number of cells to occupy
int occupyCell = population * 100; // get number between 0 - 100, use for randomness factor
int occupied = 0;
srand(time(NULL));
while(occupied != cellsToOccupy){
// randomize filling of cells
for(int i = 0; i < height; ++i){
if(occupied == cellsToOccupy){
break;
}
for(int j = 0; j < width; ++j){
// number between 1 and 100
random = rand() % 100 + 1;
// if the current cell is unoccupied
if(currentBoard[i][j] == 0){
if(random <= occupyCell){
// mark cell as occupied, bool is set to 1
currentBoard[i][j] = 1;
occupied++;
}
}
if(occupied == cellsToOccupy){
break;
}
}
}
}
currentToNext();
}
void BaseGame::printBoard(){
// prints board
for(int i = 0; i < height; ++i){
for(int j = 0; j < width; ++j){
if(currentBoard[i][j] == 1){
cout << 'X';
}
else{
cout << '-';
}
}
cout << endl;
}
}
void BaseGame::nextToCurrent(){
// copies next board to current board
for(int i = 0; i < height; ++i){
for(int j = 0; j < width; ++j){
currentBoard[i][j] = nextBoard[i][j];
}
}
}
bool BaseGame::checkStable(){
// checks if board is stable
for(int i = 0; i < height; ++i){
for(int j = 0; j < width; ++j){
if(currentBoard[i][j] != nextBoard[i][j]){
return false;
}
}
}
return true;
}
bool BaseGame::checkEmpty(){
// check if board is empty
for(int i = 0; i < height; ++i){
for(int j = 0; j < width; ++j){
if(currentBoard[i][j] == 1){
return false;
}
}
}
return true;
}