mirror of
https://github.com/wahyd4/zombie.git
synced 2026-08-09 04:35:54 +10:00
95 lines
2.4 KiB
Go
95 lines
2.4 KiB
Go
package zombie
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"github.com/wahyd4/zombie/models"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
const creatureSplitter = " "
|
|
|
|
var (
|
|
commandsMap = map[string]MoveCommand{
|
|
"U": &MoveUpCommand{},
|
|
"D": &MoveDownCommand{},
|
|
"L": &MoveLeftCommand{},
|
|
"R": &MoveRightCommand{},
|
|
}
|
|
)
|
|
|
|
// Init an application instance with given input file
|
|
func Init(input string) (*Application, error) {
|
|
file, err := os.Open(input)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to open file %v", err)
|
|
}
|
|
defer file.Close()
|
|
|
|
application := Application{
|
|
currentZombie: &models.Zombie{},
|
|
}
|
|
scanner := bufio.NewScanner(file)
|
|
for lineIndex := 0; scanner.Scan(); lineIndex++ {
|
|
text := scanner.Text()
|
|
switch lineIndex {
|
|
case 0:
|
|
gridSize, _ := strconv.Atoi(text)
|
|
application.gridSize = gridSize
|
|
application.currentZombie.GridSize = gridSize
|
|
case 1:
|
|
coordinate := parseCoordinate(text)
|
|
application.currentZombie.Coordinate = coordinate
|
|
case 2:
|
|
creatures := parseCreatures(text)
|
|
application.creatures = creatures
|
|
default:
|
|
commands := parseCommands(text)
|
|
application.commands = commands
|
|
}
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
return nil, fmt.Errorf("failed to read content from file %v", err)
|
|
}
|
|
|
|
return &application, nil
|
|
}
|
|
|
|
func parseCommands(commandString string) []MoveCommand {
|
|
commands := make([]MoveCommand, 0)
|
|
for _, commandRune := range commandString {
|
|
commands = append(commands, commandsMap[string(commandRune)])
|
|
}
|
|
return commands
|
|
}
|
|
|
|
func parseCoordinate(coordinate string) models.Coordinate {
|
|
coordinateWithoutBrackets := coordinate[1 : len(coordinate)-1]
|
|
coordinateArr := strings.Split(coordinateWithoutBrackets, ",")
|
|
if len(coordinateArr) != 2 {
|
|
log.Panic("coordinate string is not valid")
|
|
}
|
|
axisValues := make([]int, 0)
|
|
for _, axisValue := range coordinateArr {
|
|
value, err := strconv.Atoi(axisValue)
|
|
if err != nil {
|
|
log.Panic("cannot convert coordinate string into integers:" + err.Error())
|
|
}
|
|
axisValues = append(axisValues, value)
|
|
}
|
|
return models.Coordinate{XAxis: axisValues[0], YAxis: axisValues[1]}
|
|
}
|
|
|
|
func parseCreatures(coordinates string) []*models.Creature {
|
|
creatureCoordinates := strings.Split(coordinates, creatureSplitter)
|
|
creatures := make([]*models.Creature, 0)
|
|
for _, coordinate := range creatureCoordinates {
|
|
creatures = append(creatures, &models.Creature{Coordinate: parseCoordinate(coordinate)})
|
|
}
|
|
return creatures
|
|
}
|