Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
docs: add getting started
Browse files Browse the repository at this point in the history
  • Loading branch information
maolonglong committed Oct 16, 2021
1 parent b9e8143 commit e93c04b
Showing 1 changed file with 88 additions and 1 deletion.
89 changes: 88 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,101 @@ Package mcts provides parallel monte-carlo tree search for your Go applications.

## Installation

Go latest is recommended
Go latest is recommended.

```bash
go get -u github.com/go-mcts/mcts
```

## Getting started

See [examples](examples) directory.

Implements `mcts.Move` and `mcts.State`:

```go
// examples/nim/nim.go
type Move int

type State struct {
playerToMove int
chips int
}

func (s *State) PlayerToMove() int {
return s.playerToMove
}

func (s *State) HasMoves() bool {
s.checkInvariant()
return s.chips > 0
}

func (s *State) GetMoves() []mcts.Move {
s.checkInvariant()

var moves []mcts.Move
for i := 1; i <= min(3, s.chips); i++ {
moves = append(moves, i)
}
return moves
}

func (s *State) DoMove(move mcts.Move) {
m := move.(int)
if m < 1 || m > 3 {
panic("illegal move")
}
s.checkInvariant()

s.chips -= m
s.playerToMove = 3 - s.playerToMove

s.checkInvariant()
}

func (s *State) DoRandomMove(rd *rand.Rand) {
if s.chips <= 0 {
panic("invalid chips")
}
s.checkInvariant()

max := min(3, s.chips)
s.DoMove(rd.Intn(max) + 1)

s.checkInvariant()
}

func (s *State) GetResult(currentPlayerToMove int) float64 {
if s.chips != 0 {
panic("game not over")
}
s.checkInvariant()

if s.playerToMove == currentPlayerToMove {
return 1.0
}
return 0.0
}

func (s *State) Clone() mcts.State {
return &State{
playerToMove: s.playerToMove,
chips: s.chips,
}
}
```

Run `mcts.ComputeMove`:

```go
state := &State{
playerToMove: 1,
chips: chips,
}
move := mcts.ComputeMove(state, mcts.MaxIterations(100000), mcts.Verbose(true))
```

## License

This project is under the MIT License. See the [LICENSE](LICENSE) file for the full license text.

0 comments on commit e93c04b

Please sign in to comment.