tictactoe

package
v0.0.0-...-db24ddd Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

Tic-Tac-Toe Example

This is an example implementation of Tic-Tac-Toe using the go-boardgame package. This is by no means the only or necessarily best way to implement a game but is a good place to look when getting started.

Layout

builder.go

The builder object that implements bg.BoardGameBuilder lives here. This allows one to create a new game given a set of options and contains other information such as the game key.

tictactoe.go

The game object that implements bg.BoardGame lives here. This allows a user to perform an action on the game state as well as view the current state of the game.

state.go

The state object which handles all the core game logic lives here.

models.go

The models such as action types, action details, and snapshot details live here.

bgn.go

All Board Game Notation logic lives here. This is not necessary to play a game and may be ignored if desired. However, if implemented this will allow users to make use of bgn for easy game storage and retrieval.

Usage

Create Game
builder := &Builder{}
game, err := builder.Create(&bg.BoardGameOptions{
    Teams: []string{"TeamA", "TeamB"},
})
Play Game
err := game.Do(&bg.BoardGameAction{
    Team: "TeamA",
    ActionType: "MarkLocation",
    MoreDetails: MarkLocationActionDetails{
        Row: 0,
        Column: 0,
    },
})
View Game
snapshot, err := game.GetSnapshot("TeamA")

Documentation

Index

Constants

View Source
const (
	// ActionMarkLocation allows players to mark and X or O on the board
	ActionMarkLocation = "MarkLocation"
)

Action types

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct{}

Builder implements the bg.BoardGameBuilder interface

func (*Builder) Create

func (b *Builder) Create(options *bg.BoardGameOptions) (bg.BoardGame, error)

Create creates a new game instance

func (*Builder) CreateWithBGN

func (b *Builder) CreateWithBGN(options *bg.BoardGameOptions) (bg.BoardGameWithBGN, error)

CreateWithBGN creates a game with BGN functionality

func (*Builder) Key

func (b *Builder) Key() string

Key returns the key unique to the game i.e. the name of the game

func (*Builder) Load

func (b *Builder) Load(game *bgn.Game) (bg.BoardGameWithBGN, error)

Load loads a game given a bgn.Game

type MarkLocationActionDetails

type MarkLocationActionDetails struct {
	Row, Column int
}

MarkLocationActionDetails is the action details for marking an X or O on the board

type State

type State struct {
	Turn    string
	Teams   []string
	Winners []string
	Board   [size][size]string
}

state handles all the internal game logic for the game

func (*State) MarkLocation

func (s *State) MarkLocation(team string, row, column int) error

type TicTacToe

type TicTacToe struct {
	State *State
	// contains filtered or unexported fields
}

TicTacToe implements the bg.BoardGame interface

func NewTicTacToe

func NewTicTacToe(options *bg.BoardGameOptions) (*TicTacToe, error)

NewTicTacToe creates a new game instance

func (*TicTacToe) Do

func (t *TicTacToe) Do(action *bg.BoardGameAction) error

Do performs an action on the game

func (*TicTacToe) GetBGN

func (t *TicTacToe) GetBGN() *bgn.Game

GetBGN converts a TicTacToe game into a bgn.Game

func (*TicTacToe) GetSnapshot

func (t *TicTacToe) GetSnapshot(team ...string) (*bg.BoardGameSnapshot, error)

GetSnapshot returns the game from team's view TicTacToe does not require hiding information from different players so everything can be returned

type TicTacToeSnapshotData

type TicTacToeSnapshotData struct {
	Board [size][size]string
}

TicTacToeSnapshotData is the game data unique to TicTacToe

Jump to

Keyboard shortcuts

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