Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chad Clark committed Mar 1, 2024
1 parent 2b1a007 commit 447ef27
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 14 deletions.
21 changes: 14 additions & 7 deletions src/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ func NewSeededGame(seed int64, rows int, cols int, num_pieces int, piece_map [][

// GOAL: Start the main game loop
g.MainGameLoop(player_input_channel, output_state_channel)

return &g, player_input_channel, output_state_channel
}

Expand Down Expand Up @@ -410,7 +409,7 @@ func (g *Game) GetDebugState() string {
for i := 0; i < 4; i++ {
for j := 0; j < 4; j++ {
if 0 != g.PieceMap[g.Piece][g.PieceRotation][i][j] {
g.Field[g.PiecePosRow+i+1][g.PiecePosCol+j+1] = 2
g.Field[g.PiecePosRow+i][g.PiecePosCol+j] = 2
}
}
}
Expand All @@ -419,10 +418,13 @@ func (g *Game) GetDebugState() string {
for i := 0; i < g.GameRows+2; i++ {
buffer.WriteString(" ")
for j := 0; j < g.GameColumns+2; j++ {
if 0 == g.Field[i][j] {
switch g.Field[i][j] {
case 0:
buffer.WriteString(" ")
} else {
case 1:
buffer.WriteString("X")
case 2:
buffer.WriteString("*")
}
}
buffer.WriteString("\n")
Expand All @@ -432,7 +434,7 @@ func (g *Game) GetDebugState() string {
for i := 0; i < 4; i++ {
for j := 0; j < 4; j++ {
if 0 != g.PieceMap[g.Piece][g.PieceRotation][i][j] {
g.Field[g.PiecePosRow+i+1][g.PiecePosCol+j+1] = 0
g.Field[g.PiecePosRow+i][g.PiecePosCol+j] = 0
}
}
}
Expand Down Expand Up @@ -573,7 +575,8 @@ func (g *Game) ShiftRowsDown(start_row int) {
func (g *Game) MainGameLoop(player_input <-chan byte, game_state_ch chan<- *Game) {

// GOAL: Create a channel for a ticker to drop the pieces
ticker := time.NewTicker(time.Millisecond * 500) // FIXME: use a global/config for drop speed
tickDuration := time.Millisecond * 500 // FIXME: use a global/config for drop speed
ticker := time.NewTicker(tickDuration)

var key byte
go func() {
Expand All @@ -594,8 +597,12 @@ func (g *Game) MainGameLoop(player_input <-chan byte, game_state_ch chan<- *Game
switch g.State {
case StateRunning:
g.State = StatePaused
case StatePaused:
key := <- player_input
for key != PlayInputPause {
key = <- player_input
}
g.State = StateRunning
ticker.Reset(tickDuration)
}
case PlayInputMoveLeft:
if g.State == StateRunning {
Expand Down
90 changes: 83 additions & 7 deletions src/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package engine

import (
"log"
"slices"
"testing"
"time"
)

func TestCreateStopGame(t *testing.T) {
Expand Down Expand Up @@ -77,6 +79,79 @@ func TestCreateBucketGame(t *testing.T) {
}
}

func TestCompleteRow(t *testing.T) {

gameRoot, gameInput, gameOutput := NewGame()

game := <-gameOutput

if game.State != StateRunning {
t.Errorf("Game not running. %d", game.State)
}

gameInput <- PlayInputToggleDrop
game = <-gameOutput

gameInput <- PlayInputPause

// GOAL: make the game look like:
// [X X X X X X X X X X X X]
// [X 0 0 0 0 0 0 0 0 0 0 X]
// [X 0 0 0 0 0 0 0 0 0 0 X]
// [X 0 0 0 0 0 0 0 0 0 0 X]
// [X 0 0 0 0 0 0 0 0 0 0 X]
// [X 0 0 0 0 0 0 0 0 0 0 X]
// [X 0 0 0 0 0 0 0 0 0 0 X]
// [X 0 0 0 0 0 0 0 0 0 0 X]
// [X 0 0 0 0 0 0 0 0 0 0 X]
// [X 0 0 0 0 0 0 0 0 0 0 X]
// [X 0 0 0 0 0 0 0 0 0 0 X]
// [X 0 0 0 0 0 0 0 0 0 0 X]
// [X 0 0 0 0 0 0 0 0 0 0 X]
// [X 0 0 0 0 0 0 0 0 0 0 X]
// [X 0 0 0 0 0 0 0 0 0 0 X]
// [X 0 0 0 0 0 0 0 0 0 0 X]
// [X 0 0 0 0 0 0 0 0 0 0 X]
// [X * * * * 0 0 0 0 0 0 X]
// [X 0 0 0 0 X X X X X X X]
// [X X X X X X X X X X X X]

gameRoot.Piece = 0
gameRoot.PieceRotation = 0
gameRoot.PiecePosCol = 1
gameRoot.PiecePosRow = 17

gameRoot.Field[18] = []int{1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1}
//log.Printf("1: %s", gameRoot.GetDebugState())

gameInput <- PlayInputPause
game = <-gameOutput
log.Printf("2: %s", game.GetDebugState())
log.Printf("A: %+v", game.Field[18])

gameInput <- PlayInputDrop
game = <-gameOutput
log.Printf("3: %s", game.GetDebugState())
log.Printf("B: %+v", game.Field[18])
expectedRow := []int{1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1}
if !slices.Equal(game.Field[18], expectedRow) {
t.Errorf("Row not as expected. got: %+v want %+v", game.Field[18], expectedRow)
}

gameInput <- PlayInputDrop
game = <-gameOutput
log.Printf("4: %s", game.GetDebugState())
log.Printf("C: %+v", game.Field[18])
expectedRow = []int{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
if !slices.Equal(game.Field[18], expectedRow) {
t.Errorf("Row not as expected. got: %+v want %+v", game.Field[18], expectedRow)
}
}

// func TestGetDebugState(t *testing.T) {
// // FIXME
// }

func TestMove(t *testing.T) {

gameRoot, gameInput, gameOutput := NewGame()
Expand Down Expand Up @@ -159,7 +234,6 @@ func TestMove(t *testing.T) {
t.Errorf("Piece not on right wall. got: %d expected: %d", posCol, posColExpected)
}

gameInput <- PlayInputPause
game = <-gameOutput

posRow := game.PiecePosRow
Expand All @@ -185,13 +259,11 @@ func TestPauseGame(t *testing.T) {
t.Errorf("Game not running. %d", game.State)
}

gameInput <- PlayInputPause
game = <-gameOutput
log.Printf("%+v", game)
posRow := game.PiecePosRow
posRowExpected := posRow

if game.State != StatePaused {
t.Errorf("Game not paused. %d", game.State)
}
gameInput <- PlayInputPause
time.Sleep(3 * time.Second)

gameInput <- PlayInputPause
game = <-gameOutput
Expand All @@ -200,6 +272,10 @@ func TestPauseGame(t *testing.T) {
if game.State != StateRunning {
t.Errorf("Game not running. %d", game.State)
}

if posRow != posRowExpected {
t.Errorf("piece not at expected positioon. got: %d want: %d", posRow, posRowExpected)
}
}

func TestRotate(t *testing.T) {
Expand Down

0 comments on commit 447ef27

Please sign in to comment.