Documentation
¶
Overview ¶
Warships-LightGUI provides an easy to use graphical user interface for the `Warships Online` game.
Index ¶
Examples ¶
Constants ¶
const ( Left pos = iota // indicates player's board (on the left side) Right // indicates enemy's board (on the right side) )
const ( Hit state Miss Ship )
const ( EOT = 0x3 CR = 0xD )
Variables ¶
var ErrInvalidCoord = errors.New("invalid coordinate")
Functions ¶
func ReadLineWithTimer ¶
ReadLineWithTimer reads a line from stdin and displays a timer. The function blocks until [ENTER] is pressed and returns typed in string and true. If [CTRL+C] is pressed or the timer expires, the function blocks until any key is pressed and returns an empty string and false.
Types ¶
type Board ¶
type Board struct {
// contains filtered or unexported fields
}
Board represents a game board, including both player's (Left) and enemy's (Right) sides.
func New ¶
New returns a new Board.
Example (Advanced) ¶
cfg := NewConfig() cfg.HitChar = '#' cfg.HitColor = color.FgRed cfg.BorderColor = color.BgRed cfg.RulerTextColor = color.BgYellow New(cfg)
Output:
Example (Simple) ¶
cfg := NewConfig() New(cfg)
Output:
func (*Board) CreateBorder ¶
CreateBorder creates a border around a (sunken) ship, to indicate which coordinates cannot contain a ship segment and can be safely ignored.
func (*Board) Export ¶
Export exports ships from either Left (player's) or Right (enemy's) board. The return value is a slice of ship coordinates (using format expected by the game server).
Example ¶
board := New(NewConfig()) coords := []string{"A1", "A2", "A3"} _ = board.Import(coords) exported := board.Export(Left) fmt.Println(exported)
Output: [A3 A2 A1]
func (*Board) HitOrMiss ¶
HitOrMiss updates and returns the state of a coordinate on the board, depending on the previous state:
- Empty -> Miss
- Ship, Hit -> Hit
If the coordinate is invalid, the function returnes ErrInvalidCoord.
Parameters:
- p (pos): Left or Right board
- coord (string): a string representing the coordinate (e.g. "A1", "B2")
Returns:
- s (state): updated state value (Empty, Miss, or Hit)
Example ¶
board := New(NewConfig()) _ = board.Set(Left, "A1", Ship) _, err := board.HitOrMiss(Left, "A1") if err != nil { fmt.Println(err) }
Output:
func (*Board) Import ¶
Import imports player's ships from a slice of coordinates (as returned by the game server) and places them on the Left board.
If any of the coordinates is invalid, the function returnes ErrInvalidCoord.
Example ¶
board := New(NewConfig()) coords := []string{"A1", "A2", "A3"} err := board.Import(coords) if err != nil { fmt.Println(err) }
Output:
func (*Board) Set ¶
Set updates the state of a coordinate on the board.
For the Left board, the function validates the state of the coordinate based on the following logic:
- If the state is Miss and the previous state is not Empty, it does not update the state.
- If the state is Hit and the previous state is not Ship, it does not update the state.
- If the state is Ship and the previous state is not Empty, it does not update the state.
For the Right board, the function does not update the state if the previous state is not Empty.
If the coordinate is invalid, the function returnes ErrInvalidCoord.
Parameters:
- p (pos): Left or Right board
- coord (string): a string representing the coordinate (e.g. "A1", "B2")
- s (state): the state to update the coordinate to (Empty, Miss, or Ship)
Example (Enemy) ¶
board := New(NewConfig()) err := board.Set(Right, "C3", Hit) if err != nil { fmt.Println(err) }
Output:
Example (Player) ¶
board := New(NewConfig()) err := board.Set(Left, "A1", Ship) if err != nil { fmt.Println(err) }
Output:
type Config ¶
type Config struct { EmptyChar byte EmptyColor color.Attribute RulerTextColor color.Attribute ShipChar byte ShipColor color.Attribute HitChar byte HitColor color.Attribute MissChar byte MissColor color.Attribute BorderChar byte BorderColor color.Attribute }
Config stores colours and characters used to draw a board. Zero values result in no colours and no characters, so it is recommended to use NewConfig() and modify it instead.