board

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: MIT Imports: 6 Imported by: 0

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

Quick Start

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

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

To customize colours and characters used to indicate ships, misses, etc, use methods available on the config instance.

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

Documentation

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

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 (
	Empty state = iota
	Hit
	Miss
	Ship
	Border // border around a sunken ship
	Ruler
)

Variables

This section is empty.

Functions

func ConfigParams deprecated

func ConfigParams() customizer

ConfigParams returns a new config with default values.

Deprecated: use NewConfig instead.

func NewConfig

func NewConfig() *config

NewConfig returns a new config with default values.

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)
New(
	NewConfig().
		HitChar('#').
		HitColor(color.FgRed).
		BorderColor(color.BgRed).
		RulerTextColor(color.BgYellow).
		NewConfig())
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

HitOrMiss updates and returns the state of a coordinate on the board, depending on the previous state:

  • Empty -> Miss
  • Ship, Hit -> Hit

It returns Miss if the coordinate is invalid.

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)
board.HitOrMiss(Left, "A1")
Output:

func (*Board) Import

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

Import imports player's ships from a slice of coordinates (as returned by the game server) and places them on the Left board.

Example
board := New(NewConfig())
coords := []string{"A1", "A2", "A3"}
board.Import(coords)
Output:

func (*Board) Set

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

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 does not update the state.

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())
board.Set(Right, "C3", Hit)
Output:

Example (Player)
board := New(NewConfig())
board.Set(Left, "A1", Ship)
Output:

Jump to

Keyboard shortcuts

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