// Play asks for input from an external source until a valid move is given. func (human *HumanAgent) Play( referee *mancala.Referee, player mancala.PlayerIndex, ) (mancala.HouseIndex, error) { move := mancala.HouseIndex(-1) var moveLine string for move == -1 { fmt.Printf("%s, make a move:\n", strings.Title(player.String())) fmt.Scanf("%s", &moveLine) fmt.Println() parsedMove, err := strconv.Atoi(moveLine) if err != nil { fmt.Println("Input must be an integer.") fmt.Println() moveLine = "" } else if _, err := referee.SimulateMove( player, mancala.HouseIndex(parsedMove), ); err != nil { fmt.Printf("Illegal move: %v.\n", err) fmt.Println() moveLine = "" move = -1 } else { move = mancala.HouseIndex(parsedMove) } } return move, nil }
// NewRootState creates the root State of a mancala game search tree with a // mancala.Referee and mancala.PlayerIndex. func NewRootState( referee *mancala.Referee, index mancala.PlayerIndex, ) *State { return &State{ parent: nil, referee: referee, information: NewInformation(referee.Board(), index.Other(), -1), } }
// Play makes a move for an ExamplePlayer. // // If no legal moves can be found, an invalid mancala.HouseIndex is returned // along with an error. func (player ExamplePlayer) Play( referee *mancala.Referee, index mancala.PlayerIndex, ) (mancala.HouseIndex, error) { for i := 0; i < mancala.RowWidth; i++ { house := mancala.HouseIndex(i) if _, err := referee.SimulateMove(index, house); err == nil { return house, nil } } return -1, fmt.Errorf( "ExamplePlayer couldn't find a legal HouseIndex as %v on "+ "board \n%v", index, referee.Board(), ) }
// Play makes a move for a fastPlayer. // // If there are no legal moves, an invalid mancala.HouseIndex is returned along // with an error stating that the fastPlayer couldn't find a valid // mancala.houseIndex. func (fast fastPlayer) Play( referee *mancala.Referee, player mancala.PlayerIndex, ) (mancala.HouseIndex, error) { for i := 0; i < mancala.RowWidth; i++ { house := mancala.HouseIndex(i) if _, err := referee.SimulateMove(player, house); err == nil { return house, nil } } return -1, fmt.Errorf( "fastPlayer couldn't find a legal HouseIndex as %v on board\n"+ "%v", player, referee.Board(), ) }