Simulating an alien invasion using the Go programming language.
The requirements can be found here.
Run it using go run
, passing the number of aliens:
go run ./cmd/invasion 5
or to run it interactively, step by step, also pass the -i
flag:
go run ./cmd/invasion -i 12
β The world is read from the world.txt file and what's left of it after the invasion simulation is written to a world_after_invasion.txt file.
π‘ A Makefile is also provided for convenience. Please have a look at it for all the available targets, among which:
- make run_dev
- make lint (:exclamation: requires golangci-lint to be installed)
- make test
- make coverage-report
π‘ A Dockerfile is provided for running it with Docker and some convenience make targets like make build_docker_image. To run it with Docker: docker run padurean/mad-aliens 10
The code is structured as follows:
-
cmd/invasion/ is the main package containing just the main.go file. It's code parses the CLI flag and argument, reads and parses the world from the world.txt file, creates and runs a new invasion and then writes what's left of the world to a [world_after_invasion.txt] file. When creating the invasion, it also passes an
onEvent
callback to it which prints any events (basically informative strings) to the console. -
internal/world package contains structs and methods related to the world (map):
- a custom
World
type definition (for a map holding the cities per their names) with convenience methods likeString
,Read
andWrite
(from/to file) and also a more exotic oneFindGhostCities
π» - a
City
struct type which holds a city name, neighbors directions per names and aliens names. - a
Direction
enum type for convenience and a more type-safe way for dealing with related, fixed values.
- a custom
-
internal/invasion package contains a main
Invasion
struct representing the invasion itself with 2 exported methods (besideString
):New
for constructing it andRun
to run it. It also has some non-exported methods which perform specialized tasks and update the state of the world and the summary of the invasion.
For more details please have a look over the code itself. Enjoy! π