Beispiel #1
0
        public bool isValid(Pair to, Pair from, PegBoard pegBoard, Player.Color currentPlayerColor)
        {
            int moveUpDown    = from.row - to.row;
            int moveLeftRight = from.col - to.col;

            if (pegBoard.checkColor(from) != currentPlayerColor || // correct color
                pegBoard.isEmpty(from) ||  // selected empty peg slot
                !pegBoard.isEmpty(to))     // dropped on occupied slot
            {
                // conditions for immediate failure
                return(false);
            }

            else if (Math.Abs(moveUpDown) == 1 ||
                     Math.Abs(moveLeftRight) == 1)
            {
                // jump OR move 1
                return(true);
            }

            else if (Math.Abs(moveUpDown) == 2
                     ^ Math.Abs(moveLeftRight) == 2) // xor jump (2)
            {
                // Jumps A color and Jumps OTHER color
                return(!pegBoard.isEmpty(from.row - moveUpDown / 2, from.col - moveLeftRight / 2) &&
                       pegBoard.checkColor(from.row - moveUpDown / 2, from.col - moveLeftRight / 2) != currentPlayerColor);
            }

            else
            {
                // Don't jump the rainbow!
                return(false);
            }
        }
Beispiel #2
0
        private bool isValid(Pair to, Pair from, PegBoard pegBoard, Player.Color currentPlayerColor)
        {
            int moveUpDown    = from.row - to.row;
            int moveLeftRight = from.col - to.col;

            if (to.row < 0 || to.col < 0 || from.row < 0 || from.col < 0 ||
                to.row > boardSize || to.col > boardSize || from.row > boardSize || from.col > boardSize)
            {
                return(false);
            }

            else if (pegBoard.checkColor(from) != currentPlayerColor || // correct color
                     pegBoard.isEmpty(from) || // selected empty peg slot
                     !pegBoard.isEmpty(to))    // dropped on occupied slot
            {
                // Conditions for immediate failure
                return(false);
            }

            else if (Math.Abs(moveUpDown) == 1 &&
                     Math.Abs(moveLeftRight) == 1)
            {
                // Jump
                return(true);
            }

            else if (Math.Abs(moveUpDown) + Math.Abs(moveLeftRight) == 1)
            {
                // Move peg over 1
                return(true);
            }

            else if (Math.Abs(moveUpDown) + Math.Abs(moveLeftRight) == 2)
            {
                // jump (2,0) or (0,2) --> the (1,1) case has already been tested
                return(!pegBoard.isEmpty(from.row - moveUpDown / 2, from.col - moveLeftRight / 2) &&
                       pegBoard.checkColor(from.row - moveUpDown / 2, from.col - moveLeftRight / 2) != currentPlayerColor);
                // Jumps A color and Jumps OTHER color
            }

            else
            {
                // Don't jump the rainbow!
                return(false);
            }
        }