Example #1
0
        private void MoveInternal(int from, int to)
        {
            var     color   = CurrentColor.Value;
            Checker checker = null;

            // bear off
            if (to == OFFBOARD)
            {
                var rollNumber = color == CheckerColor.White ? 24 - from : from + 1;
                // roll number can be higher for bear off, use highest roll number
                if (!remainingRollNumbers.Contains(rollNumber))
                {
                    if (remainingRollNumbers.Count == 1 ||
                        remainingRollNumbers[0] >= remainingRollNumbers[1])
                    {
                        rollNumber = remainingRollNumbers[0];
                    }
                    else
                    {
                        rollNumber = remainingRollNumbers[1];
                    }
                }
                remainingRollNumbers.Remove(rollNumber);
                checker = board[from].Checkers.Pop();
                offBoard.Add(checker);
                checker.Position = OFFBOARD;
                var countOnBoard = GetCheckers().Count((c) => c.Position >= 0 && c.Position <= 23);
                if (countOnBoard == 0)
                {
                    SetWinner(color);
                }
            }
            // remove from bar or move in board
            else
            {
                // remove from bar
                if (from == BAR)
                {
                    remainingRollNumbers.Remove(color == CheckerColor.White ? to + 1 : 24 - to);
                    checker = bar.Find((c) => c.Color == color);
                    bar.Remove(checker);
                }
                // move in board
                else
                {
                    remainingRollNumbers.Remove(Math.Abs(from - to));
                    checker = board[from].Checkers.Pop();
                }
                var toPoint = board[to];
                if (toPoint.Checkers.Any() && toPoint.Checkers.Peek().Color != color)
                {
                    // blot is hit
                    var hitChecker = toPoint.Checkers.Pop();
                    hitChecker.Position = BAR;
                    bar.Add(hitChecker);
                }
                toPoint.Checkers.Push(checker);
                checker.Position = to;
            }
            if (!GameOver && remainingRollNumbers.Count == 0)
            {
                CurrentColor = color == CheckerColor.White ? CheckerColor.Black : CheckerColor.White;
                LastRoll     = CurrentRoll;
                CurrentRoll  = null;
            }
        }
Example #2
0
 public Checker(Checker c)
 {
     Id       = c.Id;
     Color    = c.Color;
     Position = c.Position;
 }