GR.Gambling.Backgammon.Board.LegalPlays C# (CSharp) Method

LegalPlays() public method

This returns all the possible legal moves for the player with given dice. It sorts the dice from highest to lowest, so it produces same results regardless of the dice order. It also finds correctly moves according to following rules: "For any roll, if a player can move both dice, that player is compelled to do so. If it is possible to move either die, but not both, the higher number must be played. Further, if one die is unable to be moved, but such a move is made possible by the moving of the other die, that move is compulsory." Source: http://en.wikipedia.org/wiki/Backgammon
public LegalPlays ( int player, int dice ) : List
player int
dice int
return List
        public List<Play> LegalPlays(int player, int[] dice)
        {
            List<Play> plays = new List<Play>(); // Contains the legal move sequences.
            List<Play> partial_plays = new List<Play>(); // Contains move sequences, which leave one or more unused dice.
            List<Move> moves_made = new List<Move>(); // Contains the moves made so far in the recursion.
            List<int> free_dice = new List<int>(); // Contains unused dice so far in the recursion.
            HashSet<string> board_hash = new HashSet<string>(); // Contains the hashes of already traversed boards.

            free_dice.Add(Math.Max(dice[0], dice[1]));
            free_dice.Add(Math.Min(dice[0], dice[1]));

            if (dice[0] == dice[1])
            {
                free_dice.Add(dice[0]);
                free_dice.Add(dice[1]);
            }

            Recursion(player, free_dice, this, moves_made, ref plays, ref partial_plays, ref board_hash);
            //Recursion(player, free_dice, this, moves_made, ref move_hints);

            if (plays.Count == 0 && partial_plays.Count > 0)
            {
                if (partial_plays.Count == 1)
                {
                    plays.Add(partial_plays[0]);
                }
                else
                {
                    if (dice[0] != dice[1])
                    {
                        foreach (Play partial_move_sequence in partial_plays)
                            if (partial_move_sequence[0].Distance == Math.Max(dice[0], dice[1]))
                                plays.Add(partial_move_sequence);
                    }
                    else
                    {
                        // I believe this should never happen
                        Console.WriteLine("LegalMoves Partialmovehints problem!");
                    }
                }
            }

            return plays;
        }