From b59649336acda491e4b796cc82dbd68744e5abda Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 31 Jul 2016 04:25:45 +0900 Subject: [PATCH] examples/2048: Refactoring: Don't animate when all tiles are not to move --- examples/2048/2048/board.go | 5 ----- examples/2048/2048/tile.go | 6 ++++++ examples/2048/2048/tile_test.go | 7 +++++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/examples/2048/2048/board.go b/examples/2048/2048/board.go index 74b0685dd..617558483 100644 --- a/examples/2048/2048/board.go +++ b/examples/2048/2048/board.go @@ -61,11 +61,6 @@ func (b *Board) Update(input *Input) error { } return nil } - for t := range b.tiles { - if t.IsAnimating() { - return nil - } - } if dir, ok := input.Dir(); ok { if err := b.Move(dir); err != nil { return err diff --git a/examples/2048/2048/tile.go b/examples/2048/2048/tile.go index 90b3a72c0..403ad770e 100644 --- a/examples/2048/2048/tile.go +++ b/examples/2048/2048/tile.go @@ -167,6 +167,12 @@ func MoveTiles(tiles map[*Tile]struct{}, size int, dir Dir) bool { t.animationCount = maxAnimationCount } } + if !moved { + for t := range tiles { + t.next = TileData{} + t.animationCount = 0 + } + } return moved } diff --git a/examples/2048/2048/tile_test.go b/examples/2048/2048/tile_test.go index 19eb5b52d..3f7ca0eb6 100644 --- a/examples/2048/2048/tile_test.go +++ b/examples/2048/2048/tile_test.go @@ -227,8 +227,11 @@ func TestMoveTiles(t *testing.T) { for _, test := range testCases { want, _ := tilesToCells(cellsToTiles(test.Want, size), size) tiles := cellsToTiles(test.Input, size) - MoveTiles(tiles, size, test.Dir) - _, got := tilesToCells(tiles, size) + moved := MoveTiles(tiles, size, test.Dir) + input, got := tilesToCells(tiles, size) + if !moved { + got = input + } if fmt.Sprint(got) != fmt.Sprint(want) { t.Errorf("dir: %s, input: %v, got %v; want %v", test.Dir.String(), test.Input, got, want) }