Exemple #1
0
        public List <Piece> CheckHorizontalDirection(ChessBoard ChessBoard, Point BoardPos, bool isKing)
        {
            PossibleMoves = new List <Piece>();
            int x                = BoardPos.X;
            int y                = BoardPos.Y;
            int DistanceToTop    = ChessBoard.BoardHeight - y;
            int DistanceToBottom = y + 1;
            int DistanceToLeft   = x + 1;
            int DistanceToRight  = ChessBoard.BoardWidth - x;


            if (isKing)
            {
                DistanceToTop    = GameSystem.GetMin(2, DistanceToTop);
                DistanceToBottom = GameSystem.GetMin(2, DistanceToBottom);
                DistanceToLeft   = GameSystem.GetMin(2, DistanceToLeft);
                DistanceToRight  = GameSystem.GetMin(2, DistanceToRight);
            }

            //check up direction

            for (int i = 1; i < DistanceToTop; i++)
            {
                Piece currentSpace = ChessBoard.BoardData[x, y + i];
                if (currentSpace.PieceColour == "-")
                {
                    PossibleMoves.Add(ChessBoard.BoardData[x, y + i]);
                }
                else if (currentSpace.PieceColour != PieceColour)
                {
                    PossibleMoves.Add(ChessBoard.BoardData[x, y + i]);
                    break;
                }
                else
                {
                    break;
                }
            }

            //check down direction

            for (int i = 1; i < DistanceToBottom; i++)
            {
                Piece currentSpace = ChessBoard.BoardData[x, y - i];
                if (currentSpace.PieceColour == "-")
                {
                    PossibleMoves.Add(ChessBoard.BoardData[x, y - i]);
                }
                else if (currentSpace.PieceColour != PieceColour)
                {
                    PossibleMoves.Add(ChessBoard.BoardData[x, y - i]);
                    break;
                }
                else
                {
                    break;
                }
            }

            //check right direction

            for (int i = 1; i < DistanceToRight; i++)
            {
                Piece currentSpace = ChessBoard.BoardData[x + i, y];
                if (currentSpace.PieceColour == "-")
                {
                    PossibleMoves.Add(ChessBoard.BoardData[x + i, y]);
                }
                else if (currentSpace.PieceColour != PieceColour)
                {
                    PossibleMoves.Add(ChessBoard.BoardData[x + i, y]);
                    break;
                }
                else
                {
                    break;
                }
            }

            //check left direction



            for (int i = 1; i < DistanceToLeft; i++)
            {
                Piece currentSpace = ChessBoard.BoardData[x - i, y];
                if (currentSpace.PieceColour == "-")
                {
                    PossibleMoves.Add(ChessBoard.BoardData[x - i, y]);
                }
                else if (currentSpace.PieceColour != PieceColour)
                {
                    PossibleMoves.Add(ChessBoard.BoardData[x - i, y]);
                    break;
                }
                else
                {
                    break;
                }
            }


            return(PossibleMoves);
        }
Exemple #2
0
        public List <Piece> CheckDiagonalDirection(ChessBoard ChessBoard, Point BoardPos, bool isKing)
        {
            PossibleMoves = new List <Piece>();
            int x = BoardPos.X;                                // 3
            int y = BoardPos.Y;                                // 1
            int DistanceToLeft   = x + 1;                      // 4
            int DistanceToRight  = ChessBoard.BoardWidth - x;  // 5
            int DistanceToTop    = ChessBoard.BoardHeight - y; // 7
            int DistanceToBottom = y + 1;                      //2


            if (isKing)
            {
                DistanceToTop    = GameSystem.GetMin(2, DistanceToTop);
                DistanceToBottom = GameSystem.GetMin(2, DistanceToBottom);
                DistanceToLeft   = GameSystem.GetMin(2, DistanceToLeft);
                DistanceToRight  = GameSystem.GetMin(2, DistanceToRight);
            }

            //forward left

            for (int i = 1; i < GameSystem.GetMin(DistanceToLeft, DistanceToTop); i++)
            {
                if (i < GameSystem.GetMin(DistanceToLeft, DistanceToTop))
                {
                    Piece currentSpace = ChessBoard.BoardData[x - i, y + i];
                    if (currentSpace.PieceColour == "-")
                    {
                        PossibleMoves.Add(ChessBoard.BoardData[x - i, y + i]);
                    }
                    else if (currentSpace.PieceColour != PieceColour)
                    {
                        PossibleMoves.Add(ChessBoard.BoardData[x - i, y + i]);
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            //down left

            for (int i = 1; i < GameSystem.GetMin(DistanceToLeft, DistanceToBottom); i++)
            {
                Piece currentSpace = ChessBoard.BoardData[x - i, y - i];
                if (currentSpace.PieceColour == "-")
                {
                    PossibleMoves.Add(ChessBoard.BoardData[x - i, y - i]);
                }
                else if (currentSpace.PieceColour != PieceColour)
                {
                    PossibleMoves.Add(ChessBoard.BoardData[x - i, y - i]);
                    break;
                }
                else
                {
                    break;
                }
            }



            //forward right

            for (int i = 1; i < GameSystem.GetMin(DistanceToRight, DistanceToTop); i++)
            {
                Piece currentSpace = ChessBoard.BoardData[x + i, y + i];
                if (currentSpace.PieceColour == "-")
                {
                    PossibleMoves.Add(ChessBoard.BoardData[x + i, y + i]);
                }
                else if (currentSpace.PieceColour != PieceColour)
                {
                    PossibleMoves.Add(ChessBoard.BoardData[x + i, y + i]);
                    break;
                }
                else
                {
                    break;
                }
            }

            //down right

            for (int i = 1; i < GameSystem.GetMin(DistanceToRight, DistanceToBottom); i++)
            {
                Piece currentSpace = ChessBoard.BoardData[x + i, y - i];
                if (currentSpace.PieceColour == "-")
                {
                    PossibleMoves.Add(ChessBoard.BoardData[x + i, y - i]);
                }
                else if (currentSpace.PieceColour != PieceColour)
                {
                    PossibleMoves.Add(ChessBoard.BoardData[x + i, y - i]);
                    break;
                }
                else
                {
                    break;
                }
            }

            return(PossibleMoves);
        }
Exemple #3
0
        static void Main(string[] args)
        {
            //Create ChessBoard and initiate game
            ChessBoard chessBoard1 = GameSystem.StartGame(8, 8);

            //Main Game loop
            while (!chessBoard1.GameOver())
            {
                // read for user inputs in the command line
                string command = Console.ReadLine();

                switch (command)
                {
                case "white":
                    chessBoard1.DrawBoard("white");
                    break;

                case "black":
                    chessBoard1.DrawBoard("black");
                    break;

                case "exit":
                    Environment.Exit(0);
                    break;

                default:
                    //checks if input was "LetterNumber LetterNumber" and a 2 valid chess coordinates
                    Regex PlayerMoveTest = new Regex(@"(^\b[A-H][1-8]\s[A-H][1-8]\b$)", RegexOptions.IgnoreCase);
                    Match PlayerMove     = PlayerMoveTest.Match(command);

                    if (PlayerMove.Success)
                    {
                        //grabs the user input if valid and converts into coordinates, "A1 = 0,0" etc
                        List <int> Coordinates;
                        Coordinates = new List <int>();
                        foreach (Char x in command)
                        {
                            if (Int32.TryParse(x.ToString(), out int j))
                            {
                                Coordinates.Add(j - 1);
                            }
                            else
                            {
                                if (x.ToString() != " ")
                                {
                                    int Index = Char.ToUpper(x) - 64;
                                    Coordinates.Add(Index - 1);
                                }
                            }
                        }

                        //beautiful
                        chessBoard1.BoardData[Coordinates[0], Coordinates[1]].MovePiece(chessBoard1, new Point(Coordinates[2], Coordinates[3]), false);
                    }
                    else
                    {
                        //if nothing happens the userinput can't have been recognised
                        Console.WriteLine("Invalid Command");
                    }
                    break;
                }
            }

            chessBoard1.ChangePlayerTurn();
            Console.WriteLine(chessBoard1.PlayerTurn + " Wins!");
            Console.ReadKey();
            Environment.Exit(0);
        }