Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add remaining SRS unit test cases #20

Merged
merged 6 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading