Help us shape the future of Go! Take the 2026 Go Developer Survey

checkersbot

package module
v0.0.0-...-28317fe Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2014 License: Apache-2.0 Imports: 13 Imported by: 3

README

Build Status

A checkers bot framework which makes it easy to build a checkers bot which can connect to a Checkers Overlord. This was built as a demonstration app for the Couchbase Lite NoSQL Mobile database.

Here is a screenshot of what the Checkers-iOS app looks like.

screenshot

Big Picture

architecture png

Bot implementations

Sample code for implementing a bot

type RandomThinker struct {
        ourTeamId int
}

func (r RandomThinker) Think(gameState cbot.GameState) (bestMove cbot.ValidMove, ok bool) {

        ok = true
        ourTeam := gameState.Teams[r.ourTeamId]
        allValidMoves := ourTeam.AllValidMoves()
        if len(allValidMoves) > 0 {
                randomValidMoveIndex := cbot.RandomIntInRange(0, len(allValidMoves))
                bestMove = allValidMoves[randomValidMoveIndex]
        } else {
                ok = false
        }

        return
}

func (r RandomThinker) GameFinished(gameState cbot.GameState) (shouldQuit bool) {
        shouldQuit = false
        return
}

func main() {
	thinker := &RandomThinker{}
	thinker.ourTeamId = cbot.RED_TEAM
	game := cbot.NewGame(thinker.ourTeamId, thinker)
	game.SetServerUrl("http://localhost:4984/checkers")
	game.GameLoop()
}


Build your own Checkers Bot

Make a copy of the Random Bot and use that as a starting point for building your own checkers bot.

Documentation

Index

Constants

View Source
const (
	DEFAULT_SERVER_URL = "http://localhost:4984/checkers"
	GAME_DOC_ID        = "game:checkers"
	VOTES_DOC_ID       = "votes:checkers"
)
View Source
const (
	RED_TEAM = TeamType(iota)
	BLUE_TEAM
)
View Source
const (
	NORMAL = FeedType(iota)
	LONGPOLL
)

Variables

This section is empty.

Functions

func CorrespondingValidMoveIndex

func CorrespondingValidMoveIndex(move core.Move, allValidMoves []ValidMove) (found bool, index int)

func EqualsCoreMove

func EqualsCoreMove(validMove ValidMove, move core.Move) bool

func ExportCoreLocation

func ExportCoreLocation(location core.Location) int

func GetCoreLocation

func GetCoreLocation(location int) core.Location

func GetCorePlayer

func GetCorePlayer(teamId TeamType) core.Player

func RandomIntInRange

func RandomIntInRange(min, max int) int

Types

type Capture

type Capture struct {
	TeamID  int `json:"team"`
	PieceId int `json:"piece"`
}

type Changes

type Changes map[string]interface{}

type CheckersBotFlags

type CheckersBotFlags struct {
	Team                  TeamType
	SyncGatewayUrl        string
	FeedType              FeedType
	RandomDelayBeforeMove int
}

func ParseCmdLine

func ParseCmdLine() (checkersBotFlags CheckersBotFlags)

type CheckersBotRawFlags

type CheckersBotRawFlags struct {
	TeamString            string
	SyncGatewayUrl        string
	FeedString            string
	RandomDelayBeforeMove int
}

func GetCheckersBotRawFlags

func GetCheckersBotRawFlags() *CheckersBotRawFlags

func (*CheckersBotRawFlags) GetCheckersBotFlags

func (rawFlags *CheckersBotRawFlags) GetCheckersBotFlags() CheckersBotFlags

type FeedType

type FeedType int

type Game

type Game struct {
	// contains filtered or unexported fields
}

func NewGame

func NewGame(ourTeamId TeamType, thinker Thinker) *Game

func (*Game) CreateRemoteUser

func (game *Game) CreateRemoteUser()

func (*Game) GameLoop

func (game *Game) GameLoop()

Follow the changes feed and on each change callback call game.handleChanges() which will drive the game

func (*Game) InitDbConnection

func (game *Game) InitDbConnection()

func (*Game) InitGame

func (game *Game) InitGame()

func (*Game) OutgoingVoteFromMove

func (game *Game) OutgoingVoteFromMove(validMove ValidMove) (votes *OutgoingVotes)

Given a validmove (as chosen by the Thinker), create an "Outgoing Vote" that can be passed to the server. NOTE: the struct OutgoingVotes needs to be renamed from plural to singular

func (*Game) PostChosenMove

func (game *Game) PostChosenMove(votes *OutgoingVotes)

func (*Game) ServerUrl

func (game *Game) ServerUrl() string

func (*Game) SetDelayBeforeMove

func (game *Game) SetDelayBeforeMove(delayBeforeMove int)

func (*Game) SetFeedType

func (game *Game) SetFeedType(feedType FeedType)

func (*Game) SetServerUrl

func (game *Game) SetServerUrl(serverUrl string)

func (*Game) Turn

func (game *Game) Turn() int

func (*Game) WaitForNextGame

func (game *Game) WaitForNextGame()

Wait until the game number increments

type GameState

type GameState struct {
	Id           string        `json:"_id"`
	Rev          string        `json:"_rev"`
	Teams        []Team        `json:"teams"`
	ActiveTeam   TeamType      `json:"activeTeam"`
	WinningTeam  TeamType      `json:"winningTeam"`
	Number       int           `json:"number"`
	Turn         int           `json:"turn"`
	MoveInterval int           `json:"moveInterval"`
	Moves        []MoveHistory `json:"moves"`
}

data structure that corresponds to the checkers:game json doc

func NewGameStateFromString

func NewGameStateFromString(jsonString string) GameState

func (GameState) Export

func (gamestate GameState) Export() core.Board

type MoveHistory

type MoveHistory struct {
	Piece     int      `json:"piece"`
	Team      TeamType `json:"team"`
	Locations []int    `json:"locations"`
}

type Observer

type Observer interface {
	GameFinished(gameState GameState) (shouldQuit bool)
}

type OutgoingVotes

type OutgoingVotes struct {
	Id        string                 `json:"_id"`
	Rev       string                 `json:"_rev"`
	Revisions map[string]interface{} `json:"_revisions"`
	Channels  []interface{}          `json:"channels"` // ??
	Moves     []VoteMove             `json:"moves"`    // ??
	TeamId    TeamType               `json:"team"`
	GameId    int                    `json:"game"`
	Count     int                    `json:"count"`
	Turn      int                    `json:"turn"`
	PieceId   int                    `json:"piece"`
	Locations []int                  `json:"locations"`
}

type Piece

type Piece struct {

	// the locations are numbered from 1 to 32 where 1
	// represents the top-left dark square for the red team,
	// and 32 represents the bottom-right dark square for blue team.
	Location   int         `json:"location"`
	King       bool        `json:"king"`
	Captured   bool        `json:"captured"`
	ValidMoves []ValidMove `json:"validMoves"`
	PieceId    int
}

type Team

type Team struct {
	Score            int     `json:"score"`
	ParticipantCount int     `json:"participantCount"`
	Pieces           []Piece `json:"pieces"`
}

func (Team) AllValidMoves

func (t Team) AllValidMoves() (validMoves []ValidMove)

type TeamType

type TeamType int

func (TeamType) Opponent

func (t TeamType) Opponent() TeamType

func (TeamType) String

func (t TeamType) String() string

type Thinker

type Thinker interface {
	Think(gameState GameState) (validMove ValidMove, ok bool)
}

type User

type User struct {
	Id         string   `json:"_id"`
	Rev        string   `json:"_rev"`
	TeamId     TeamType `json:"team"`
	GameNumber int      `json:"game"`
}

type ValidMove

type ValidMove struct {

	// in the case of a normal move or single jump, this will contain a single
	// location which contains the location value of the move destination.
	// (the move starting point is contained in the outer Piece struct)
	// in the case of a double+ jump however, this will contain an array of
	// the locations - the first location will be the first jump landing spot,
	// the second location will be the second jump landing spot, etc..
	// so for example, in this position: http://cl.ly/image/3k470u1P0G3M
	// the piece location will be 24, and the locations will be: "locations":[15,6]
	// which means 24->15,15->6
	Locations     []int     `json:"locations"`
	Captures      []Capture `json:"captures"`
	King          bool      `json:"king"`
	PieceId       int
	StartLocation int
}

func (ValidMove) EndLocation

func (validMove ValidMove) EndLocation() int

func (ValidMove) String

func (validMove ValidMove) String() string

type VoteMove

type VoteMove struct {
	GameId    int      `json:"game"`
	Count     int      `json:"count"`
	Locations []int    `json:"locations"`
	PieceId   int      `json:"piece"`
	TeamId    TeamType `json:"team"`
	Turn      int      `json:"turn"`
}

Jump to

Keyboard shortcuts

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