-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameFunctions.h
145 lines (126 loc) · 4.19 KB
/
GameFunctions.h
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
#ifndef GAMEFUNCTIONS_H
#define GAMEFUNCTIONS_H
static int points = 0;
const static int rows = 10;
const static int columns = 6;
const static int cols = columns;
void printGameRules(){
std::cout << "Game Play:\n" <<
"In each turn, you are given a block of varying length.\n" <<
"Type in the position to place the block on the board.\n" <<
"The block will be placed in the first available row from the bottom at the given position.\n" <<
"If the placement is outside of the board, you will be given the same block again.\n" <<
"Earning (or losing :)) points:\n" <<
"Successful block placement: +1\n" <<
"Unsuccessful block placement: -1\n" <<
"Clearing a row: +5\n" <<
"Winning and Losing: \n" <<
"If the blocks reach the top, you lose!\n" <<
"There is no Winning, so play until you lose.\n" <<
"Comment your highscore in the comment section below!\n" <<
"----------------------------" << std::endl;
}
template <typename TRow>
void printRow(const TRow& row){
std::cout << "|";
for(auto cell : row){
if(cell == true){
std::cout << "#";
} else{
std::cout << "_";
}
std::cout << "|";
}
std::cout << std::endl;
}
template <typename TBoard>
void printBoard(const TBoard& board){
// |_|_|_|_|_|_|
// |X|_|_|_|_|_|
for(auto it = board.crbegin(); it != board.crend(); ++it){
printRow(*it);
}
for(int i = 0; i < board[0].size(); ++i){
std::cout << " " << i;
}
std::cout << std::endl;
}
template <typename TRow>
bool updateRow(TRow& row,
int blockPos,
int blockSize){
TRow newRow{row};
for(int i = blockPos; i < blockPos + blockSize; ++i){
if(newRow[i] == true){
return false;
}
newRow[i] = true;
}
row = newRow;
return true;
}
template <typename TRow>
bool isRowFull(const TRow& row){
for(auto cell : row){
if(cell == false){
return false;
}
}
return true;
}
template <typename TBoard>
bool updateBoard(TBoard& board,
int blockPos,
int blockSize){
TBoard newBoard;
const int rowSize = board.size();
const int colSize = board[0].size();
bool updateSuccess = false;
int extraRow = 0;
while(!board.empty()){
auto row = board.front();
board.pop_front();
if(!updateSuccess){
updateSuccess = updateRow(row, blockPos, blockSize);
if(!isRowFull(row)){
newBoard.push_back(row);
}
if(isRowFull(row)){
extraRow++;
}
} else{
newBoard.push_back(row);
}
}
if(board.empty() && !updateSuccess){
throw std::runtime_error("You lost the game!");
}
// +1 for successful placement.
points++;
if(extraRow > 0){
// +5 for every cleared row.
points += 5;
newBoard.push_back(std::vector<bool>(colSize, false));
}
board = newBoard;
return updateSuccess;
}
int makeBlock(){
// return a block between 1 and 4
return rand() % 5 + 1;
}
bool checkIfPositionValid(int blockSize, int blockPos, int rowSize){
if(blockPos < 0 || blockPos >= rowSize ||
(blockPos + blockSize - 1) >= rowSize){
return false;
}
return true;
}
std::deque<std::vector<bool>> makeBoard(int row, int col){
std::deque<std::vector<bool>> board;
for(int i = 0; i < row; ++i){
board.push_back(std::vector<bool>(col, false));
}
return board;
}
#endif