Exemplo n.º 1
0
        public bool lightPlayerTurn = true;    // If true then it is light players turn
                                               // while (gameEnd == false)
                                               //{

        public void ProcessInput(string input) //Reads the file and either place or move pieces as written.
        {
            string Origin;
            string placed;
            bool   checkMate = false;
            string movePiece = ("^[a-h][1-8] [a-h][1-8]\\*?$");     //Rexex pattern that will only accept [x axis location][y axis location]
                                                                    //[location on the x axis][location on y axis]
            Match MoveMatch;

            bool captured = false;

            MoveMatch = Regex.Match(input, movePiece, RegexOptions.IgnoreCase);
            if (MoveMatch.Success)
            {
                //Previously, this part of the file read commands for placement of pieces. Since this program is supposed to be without said lines, It was removed
                captured = false;
                //Splits the line in order to gather data on the starting and ending point
                Origin = input.Split(' ')[0];
                placed = input.Split(' ')[1];
                //Checks the end of the line to see if a piece is captured
                if (placed.Substring(placed.Length - 1).Equals("*"))
                {
                    captured = true;
                }
                //If it didn't capture a piece, it simply moves the piece from point a to point b, using methods previously talked about
                if (!captured)
                {
                    int StartX = char.ToUpper(Origin.First <char>()) - 65; //This is set to 65 to make logic easier so that a is equal to 0 instead of one
                    int StartY = int.Parse(Origin.Substring(Origin.Length - 1));
                    int EndX   = char.ToUpper(placed.First <char>()) - 65;
                    int EndY   = int.Parse(placed.Substring(placed.Length - 1));

                    Piece p = board[StartX, 8 - StartY];
                    if (p == null)
                    {
                        string atemp = ("The space at those cordinates on the board is null");
                        PrintBoard(atemp);
                        //Console.ReadLine();
                    }
                    else
                    {
                        if (lightPlayerTurn == true && p.color == 'l')
                        {
                            //lightplayer
                            PieceMove(StartX, 8 - StartY, EndX, 8 - EndY, p, input, p.color);
                        }
                        else if (lightPlayerTurn == false && p.color == 'd')
                        {
                            PieceMove(StartX, 8 - StartY, EndX, 8 - EndY, p, input, p.color);
                            //darkplayer
                        }
                        else
                        {
                            string temp = ("Invalid move: It is currently the other players turn.");
                            PrintBoard(temp);
                            //Console.ReadKey();
                        }
                    }
                }
                //If a piece was captured, the parsing for the ending y axis point is moddified to be accepted as the * at the end could cause issues
                else if (captured)
                {
                    int StartX = char.ToUpper(Origin.First <char>()) - 65;
                    int StartY = int.Parse(Origin.Substring(Origin.Length - 1));
                    int EndX   = char.ToUpper(placed.First <char>()) - 65;
                    int EndY   = int.Parse(placed.Substring(placed.Length - 2, placed.Length - 2));
                    //game.pieceMove(StartX, 8 - StartY, EndX, 8 - EndY);
                    Piece p = board[StartX, 8 - StartY];
                    if (p == null)
                    {
                        Console.WriteLine("The space at those cordinates on the board is null");
                        //Console.ReadLine();
                    }
                    else
                    {
                        if (lightPlayerTurn && p.color == 'l')
                        {
                            //lightplayer
                            PieceMove(StartX, 8 - StartY, EndX, 8 - EndY, p, input, p.color);
                        }
                        else if (!lightPlayerTurn && p.color == 'd')
                        {
                            PieceMove(StartX, 8 - StartY, EndX, 8 - EndY, p, input, p.color);
                            //darkplayer
                        }
                        else
                        {
                            string temp = ("Invalid move: It is currently the other players turn.");
                            PrintBoard(temp);
                            //Console.ReadKey();
                        }
                    }
                }
                else
                {
                    Console.WriteLine($"Invalid Line: {input}");
                }
            }
            if (checkMate == true)
            {
                gameEnd = true;
            }
        }