Skip to content

Commit edb7974

Browse files
fix: rotate counter clockwise was going clockwise
1 parent 6b38b1c commit edb7974

File tree

2 files changed

+304
-279
lines changed

2 files changed

+304
-279
lines changed

pkg/tetris/tetrimino.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type rotationCompass [4]rotationSet
2929
// If the first coordinate cannot be used the next will be attempted.
3030
// This continues until there are no more coordinates to fall back on (in which case rotation is not possible).
3131
// This is part of the Super Rotation System (SRS).
32-
type rotationSet []Coordinate
32+
type rotationSet []*Coordinate
3333

3434
// RotationCompasses is a map of Tetrimino values to the coordinates used for rotation.
3535
// Each slice should contain a coordinate for north, east, south, and west in that order.
@@ -324,23 +324,28 @@ func (t *Tetrimino) rotateCounterClockwise(matrix Matrix) (bool, error) {
324324

325325
t.transpose()
326326

327-
var err error
328-
t.CompassDirection, err = positiveMod(t.CompassDirection+1, len(t.RotationCompass))
329-
if err != nil {
330-
return false, fmt.Errorf("failed to get positive mod: %w", err)
331-
}
332-
327+
foundValid := false
333328
originalX, originalY := t.Pos.X, t.Pos.Y
334329
for _, coord := range t.RotationCompass[t.CompassDirection] {
335330
t.Pos.X = originalX - coord.X
336331
t.Pos.Y = originalY - coord.Y
337332

338333
if t.isValid(matrix) {
339-
return true, nil
334+
foundValid = true
335+
break
340336
}
341337
}
338+
if !foundValid {
339+
return false, nil
340+
}
342341

343-
return false, nil
342+
var err error
343+
t.CompassDirection, err = positiveMod(t.CompassDirection-1, len(t.RotationCompass))
344+
if err != nil {
345+
return false, fmt.Errorf("failed to get positive mod: %w", err)
346+
}
347+
348+
return true, nil
344349
}
345350

346351
func (t *Tetrimino) transpose() {

0 commit comments

Comments
 (0)