examples/2048: Refactoring: Don't animate when all tiles are not to move

This commit is contained in:
Hajime Hoshi 2016-07-31 04:25:45 +09:00
parent 3153720e9a
commit b59649336a
3 changed files with 11 additions and 7 deletions

View File

@ -61,11 +61,6 @@ func (b *Board) Update(input *Input) error {
} }
return nil return nil
} }
for t := range b.tiles {
if t.IsAnimating() {
return nil
}
}
if dir, ok := input.Dir(); ok { if dir, ok := input.Dir(); ok {
if err := b.Move(dir); err != nil { if err := b.Move(dir); err != nil {
return err return err

View File

@ -167,6 +167,12 @@ func MoveTiles(tiles map[*Tile]struct{}, size int, dir Dir) bool {
t.animationCount = maxAnimationCount t.animationCount = maxAnimationCount
} }
} }
if !moved {
for t := range tiles {
t.next = TileData{}
t.animationCount = 0
}
}
return moved return moved
} }

View File

@ -227,8 +227,11 @@ func TestMoveTiles(t *testing.T) {
for _, test := range testCases { for _, test := range testCases {
want, _ := tilesToCells(cellsToTiles(test.Want, size), size) want, _ := tilesToCells(cellsToTiles(test.Want, size), size)
tiles := cellsToTiles(test.Input, size) tiles := cellsToTiles(test.Input, size)
MoveTiles(tiles, size, test.Dir) moved := MoveTiles(tiles, size, test.Dir)
_, got := tilesToCells(tiles, size) input, got := tilesToCells(tiles, size)
if !moved {
got = input
}
if fmt.Sprint(got) != fmt.Sprint(want) { if fmt.Sprint(got) != fmt.Sprint(want) {
t.Errorf("dir: %s, input: %v, got %v; want %v", test.Dir.String(), test.Input, got, want) t.Errorf("dir: %s, input: %v, got %v; want %v", test.Dir.String(), test.Input, got, want)
} }