-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.cc
155 lines (145 loc) · 6.15 KB
/
tetris.cc
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
#include <unistd.h>
#include <algorithm>
#include <memory>
#include "gameboard.h"
#include "block.hpp"
#include "piece_selector.hpp"
static void shiftRowsDown(GameBoard & gb,blist & blocks, const int row) {
std::for_each(blocks.begin(), blocks.end(), [&row, &gb ](Block & b) {if (b.y_ == row ){ b.unmark(gb);};});
blocks.erase(std::remove_if(blocks.begin(), blocks.end(), [&row](Block & b)->bool { return b.y_ == row;}), blocks.end() );
std::for_each(blocks.begin(), blocks.end(), [&row, &gb ](Block & b) {if (b.y_ < row ){ b.unmark(gb); b.y_++; b.mark(gb);};});
std::for_each(blocks.begin(), blocks.end(), [&row, &gb ](Block & b) {b.mark(gb);});
}
static int checkCompleteRows(GameBoard & gb, blist & blocks) {
int rowsRemoved=0;
int i=gb.maxy();
while (i > 0) {
int j=0;
for (; j<= gb.maxx();j++) {
if (gb.isClear(j,i))
break;
}
if (gb.maxx() +1 ==j) {
rowsRemoved++;
shiftRowsDown(gb,blocks, i);
} else {
i--;
}
}
return rowsRemoved;
}
static bool gameOver(const std::shared_ptr<Piece> curPiecep) {
return curPiecep->findIf([](Block &b)->bool {return b.getY() == 0;});
}
#include "gamescreen.hpp"
#define MIN(A,B) (A >B ? B :A)
class TetrisGame {
public:
TetrisGame(GameScreen &gameScreen) : gameScreen_(gameScreen), delay_(gameScreen.getDelay()) {}
bool handleKeyBoard(GameBoard & tetrisGameBoard, bool & done,std::shared_ptr<Piece>& curPiecep) {
if (!gameScreen_.kbhit()) {
return false;
}
char c = gameScreen_.lgetch();
switch (c){
case 'q': done=true;break;
case 'a': curPiecep->left(tetrisGameBoard);break;
case 'd': curPiecep->right(tetrisGameBoard);break;
case 'z': curPiecep->rotateCounterClockwise(tetrisGameBoard);break;
case 'c': curPiecep->rotateClockwise(tetrisGameBoard);break;
case 's': curPiecep->move(tetrisGameBoard);break;
case ' ':
while (!curPiecep->done_moving()) {
curPiecep->move(tetrisGameBoard);
}
break;
}
return true;
}
void play() {
int real_max_x, real_max_y;
gameScreen_.getMaxyx(real_max_y, real_max_x);
int centerX = real_max_x/2;
int offsetCenterX = centerX + TETRIS_DEFAULT_WIDTH/2 -10;
int max_x = MIN(real_max_x,TETRIS_DEFAULT_WIDTH);
int max_y = real_max_y;
Window_interface & board_win = gameScreen_.getWindow(max_y-4,12,0, offsetCenterX-1);
Window_interface & score_win = gameScreen_.getWindow(3, 12, max_y-4, offsetCenterX-1);
Window_interface & next_piece_win = gameScreen_.getWindow(5, 6, 2, offsetCenterX + 11);
max_y = max_y-6;
GameBoard tetrisGameBoard(max_x, max_y);
bool done= false;
int moveDownCount = 100;
int currentCount = 1;
PieceSelector pieceSelector;
std::shared_ptr<Piece> curPiecep(pieceSelector.getNextPiece(blocks_));
int moveRight = rand() %(TETRIS_DEFAULT_WIDTH -1);
for (int i = 0; i < moveRight; i++) {
curPiecep->right(tetrisGameBoard);
}
std::shared_ptr<Piece> nextPiecep(pieceSelector.getNextPiece(nextblocks_));
std::for_each(nextblocks_.begin(), nextblocks_.end(), [&](Block & b) {next_piece_win.draw(b, 1, 1);});
while(!done) {
bool needRedraw =false;
if (currentCount % moveDownCount == 0 ) {
curPiecep->move(tetrisGameBoard);
needRedraw = true;
}
currentCount++;
needRedraw |= handleKeyBoard(tetrisGameBoard,done,curPiecep);
usleep(delay_);
if (needRedraw) {
gameScreen_.clear();
board_win.clear();
if (curPiecep->done_moving()) {
if (gameOver(curPiecep))
done = true;
switch (checkCompleteRows(tetrisGameBoard, blocks_)) {
case 4: score_ +=100; break;
case 3: score_ +=50; break;
case 2: score_ +=25; break;
case 1: score_ +=10; break;
default:
case 0: score_ +=1; break;
}
int moveRight = rand() %8;
for (int i = 0; i < moveRight; i++) {
nextPiecep->right(tetrisGameBoard);
}
next_piece_win.clear();
blocks_.splice(blocks_.begin(), nextblocks_);
curPiecep = std::move(nextPiecep);
nextPiecep.reset( pieceSelector.getNextPiece(nextblocks_));
std::for_each(nextblocks_.begin(), nextblocks_.end(), [&](Block & b) {next_piece_win.draw(b, 1, 1);});
curPiecep->markAll(tetrisGameBoard);
}
std::for_each(blocks_.begin(), blocks_.end(), [&](Block & b) {board_win.draw(b,1,1);});
gameScreen_.refreshMain();
board_win.refresh();
score_win.text(1, 1, "score: %d",score_);
score_win.refresh();
next_piece_win.refresh();
gameScreen_.doupdate();
}
}
}
int getScore() {
return score_;
}
private:
GameScreen &gameScreen_;
int delay_;
blist blocks_;
blist nextblocks_;
int score_ = 0;
};
#include "gamescreen.hpp"
#include <iostream>
int main() {
/* initialize random seed: */
srand (time(NULL));
GameScreen gameScreen;
TetrisGame tetris(gameScreen);
tetris.play();
std::cout << "final score:" << tetris.getScore() << std::endl;
};