Skip to content

Commit

Permalink
Add linter
Browse files Browse the repository at this point in the history
  • Loading branch information
z-riley committed Nov 10, 2024
1 parent 0c7dc95 commit 06421f8
Show file tree
Hide file tree
Showing 19 changed files with 149 additions and 142 deletions.
52 changes: 52 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
linters:
disable:
- depguard
- err113
- exhaustive
- exhaustruct
- forcetypeassert
- funlen
- gochecknoglobals
- gosec
- inamedparam
- ireturn
- mnd
- nlreturn
- nonamedreturns
- paralleltest
- testpackage
- varnamelen
- wrapcheck
- wsl
- deadcode # deprecated
- exhaustivestruct # deprecated
- golint # deprecated
- ifshort # deprecated
- interfacer # deprecated
- maligned # deprecated
- gomnd # deprecated
- nosnakecase # deprecated
- scopelint # deprecated
- structcheck # deprecated
- varcheck # deprecated
presets:
- bugs
- comment
- complexity
- error
- format
- import
- metalinter
- module
- performance
- sql
- style
- unused

issues:
max-issues-per-linter: 0 # unlimited
max-same-issues: 0 # unlimited
exclude-rules:
- path: _test\.go
linters:
- dupl
2 changes: 1 addition & 1 deletion backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (g *Game) ExecuteMove(dir grid.Direction) {
g.Timer.Resume()
}

// Note: the game should save on exit anyway but save after after move just in case
// Note: The game should save on exit anyway but save after move just in case
if g.opts.SaveToDisk {
go func() {
if err := g.Save(); err != nil {
Expand Down
7 changes: 6 additions & 1 deletion backend/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ func TestSerialiseDeserialise(t *testing.T) {

// Create an empty game and set it to the previous game's state
g := Game{}
g.Deserialise(b)
if err := g.Deserialise(b); err != nil {
t.Error(err)
}

// Check that the two games are identical
expected, err := game.Serialise()
if err != nil {
t.Error(err)
}
got, err := g.Serialise()
if err != nil {
t.Error(err)
Expand Down
48 changes: 23 additions & 25 deletions backend/grid/grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ func (g *Grid) spawnTile() {
// Returns true if any tiles were moved from the attempt, and the added score from any combinations.
func (g *Grid) move(dir Direction) (bool, int) {
// Clear all of the "combined this turn" flags
for i := 0; i < gridWidth; i++ {
for j := 0; j < gridHeight; j++ {
for i := range gridWidth {
for j := range gridHeight {
g.Tiles[i][j].Cmb = false
}
}
Expand All @@ -143,7 +143,7 @@ func (g *Grid) move(dir Direction) (bool, int) {
// Execute moves until grid can no longer move
for {
movedThisTurn := false
for row := 0; row < gridHeight; row++ {
for row := range gridHeight {
var rowMoved bool
var points int

Expand Down Expand Up @@ -183,9 +183,9 @@ func moveStep(g [gridWidth]Tile, dir Direction) ([gridWidth]Tile, bool, int) {
reverse = true
}

iter := NewIter(len(g), reverse)
for iter.HasNext() {
i := iter.Next()
iter := newIter(len(g), reverse)
for iter.hasNext() {
i := iter.next()
// Calculate the hypothetical next position for the tile
newPos := i - 1
if reverse {
Expand All @@ -212,7 +212,6 @@ func moveStep(g [gridWidth]Tile, dir Direction) ([gridWidth]Tile, bool, int) {
g[i].Val = emptyTile // clear the old location
g[i].UUID = uuid.Must(uuid.NewV7())
return g, true, valAfterCombine

} else if g[newPos].Val != emptyTile {
// Move blocked by another tile
continue
Expand All @@ -232,25 +231,25 @@ func moveStep(g [gridWidth]Tile, dir Direction) ([gridWidth]Tile, bool, int) {
// isLoss returns true if the grid is in a losing state (gridlocked).
func (g *Grid) isLoss() bool {
// False if any empty spaces exist
for i := 0; i < gridHeight; i++ {
for j := 0; j < gridWidth; j++ {
for i := range gridHeight {
for j := range gridWidth {
if g.Tiles[i][j].Val == emptyTile {
return false
}
}
}

// False if any similar tiles exist next to each other
for i := 0; i < gridHeight; i++ {
for j := 0; j < gridWidth-1; j++ {
for i := range gridHeight {
for j := range gridWidth - 1 {
if g.Tiles[i][j].Val == g.Tiles[i][j+1].Val {
return false
}
}
}
t := Transpose(g.Tiles)
for i := 0; i < gridHeight; i++ {
for j := 0; j < gridWidth-1; j++ {
for i := range gridHeight {
for j := range gridWidth - 1 {
if t[i][j].Val == t[i][j+1].Val {
return false
}
Expand All @@ -276,7 +275,7 @@ func (g *Grid) HighestTile() int {
// Debug arranges the grid into a human readable Debug for debugging purposes.
func (g *Grid) Debug() string {
var out string
for row := 0; row < gridHeight; row++ {
for row := range gridHeight {
for col := range gridWidth {
out += g.Tiles[row][col].paddedString() + "|"
}
Expand All @@ -288,8 +287,8 @@ func (g *Grid) Debug() string {
// Transpose returns a transposed version of the grid.
func Transpose(matrix [gridWidth][gridHeight]Tile) [gridHeight][gridWidth]Tile {
var transposed [gridHeight][gridWidth]Tile
for i := 0; i < gridWidth; i++ {
for j := 0; j < gridHeight; j++ {
for i := range gridWidth {
for j := range gridHeight {
transposed[j][i] = matrix[i][j]
}
}
Expand Down Expand Up @@ -348,17 +347,17 @@ func (t *Tile) paddedString() string {
}
}

// Equal returns whether tile t1 is equal to t2.
func (t1 *Tile) Equal(t2 Tile) bool {
return t1.Val == t2.Val &&
t1.Cmb == t2.Cmb &&
t1.UUID == t2.UUID
// Equal returns whether tile t is equal to t2.
func (t *Tile) Equal(t2 Tile) bool {
return t.Val == t2.Val &&
t.Cmb == t2.Cmb &&
t.UUID == t2.UUID
}

// EqualGrid returns whether grid g1 is equal to g2.
func EqualGrid(g1, g2 [gridWidth][gridHeight]Tile) bool {
for i := 0; i < gridWidth; i++ {
for j := 0; j < gridHeight; j++ {
for i := range gridWidth {
for j := range gridHeight {
if !g1[i][j].Equal(g2[i][j]) {
return false
}
Expand All @@ -371,7 +370,6 @@ func EqualGrid(g1, g2 [gridWidth][gridHeight]Tile) bool {
func newTileVal() int {
if rand.Float64() >= 0.9 {
return 4
} else {
return 2
}
return 2
}
1 change: 0 additions & 1 deletion backend/grid/grid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func TestMove(t *testing.T) {
if !gridsAreEqual(expected.Tiles, got.Tiles) {
t.Errorf("Expected:\n<%v>\nGot:\n<%v>", expected.Debug(), got.Debug())
}

}

func TestMoveStep(t *testing.T) {
Expand Down
32 changes: 13 additions & 19 deletions backend/grid/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,35 @@ type iter struct {
idx int
}

// NewIter constructs a new iterator.
func NewIter(length int, reverse bool) *iter {
// newIter constructs a new iterator.
func newIter(length int, reverse bool) *iter {
if reverse {
return &iter{length: length, reverse: true, idx: length - 1}
} else {
return &iter{length: length, reverse: false, idx: 0}
}
return &iter{length: length, reverse: false, idx: 0}
}

// HasNext returns true if another iteration is available.
func (i *iter) HasNext() bool {
// hasNext returns true if another iteration is available.
func (i *iter) hasNext() bool {
if i.reverse {
return i.idx >= 0
} else {
return i.idx < i.length
}
return i.idx < i.length
}

// Next returns the index of the Next index.
func (i *iter) Next() int {
if !i.HasNext() {
// next returns the index of the next index.
func (i *iter) next() int {
if !i.hasNext() {
panic("no more elements")
}

if i.reverse {
out := i.idx
i.idx--
return out
} else {
out := i.idx
i.idx++
return out
}
}

// Len returns the length of the iterator.
func (i *iter) Len() int {
return i.length
out := i.idx
i.idx++
return out
}
11 changes: 1 addition & 10 deletions backend/timer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package backend

import (
"fmt"
"time"
)

Expand Down Expand Up @@ -47,7 +46,7 @@ func (t *Timer) Reset() *Timer {
return t
}

// Set sets the timer to the specified duration
// Set sets the timer to the specified duration.
func (t *Timer) Set(d time.Duration) *Timer {
t.Time = d
return t
Expand All @@ -57,11 +56,3 @@ func (t *Timer) Set(d time.Duration) *Timer {
func (t *Timer) Duration() time.Duration {
return t.Time
}

// format formats a duration into the format "HH:MM:SS".
func format(t time.Duration) string {
hours := int(t.Hours())
minutes := int(t.Minutes()) % 60
seconds := int(t.Seconds()) % 60
return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
}
Loading

0 comments on commit 06421f8

Please sign in to comment.