// 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 }
// TestResultLastMove tests that manacala.Result::LastMove returns the last // move. func TestResultLastMove(t *testing.T) { t.Parallel() expected := mancala.HouseIndex(1) actual := mancala.NewResult( mancala.NewBoard(), -1, 0, expected, nil, ).LastMove() if actual != expected { t.Errorf("result.LastMove() = %d, want %d", actual, expected) } }
// 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(), ) }
// ReachableStates returns all the States reachable from the current State. func (state *State) ReachableStates() []*State { var states []*State playerIndex := state.Information().PlayerIndex().Other() for i := 0; i < mancala.RowWidth; i++ { houseIndex := mancala.HouseIndex(i) if board, err := state.referee.SimulateMove( playerIndex, houseIndex, ); err == nil { information := NewInformation( board, playerIndex, houseIndex, ) states = append(states, NewState(state, information)) } } return states }