예제 #1
0
        public static void WhiteWin(CreeperBoard board)
        {
            Position startPosition;
            Position endPosition;
            Move move;
            bool pausePrint = false;

            for (int i = 0; i < 4; i++)
            {
                startPosition = new Position(i, i + 1);
                endPosition = new Position(i + 1, i + 2);
                move = new Move(startPosition, endPosition, CreeperColor.Fire);
                board.Move(move);
                board.PrintToConsole(pausePrint);
            }
            for (int i = 0; i < 3; i++)
            {
                startPosition = new Position(i, i + 2);
                endPosition = new Position(i + 1, i + 3);
                move = new Move(startPosition, endPosition, CreeperColor.Fire);
                board.Move(move);
                board.PrintToConsole(pausePrint);
            }

            startPosition = new Position(4, 6);
            endPosition = new Position(5, 5);
            move = new Move(startPosition, endPosition, CreeperColor.Fire);
            board.Move(move);
            board.PrintToConsole(pausePrint);

            startPosition = new Position(4, 5);
            endPosition = new Position(5, 4);
            move = new Move(startPosition, endPosition, CreeperColor.Fire);
            board.Move(move);
            board.PrintToConsole(pausePrint);

            if(board.GetGameState(CreeperColor.Fire) == CreeperGameState.Complete)
            {
                Console.WriteLine("White wins!");
            }
        }
예제 #2
0
        public void PushMove(Move move)
        {
            MoveHistory.Push(move);

            ((move.PlayerColor == CreeperColor.Ice) ? BlackPegs : WhitePegs).Remove(PegBoard[move.StartPosition.Row, move.StartPosition.Column]);
            PegBoard[move.StartPosition.Row, move.StartPosition.Column].Color = CreeperColor.Empty;
            PegBoard[move.EndPosition.Row, move.EndPosition.Column].Color = move.PlayerColor;
            ((move.PlayerColor == CreeperColor.Ice) ?  BlackPegs : WhitePegs ).Add(PegBoard[move.EndPosition.Row, move.EndPosition.Column]);

            if (IsFlipMove(move))
            {
                Flip(move);
            }
            else if (IsCaptureMove(move))
            {
                Capture(move);
            }

            GameStateHistory.Push(CouldBeFinished(move.PlayerColor) ? GetGameState(move.PlayerColor) : CreeperGameState.Unfinished);
        }
예제 #3
0
        public bool IsValidMove(Move move)
        {
            bool valid = true;

            //is the move in bounds?
            if (!IsValidPosition(move.StartPosition.Row, move.StartPosition.Column, PieceType.Peg)
                || !IsValidPosition(move.EndPosition.Row, move.EndPosition.Column, PieceType.Peg))
            {
                valid = false;
            }

            //Does the start space have the player's piece?
            else if (PegBoard[move.StartPosition.Row, move.StartPosition.Column].Color != move.PlayerColor)
            {
                valid = false;
            }

            //Is the end space empty?
            else if (PegBoard[move.EndPosition.Row, move.EndPosition.Column].Color != CreeperColor.Empty)
            {
                valid = false;
            }

            else
            {
                if ((Math.Abs(move.StartPosition.Row - move.EndPosition.Row) > 1)
                || (Math.Abs(move.StartPosition.Column - move.EndPosition.Column) > 1)
                || (move.StartPosition.Equals(move.EndPosition)))
                {
                    valid = false;
                }

                if (Math.Abs(Math.Abs(move.StartPosition.Row - move.EndPosition.Row) - Math.Abs(move.StartPosition.Column - move.EndPosition.Column)) == 2
                    && PegBoard[move.StartPosition.Row + ((move.EndPosition.Row - move.StartPosition.Row) / 2), move.StartPosition.Column + ((move.EndPosition.Column - move.StartPosition.Column) / 2)].Color == move.PlayerColor.Opposite())
                {
                    valid = true;
                }

                else if (move.PlayerColor == CreeperColor.Empty || move.PlayerColor == CreeperColor.Invalid)
                {
                    throw new ArgumentOutOfRangeException(move.ToString());
                }

            }

            return valid;
        }
예제 #4
0
 public bool IsFlipMove(Move move)
 {
     return Math.Abs(move.StartPosition.Row - move.EndPosition.Row) * Math.Abs(move.StartPosition.Column - move.EndPosition.Column) == 1;
 }
예제 #5
0
 public bool IsCaptureMove(Move move)
 {
     return (Math.Abs(move.StartPosition.Row - move.EndPosition.Row) == 2) != (Math.Abs(move.StartPosition.Column - move.EndPosition.Column) == 2);
 }
예제 #6
0
 public AIBoardNode GetFlippedTileCopy(Move move)
 {
     return new AIBoardNode(GetFlippedTile(move));
 }
예제 #7
0
        public static Position GetFlippedPosition(Move move)
        {
            CardinalDirection direction = move.EndPosition.Row < move.StartPosition.Row ? (move.EndPosition.Column < move.StartPosition.Column ? CardinalDirection.Northwest : CardinalDirection.Northeast)
                : (move.EndPosition.Column < move.StartPosition.Column ? CardinalDirection.Southwest : CardinalDirection.Southeast);

            switch (direction)
            {
                case CardinalDirection.Northwest:
                    return new Position(move.EndPosition.Row, move.EndPosition.Column);

                case CardinalDirection.Northeast:
                    return new Position(move.EndPosition.Row, move.EndPosition.Column - 1);

                case CardinalDirection.Southwest:
                    return new Position(move.EndPosition.Row - 1, move.EndPosition.Column);

                case CardinalDirection.Southeast:
                    return new Position(move.StartPosition.Row, move.StartPosition.Column);

                default:
                    throw new ArgumentException("No flipped tile for this move.");
            }
        }
예제 #8
0
 private void UnCapture(Move move)
 {
     if (move.StartPosition.Row + 2 == move.EndPosition.Row && move.StartPosition.Column == move.EndPosition.Column)
     {
         PegBoard[move.StartPosition.Row + 1, move.StartPosition.Column].Color = move.PlayerColor.Opposite();
         ((move.PlayerColor == CreeperColor.Ice) ? WhitePegs : BlackPegs).Add(PegBoard[move.StartPosition.Row + 1, move.StartPosition.Column]);
     }
     else if (move.StartPosition.Row - 2 == move.EndPosition.Row && move.StartPosition.Column == move.EndPosition.Column)
     {
         PegBoard[move.StartPosition.Row - 1, move.StartPosition.Column].Color = move.PlayerColor.Opposite();
         ((move.PlayerColor == CreeperColor.Ice) ? WhitePegs : BlackPegs).Add(PegBoard[move.StartPosition.Row - 1, move.StartPosition.Column]);
     }
     else if (move.StartPosition.Row == move.EndPosition.Row && move.StartPosition.Column + 2 == move.EndPosition.Column)
     {
         PegBoard[move.StartPosition.Row, move.StartPosition.Column + 1].Color = move.PlayerColor.Opposite();
         ((move.PlayerColor == CreeperColor.Ice) ? WhitePegs : BlackPegs).Add(PegBoard[move.StartPosition.Row, move.StartPosition.Column + 1]);
     }
     else if (move.StartPosition.Row == move.EndPosition.Row && move.StartPosition.Column - 2 == move.EndPosition.Column)
     {
         PegBoard[move.StartPosition.Row, move.StartPosition.Column - 1].Color = move.PlayerColor.Opposite();
         ((move.PlayerColor == CreeperColor.Ice) ? WhitePegs : BlackPegs).Add(PegBoard[move.StartPosition.Row, move.StartPosition.Column - 1]);
     }
 }
예제 #9
0
 private void Flip(Move move)
 {
     Piece tile = GetFlippedTile(move);
     if (!IsCorner(tile))
     {
         tile.Color = move.PlayerColor;
     }
 }
예제 #10
0
 private Piece GetFlippedTile(Move move)
 {
     return Tiles.At(GetFlippedPosition(move));
 }
예제 #11
0
 private void Capture(Move move)
 {
     Pegs.At(GetCapturedPegPosition(move)).Color = CreeperColor.Empty;
 }
예제 #12
0
        public bool Move(Move move)
        {
            bool isValid = false;

            if (IsValidMove(move))
            {
                isValid = true;

                Pegs.First(x => x.Position.Equals(move.StartPosition)).Color = CreeperColor.Empty;
                Pegs.First(x => x.Position.Equals(move.EndPosition)).Color = move.PlayerColor;

                if (IsFlipMove(move))
                {
                    Flip(move);
                }
                else if (IsCaptureMove(move))
                {
                    Capture(move);
                }

            }

            return isValid;
        }
예제 #13
0
 public bool IsValidMove(Move move)
 {
     return Pegs.At(move.StartPosition).PossibleMoves(this).Any(x => x.EndPosition == move.EndPosition  && x.PlayerColor == move.PlayerColor);
 }
예제 #14
0
        private void Flip(Move move)
        {
            AIBoardNode tile = GetFlippedTile(move);
            if (tile.Color != CreeperColor.Invalid)
            {
                TileHistory.Push(tile.Color);
                if (tile.Color == CreeperColor.Empty)
                {
                    tile.Color = move.PlayerColor;
                    AddTileToTeam(tile);
                }
                else if (tile.Color != move.PlayerColor)
                {
                    RemoveTileFromTeam(tile);
                    tile.Color = move.PlayerColor;
                    AddTileToTeam(tile);
                }

                UpdateListHeads(tile.Row, tile.Column, tile.Color);
            }
        }
예제 #15
0
 public Move(Move move)
 {
     this.StartPosition = new Position(move.StartPosition);
     this.EndPosition = new Position(move.EndPosition);
     this.PlayerColor = move.PlayerColor;
 }
예제 #16
0
        private AIBoardNode GetFlippedTile(Move move)
        {
            CardinalDirection direction = move.EndPosition.Row < move.StartPosition.Row ? (move.EndPosition.Column < move.StartPosition.Column ? CardinalDirection.Northwest : CardinalDirection.Northeast)
                : (move.EndPosition.Column < move.StartPosition.Column ? CardinalDirection.Southwest : CardinalDirection.Southeast);

            switch (direction)
            {
                case CardinalDirection.Northwest:
                    return TileBoard[move.EndPosition.Row, move.EndPosition.Column];

                case CardinalDirection.Northeast:
                    return TileBoard[move.EndPosition.Row, move.EndPosition.Column - 1];

                case CardinalDirection.Southwest:
                    return TileBoard[move.EndPosition.Row - 1, move.EndPosition.Column];

                case CardinalDirection.Southeast:
                    return TileBoard[move.StartPosition.Row, move.StartPosition.Column];

                default:
                    throw new ArgumentOutOfRangeException(move.ToString() + " must be a flipping move!");
            }
        }
예제 #17
0
        public Move[] AllPossibleMoves(CreeperColor color)
        {
            if (color == CreeperColor.Invalid || color == CreeperColor.Empty) throw new ArgumentOutOfRangeException(color.ToString());

            Position startPosition = new Position();
            Move[] possibleMoves = new Move[100];
            int index = 0;
            foreach (AIBoardNode peg in (color == CreeperColor.Ice) ? BlackPegs : WhitePegs)
            {
                startPosition = new Position(peg.Row, peg.Column);

                Move[] movesForPeg = new Move[]
                    {
                        new Move(startPosition, new Position(peg.Row + 1, peg.Column), color),
                        new Move(startPosition, new Position(peg.Row - 1, peg.Column), color),
                        new Move(startPosition, new Position(peg.Row, peg.Column + 1), color),
                        new Move(startPosition, new Position(peg.Row, peg.Column - 1), color),
                        new Move(startPosition, new Position(peg.Row + 1, peg.Column + 1), color),
                        new Move(startPosition, new Position(peg.Row + 1, peg.Column - 1), color),
                        new Move(startPosition, new Position(peg.Row - 1, peg.Column + 1), color),
                        new Move(startPosition, new Position(peg.Row - 1, peg.Column - 1), color),
                        new Move(startPosition, new Position(peg.Row + 2, peg.Column), color),
                        new Move(startPosition, new Position(peg.Row - 2, peg.Column), color),
                        new Move(startPosition, new Position(peg.Row, peg.Column + 2), color),
                        new Move(startPosition, new Position(peg.Row, peg.Column - 2), color),
                    };

                for (int i = 0; i < movesForPeg.Length; i++)
                {
                    if (IsValidMove(movesForPeg[i]))
                    {
                        possibleMoves[index++] = movesForPeg[i];
                    }
                }
            }

            Array.Resize(ref possibleMoves, index);
            return possibleMoves;
        }
예제 #18
0
        private void UnFlip(Move move)
        {
            AIBoardNode tile = GetFlippedTile(move);
            if (tile.Color != CreeperColor.Invalid)
            {
                CreeperColor color = TileHistory.Pop();
                if (color == CreeperColor.Empty)
                {
                    RemoveTileFromTeam(tile);
                    tile.Color = color;
                }
                // Flipping opposite team's tile.
                else if (color == move.PlayerColor.Opposite())
                {
                    RemoveTileFromTeam(tile);
                    tile.Color = color;
                    AddTileToTeam(tile);
                }

                UpdateListHeads(tile.Row, tile.Column, move.PlayerColor.Opposite());
            }
        }
예제 #19
0
        public static Position GetCapturedPegPosition(Move move)
        {
            foreach (CardinalDirection direction in new[] { CardinalDirection.North, CardinalDirection.South, CardinalDirection.East, CardinalDirection.West })
            {
                if (move.EndPosition == move.StartPosition.AtDirection(direction).AtDirection(direction))
                {
                    return move.StartPosition.AtDirection(direction);
                }
            }

            throw new ArgumentException("No captured peg for this move.");
        }