checkers

package
v0.0.0-...-3fc78ee Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BoardSideSize = 8 // TODO: actually make it so can play different size boards?
	BoardSize     = BoardSideSize * BoardSideSize
)

Variables

View Source
var (
	RedPieces  = [...]BoardSlot{RedPiece, RedKing}
	BluePieces = [...]BoardSlot{BluePiece, BlueKing}
)
View Source
var (
	// blue goes down
	BlueDirCalcs = [...]int{downLeftCalc, downRightCalc}
	// red goes up
	RedDirCalcs = [...]int{upLeftCalc, upRightCalc}

	// TODO: maybe also flying kings for non American checkers?
	KingDirCalcs = [...]int{downLeftCalc, downRightCalc, upLeftCalc, upRightCalc}
)

Functions

func GetDirectionsToUse

func GetDirectionsToUse(plr Player, slot BoardSlot) (directionsToUse []int, match bool)

returns `directionsToUse` and whether the slot matches plr - if not wrong turn and wrong slot

Types

type Board

type Board [BoardSize]BoardSlot

Board is a 2-d arrays, that's represented in a 1-d array. Given that 0 is the top left corner and going to higher index means right/down, these are true:

i = (cols_num * row) + col col = mod(i, cols_num) row = floor(i / cols_num)

TODO: this only needs to hold 32 slots (given that we don't care about Nas) TODO: should really be using bit-boards

func NewBoard

func NewBoard() Board

NewBoard() returns an initialized board set-up for a checkers game

type BoardSlot

type BoardSlot uint8
const (
	NaS   BoardSlot = iota // Not A Spot (a light square)
	Empty                  // an unoccupied dark square
	BluePiece
	BlueKing
	RedPiece
	RedKing
)

func GetSlotAfterCalc

func GetSlotAfterCalc(b Board, i, dirCalc int) (BoardSlot, int)

returns a slot and its I (after calc)

func (BoardSlot) String

func (s BoardSlot) String(coord int) (str string)

TODO: make clean coord - needs to know location to display the number, -1 means should not display the coord

type Game

type Game struct {
	State      GameState
	PlrTurn    Player // who's the current player's turn
	Board      Board
	TurnNumber int // number of half turns that finished

	// this is to check draw condition
	TimeSinceExcitingMove int // time since capture/non-king move
}

func NewGame

func NewGame() *Game

func (*Game) GetLegalMoves

func (g *Game) GetLegalMoves() []Move

GetLegalMoves returns a slice of all legal moves in position

func (*Game) PlayMove

func (g *Game) PlayMove(m Move)

func (Game) String

func (g Game) String() string

String() returns a pretty-print of the board

type GameState

type GameState string
const (
	Playing GameState = "Playing"
	BlueWon GameState = "Blue Won"
	RedWon  GameState = "Red Won"
	Draw    GameState = "Draw"
)

type Move

type Move struct {
	StartI int
	EndI   int

	CapturedPiecesI []int
}

func GetCapturesForSlotI

func GetCapturesForSlotI(b Board, i int, directionCalcs []int, enemyPieces []BoardSlot) []Move

get legal capturing moves

func GetMovingsForSlotI

func GetMovingsForSlotI(b Board, i int, dirCalcs []int) []Move

get legal *moving* moves

type Player

type Player uint8
const (
	BluePlayer Player = iota
	RedPlayer
)

Jump to

Keyboard shortcuts

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