Example #1
0
// 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
}
Example #2
0
// 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)
	}
}
Example #3
0
// 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(),
	)
}
Example #4
0
// 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(),
	)
}
Example #5
0
// 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
}