-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tetris.cpp
114 lines (100 loc) · 2.53 KB
/
Tetris.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
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include "Point.h"
#include "Shape.h"
#include "Tetris.h"
#include "Board.h"
using namespace std;
Shape* Tetris::newShape(Board* board,int& piecesCounter, int score) const
{
Shape* shape = nullptr;
Shape::eType shape_choice = Shape::eType(rand() % 4);
shape = new Shape(7, 3, board, shape_choice);
piecesCounter++;
gotoxy(0, 0);
cout << "How many pieces has been droppen: " << piecesCounter << endl;
cout << "Score: " << score << endl;
cout << "(1)Initalize game, (2)Pause/Continue game, (3)Speed shapes, (4)Slow shapes, (9)Exit" << endl;
return shape;
}
void Tetris::run() const
{
int currentSpeed = 500;
Board* board = new Board();
srand((unsigned int)time(NULL));
int piecesCounter = 0, score = 0;
Shape* shape = newShape(board,piecesCounter,score);
while (true)
{
char keyPressed = 0;
if (_kbhit()) // checks if there is anything in the buffer
{
keyPressed = _getch(); // take the head of the buffer
switch (keyPressed)
{
case Point::eKEYS::RESTART:
clrscr();
delete shape;
shape = newShape(board, piecesCounter = 0, score = 0);
delete board;
board = new Board();
continue;
case Point::eKEYS::PAUSE_CONTINUE:
while (_getch() != Point::eKEYS::PAUSE_CONTINUE);
continue;
case Point::eKEYS::SPEED:
if (currentSpeed > 100)
{
currentSpeed -= 100;
}
break;
case Point::eKEYS::SLOW:
currentSpeed += 100;
break;
case Point::eKEYS::EXIT:
return;
case Point::eKEYS::LEFT:
if (shape->CheckIfNextToWall() != Shape::eWall::LEFT_WALL)
{
shape->move(Point::LEFT);
}
break;
case Point::eKEYS::RIGHT:
if (shape->CheckIfNextToWall() != Shape::eWall::RIGHT_WALL)
{
shape->move(Point::RIGHT);
}
break;
case Point::eKEYS::FLIP:
shape->Flip();
}
}
bool shapeHasStopped;
do
{
shape->move(Point::DOWN);
shapeHasStopped = shape->updateBoardAndChecksArrival(keyPressed, score);
if (keyPressed == Point::eKEYS::DOWN)
score++;
} while (keyPressed == Point::eKEYS::DOWN && !shapeHasStopped);
if (shapeHasStopped)
{
delete shape;
shape = newShape(board,piecesCounter,score);
if (shape->hasDownNeighborsInBoard())
{
gotoxy(5,5);
cout << "GAME OVER" << endl;
_getch();
break;
}
}
Sleep(currentSpeed);
}
delete shape;
return;
}