forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tetris.h
413 lines (316 loc) · 13.5 KB
/
Tetris.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
///////////////////////////////////////////////////////////////////
// ROOT implementation of the simple Tetris game
// Layout and some hints were taken from Qt /examples/tetris
//
// To run this game do the following:
// $ root
// root [0] gSystem.Load("Tetris")
// root [1] Tetris t
// <play game>
// root [2] .q
//
// Other ROOT fun examples: Hello, Aclock ...
///////////////////////////////////////////////////////////////////
#ifndef TETRIS_H
#define TETRIS_H
#include <TTimer.h>
#include <TCanvas.h>
#include <TGFrame.h>
#include <TRandom.h>
#include <TButton.h>
#include <TWbox.h>
#include <TText.h>
#include <TSystem.h>
class Tetris;
class TetrisBoard;
///////////////////////////////////////////////////////////////////
// TetrisBox - the brick of the game
///////////////////////////////////////////////////////////////////
class TetrisBox : public TWbox {
private:
Int_t fX; // X position(column) in the fPad
Int_t fY; // Y position(line) in the fPad
UInt_t fType; // if type==0 box is invisible (hide/show state)
TPad *fPad; // pad where box is in
public:
TetrisBox(Int_t x=0, Int_t y=0, UInt_t type=0, TPad *pad=(TPad*)TVirtualPad::Pad());
virtual ~TetrisBox() { }
Int_t GetX() { return fX; }
Int_t GetY() { return fY; }
UInt_t GetType() { return fType; }
void SetX(Int_t x);
void SetY(Int_t y);
void SetXY(Int_t x, Int_t y) { SetX(x); SetY(y); }
void SetType(UInt_t type) { fType=type; }
Bool_t IsHidden() { return (fType==0); }
void Hide() { SetType(0); }
void Show() { SetType(1); }
virtual void MoveOneLineDown() { SetY(GetY()-1); }
virtual void MoveRight() { SetX(GetX()+1); }
virtual void MoveLeft() { SetX(GetX()-1); }
void Erase();
void Paint(Option_t *option="");
void ExecuteEvent(Int_t, Int_t, Int_t) { return; } // disable any actions on it
};
///////////////////////////////////////////////////////////////////
// TetrisPiece - tetris piece is set of up to 4 TetrisBoxes
///////////////////////////////////////////////////////////////////
class TetrisPiece {
protected:
TetrisBox *fBoxes[4]; // TetrisPiece skeleton shape (up to 10 different shapes available)
UInt_t fType; // type of piece
Int_t fY; // current Y position (line) of the piece
Int_t fX; // current X position (column) of the piece
private:
void Initialize(UInt_t type, TPad *pad);
UInt_t GetRandomType() { return (UInt_t)(gRandom->Rndm()*9)+1; } // random 1..10
void HideSomeBoxes(UInt_t type);
protected:
void SetXx(int index,int value) { fBoxes[index]->SetX(value+fX); }
void SetYy(int index,int value) { fBoxes[index]->SetY(fY+value); }
void SetXxYy(int index,int x,int y) { fBoxes[index]->SetX(x+fX); fBoxes[index]->SetY(fY+y); }
Int_t GetXx(int index) { return fBoxes[index]->GetX()-fX; }
Int_t GetYy(int index) { return fBoxes[index]->GetY()-fY; }
void GetXxYy(int index,int &x,int&y) { x = fBoxes[index]->GetX()-fX;
y = fBoxes[index]->GetY()-fY; }
public:
TetrisPiece(TPad *pad = (TPad*)TVirtualPad::Pad()) { Initialize(GetRandomType()%11, pad); }
TetrisPiece(UInt_t type, TPad *pad=(TPad*)TVirtualPad::Pad()) { Initialize(type%11, pad); }
virtual ~TetrisPiece();
void SetX(int index,int value) { fBoxes[index]->SetX(value); }
void SetY(int index,int value) { fBoxes[index]->SetY(value); }
void SetXY(int index,int x,int y) { fBoxes[index]->SetX(x); fBoxes[index]->SetY(y); }
UInt_t GetType() { return fType; }
virtual void SetType(UInt_t type);
virtual void SetRandomType() { SetType(GetRandomType()); }
Int_t GetX(int index) { return fBoxes[index]->GetX(); }
Int_t GetY(int index) { return fBoxes[index]->GetY(); }
void GetXY(int index,int &x,int&y) { x = fBoxes[index]->GetX();
y = fBoxes[index]->GetY(); }
Int_t GetX() { return fX; }
Int_t GetY() { return fY; }
TetrisBox *GetTetrisBox(Int_t i) { return fBoxes[i]; }
void SetX(Int_t x);
void SetY(Int_t y);
void SetXY(Int_t x,Int_t y);
virtual void Hide();
virtual void Show();
virtual Bool_t RotateLeft();
virtual Bool_t RotateRight();
};
typedef TetrisBox* TetrisBoxPtr;
///////////////////////////////////////////////////////////////////
// TetrisBoard - the game board
///////////////////////////////////////////////////////////////////
class TetrisBoard : public TPad {
friend class Tetris;
private:
Int_t fHeight; // board height
Int_t fWidth; // board width
Bool_t fIsDropped; // kTRUE when piece stopped
TetrisBoxPtr *fBoard; //! 2d array of pointers to Tetrisboxes,
// if pointer is 0 - the cell is empty
Int_t fFilledLines; // number of non empty lines in pad
void AllAboveLinesDown(Int_t line) // assume that line is empty
{ for (int i = line; i < fFilledLines; i++) MoveOneLineDown(i); }
void MoveOneLineDown(Int_t line);
void RemoveFullLines();
void RemoveLine(int line);
Bool_t IsLineFull(int line);
Bool_t IsLineEmpty(int line);
void GluePiece(TetrisPiece *piece);
void Clear(Option_t *option = "");
void Hide();
void Show();
void Print(const char *option = "") const;
void Print(const char *, Option_t *) { } // removes "hiding" warning
Bool_t IsEmptyLine(int line);
Bool_t IsFullLine(Int_t line);
TetrisBoxPtr &Board(Int_t x, Int_t y) { return fBoard[fWidth*y + x]; }
public:
TetrisBoard(Float_t xlow, Float_t ylow, Float_t xup, Float_t yup);
virtual ~TetrisBoard() { }
Int_t GetHeight() { return fHeight; }
Int_t GetWidth() { return fWidth; }
Bool_t IsEmpty(Int_t x, Int_t y) { return !Board(x,y); }
void SetDropped(Bool_t flag=kTRUE) { fIsDropped=flag; }
virtual void PaintModified();
void PieceDropped(TetrisPiece *piece, Int_t height);
void ExecuteEvent(Int_t, Int_t, Int_t) { return; } // disable any actions on it
};
///////////////////////////////////////////////////////////////////
// CurrentPiece = TetrisPiece + TTimer = live TetrisPiece
///////////////////////////////////////////////////////////////////
class CurrentPiece : public TetrisPiece, public TTimer {
private:
TetrisBoard *fBoard; // tetris board
protected:
Bool_t CanPosition();
Bool_t CanMoveTo(int xPosition, int line);
void MoveTo(int xPosition,int line);
void Erase();
public:
CurrentPiece(UInt_t type, TetrisBoard* board);
~CurrentPiece() { }
Bool_t MoveLeft(Int_t steps = 1);
Bool_t MoveRight(Int_t steps = 1);
Bool_t RotateLeft();
Bool_t RotateRight();
Bool_t DropDown();
Bool_t OneLineDown();
Bool_t Notify();
void SetSpeed();
void Paint(Option_t *option="");
void ExecuteEvent(Int_t, Int_t, Int_t) { return; } // disable any actions on it
};
///////////////////////////////////////////////////////////////////
// NextPiecePad
// used to show next piece.
///////////////////////////////////////////////////////////////////
class NextPiecePad : public TPad {
private:
TetrisPiece *fPiece; // next piece
public:
NextPiecePad(Float_t xlow, Float_t ylow, Float_t xup, Float_t yup);
~NextPiecePad() { }
void NewPiece() { fPiece->SetRandomType(); fPiece->Show(); Modified(kTRUE); }
void Hide() { fPiece->Hide(); Modified(kTRUE); }
void Show() { fPiece->Show(); Modified(kTRUE); }
TetrisPiece *GetPiece() { return fPiece; }
void ExecuteEvent(Int_t, Int_t, Int_t) { return; } // disable any actions on it
};
///////////////////////////////////////////////////////////////////
// QuitButton
// ExecuteEvent mehtod is overloaded.
///////////////////////////////////////////////////////////////////
class QuitButton : public TButton {
public:
QuitButton(Float_t xlow, Float_t ylow, Float_t xup, Float_t yup);
~QuitButton() { }
void ExecuteEvent(Int_t event, Int_t px, Int_t py);
};
///////////////////////////////////////////////////////////////////
// PauseButton - push button
// ExecuteEvent mehtod used to pause the game
///////////////////////////////////////////////////////////////////
class PauseButton : public TButton {
private:
Bool_t fPressed; // button press state
public:
PauseButton(Float_t xlow, Float_t ylow, Float_t xup, Float_t yup);
~PauseButton() { }
void SetPressed(Bool_t state) {
fPressed = state;
state ? SetBorderMode(-1) : SetBorderMode(1);
Modified(kTRUE);
Update();
}
Bool_t IsPressed() { return fPressed; }
void ExecuteEvent(Int_t event, Int_t px, Int_t py);
};
///////////////////////////////////////////////////////////////////
// NewGameButton
// ExecuteEvent mehtod used to start new game
///////////////////////////////////////////////////////////////////
class NewGameButton : public TButton {
private:
Bool_t fPressed; // button press state
public:
NewGameButton(Float_t xlow, Float_t ylow, Float_t xup, Float_t yup);
~NewGameButton() { }
void SetPressed(Bool_t state) {
fPressed = state;
state ? SetBorderMode(-1) : SetBorderMode(1);
Modified(kTRUE);
Update();
}
Bool_t IsPressed() { return fPressed; }
void ExecuteEvent(Int_t event, Int_t px, Int_t py);
};
///////////////////////////////////////////////////////////////////
// InfoPad - used to display digital info
///////////////////////////////////////////////////////////////////
class InfoPad : public TPad, public TAttText {
protected:
UInt_t fValue; // value to be displayed
public:
InfoPad(const char *title="",Float_t xlow=0, Float_t ylow=0, Float_t xup=0, Float_t yup=0);
virtual ~InfoPad() { }
UInt_t GetValue() { return fValue; }
void SetValue(Int_t value) { fValue = value; Modified(kTRUE); }
void Reset(Option_t * = "") { SetValue(0); }
virtual void AddValue(Int_t addValue=1) { fValue = fValue+addValue; Modified(kTRUE); }
virtual void PaintModified();
void ExecuteEvent(Int_t, Int_t, Int_t) { return; } // disable any actions on it
};
///////////////////////////////////////////////////////////////////
// KeyHandler = virtual frame
// used to catch and handle key events in Tetris canvas
///////////////////////////////////////////////////////////////////
class KeyHandler : public TGFrame {
public:
KeyHandler();
~KeyHandler();
Bool_t HandleKey(Event_t *event); // handler of the key events
};
///////////////////////////////////////////////////////////////////
// UpdateLevelTimer used to periodically update game level
///////////////////////////////////////////////////////////////////
class UpdateLevelTimer : public TTimer {
public:
UpdateLevelTimer(ULong_t time);
~UpdateLevelTimer() { }
Bool_t Notify();
};
//////////////////////////////////////////////////////////////////
// Tetris = Game manager
//////////////////////////////////////////////////////////////////
class Tetris : public TCanvas {
friend class KeyHandler;
friend class TetrisBoard;
friend class UpdateLevelTimer;
private:
CurrentPiece *fCurrentPiece; // live tetris piece
TetrisBoard *fBoard; // pad were everything is going on
NextPiecePad *fNextPiece; // pad which show next piece
InfoPad *fLinesRemoved; // number of removed lines
InfoPad *fLevel; // game level
InfoPad *fScore; // game score
NewGameButton *fNewGame; // clicking on button initiates new game
QuitButton *fQuit; // clicking on button makes game over
PauseButton *fPause; // pause/continue button
KeyHandler *fKeyHandler; // handler for arrow keys
Int_t fPiecesDropped; // number of pieces dropped
UpdateLevelTimer *fUpdateLevelTimer; // periodically updates game level
protected:
void SetFixedSize();
void CreateNewPiece();
void UpdatePiecesDropped() { fPiecesDropped++; }
void UpdateLinesRemoved() { fLinesRemoved->AddValue(1); }
void UpdateScore(Int_t add_value) { fScore->AddValue(add_value); }
void UpdateLevel() { if (GetLevel()<10) fLevel->AddValue(1); }
void PrintHelpInfo();
virtual void MoveLeft();
virtual void MoveRight();
virtual void DropDown();
virtual void RotateRight();
virtual void RotateLeft();
public:
Tetris();
virtual ~Tetris() { delete fKeyHandler; }
Int_t GetLevel() { return fLevel->GetValue(); }
Int_t GetLinesRemoved() { return fLinesRemoved->GetValue(); }
Int_t GetPiecesDropped() { return fPiecesDropped; }
Int_t GetScore() { return fScore->GetValue(); }
Bool_t IsGameOn() { return fNewGame->IsPressed(); }
Bool_t IsPaused() { return fPause->IsPressed(); }
Bool_t IsWaiting() { return kFALSE; }
void SetLevel(int level);
void Quit();
void Pause();
void Continue();
void NewGame();
void StopGame();
ClassDef(Tetris,0) // ROOT implementation of the Tetris game
};
#endif // TETRIS_H