-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovement_test.go
80 lines (66 loc) · 2.76 KB
/
movement_test.go
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
package main
import "testing"
func TestLeftRightMovementBlockedAtWalls(t *testing.T) {
game := NewGame()
tetromino, _ := NewTetromino("I")
game.tetromino = tetromino
// Attempt to place tetromino at the left-most column and try to move left
game.tetromino.position = [2]int{0, 0}
canMoveLeft := game.grid.MoveTetromino(game.tetromino, "left")
if canMoveLeft {
t.Error("Expected tetromino to be blocked moving left at the wall")
}
// Attempt to place tetromino at the right-most column and try to move right
rightMostPosition := glassCols - len(tetromino.GetCurrentShape()[0])
game.tetromino.position = [2]int{0, rightMostPosition}
canMoveRight := game.grid.MoveTetromino(game.tetromino, "right")
if canMoveRight {
t.Error("Expected tetromino to be blocked moving right at the wall")
}
}
func TestLeftRightMovementBlockedByBlocks(t *testing.T) {
game := NewGame()
tetromino, _ := NewTetromino("I")
game.tetromino = tetromino
// Place tetromino and block to the left, then try to move left
game.tetromino.position = [2]int{1, 1}
game.grid.cells[1][0] = CellStateFilled // Blocking left movement
canMoveLeft := game.grid.MoveTetromino(game.tetromino, "left")
if canMoveLeft {
t.Error("Expected tetromino to be blocked moving left by another block")
}
// Place tetromino and block to the right, then try to move right
game.tetromino.position = [2]int{1, 0}
game.grid.cells[1][4] = CellStateFilled // Blocking right movement
canMoveRight := game.grid.MoveTetromino(game.tetromino, "right")
if canMoveRight {
t.Error("Expected tetromino to be blocked moving right by another block")
}
}
// TestSoftDropSpeedsUpGravity checks that the soft drop speeds up tetromino descent
func TestSoftDropSpeedsUpGravity(t *testing.T) {
game := NewGame()
tetromino, _ := NewTetromino("I")
game.tetromino = tetromino
initialPosition := game.tetromino.position
// Simulate holding the soft drop for faster descent
game.softDrop = true
for i := 0; i < 5; i++ { // let it fall for 5 ticks
game.grid.MoveTetromino(game.tetromino, "down")
}
if game.tetromino.position[0] <= initialPosition[0] {
t.Errorf("Expected tetromino to have moved downward with soft drop active, but got position %v", game.tetromino.position)
}
}
// TestHardDropPlacesPieceInstantly checks that the hard drop places a tetromino instantly at the bottom
func TestHardDropPlacesPieceInstantly(t *testing.T) {
game := NewGame()
tetromino, _ := NewTetromino("I")
game.tetromino = tetromino
game.hardDrop()
// Tetromino should be locked immediately at the bottom
expectedRow := glassRows - len(tetromino.GetCurrentShape()) // Lowest possible position
if tetromino.position[0] != expectedRow {
t.Errorf("Expected tetromino to hard-drop to bottom (row %d), but was %d", expectedRow, game.tetromino.position[0])
}
}