tic-tac-go

command module
v0.0.0-...-622d342 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2018 License: MIT Imports: 5 Imported by: 0

README

Tic-Tac-Go

A tic-tac-toe game written in Go.

Build Status Maintainability Go Report Card

Motivation

Tic-tac-toe has always been one of my favourite games, but one I rarely won at. I thought I would capture the tactical and strategic complexity of this game in a little Go app. The AI uses the MiniMax algorithm to choose its moves so, unfortunately, I am yet to beat this version too.

Please let me know if you win.

Usage

Installation

To install the app follow these steps from a directory including your $GOPATH (GOPATH explanation here):

$ git clone https://github.com/camjw/tic-tac-go.git
$ cd tic-tac-go
$ go install .

This will create an executable tic-tac-go which you can then run to start a game of Tic-Tac-Toe!

Testing

To run the tests for this repo run the following command:

$ go test ./lib --cover
$ #=> ok  	github.com/camjw/tic-tac-go/lib	0.015s	coverage: 100.0% of statements

The MiniMax algorithm

The ComputerPlayer struct uses the MiniMax algorithm to choose its next move. Roughly speaking, the AI checks all the possible moves that could me made at each turn and chooses the one which maximises its chance of winning and assumes the opponent which minimises its chance of winning.

This is done in the following function:

func (c *ComputerPlayer) miniMax(board BoardToPlay) float64 {
	if board.GameOver() {
		return c.score(board)
	}

	scores := []float64{}
	moves := []int{}

	for _, move := range board.GetValidMoves() {
		possibleBoard := board.Clone()
		possibleBoard.PlayMove(move, possibleBoard.WhoseTurn())
		scores = append(scores, 0.9*c.miniMax(&possibleBoard))
		moves = append(moves, move)
	}

	return c.scorePosition(board, moves, scores)
}

Contributing

If you want to contribute to this repo please open a PR!

License

This repo is licensed under the MIT license

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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