simulator

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RandomSimple

type RandomSimple struct {
}

RandomSimple is a simple random integer generator Important: it does not rely on crypto safe random generator

func NewRandomSimple

func NewRandomSimple() *RandomSimple

NewRandomSimple is a simple random generator

func (*RandomSimple) GetRandomInt

func (rs *RandomSimple) GetRandomInt(n int) (int, error)

GetRandomInt retrieves a random integer between 0 and n-1 given n Returns an error if n <= 0

type Randomer

type Randomer interface {
	// GetRandomInt retrieves a random integer between 0 and n-1 given n
	GetRandomInt(n int) (int, error)
}

Randomer is a random generator

type RandomerMock

type RandomerMock struct {
	mock.Mock
}

RandomerMock mocks a Randomer

func (*RandomerMock) GetRandomInt

func (r *RandomerMock) GetRandomInt(n int) (int, error)

GetRandomInt retrieves a random integer between 0 and n-1 given n

type SimulationEngine

type SimulationEngine struct {
	// contains filtered or unexported fields
}

SimulationEngine is a simple implementation of an alien invasion simulator

func NewSimulationEngine

func NewSimulationEngine(startAliens, maxSteps uint, world WorldStorer, random Randomer, in io.Reader, out io.Writer) *SimulationEngine

NewSimulationEngine is a simulation engine constructor

func (*SimulationEngine) Finalize

func (s *SimulationEngine) Finalize(ctx context.Context) error

Finalize finalizes the simulation

func (*SimulationEngine) HasNextStep

func (s *SimulationEngine) HasNextStep(ctx context.Context) (bool, error)

HasNextStep computes if a next step of the simulation exists

func (*SimulationEngine) Prepare

func (s *SimulationEngine) Prepare(ctx context.Context) error

Prepare prepares the simulation

func (*SimulationEngine) Run

func (s *SimulationEngine) Run(ctx context.Context) error

Run simulates an alien invasion

func (*SimulationEngine) SimulateNextStep

func (s *SimulationEngine) SimulateNextStep(ctx context.Context) error

SimulateNextStep simulates the next step of the simulation

type Simulator

type Simulator interface {
	// Prepare prepares the simulation
	Prepare(ctx context.Context) error
	// HasNextStep computes if a next step of the simulation exists
	HasNextStep(ctx context.Context) (bool, error)
	// SimulateNextStep simulates the next step of the simulation
	SimulateNextStep(ctx context.Context) error
	// Run simulates an alien invasion
	Run(ctx context.Context) error
	// Finalize finalizes the simulation
	Finalize(ctx context.Context) error
}

Simulator is an alien invasion simulator interface

type SimulatorMock

type SimulatorMock struct {
	mock.Mock
}

SimulatorMock mocks a Simulator

func (*SimulatorMock) Finalize

func (s *SimulatorMock) Finalize(ctx context.Context) error

Finalize finalizes the simulation

func (*SimulatorMock) HasNextStep

func (s *SimulatorMock) HasNextStep(ctx context.Context) (bool, error)

HasNextStep computes if a next step of the simulation exists

func (*SimulatorMock) Prepare

func (s *SimulatorMock) Prepare(ctx context.Context) error

Prepare prepares the simulation

func (*SimulatorMock) Run

func (s *SimulatorMock) Run(ctx context.Context) error

Run simulates an alien invasion

func (*SimulatorMock) SimulateNextStep

func (s *SimulatorMock) SimulateNextStep(ctx context.Context) error

SimulateNextStep simulates the next step of the simulation

type World

type World struct {
	// contains filtered or unexported fields
}

World represents the world with cities, aliens, and links

func NewWorld

func NewWorld() *World

NewWorld is a world constructor

func (*World) AddAlien

func (w *World) AddAlien(ctx context.Context, alienID int) (*entity.Alien, error)

AddAlien adds an alien to the world

func (*World) AddCity

func (w *World) AddCity(ctx context.Context, cityName string) (*entity.City, error)

AddCity adds a city to the world

func (w *World) AddLink(ctx context.Context, cityFrom, cityTo *entity.City, direction entity.Direction) error

AddLink adds a link from a city to another city given a direction

func (*World) DestroyCity

func (w *World) DestroyCity(ctx context.Context, city *entity.City) error

DestroyCity destroys a city

func (*World) GetAlien

func (w *World) GetAlien(ctx context.Context, alienID int) (*entity.Alien, error)

GetAlien retrieves an alien

func (*World) GetAlienAtCity

func (w *World) GetAlienAtCity(ctx context.Context, city *entity.City) (*entity.Alien, error)

GetAlienAtCity retrieves the alien at a city

func (*World) GetAliveCities

func (w *World) GetAliveCities(ctx context.Context) ([]*entity.City, error)

GetAliveCities retrieves the list of non destroyed cities

func (*World) GetCity

func (w *World) GetCity(ctx context.Context, cityName string) (*entity.City, error)

GetCity retrieves a city

func (*World) GetUntrappedAliens

func (w *World) GetUntrappedAliens(ctx context.Context) ([]*entity.Alien, error)

GetUntrappedAliens retrieves the list of untrapped aliens

func (*World) IsTrappedAlien

func (w *World) IsTrappedAlien(ctx context.Context, alien *entity.Alien) (bool, error)

IsTrappedAlien checks if an alien is trapped

func (*World) MoveAlien

func (w *World) MoveAlien(ctx context.Context, alien *entity.Alien, city *entity.City) error

MoveAlien moves an alien to a city

func (*World) TrapAlien

func (w *World) TrapAlien(ctx context.Context, alien *entity.Alien) error

TrapAlien traps an alien

type WorldStorer

type WorldStorer interface {
	// GetCity retrieves a city
	GetCity(ctx context.Context, cityName string) (*entity.City, error)
	// GetAliveCities retrieves the list of non destroyed cities
	GetAliveCities(ctx context.Context) ([]*entity.City, error)
	// AddCity adds a city
	AddCity(ctx context.Context, cityName string) (*entity.City, error)
	// DestroyCity destroys a city
	DestroyCity(ctx context.Context, city *entity.City) error
	// AddLink adds a link from a city to another city given a direction
	AddLink(ctx context.Context, cityFrom, cityTo *entity.City, direction entity.Direction) error
	// GetAlien retrieves an alien
	GetAlien(ctx context.Context, alienID int) (*entity.Alien, error)
	// AddAlien adds an alien
	AddAlien(ctx context.Context, alienID int) (*entity.Alien, error)
	// MoveAlien moves an alien to a city
	MoveAlien(ctx context.Context, alien *entity.Alien, city *entity.City) error
	// IsTrappedAlien checks if an alien is trapped
	IsTrappedAlien(ctx context.Context, alien *entity.Alien) (bool, error)
	// TrapAlien traps an alien
	TrapAlien(ctx context.Context, alien *entity.Alien) error
	// GetAlienAtCity retrieves the alien at a given city
	GetAlienAtCity(ctx context.Context, city *entity.City) (*entity.Alien, error)
	// GetUntrappedAliens retrieves the list of untrapped aliens
	GetUntrappedAliens(ctx context.Context) ([]*entity.Alien, error)
}

WorldStorer is a world store interface

type WorldStorerMock

type WorldStorerMock struct {
	mock.Mock
}

WorldStorerMock mocks a WorldStorer

func (*WorldStorerMock) AddAlien

func (w *WorldStorerMock) AddAlien(ctx context.Context, alienID int) (*entity.Alien, error)

AddAlien adds an alien

func (*WorldStorerMock) AddCity

func (w *WorldStorerMock) AddCity(ctx context.Context, cityName string) (*entity.City, error)

AddCity adds a city

func (w *WorldStorerMock) AddLink(ctx context.Context, cityFrom, cityTo *entity.City, direction entity.Direction) error

AddLink adds a link from a city to another city given a direction

func (*WorldStorerMock) DestroyCity

func (w *WorldStorerMock) DestroyCity(ctx context.Context, city *entity.City) error

DestroyCity destroys a city

func (*WorldStorerMock) GetAlien

func (w *WorldStorerMock) GetAlien(ctx context.Context, alienID int) (*entity.Alien, error)

GetAlien retrieves an alien

func (*WorldStorerMock) GetAlienAtCity

func (w *WorldStorerMock) GetAlienAtCity(ctx context.Context, city *entity.City) (*entity.Alien, error)

GetAlienAtCity retrieves the alien at a given city

func (*WorldStorerMock) GetAliveCities

func (w *WorldStorerMock) GetAliveCities(ctx context.Context) ([]*entity.City, error)

GetAliveCities retrieves the list of non destroyed cities

func (*WorldStorerMock) GetCity

func (w *WorldStorerMock) GetCity(ctx context.Context, cityName string) (*entity.City, error)

GetCity retrieves a city

func (*WorldStorerMock) GetUntrappedAliens

func (w *WorldStorerMock) GetUntrappedAliens(ctx context.Context) ([]*entity.Alien, error)

GetUntrappedAliens retrieves the list of untrapped aliens

func (*WorldStorerMock) IsTrappedAlien

func (w *WorldStorerMock) IsTrappedAlien(ctx context.Context, alien *entity.Alien) (bool, error)

IsTrappedAlien checks if an alien is trapped

func (*WorldStorerMock) MoveAlien

func (w *WorldStorerMock) MoveAlien(ctx context.Context, alien *entity.Alien, city *entity.City) error

MoveAlien moves an alien to a city

func (*WorldStorerMock) TrapAlien

func (w *WorldStorerMock) TrapAlien(ctx context.Context, alien *entity.Alien) error

TrapAlien traps an alien

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL