Пример #1
0
        public Board()
        {
            board = new Tool[9, 9];

            board[1, 1] = new Rook("BlackRook", false);
            board[2, 1] = new Knight("BlackKnight", false);
            board[3, 1] = new Bishop("BlackBishop", false);
            board[4, 1] = new Queen("BlackQueen", false);
            board[5, 1] = new King("BlackKing", false);
            board[6, 1] = new Bishop("BlackBishop", false);
            board[7, 1] = new Knight("BlackKnight", false);
            board[8, 1] = new Rook("BlackRook", false);
            for (int i = 1; i < 9; i++)
            {
                board[i, 2] = new Pawn("BlackPawn", false);
            }
            board[1, 8] = new Rook("WhiteRook", true);
            board[2, 8] = new Knight("WhiteKnight", true);
            board[3, 8] = new Bishop("WhiteBishop", true);
            board[4, 8] = new Queen("WhiteQueen", true);
            board[5, 8] = new King("WhiteKing", true);
            board[6, 8] = new Bishop("WhiteBishop", true);
            board[7, 8] = new Knight("WhiteKnight", true);
            board[8, 8] = new Rook("WhiteRook", true);
            for (int i = 1; i < 9; i++)
            {
                board[i, 7] = new Pawn("WhitePawn", true);
            }
        }
Пример #2
0
        public void choosingAct()
        {
            int    wrong         = 0; // its going to count the errors of user's choices
            int    initialPlaceX = 0;
            int    initialPlaceY = 0;
            int    chosenPlaceX  = 0;
            int    chosenPlaceY  = 0;
            string input         = "";

            do
            {
                wrong         = 0;
                initialPlaceX = 0;
                initialPlaceY = 0;
                chosenPlaceX  = 0;
                chosenPlaceY  = 0;
                Console.WriteLine("please enter the move you wanna do");
                input = Console.ReadLine();

                switch (input[0])
                {
                case 'A':
                    initialPlaceX = 1;
                    break;

                case 'B':
                    initialPlaceX = 2;
                    break;

                case 'C':
                    initialPlaceX = 3;
                    break;

                case 'D':
                    initialPlaceX = 4;
                    break;

                case 'E':
                    initialPlaceX = 5;
                    break;

                case 'F':
                    initialPlaceX = 6;
                    break;

                case 'G':
                    initialPlaceX = 7;
                    break;

                case 'H':
                    initialPlaceX = 8;
                    break;
                }
                if (initialPlaceX == 0)
                {
                    wrong++;
                }
                string numbers = "123456789";
                initialPlaceY = numbers.IndexOf(input[1] + "") + 1;
                if (initialPlaceY == -1)
                {
                    wrong++;
                }


                switch (input[2])
                {
                case 'a':
                    chosenPlaceX = 1;
                    break;

                case 'b':
                    chosenPlaceX = 2;
                    break;

                case 'c':
                    chosenPlaceX = 3;
                    break;

                case 'd':
                    chosenPlaceX = 4;
                    break;

                case 'e':
                    chosenPlaceX = 5;
                    break;

                case 'f':
                    chosenPlaceX = 6;
                    break;

                case 'g':
                    chosenPlaceX = 7;
                    break;

                case 'h':
                    chosenPlaceX = 8;
                    break;
                }
                if (chosenPlaceX == 0)
                {
                    wrong++;
                }
                chosenPlaceY = numbers.IndexOf(input[3] + "") + 1;
                if (chosenPlaceY == -1)
                {
                    wrong++;
                }
            } while (wrong > 0);

            /* need to check if its needed
             * if (initialPlaceX > 8 || initialPlaceX < 1 || initialPlaceY > 8 || initialPlaceY < 1 || chosenPlaceX > 8 || chosenPlaceX < 1 || chosenPlaceX > 8 || chosenPlaceX < 1)
             * {
             *  Console.WriteLine("This square is not in board");
             *  choosingAct();
             * }*/
            if (board[initialPlaceX, initialPlaceY] != null && board[initialPlaceX, initialPlaceY].getIsWhite())                                                                                                                                                                         // this is white tool. user cant enter null place as initial
            {
                if (countSteps % 2 == 0)                                                                                                                                                                                                                                                 // its the white turn
                {
                    if (board[initialPlaceX, initialPlaceY].go(initialPlaceX, initialPlaceY, chosenPlaceX, chosenPlaceY, countSteps, checkIfChosenPlaceIsEmpty(chosenPlaceX, chosenPlaceY)))                                                                                             // checking if step is valid according to each tool
                    {
                        if (checkIfRowIsFree(initialPlaceX, initialPlaceY, chosenPlaceX, chosenPlaceY, board[initialPlaceX, initialPlaceY].getNmae()) && (checkIfColumnIsFree(initialPlaceX, initialPlaceY, chosenPlaceX, chosenPlaceY, board[initialPlaceX, initialPlaceY].getNmae()))) // ensure that there is no tools between initial row and chosen
                        {
                            if (board[chosenPlaceX, chosenPlaceY] != null && !(board[chosenPlaceX, chosenPlaceY].getIsWhite()))                                                                                                                                                          // the chosen place is not empty, but with negative color tool
                            {
                                if (!(ToolIsUnderThreat(findXLocationOfKing(true), findYLocationOfKing(true), true, chosenPlaceX, chosenPlaceY)))                                                                                                                                        // there is no threat on white king
                                {
                                    setChosenPlace(initialPlaceX, initialPlaceY, chosenPlaceX, chosenPlaceY);
                                }
                                else
                                {
                                    Console.WriteLine("white king is in danger!");
                                }
                            }
                            else if (board[chosenPlaceX, chosenPlaceY] == null)
                            {
                                if (!(ToolIsUnderThreat(findXLocationOfKing(true), findYLocationOfKing(true), true, chosenPlaceX, chosenPlaceY))) // there is no threat on white king
                                {
                                    setChosenPlace(initialPlaceX, initialPlaceY, chosenPlaceX, chosenPlaceY);
                                }
                                else
                                {
                                    Console.WriteLine("white king is in danger!");
                                }
                            }
                            else
                            {
                                Console.WriteLine("you can not pass on your own tools! \n");
                            }
                            if ((board[chosenPlaceX, chosenPlaceY].getNmae() == "WhitePawn") && (chosenPlaceY == 1)) // white pawn reach the first line of black and get new rule
                            {
                                bool isValid = true;
                                do
                                {
                                    Console.WriteLine("which tool do you choose for the pawn? (QUEEN / BISHOP / ROOK / KNIGHT)?");
                                    Console.WriteLine("choose and press ENTER");
                                    string choiceNewRuleForPawn = Console.ReadLine();
                                    switch (choiceNewRuleForPawn)
                                    {
                                    case "QUEEN":
                                        board[chosenPlaceX, chosenPlaceY] = new Queen("WhiteQueen", true);
                                        isValid = true;
                                        break;

                                    case "BISHOP":
                                        board[chosenPlaceX, chosenPlaceY] = new Bishop("WhiteBishop", true);
                                        isValid = true;
                                        break;

                                    case "ROOK":
                                        board[chosenPlaceX, chosenPlaceY] = new Rook("WhiteRook", true);
                                        isValid = true;
                                        break;

                                    case "KNIGHT":
                                        board[chosenPlaceX, chosenPlaceY] = new Knight("WhiteKnight", true);
                                        isValid = true;
                                        break;

                                    default:
                                        Console.WriteLine("enter a valid name of tool");
                                        isValid = false;
                                        break;
                                    }
                                } while (!isValid);
                            }
                        }
                        else
                        {
                            Console.WriteLine("sorry, there is tool in your way");
                        }
                    }
                    else
                    {
                        Console.WriteLine("this tool is not permitted to do such act\n");
                        choosingAct();
                    }
                }
            }
            else if (board[initialPlaceX, initialPlaceY] != null)                                                                                                                                                                                                                        // this is black tool, user cant enter null place
            {
                if (countSteps % 2 == 1)                                                                                                                                                                                                                                                 // its the black turn
                {
                    if (board[initialPlaceX, initialPlaceY].go(initialPlaceX, initialPlaceY, chosenPlaceX, chosenPlaceY, countSteps, checkIfChosenPlaceIsEmpty(chosenPlaceX, chosenPlaceY)))                                                                                             // checking if step is valid according to each tool
                    {
                        if (checkIfRowIsFree(initialPlaceX, initialPlaceY, chosenPlaceX, chosenPlaceY, board[initialPlaceX, initialPlaceY].getNmae()) && (checkIfColumnIsFree(initialPlaceX, initialPlaceY, chosenPlaceX, chosenPlaceY, board[initialPlaceX, initialPlaceY].getNmae()))) // ensure that there is no tools between initial row and chosen
                        {
                            if (board[chosenPlaceX, chosenPlaceY] != null && board[chosenPlaceX, chosenPlaceY].getIsWhite())                                                                                                                                                             // the chosen place is white, so the tool can go
                            {
                                if (!(ToolIsUnderThreat(findXLocationOfKing(false), findYLocationOfKing(false), false)))                                                                                                                                                                 // there is no threat on black king
                                {
                                    setChosenPlace(initialPlaceX, initialPlaceY, chosenPlaceX, chosenPlaceY);                                                                                                                                                                            // place was settled
                                }
                            }
                            else if (board[chosenPlaceX, chosenPlaceY] == null)
                            {
                                if (!(ToolIsUnderThreat(findXLocationOfKing(false), findYLocationOfKing(false), false))) // there is no threat on black king
                                {
                                    setChosenPlace(initialPlaceX, initialPlaceY, chosenPlaceX, chosenPlaceY);            // place was settled
                                }
                            }
                            else
                            {
                                Console.WriteLine("you can not pass on your own tools!");
                            }
                            if ((board[chosenPlaceX, chosenPlaceY].getNmae() == "BlackPawn") && (chosenPlaceY == 8)) // black pawn reach the first line of white and get new rule
                            {
                                bool isValid = true;
                                do
                                {
                                    Console.WriteLine("which tool do you choose for the pawn? (QUEEN / BISHOP / ROOK / KNIGHT)?");
                                    Console.WriteLine("choose and press ENTER");
                                    string choiceNewRuleForPawn = Console.ReadLine();
                                    switch (choiceNewRuleForPawn)
                                    {
                                    case "QUEEN":
                                        board[chosenPlaceX, chosenPlaceY] = new Queen("BlackQueen", false);
                                        isValid = true;
                                        break;

                                    case "BISHOP":
                                        board[chosenPlaceX, chosenPlaceY] = new Bishop("BlackBishop", false);
                                        isValid = true;
                                        break;

                                    case "ROOK":
                                        board[chosenPlaceX, chosenPlaceY] = new Rook("BlackRook", false);
                                        isValid = true;
                                        break;

                                    case "KNIGHT":
                                        board[chosenPlaceX, chosenPlaceY] = new Knight("BlackKnight", false);
                                        isValid = true;
                                        break;

                                    default:
                                        Console.WriteLine("enter a valid name of tool");
                                        isValid = false;
                                        break;
                                    }
                                } while (!isValid);
                            }
                        }
                        else
                        {
                            Console.WriteLine("sorry, there is tool in your way");
                        }
                    }
                    else
                    {
                        Console.WriteLine("this tool is not permitted to do such act\n");
                        choosingAct();
                    }
                }
            }
            else // its null place
            {
                Console.WriteLine("you entered empty place ant not tool. try again");
                choosingAct();
            }
        }