From cf591dbb95ae35ca43f99c4a9a529399efbf9d61 Mon Sep 17 00:00:00 2001 From: Junwei Zhao Date: Sun, 15 Mar 2020 23:36:39 +1100 Subject: [PATCH] code refactoring --- app.go | 32 ++++++++++++++++++++------------ parse.go | 6 +++--- parse_test.go | 2 +- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/app.go b/app.go index eee07d8..95fe9ef 100644 --- a/app.go +++ b/app.go @@ -7,33 +7,37 @@ import ( // Application presents the actual the runner type Application struct { - GridSize int - Zombie *models.Zombie - Creatures []*models.Creature - Commands []MoveCommand - + GridSize int + Creatures []*models.Creature + Commands []MoveCommand + CurrentZombie *models.Zombie NextZombie *TransformedZombie FinishedZombies []*models.Zombie Score int } +// TransformedZombie defines the zombies transformed from creatures, +// and these type of zombies are linked one by one type TransformedZombie struct { Zombie *models.Zombie Next *TransformedZombie } +// Run starts zombie with movements func (app *Application) Run() { for _, command := range app.Commands { - command.Execute(app.Zombie) + command.Execute(app.CurrentZombie) + collided, creature := app.checkCollisionWithCreature() if collided { app.transformCreatureToZombie(creature) app.IncreaseScore() } } - app.updateFinishedZombies() - if app.NextZombie != nil { + app.recordedFinishedZombies() + + if app.hasTransformedZombie() { app.updateCurrentZombie() app.updateNextZombie() app.Run() @@ -53,7 +57,7 @@ func (app *Application) Stats() { func (app *Application) checkCollisionWithCreature() (bool, *models.Creature) { for _, creature := range app.Creatures { - if app.Zombie.Coordinate == creature.Coordinate { + if app.CurrentZombie.Coordinate == creature.Coordinate { return true, creature } } @@ -100,8 +104,8 @@ func (app *Application) IncreaseScore() { app.Score++ } -func (app *Application) updateFinishedZombies() { - app.FinishedZombies = append(app.FinishedZombies, app.Zombie) +func (app *Application) recordedFinishedZombies() { + app.FinishedZombies = append(app.FinishedZombies, app.CurrentZombie) } func (app *Application) updateNextZombie() { @@ -109,5 +113,9 @@ func (app *Application) updateNextZombie() { } func (app *Application) updateCurrentZombie() { - app.Zombie = app.NextZombie.Zombie + app.CurrentZombie = app.NextZombie.Zombie +} + +func (app *Application) hasTransformedZombie() bool { + return app.NextZombie != nil } diff --git a/parse.go b/parse.go index 77f608d..5f65a52 100644 --- a/parse.go +++ b/parse.go @@ -30,7 +30,7 @@ func Init(input string) (*Application, error) { defer file.Close() application := Application{ - Zombie: &models.Zombie{}, + CurrentZombie: &models.Zombie{}, } scanner := bufio.NewScanner(file) for lineIndex := 0; scanner.Scan(); lineIndex++ { @@ -39,10 +39,10 @@ func Init(input string) (*Application, error) { case 0: gridSize, _ := strconv.Atoi(text) application.GridSize = gridSize - application.Zombie.GridSize = gridSize + application.CurrentZombie.GridSize = gridSize case 1: coordinate := parseCoordinate(text) - application.Zombie.Coordinate = coordinate + application.CurrentZombie.Coordinate = coordinate case 2: creatures := parseCreatures(text) application.Creatures = creatures diff --git a/parse_test.go b/parse_test.go index 781b7b9..2cfe506 100644 --- a/parse_test.go +++ b/parse_test.go @@ -21,7 +21,7 @@ func TestInit(t *testing.T) { args{input: "test_data/input_test.txt"}, &Application{ GridSize: 4, - Zombie: &models.Zombie{ + CurrentZombie: &models.Zombie{ GridSize: 4, Coordinate: models.Coordinate{ XAxis: 2,