Tetris. Informal description Tetris is a 2d computer game. It consists of an Matrix and we have...

13
Tetris

Transcript of Tetris. Informal description Tetris is a 2d computer game. It consists of an Matrix and we have...

Page 1: Tetris. Informal description Tetris is a 2d computer game. It consists of an Matrix and we have Tiles. One new tile is introduced at a time and enters.

Tetris

Page 2: Tetris. Informal description Tetris is a 2d computer game. It consists of an Matrix and we have Tiles. One new tile is introduced at a time and enters.

Informal description

• Tetris is a 2d computer game. It consists of an Matrix and we have Tiles. One new tile is introduced at a time and enters from the top of the game area. For each ClockTick the whole Tile moves one Unit downwards.

• And the user can control the FallingTile by rotating and shifting.

• A FallingTile becomes inactive when it hits an occupied  Cell or the bottom of the Area.

• A row is cleared as soon as all Cells in it are occupied. All the occupied Cells above it moves down one Cell.

• Game over when the pile of blocks hits the ‘ceil’ of the area  

Page 3: Tetris. Informal description Tetris is a 2d computer game. It consists of an Matrix and we have Tiles. One new tile is introduced at a time and enters.

Definitions

• Cell = rendered square areao Cell.occupied = x E [0,1]o Cell.color =  color codeo Cell.active = x E [false,true]

• Type Matrix = rectangular form which shape is dictated by Rows × Columns

• GameMatrix :: Matrix • Rows= <Row1,Row2,…Rown>• Columns= <Column1, Column 2,… Columnn>• Celli,j = (Rowi, Columnj)  • Tile is a Matrix whose number of rows and columns are less

than or equal to the GameMatrix.• Tile = 1..* Cells. For all Cell1, Cell2, that are part of Tile this

holds: Reachable(Cell1,Cell2)

Page 4: Tetris. Informal description Tetris is a 2d computer game. It consists of an Matrix and we have Tiles. One new tile is introduced at a time and enters.

Definitions (ctd)

• Reachable(Celi,j,Cellm,n) =         ((|m – i| =1 AND j = n) OR        (|j –  n| = 1 AND i = m))        OR            There exists a Cellk,l such that  IF Reachable (Celli,j, Cellk,l) AND Reachable(Cellk,l,Cellm,n) THEN Reachable(Celli,j, Cellm,n)

Page 5: Tetris. Informal description Tetris is a 2d computer game. It consists of an Matrix and we have Tiles. One new tile is introduced at a time and enters.

Definitions (ctd)

• Clock ticks form a series of equally distributed points in time .oSimplyfing assumption: ClockTicks at the

end of equal time intervals.• In that series: for each predecessor t1 of

each Tick t2 (if any) it holds that time of t1 < time of t2

• Clock tick = the base measure of time

Page 6: Tetris. Informal description Tetris is a 2d computer game. It consists of an Matrix and we have Tiles. One new tile is introduced at a time and enters.

Actions

• These actions must be initiated by the User input: o SL = moves a Tile one Cell to the left in the Areao SR =  moves a Tile one Cell to the right in the Areao RL = rotates a Tile 90 degrees left o RR = rotates a Tile 90 degrees right

• Invariant: Each ClockTick the Tile moves down one Cell• Invariant: Each action results in an screen update

  

Page 7: Tetris. Informal description Tetris is a 2d computer game. It consists of an Matrix and we have Tiles. One new tile is introduced at a time and enters.

Rendering on screen

• Every cell is rendered as a square• A rectangle must represent the GameMatrix.

o Size of rectangle should be configurable -- so for instance it can be fullscreen

o Width and height of the GameMatrix are dependent upon each other, because of the constraint that the contained cells must appear as squares.

• The rectangle is populated by cells according to their row/column indexes.

Page 8: Tetris. Informal description Tetris is a 2d computer game. It consists of an Matrix and we have Tiles. One new tile is introduced at a time and enters.

Initial State / New Game

• Initial State:  o GameMatrix doesn't have any inactive Cells.o A new Tile is generatedo Program starts to generate Clock Ticks.

Page 9: Tetris. Informal description Tetris is a 2d computer game. It consists of an Matrix and we have Tiles. One new tile is introduced at a time and enters.

End State / Game over

Program stops to generate Clock Ticks.Gives the user an option to start a new game, which brings the program in Initial State, or to exit the program. 

Page 10: Tetris. Informal description Tetris is a 2d computer game. It consists of an Matrix and we have Tiles. One new tile is introduced at a time and enters.

New Tile

When a New Tile is generated, the highest Cell of the Tile is on the highest row of the GameMatrix, if the cells to occupy were not occupied before.Otherwise the game is over, which is the EndState.

The Cell positions of the Tile are marked as active within the GameMatrix.

Page 11: Tetris. Informal description Tetris is a 2d computer game. It consists of an Matrix and we have Tiles. One new tile is introduced at a time and enters.

Downward movement

• For each Clock Tick the falling Tile moves one Cell to the Bottom, unless the falling Tile is a StuckTile. In the latter case, a new falling Tile is generated.

• StuckTile is a Tile which has Cells that are on the same level as the lowest level  of Area OR

• StuckTile is block which has Tile that are just one level higher than one or more Tile of an other StuckBlock.

Page 12: Tetris. Informal description Tetris is a 2d computer game. It consists of an Matrix and we have Tiles. One new tile is introduced at a time and enters.

Action: Shift left/right

Function MoveRight()    For All active cells s in GameMatrix        If cell[s.x-1,s.y].occupied = false            flag = true        else             flag = false            return     For All active cells s        Cell(s.x,s.y).active = false        Cell(s.x-1,s.y).active = true

MoveLeft is the same, but replace all occurences within the function MoveRight of 'x-1' with 'x+1' excluding quotation marks.            

Page 13: Tetris. Informal description Tetris is a 2d computer game. It consists of an Matrix and we have Tiles. One new tile is introduced at a time and enters.

Action: Rotate