game

package
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2018 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Points awarded for scoring hit
	HIT_POINT        = 8
	HIT_STREAK_POINT = 10
	SINK_POINT       = 18
	DISCOVERY_POINT  = 1

	DEPLOY_COST        = 50
	DEPLOY_PER_SQ_COST = 10
	MOVE_COST          = 1
	NEW_TEAM_COST      = 200
)

Point values

View Source
const (
	// ICON_ALIVE is the icon represent a portion of a ship that has not been hit
	ICON_ALIVE = '+'

	// ICON_DEAD is the icon represent a portion of a ship that has been hit
	ICON_DEAD = '@'

	// ICON_MISS is the icon representing a missed attempted attack on a team map
	ICON_MISS = "-"

	ICON_HIT = "x"
)

Map icons

Variables

This section is empty.

Functions

func GetHealthBitfield

func GetHealthBitfield(size uint8) uint8

GetHealthBitfield returns a bitfield representing the Ship and thats parts of it that are hit and unscathed.

func ProduceHitBitmask

func ProduceHitBitmask(offset uint8) uint8

ProduceHitBitmask creates a bitfield of all ones, except for a single zero at the offset position. This bitmask & ship.health will zero the bit at the offset.

func RandomId

func RandomId(input string, length int) string

func SwitchTeam

func SwitchTeam(player *Player, destTeam *Team)

SwitchTeam switches a Players Team

Types

type Coordinate

type Coordinate struct {
	X uint8
	Y uint8
}

Coordinate is the actually row/column square

func (Coordinate) ToTarget

func (coordinate Coordinate) ToTarget() Target

type Game

type Game struct {

	// As long as this is true server will keep running
	Live bool

	// Game server info
	Port          uint16
	Password      string
	StartTime     time.Time
	AdminPassword string

	MaxPlayers uint8
	ShipLimit  uint8
	BoardSize  uint8

	Teams []*Team

	StartDeployPts int
}

Game represents a running with all Game settings and teams

func (*Game) BoardCoordinates

func (game *Game) BoardCoordinates() <-chan Coordinate

BoardCoordinates creates an iterator that iterates over ever Coordinate of the Board

func (*Game) GetMap

func (game *Game) GetMap(team *Team) [][]string

func (*Game) GetPlayerById

func (game *Game) GetPlayerById(id string) *Player

GetPlaerById finds and return a Player using their Player ID

func (*Game) GetRadar

func (game *Game) GetRadar(team *Team, targetTeam *Team) [][]string

func (*Game) GetSmallestTeam

func (game *Game) GetSmallestTeam() *Team

GetSmallestTeam when called on a Game returns the Team in the game with the least userse

func (*Game) Join

func (game *Game) Join(username, password string) (*Player, bool, error)

Adds a new Player to a Game, returns pointer to new Player This should be used to instantiate a new Player

func (*Game) NewTeam

func (game *Game) NewTeam() *Team

NewTeam Instantiates a new Team

func (*Game) UniqueTeamName

func (game *Game) UniqueTeamName(name string) bool

type Orientation

type Orientation uint8

Orientation is integer used to represent the Orientation enum options. Represents the direction a Ship is pointing

const (
	VERTICAL Orientation = iota
	HORIZONTAL
)

Orientation is the direction a Ship is pointing towards

type Player

type Player struct {
	Username string
	Password string

	// Pointer to Team this player is on
	Team *Team

	Id string

	Points int

	// How many hits this player has gotten in a row
	HitStreak int

	PreviousTeam *Team
}

Player represents a single human user connected to game

type Ship

type Ship struct {
	Team *Team

	Size        uint8
	Orientation Orientation
	Location    Coordinate

	Health uint8 // Bit-field representing spots hit on this Ship

}

Ship represents a single ship

func CheckLocation

func CheckLocation(targetTeam *Team, target Coordinate) *Ship

CheckLocation will loop through each Ship on the target Team and see if any Ships occupy the target space

func (Ship) GetOccupyingSpaces

func (ship Ship) GetOccupyingSpaces() []Coordinate

getOccupyingSpaces returns an array of Coordinates that are occupied by this Ship

func (*Ship) GetOffset

func (ship *Ship) GetOffset(coordinate Coordinate) uint8

GetOffset returns the number of squares a coordinate is away from the "location" of this ship

func (*Ship) Hit

func (ship *Ship) Hit(player *Player, coordinate Coordinate) ShotResult

Hit marks a hit on the Ship and returns either HIT if the Ship is still alive or SINK if dead. If the hit occurred in a spot on the Ship take has already taken damage, the ship health will remain the same and we return REPEAT_HIT

func (*Ship) ShipIcon

func (ship *Ship) ShipIcon(coordinate Coordinate) rune

ShipIcon returns, given a Ship and a coordinate, what icon should be displayed at this coordinate?

type ShipCoord

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

type ShotResult

type ShotResult uint8

ShotResult is integer used to represent the ShotResult enum options. Represents the result of fired shot

const (
	SINK ShotResult = iota
	HIT
	MISS
	REPEAT_HIT
)

ShotResult is the result of a shot, a Hit if it hits a Ship, a miss if it doesn't and a Sink if it was a killing Hit

func FireShot

func FireShot(player *Player, targetTeam *Team, target Target) ShotResult

FireShot handles a Player firing a shot on another Team. Returns either MISS, HIT or SINK

type Target

type Target struct {
	X string
	Y uint8
}

Target is the human-way of representing a square most similar to the board game (A1 -> Z26)

func StringToTarget

func StringToTarget(targetString string) (Target, error)

func (Target) ToCoordinate

func (target Target) ToCoordinate() Coordinate

ToCoordinate converts a Target (base64/number pair ex: B12) to a Coordinate (X,Y pair)

type Team

type Team struct {
	Name string

	Game *Game

	// Array of pointers to Player
	Players []*Player

	// Number of Players currently on Team
	NumPlayers int

	// A log of shots fired by this Team and shots upon this Team
	Hits      map[*Team][]Coordinate
	Misses    map[*Team][]Coordinate
	ShotsUpon []Coordinate

	// This Team's Ships
	Ships []*Ship

	DeploymentPoints int
}

Team is a collection of Players working together on the same team

func (*Team) NewShip

func (team *Team) NewShip(size uint8, orientation Orientation, coordinate Coordinate) (*Ship, error)

NewShip generates a new Ship and adds it to a Team, then returns a pointer to the new Ship

func (*Team) ShipCoordinates

func (team *Team) ShipCoordinates() chan ShipCoord

ShipCoordinates creates an iterator that iterates through the Coordinates of all the ship on a Team. It uses the channel iterator pattern, meaning it creates a Goroutine that loops through all the values and pushes them into a channel to be consumed in the main thread

func (*Team) TopPlayer

func (team *Team) TopPlayer() *Player

TopPlayer finds a returns the player on a team with the most points (the leader)

Jump to

Keyboard shortcuts

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