Skip to content

Commit

Permalink
Allow tiles with value 4 to spawn on new game
Browse files Browse the repository at this point in the history
  • Loading branch information
z-riley committed Oct 19, 2024
1 parent 96d7add commit ada69d6
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions backend/grid/grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ func (g *Grid) Reset() {
// Try again until they're unique
tile2 = pos{rand.Intn(gridWidth), rand.Intn(gridHeight)}
}
// FIXME: starting values of 4 should also be possible
g.Tiles[tile1.x][tile1.y].Val = 2
g.Tiles[tile2.x][tile2.y].Val = 2
g.Tiles[tile1.x][tile1.y].Val = newTileVal()
g.Tiles[tile2.x][tile2.y].Val = newTileVal()
}

// ClearCmbFlags clears the Cmb flag of every tile.
Expand Down Expand Up @@ -118,18 +117,13 @@ func (g *Grid) Outcome() Outcome {
// spawnTile spawns a single new tile in a random location on the grid. The value of the
// tile is either 2 (90% chance) or 4 (10% chance).
func (g *Grid) spawnTile() {
val := 2
if rand.Float64() >= 0.9 {
val = 4
}

x, y := rand.Intn(gridWidth), rand.Intn(gridHeight)
for g.Tiles[x][y].Val != emptyTile {
// Try again until they're unique
x, y = rand.Intn(gridWidth), rand.Intn(gridHeight)
}

g.Tiles[x][y].Val = val
g.Tiles[x][y].Val = newTileVal()
g.Tiles[x][y].UUID = uuid.Must(uuid.NewV7())
}

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

iter := NewIter(len(g), reverse) // TODO: Use Go 1.23 iterators instead?
iter := NewIter(len(g), reverse)
for iter.HasNext() {
i := iter.Next()
// Calculate the hypothetical next position for the tile
Expand Down Expand Up @@ -372,3 +366,12 @@ func EqualGrid(g1, g2 [gridWidth][gridHeight]Tile) bool {
}
return true
}

// newTileVal generates the value of a new tile.
func newTileVal() int {
if rand.Float64() >= 0.9 {
return 4
} else {
return 2
}
}

0 comments on commit ada69d6

Please sign in to comment.