Exemplo n.º 1
0
        public static bool ParseLine(String line, out String message)
        {
            string placingPattern = "^([KQBNRP])([ld])([a-h, A-H][1-8])$";
            string movingPattern = "^([a-h,A-H][1-8])\\s([a-h,A-H][1-8])$";
            string doubleMovePattern = "^([a-h,A-H][1-8])\\s([a-h,A-H][1-8])\\s([a-h,A-H][1-8])\\s([a-h,A-H][1-8])$";

            message = "";
            if (Regex.IsMatch(line, placingPattern))
            {
                char piece = line.ElementAt(0);
                char color = line.ElementAt(1);
                char rank = line.ElementAt(2);
                char file = line.ElementAt(3);
                Piece p = new Piece(piece, color);
                message = string.Concat("Place the ", p.GetColor(), " ", p.GetPieceType(), " at ", rank, file, "\n");
                validPlacements.Add(line);
                return true;
            }
            else if (Regex.IsMatch(line, movingPattern))
            {
                char startingRank = line.ElementAt(0);
                char startingFile = line.ElementAt(1);
                char endingRank = line.ElementAt(3);
                char endingFile = line.ElementAt(4);
                message = string.Concat("Move the piece at ", startingRank, startingFile, " to ", endingRank, endingFile, "\n");
                validMoves.Add(line);
                return true;
            }
            else if (Regex.IsMatch(line, doubleMovePattern))
            {
                char firstStartingRank = line.ElementAt(0);
                char firstStartingFile = line.ElementAt(1);
                char firstEndingRank = line.ElementAt(3);
                char firstEndingFile = line.ElementAt(4);
                char secondStartingRank = line.ElementAt(6);
                char secondStartingFile = line.ElementAt(7);
                char secondEndingRank = line.ElementAt(9);
                char secondEndingFile = line.ElementAt(10);
                message = string.Concat("Move the piece at ", firstStartingRank, firstStartingFile, " to ", firstEndingRank, firstEndingFile, " and the piece at ", secondStartingRank, secondStartingFile, " to ", secondEndingRank, secondEndingFile,"\n");
                validMoves.Add(line);
                return true;
            }
            else if (!Regex.IsMatch(line, "^($)"))
            {
                PrintInfo(line, "This line is invalid input.");
                return false;
            }
            return false;
        }
Exemplo n.º 2
0
        public static bool ParseLine(String line, out String message)
        {
            string placingPattern    = "^([KQBNRP])([ld])([a-h, A-H][1-8])$";
            string movingPattern     = "^([a-h,A-H][1-8])\\s([a-h,A-H][1-8])$";
            string doubleMovePattern = "^([a-h,A-H][1-8])\\s([a-h,A-H][1-8])\\s([a-h,A-H][1-8])\\s([a-h,A-H][1-8])$";

            message = "";
            if (Regex.IsMatch(line, placingPattern))
            {
                char  piece = line.ElementAt(0);
                char  color = line.ElementAt(1);
                char  rank  = line.ElementAt(2);
                char  file  = line.ElementAt(3);
                Piece p     = new Piece(piece, color);
                message = string.Concat("Place the ", p.GetColor(), " ", p.GetPieceType(), " at ", rank, file, "\n");
                validPlacements.Add(line);
                return(true);
            }
            else if (Regex.IsMatch(line, movingPattern))
            {
                char startingRank = line.ElementAt(0);
                char startingFile = line.ElementAt(1);
                char endingRank   = line.ElementAt(3);
                char endingFile   = line.ElementAt(4);
                message = string.Concat("Move the piece at ", startingRank, startingFile, " to ", endingRank, endingFile, "\n");
                validMoves.Add(line);
                return(true);
            }
            else if (Regex.IsMatch(line, doubleMovePattern))
            {
                char firstStartingRank  = line.ElementAt(0);
                char firstStartingFile  = line.ElementAt(1);
                char firstEndingRank    = line.ElementAt(3);
                char firstEndingFile    = line.ElementAt(4);
                char secondStartingRank = line.ElementAt(6);
                char secondStartingFile = line.ElementAt(7);
                char secondEndingRank   = line.ElementAt(9);
                char secondEndingFile   = line.ElementAt(10);
                message = string.Concat("Move the piece at ", firstStartingRank, firstStartingFile, " to ", firstEndingRank, firstEndingFile, " and the piece at ", secondStartingRank, secondStartingFile, " to ", secondEndingRank, secondEndingFile, "\n");
                validMoves.Add(line);
                return(true);
            }
            else if (!Regex.IsMatch(line, "^($)"))
            {
                PrintInfo(line, "This line is invalid input.");
                return(false);
            }
            return(false);
        }
Exemplo n.º 3
0
        //Method to move valid piece to valid location
        //		returns (true, null) if move was successful and neither king was checked
        //		returns (true, " ... ") if enemy king was checked or checkmated
        private static (bool, string) MovePiece(Piece piece, Point target)
        {
            string message     = null;
            Point  oldPoint    = piece.GetLoc();
            Piece  overwritten = Chess.GetBoard().MovePiece(piece, target);

            bool blackChecked = blackKing.CheckCheck();
            bool whiteChecked = whiteKing.CheckCheck();

            Func <bool, char, char, bool> CheckSelf = (isChecked, mine, kings) => isChecked && (mine == kings);

            bool checkSelf = CheckSelf(blackChecked, piece.GetColor(), 'b') || CheckSelf(whiteChecked, piece.GetColor(), 'w');

            //Return false with error message 'That would check your own king!'
            //		return board to original state
            if (checkSelf)
            {
                //Restoring board to original position
                Chess.GetBoard().MovePiece(piece, oldPoint);
                Chess.GetBoard().MovePiece(overwritten, target);

                return(false, "That move would check your king!");
            }
            else
            {
                //Now that we know we are not checked, check if enemy is checkmated
                bool enemyChecked;
                King checkedKing;
                (enemyChecked, checkedKing) = (blackChecked) ? (true, blackKing) : (whiteChecked, whiteKing);

                //If the enemy is checked, check if they are also checkmated
                if (enemyChecked)
                {
                    message = "Enemy king checked!";
                    bool enemeyCheckMated = checkedKing.CheckMate();

                    if (enemeyCheckMated)
                    {
                        message = "Check mate!";
                        return(false, message);
                    }
                }
            }
            return(true, message);
        }
Exemplo n.º 4
0
        private void InitGrid()
        {
            var images = new Dictionary <Color, Dictionary <Type, Bitmap> >();

            #region filling up images
            var whites = new Dictionary <Type, Bitmap>();
            var blacks = new Dictionary <Type, Bitmap>();
            images[Color.White] = whites;
            images[Color.Black] = blacks;

            whites[Type.Bishop] = Properties.Resources.BishopW;
            whites[Type.Pawn]   = Properties.Resources.PawnW;
            whites[Type.Knight] = Properties.Resources.KnightW;
            whites[Type.Queen]  = Properties.Resources.QueenW;
            whites[Type.King]   = Properties.Resources.KingW;
            whites[Type.Rook]   = Properties.Resources.RookW;

            blacks[Type.Bishop] = Properties.Resources.BishopB;
            blacks[Type.Pawn]   = Properties.Resources.PawnB;
            blacks[Type.Knight] = Properties.Resources.KnightB;
            blacks[Type.Queen]  = Properties.Resources.QueenB;
            blacks[Type.King]   = Properties.Resources.KingB;
            blacks[Type.Rook]   = Properties.Resources.RookB;
            #endregion

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    grid[i, j] = new GraphicCell(i, j, this);

                    Piece piece = logic.getCellContent(i, j);
                    if (piece != null)
                    {
                        grid[i, j].Image = images[piece.GetColor()][piece.GetType()];
                    }
                }
            }
        }
Exemplo n.º 5
0
        //Turn function
        public static bool turn()
        {
            //determine current player
            string player = PLAYERS[turns % 2];

            //Draw board and prompt
            Console.Clear();
            Console.WriteLine(board);
            Console.WriteLine(String.Format("Where would you like to move, {0}?", player));
            Console.WriteLine("\tType 'h' for help");

            //request input
            string input = Console.ReadLine();

            //Lambda for printing help menu;
            Action printH = () => {
                Console.WriteLine("Type [piece],[location] to move your piece.");
                Console.WriteLine("\tEx: 'wp1,a-3'");
                Console.WriteLine("Type [piece] to see a piece's moves.");
                Console.WriteLine("\tEx: 'wp1'");
                //After, printing instructions, request another input and
                //		return to original logic flow
                input = Console.ReadLine();
            };

            //Check for 'h' (or 'H')
            if (input.ToUpper().Equals("H"))
            {
                printH();
            }

            //loop variables
            bool   validMove = false;
            string message   = null;

            //Do, while the user has not entered a valid input, or moved
            do
            {
                while (!verify.Match(input).Success || message != null)
                {
                    if (message != null)
                    {
                        Console.WriteLine(message);
                    }
                    Console.WriteLine("Invalid input!  Try again!");
                    message = null;
                    input   = Console.ReadLine();
                    //Check for 'h' (or 'H')
                    if (input.ToUpper().Equals("H"))
                    {
                        printH();
                    }
                }
                //If input contains a "," and it passed the Regex match, then it must be a movement action
                if (input.Contains(","))
                {
                    string   piece;
                    string   move;
                    string[] splits = input.Split(",");
                    piece = splits[0];
                    move  = splits[1];

                    //Retrieving piece from board
                    Piece p = Chess.GetBoard().GetPiece(piece);
                    if (p != null)
                    {
                        if (p.GetColor() == PLAYTYPES[turns % 2])
                        {
                            //Parsing moveTo location
                            int   col    = ((int)move[0]) - 'a';
                            int   row    = 8 - (((int)move[2]) - '0');
                            Point target = new Point(row, col);

                            //Getting piece's moves
                            List <Point> moves = p.GetMoves();


                            //Searching through movelist for a point matching the moveTo coordinate
                            bool found = moves.Exists((point) => point.Equals(target));

                            //movepiece
                            if (found)
                            {
                                bool moved;
                                (moved, message) = MovePiece(p, target);

                                //If piece was able to move
                                if (moved)
                                {
                                    if (p.GetType() == typeof(Pawn))
                                    {
                                        ((Pawn)p).SetMoved(moved);

                                        //A pawn cannot move backwards, so the only time
                                        //		any pawn can reach the edge of the field is when it
                                        //		hits the opposite edge, turn the piece into a queen
                                        if (p.GetLoc().X == 7 || p.GetLoc().X == 0)
                                        {
                                            Chess.GetBoard().SetPiece(new Queen(p.GetColor(), p.GetLoc()), p.GetLoc());
                                        }
                                    }

                                    if (message != null && message.Equals("Check mate!"))
                                    {
                                        return(false);
                                    }
                                    validMove = true;
                                }
                                //Else if piece couldn't move (checking self)!
                                //		move was not valid, try again
                                else
                                {
                                }
                            }
                            else
                            {
                                message = "Cannot move there!";
                                //return false;
                            }
                        }
                        else
                        {
                            message = "Not your piece";
                            //return false;
                        }
                    }
                    //Invalid move location
                    else
                    {
                        message = String.Format("Cannot move there!\n\t" +
                                                "Type '{0}' to view the moves of piece {0}", piece);
                    }
                }
                //Else, printing moves of piece
                else
                {
                    //Getting piece
                    Piece piece = Chess.GetBoard().GetPiece(input);

                    //Getting piece's moves
                    List <Point> moves = piece.GetMoves();


                    string str = Chess.GetBoard().ToString(moves);


                    //Prining new board
                    Console.Clear();
                    Console.WriteLine(Chess.GetBoard().ToString(moves));
                    Console.WriteLine("Type 'x' to remove x's!");
                    input = Console.ReadLine();
                    //Check for 'h' (or 'H')
                    if (input.ToUpper().Equals("H"))
                    {
                        printH();
                    }
                    else if (input.ToUpper().Equals("X"))
                    {
                        //Printing cleared board
                        Console.Clear();
                        Console.WriteLine(Chess.GetBoard());
                        Console.WriteLine("Continue!");
                        input = Console.ReadLine();
                    }
                }
            }while (!validMove);


            turns++;
            return(true);
        }