-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
170 lines (161 loc) · 5.25 KB
/
main.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "piece.h"
#include "board.h"
#include "graphics.h"
#include <dos.h>
#include <stdlib.h>
#include <conio.h>
#include <fstream>
#include <ctime>
using namespace std;
const int d = 100;
int drop (Board &b, Piece &piece, Point &pos);
void save (Board &b);
void load (Board &b);
void welcome();
int random (int);
int main() {
int gd = DETECT, gm, i, placed;
initwindow ( 800 , 640 , "Tetris" );
INIT:
char c;
Point pos;
Piece *pieces = Piece::getPieces();
Piece *piece;
Board b;
srand (time (NULL) );
clearviewport();
welcome();
if (getch() == 'l') {
load (b);
}
clearviewport();
do {
START:
piece = pieces + random (7);
for (i = 0; i < random (10); i++) {
piece = piece -> getRotation();
}
pos = Point (4, 19);
do {
while (!kbhit() ) {
pos.y -= 1;
delay (d);
if (b.place (*piece, pos) == PLACE_BAD) {
b.undo();
if (drop (b, *piece, pos) ) {
goto START;
} else {
goto INIT;
}
}
b.display();
b.undo();
}
c = getch();
switch (c) {
case 'd': pos.x++; break;
case 'a': pos.x--; break;
case 'w': piece = piece -> getRotation(); break;
case 's': if (drop (b, *piece, pos) ) {
goto START;
} else {
goto INIT;
}
case 'e': exit (0);
case 'q': save (b); break;
default: break;
}
placed = b.place (*piece, pos);
if (placed == PLACE_OUT_BOUNDS) {
if (pos.x < 0) {
pos.x = 0;
} else {
pos.x = 10 - piece->getWidth();
}
}
b.undo();
while (b.place (*piece, pos) == PLACE_BAD) {
b.undo();
if (c == 'a') {
pos.x++;
} else if (c == 'd') {
pos.x--;
}
}
b.undo();
} while (1);
} while (1);
}
int drop (Board &b, Piece &piece, Point &pos) {
pos.y = b.drop (piece, pos);
int placed = b.place (piece, pos);
char go[] = "GAME OVER!";
char ex[] = "Press `Esc' to exit, anything else to continue";
char c;
if (placed == PLACE_ROW_FILLED) {
b.scored();
b.clearRows();
} else if (placed == PLACE_FULL) {
b.display();
clearviewport();
settextstyle (TRIPLEX_FONT, HORIZ_DIR, 8);
outtextxy ( (getmaxx() - textwidth (go) ) / 2,
(getmaxy() - 2 * textheight (go) ) / 2,
go);
settextstyle (GOTHIC_FONT, HORIZ_DIR, 2);
outtextxy ( (getmaxx() - textwidth (ex) ) / 2,
(getmaxy() + textheight (go) ) / 2 + 5,
ex);
c = getch();
if (c == 27) {
exit (0);
} else {
return 0;
}
}
b.backup();
b.display();
return 1;
} \
void welcome() {
char tet[] = "TETRIS";
char ins[14][50] = {"Welcome to tetris. The objective of the game is",
"to clear the board of pieces.",
"A row is cleared when it is completely filled.",
"Pieces appear at random at the",
"top of the board. The controls are: ",
"a - Move piece left ",
"d - Move piece right ",
"s - Drop piece ",
"w - Change rotation ",
"q - Quicksave (overwrites previous saves)",
"e - Exit (you will lose all progress)",
"Press 'l' if you would like to load a previously",
"saved game. Press any other ",
"key to start a new game."
};
int i;
settextstyle (TRIPLEX_FONT, HORIZ_DIR, 10);
outtextxy ( (getmaxx() - textwidth (tet) ) / 2,
5,
tet);
settextstyle (SANS_SERIF_FONT, HORIZ_DIR, 1);
for (i = 0; i < 14; i++)
outtextxy ( (getmaxx() - textwidth (ins[i]) ) / 2,
(10 * textheight (tet) + i * textheight (ins[0]) + 5),
ins[i]);
}
void save (Board &b) {
fstream f;
f.open ("saves.dat", ios::out | ios::binary | ios::trunc);
f.write ( (char *) (&b), sizeof (b) );
outtextxy (10, 40, "Saved");
}
void load (Board &b) {
fstream f;
f.open ("saves.dat", ios::in | ios::binary);
f.read ( (char *) (&b), sizeof (b) );
}
int random (int a) {
return rand() % a;
}