Skip to content

Commit

Permalink
test: add remaining SRS unit test cases (#20)
Browse files Browse the repository at this point in the history
* refactor: return rotation point instead of bool for SRS

* test: finish adding cases for TestTetrimino_rotateClockwise

* test: add invalid cases for TestTetrimino_rotateCounterClockwise

* test: finish adding cases for TestTetrimino_rotateCounterClockwise

* docs(readme): update todo list
  • Loading branch information
Broderick-Westrope authored Sep 27, 2024
1 parent 76b50e7 commit 8306cdc
Show file tree
Hide file tree
Showing 3 changed files with 1,914 additions and 215 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ The ordered priorities for testing are:
### TODO

- Add more tests
- Finish Super Rotation System testing in [tetrimino_test.go](./pkg/tetris/tetrimino_test.go).
- Add tests for scoring endOnMaxLevel.
- Revisit scoring tests.
- Add the remaining Lock Down options.
Expand Down
44 changes: 24 additions & 20 deletions pkg/tetris/tetrimino.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"math"
)

const invalidRotationPoint = -1

// A Tetrimino is a geometric Tetris shape formed by four Minos connected along their sides.
type Tetrimino struct {
// The value of the Tetrimino. This is the character that will be used to represent the Tetrimino in the matrix.
Expand Down Expand Up @@ -252,26 +254,28 @@ func (t *Tetrimino) Rotate(matrix Matrix, clockwise bool) error {

rotated := t.DeepCopy()
var err error
var foundValid bool
var rotationPoint int
if clockwise {
foundValid, err = rotated.rotateClockwise(matrix)
rotationPoint, err = rotated.rotateClockwise(matrix)
} else {
foundValid, err = rotated.rotateCounterClockwise(matrix)
rotationPoint, err = rotated.rotateCounterClockwise(matrix)
}
if err != nil {
return fmt.Errorf("failed to rotate tetrimino: %w", err)
}

if foundValid {
t.Pos = rotated.Pos
t.Minos = rotated.Minos
t.CompassDirection = rotated.CompassDirection
foundValid := rotationPoint != invalidRotationPoint
if !foundValid {
return nil
}

t.Pos = rotated.Pos
t.Minos = rotated.Minos
t.CompassDirection = rotated.CompassDirection
return nil
}

func (t *Tetrimino) rotateClockwise(matrix Matrix) (bool, error) {
func (t *Tetrimino) rotateClockwise(matrix Matrix) (int, error) {
// Reverse the order of the rows
for i, j := 0, len(t.Minos)-1; i < j; i, j = i+1, j-1 {
t.Minos[i], t.Minos[j] = t.Minos[j], t.Minos[i]
Expand All @@ -282,23 +286,23 @@ func (t *Tetrimino) rotateClockwise(matrix Matrix) (bool, error) {
var err error
t.CompassDirection, err = positiveMod(t.CompassDirection+1, len(t.RotationCompass))
if err != nil {
return false, fmt.Errorf("failed to get positive mod: %w", err)
return invalidRotationPoint, fmt.Errorf("failed to get positive mod: %w", err)
}

originalX, originalY := t.Pos.X, t.Pos.Y
for _, coord := range t.RotationCompass[t.CompassDirection] {
for i, coord := range t.RotationCompass[t.CompassDirection] {
t.Pos.X = originalX + coord.X
t.Pos.Y = originalY + coord.Y

if t.isValid(matrix) {
return true, nil
return i + 1, nil
}
}

return false, nil
return invalidRotationPoint, nil
}

func (t *Tetrimino) rotateCounterClockwise(matrix Matrix) (bool, error) {
func (t *Tetrimino) rotateCounterClockwise(matrix Matrix) (int, error) {
// Reverse the order of the columns
for _, row := range t.Minos {
for i, j := 0, len(row)-1; i < j; i, j = i+1, j-1 {
Expand All @@ -308,28 +312,28 @@ func (t *Tetrimino) rotateCounterClockwise(matrix Matrix) (bool, error) {

t.transpose()

foundValid := false
rotationPoint := invalidRotationPoint
originalX, originalY := t.Pos.X, t.Pos.Y
for _, coord := range t.RotationCompass[t.CompassDirection] {
for i, coord := range t.RotationCompass[t.CompassDirection] {
t.Pos.X = originalX - coord.X
t.Pos.Y = originalY - coord.Y

if t.isValid(matrix) {
foundValid = true
rotationPoint = i + 1
break
}
}
if !foundValid {
return false, nil
if rotationPoint == invalidRotationPoint {
return rotationPoint, nil
}

var err error
t.CompassDirection, err = positiveMod(t.CompassDirection-1, len(t.RotationCompass))
if err != nil {
return false, fmt.Errorf("failed to get positive mod: %w", err)
return invalidRotationPoint, fmt.Errorf("failed to get positive mod: %w", err)
}

return true, nil
return rotationPoint, nil
}

func (t *Tetrimino) transpose() {
Expand Down
Loading

0 comments on commit 8306cdc

Please sign in to comment.