code refactoring

This commit is contained in:
2020-03-15 23:36:39 +11:00
parent 226d8d49f9
commit cf591dbb95
3 changed files with 24 additions and 16 deletions
+20 -12
View File
@@ -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
}
+3 -3
View File
@@ -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
+1 -1
View File
@@ -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,