-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
191 lines (171 loc) · 6.37 KB
/
game.cpp
File metadata and controls
191 lines (171 loc) · 6.37 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>
#include <vector>
#include <thread>
#include <chrono>
#include <mutex>
#include "tetromino.hpp"
#include "tetromino_controller.hpp"
#include "game_constants.hpp"
#include "utility_tetris.hpp"
#include "tetris_exception.hpp"
using namespace std;
char gameBoard[game_constants::board_width][game_constants::board_height];
static mutex inputMutex;
static char input = ' ';
static mutex stopMutex;
static bool stop;
//Sort Of Beautified Version of the Output
void print_board(const char (&board)[game_constants::board_width][game_constants::board_height]) {
//clear terminal
cout << "\033[2J\033[1;1H";
cout << "\t\t\t +";
for(int i = 0; i < game_constants::board_width; i++) cout << "=";
cout << "+" << endl;
for(int y = 0; y < game_constants::board_height; y++) {
cout << "\t\t\t = ";
for(int x = 0; x < game_constants::board_width; x++) {
cout << board[x][y];
}
cout << " = " << endl;
}
cout << "\t\t\t +";
for(int i = 0; i < game_constants::board_width; i++) cout << "=";
cout << "+" << endl;
}
//Barebone print of the GameBoard
void print_board_2(const char (&board)[game_constants::board_width][game_constants::board_height]) {
//clear terminal
cout << "\033[2J\033[1;1H";
for(int y = 0; y < game_constants::board_height; y++) {
for(int x = 0; x < game_constants::board_width; x++) {
cout << board[x][y];
}
cout << endl;
}
}
//For Solution with thread -> needs while Loop
void getCharWithoutEnterVoid() {
while(!stop) {
char buffer = 0;
struct termios old = {0};
if(tcgetattr(0, &old) < 0) perror("tcsetattr()");
//Disable canonical Mode
//Supress Echo
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if(tcsetattr(0, TCSANOW, &old) < 0) perror("tcsetattr ICANONO");
//Read Char
if(read(0, &buffer, 1) < 0) perror("read()");
//Reset Changes
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if(tcsetattr(0, TCSADRAIN, &old) < 0) perror("tcsetattr ~ICANON");
//return Char
inputMutex.lock();
input = buffer;
inputMutex.unlock();
}
}
void waitThreadCallback(const std::chrono::milliseconds &waitTime) {
this_thread::sleep_for(waitTime);
}
int main() {
initialize_board(gameBoard);
unique_ptr<tetromino_controller> controller(new tetromino_controller(gameBoard));
//controller->generateNewTetromino(1, pair<int, int>(3, 2), 0, 1);
controller->generateNewRandomTetromino();
controller->getCurrentTetromino().drawTetromino(gameBoard);
print_board(gameBoard);
//starting thread for Inputs;
thread inputThread(getCharWithoutEnterVoid);
int dropdownCounter = 0;
while(!stop) {
thread waitThread(waitThreadCallback, game_constants::wait_time);
//Ensuring Thread/Variable Integrety through Mutex -> new Variable required
inputMutex.lock();
char inputDummy = input;
inputMutex.unlock();
switch(inputDummy) {
case 'a' :
controller->getCurrentTetromino().moveLeft(gameBoard);
controller->getCurrentTetromino().drawTetromino(gameBoard);
print_board(gameBoard);
input = ' ';
break;
case 'd' :
controller->getCurrentTetromino().moveRight(gameBoard);
controller->getCurrentTetromino().drawTetromino(gameBoard);
print_board(gameBoard);
input = ' ';
break;
case 's' :
try {
controller->getCurrentTetromino().moveDown(gameBoard);
controller->getCurrentTetromino().drawTetromino(gameBoard);
print_board(gameBoard);
input = ' ';
break;
} catch(const Tetromino_Stuck &tetromino_stuck_exp) {
//controller->generateNewTetromino(0, pair<int, int>(0,2), 0, 0);
removeCompletedRows(gameBoard);
controller->generateNewRandomTetromino();
controller->getCurrentTetromino().drawTetromino(gameBoard);
print_board(gameBoard);
break;
}
case 'w' :
controller->getCurrentTetromino().moveUpDebug(gameBoard);
controller->getCurrentTetromino().drawTetromino(gameBoard);
print_board(gameBoard);
input = ' ';
break;
case 'r' :
try {
controller->getCurrentTetromino().rotateChecked(gameBoard);
controller->getCurrentTetromino().drawTetromino(gameBoard);
print_board(gameBoard);
input = ' ';
break;
} catch(Tetris_Bounds &rotation_exp) {
cout << rotation_exp.print_what() << endl;
this_thread::sleep_for(5s);
}
break;
case 'q' :
print_board(gameBoard);
cout << "Quitting..." << endl;
stop = 1;
break;
//Normal input if nothing pressed
case ' ' :
break;
default :
cout << "Wrong Input" << endl;
input = ' ';
break;
}
waitThread.join();
dropdownCounter++;
try {
if(dropdownCounter == game_constants::falltime_factor) {
dropdownCounter = 0;
controller->getCurrentTetromino().moveDown(gameBoard);
controller->getCurrentTetromino().drawTetromino(gameBoard);
print_board(gameBoard);
}
} catch(const Tetromino_Stuck &tetromino_stuck_exp) {
//controller->generateNewTetromino(0, pair<int, int>(0,2), 0, 0);
//ADD TRY FOR GAME OVER TODO
removeCompletedRows(gameBoard);
controller->generateNewRandomTetromino();
controller->getCurrentTetromino().drawTetromino(gameBoard);
print_board(gameBoard);
//cout << tetromino_stuck_exp.print_what();
//this_thread::sleep_for(2s);
}
}
inputThread.join();
return 0;
}