diff --git a/app.go b/app.go index 95fe9ef..71032ce 100644 --- a/app.go +++ b/app.go @@ -7,13 +7,13 @@ import ( // Application presents the actual the runner type Application struct { - GridSize int - Creatures []*models.Creature - Commands []MoveCommand - CurrentZombie *models.Zombie - NextZombie *TransformedZombie - FinishedZombies []*models.Zombie - Score int + gridSize int + creatures []*models.Creature + commands []MoveCommand + currentZombie *models.Zombie + nextZombie *TransformedZombie + finishedZombies []*models.Zombie + score int } // TransformedZombie defines the zombies transformed from creatures, @@ -25,8 +25,8 @@ type TransformedZombie struct { // Run starts zombie with movements func (app *Application) Run() { - for _, command := range app.Commands { - command.Execute(app.CurrentZombie) + for _, command := range app.commands { + command.Execute(app.currentZombie) collided, creature := app.checkCollisionWithCreature() if collided { @@ -46,9 +46,9 @@ func (app *Application) Run() { // Stats prints out statistic information func (app *Application) Stats() { - fmt.Printf("zombies score: %d \n", app.Score) + fmt.Printf("zombies score: %d \n", app.score) var locations string - for _, zombie := range app.FinishedZombies { + for _, zombie := range app.finishedZombies { locations += fmt.Sprintf("%s ", zombie.PrintLocation()) } @@ -56,8 +56,8 @@ func (app *Application) Stats() { } func (app *Application) checkCollisionWithCreature() (bool, *models.Creature) { - for _, creature := range app.Creatures { - if app.CurrentZombie.Coordinate == creature.Coordinate { + for _, creature := range app.creatures { + if app.currentZombie.Coordinate == creature.Coordinate { return true, creature } } @@ -66,14 +66,14 @@ func (app *Application) checkCollisionWithCreature() (bool, *models.Creature) { func (app *Application) transformCreatureToZombie(creature *models.Creature) { app.appendTransformedZombie(&models.Zombie{ - GridSize: app.GridSize, + GridSize: app.gridSize, Coordinate: creature.Coordinate, }) app.removeCreature(creature) } func (app *Application) removeCreature(creature *models.Creature) { - creatures := app.Creatures + creatures := app.creatures var creatureIndex int for index, tempCreature := range creatures { if creature == tempCreature { @@ -83,39 +83,39 @@ func (app *Application) removeCreature(creature *models.Creature) { // assign the last index item to the deleted index, then shrink the slice creatures[creatureIndex] = creatures[len(creatures)-1] creatures[len(creatures)-1] = nil - app.Creatures = creatures[:len(creatures)-1] + app.creatures = creatures[:len(creatures)-1] } func (app *Application) appendTransformedZombie(zombie *models.Zombie) { - next := app.NextZombie + next := app.nextZombie for next != nil { next = next.Next } - app.NextZombie = &TransformedZombie{ + app.nextZombie = &TransformedZombie{ Zombie: zombie, Next: nil, } } func (app *Application) IncreaseScore() { - app.Score++ + app.score++ } func (app *Application) recordedFinishedZombies() { - app.FinishedZombies = append(app.FinishedZombies, app.CurrentZombie) + app.finishedZombies = append(app.finishedZombies, app.currentZombie) } func (app *Application) updateNextZombie() { - app.NextZombie = app.NextZombie.Next + app.nextZombie = app.nextZombie.Next } func (app *Application) updateCurrentZombie() { - app.CurrentZombie = app.NextZombie.Zombie + app.currentZombie = app.nextZombie.Zombie } func (app *Application) hasTransformedZombie() bool { - return app.NextZombie != nil + return app.nextZombie != nil } diff --git a/app_test.go b/app_test.go new file mode 100644 index 0000000..96c371c --- /dev/null +++ b/app_test.go @@ -0,0 +1,159 @@ +package zombie + +import ( + "github.com/wahyd4/zombie/models" + "reflect" + "testing" +) + +func TestApplication_Run(t *testing.T) { + type fields struct { + GridSize int + Creatures []*models.Creature + Commands []MoveCommand + CurrentZombie *models.Zombie + NextZombie *TransformedZombie + FinishedZombies []*models.Zombie + Score int + } + tests := []struct { + name string + fields fields + wantFinishedZombies []*models.Zombie + wantScore int + }{ + { + "can run a simple command", + fields{ + GridSize: 4, + Creatures: []*models.Creature{ + &models.Creature{ + Coordinate: models.Coordinate{ + XAxis: 1, + YAxis: 1, + }, + }, + }, + Commands: []MoveCommand{ + &MoveUpCommand{}, + }, + CurrentZombie: &models.Zombie{ + GridSize: 4, + Coordinate: models.Coordinate{ + XAxis: 3, + YAxis: 3, + }, + }, + NextZombie: nil, + FinishedZombies: nil, + Score: 0, + }, + []*models.Zombie{ + &models.Zombie{ + GridSize: 4, + Coordinate: models.Coordinate{ + XAxis: 3, + YAxis: 2, + }, + }, + }, + 0, + }, + { + "can run some more commands", + fields{ + GridSize: 4, + Creatures: []*models.Creature{ + &models.Creature{ + Coordinate: models.Coordinate{ + XAxis: 0, + YAxis: 1, + }, + }, + &models.Creature{ + Coordinate: models.Coordinate{ + XAxis: 1, + YAxis: 2, + }, + }, + &models.Creature{ + Coordinate: models.Coordinate{ + XAxis: 3, + YAxis: 1, + }, + }, + }, + Commands: []MoveCommand{ + &MoveDownCommand{}, + &MoveLeftCommand{}, + &MoveUpCommand{}, + &MoveUpCommand{}, + &MoveRightCommand{}, + &MoveRightCommand{}, + }, + CurrentZombie: &models.Zombie{ + GridSize: 4, + Coordinate: models.Coordinate{ + XAxis: 2, + YAxis: 1, + }, + }, + NextZombie: nil, + FinishedZombies: nil, + Score: 0, + }, + []*models.Zombie{ + &models.Zombie{ + GridSize: 4, + Coordinate: models.Coordinate{ + XAxis: 3, + YAxis: 0, + }, + }, + &models.Zombie{ + GridSize: 4, + Coordinate: models.Coordinate{ + XAxis: 2, + YAxis: 1, + }, + }, + &models.Zombie{ + GridSize: 4, + Coordinate: models.Coordinate{ + XAxis: 1, + YAxis: 0, + }, + }, + &models.Zombie{ + GridSize: 4, + Coordinate: models.Coordinate{ + XAxis: 0, + YAxis: 0, + }, + }, + }, + 3, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + app := &Application{ + gridSize: tt.fields.GridSize, + creatures: tt.fields.Creatures, + commands: tt.fields.Commands, + currentZombie: tt.fields.CurrentZombie, + nextZombie: tt.fields.NextZombie, + finishedZombies: tt.fields.FinishedZombies, + score: tt.fields.Score, + } + app.Run() + if ok := reflect.DeepEqual(tt.wantFinishedZombies, app.finishedZombies); !ok { + t.Errorf("expected wantFinishedZombies to be %v but got %v", tt.wantFinishedZombies, app.finishedZombies) + } + + if ok := reflect.DeepEqual(tt.wantScore, app.score); !ok { + t.Errorf("expected score to be %v but got %v", tt.wantScore, app.score) + } + }) + } +} diff --git a/parse.go b/parse.go index 5f65a52..9866516 100644 --- a/parse.go +++ b/parse.go @@ -30,7 +30,7 @@ func Init(input string) (*Application, error) { defer file.Close() application := Application{ - CurrentZombie: &models.Zombie{}, + currentZombie: &models.Zombie{}, } scanner := bufio.NewScanner(file) for lineIndex := 0; scanner.Scan(); lineIndex++ { @@ -38,17 +38,17 @@ func Init(input string) (*Application, error) { switch lineIndex { case 0: gridSize, _ := strconv.Atoi(text) - application.GridSize = gridSize - application.CurrentZombie.GridSize = gridSize + application.gridSize = gridSize + application.currentZombie.GridSize = gridSize case 1: coordinate := parseCoordinate(text) - application.CurrentZombie.Coordinate = coordinate + application.currentZombie.Coordinate = coordinate case 2: creatures := parseCreatures(text) - application.Creatures = creatures + application.creatures = creatures default: commands := parseCommands(text) - application.Commands = commands + application.commands = commands } } diff --git a/parse_test.go b/parse_test.go index 2cfe506..3ef6eef 100644 --- a/parse_test.go +++ b/parse_test.go @@ -20,20 +20,20 @@ func TestInit(t *testing.T) { "can parse the input data file and init an application", args{input: "test_data/input_test.txt"}, &Application{ - GridSize: 4, - CurrentZombie: &models.Zombie{ + gridSize: 4, + currentZombie: &models.Zombie{ GridSize: 4, Coordinate: models.Coordinate{ XAxis: 2, YAxis: 1, }, }, - Creatures: []*models.Creature{ + creatures: []*models.Creature{ &models.Creature{Coordinate: models.Coordinate{0, 1}}, &models.Creature{Coordinate: models.Coordinate{XAxis: 1, YAxis: 2}}, &models.Creature{Coordinate: models.Coordinate{XAxis: 3, YAxis: 1}}, }, - Commands: []MoveCommand{ + commands: []MoveCommand{ &MoveDownCommand{}, &MoveLeftCommand{}, &MoveUpCommand{},