board

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2023 License: MIT Imports: 12 Imported by: 1

README

Warships-LightGUI

Warships-LightGUI provides an easy to use graphical user interface for the Warships Online game.

This package is suggested for beginners. If you want a more sophisticated GUI, please check out the Warships-GUI package: http://github.com/grupawp/warships-gui

Installation

go get github.com/grupawp/warships-lightgui/v2

Quick Start

To initialize the board use the New() method. NewConfig() will create a default configuration.

board := gui.New(gui.NewConfig())
board.Display()

To customize colours and characters used to indicate ships, misses, etc, create and pass custom Config.

cfg := NewConfig()
cfg.HitChar = '#'
cfg.HitColor = color.FgRed
cfg.BorderColor = color.BgRed
cfg.RulerTextColor = color.BgYellow
New(cfg)
board.Display()

Documentation

https://pkg.go.dev/github.com/grupawp/warships-lightgui/v2

Documentation

Overview

Warships-LightGUI provides an easy to use graphical user interface for the `Warships Online` game.

Index

Examples

Constants

View Source
const (
	Left  pos = iota // indicates player's board (on the left side)
	Right            // indicates enemy's board (on the right side)
)
View Source
const (
	Hit state
	Miss
	Ship
)
View Source
const (
	EOT = 0x3
	CR  = 0xD
)

Variables

View Source
var ErrInvalidCoord = errors.New("invalid coordinate")

Functions

func ReadLineWithTimer added in v2.1.0

func ReadLineWithTimer(prompt string, timer time.Duration) (out string, ok bool)

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

func New(c *Config) *Board

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

func (b *Board) CreateBorder(p pos, coord string)

CreateBorder creates a border around a (sunken) ship, to indicate which coordinates cannot contain a ship segment and can be safely ignored.

func (*Board) Display

func (b *Board) Display()

Display clears the terminal and prints the board.

func (*Board) Export

func (b *Board) Export(p pos) []string

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

func (b *Board) HitOrMiss(p pos, coord string) (state, error)

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

func (b *Board) Import(coords []string) error

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

func (b *Board) Set(p pos, coord string, s state) error

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.

func NewConfig

func NewConfig() *Config

NewConfig returns a new config with default values.

Jump to

Keyboard shortcuts

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