Exemple #1
0
 private BackgammonBoard(BackgammonBoard source)
 {
     WhitePlayer          = source.WhitePlayer;
     BlackPlayer          = source.BlackPlayer;
     CurrentColor         = source.CurrentColor;
     Winner               = source.Winner;
     GameOver             = source.GameOver;
     GameStarted          = source.GameStarted;
     Gammon               = source.Gammon;
     Backgammon           = source.Backgammon;
     CurrentRoll          = source.CurrentRoll;
     LastRoll             = source.LastRoll;
     remainingRollNumbers = new List <int>(source.remainingRollNumbers);
     board = new Point[24];
     for (int pos = 0; pos <= 23; pos++)
     {
         board[pos] = new Point(source.board[pos]);
     }
     bar = new List <Checker>();
     foreach (var c in source.bar)
     {
         bar.Add(new Checker(c));
     }
     offBoard = new List <Checker>();
     foreach (var c in source.offBoard)
     {
         offBoard.Add(new Checker(c));
     }
     startRollNumbers = new Dictionary <CheckerColor, int>(source.startRollNumbers);
 }
Exemple #2
0
        public List <(int, int)> GetAllMoves()
        {
            // A player must use both numbers of a roll if this is legally possible
            // (or all four numbers of a double).
            // When only one number can be played, the player must play that number.
            // Or if either number can be played but not both,
            // the player must play the larger one.
            // When neither number can be used, the player loses his turn.
            // In the case of doubles, when all four numbers cannot be played,
            // the player must play as many numbers as he can.
            var allMoves = new List <(int, int)>();

            if (GameStarted && !GameOver && remainingRollNumbers.Any())
            {
                if (HasCheckersOnBar())
                {
                    if (remainingRollNumbers.Count == 1 ||
                        remainingRollNumbers[0] == remainingRollNumbers[1])
                    {
                        allMoves.AddRange(GetAllMoves(remainingRollNumbers[0]));
                    }
                    else
                    {
                        allMoves.AddRange(GetAllMoves(remainingRollNumbers[0]));
                        allMoves.AddRange(GetAllMoves(remainingRollNumbers[1]));
                    }
                }
                else if (remainingRollNumbers.Count == 1 ||
                         remainingRollNumbers[0] == remainingRollNumbers[1])
                {
                    allMoves.AddRange(GetAllMoves(remainingRollNumbers[0]));
                }
                else if (IsInHomeboard())
                {
                    allMoves.AddRange(GetAllMoves(remainingRollNumbers[0]));
                    allMoves.AddRange(GetAllMoves(remainingRollNumbers[1]));
                }
                else
                {
                    bool onlyMaxAllowed = false;
                    var  max            = Math.Max(remainingRollNumbers[0], remainingRollNumbers[1]);
                    var  min            = Math.Min(remainingRollNumbers[0], remainingRollNumbers[1]);
                    var  maxMoves       = GetAllMoves(max);
                    var  minMoves       = GetAllMoves(min);
                    if (maxMoves.Any() && minMoves.Any())
                    {
                        onlyMaxAllowed = true;
                        foreach (var move in minMoves)
                        {
                            var temp = new BackgammonBoard(this);
                            temp.MoveInternal(move.Item1, move.Item2);
                            var tempMaxMoves = temp.GetAllMoves(max);
                            if (tempMaxMoves.Any())
                            {
                                onlyMaxAllowed = false;
                                break;
                            }
                        }
                    }
                    allMoves.AddRange(maxMoves);
                    if (!onlyMaxAllowed)
                    {
                        allMoves.AddRange(minMoves);
                    }
                }
            }
            return(allMoves);
        }