Пример #1
0
        public bool moveTool(int row, int column, int row2, int column2, string[,] tools)
        {
            bool move = true;

            switch (tools[row, column])
            {
            case "WP": WPion wp = new WPion("WP", row2, column2);
                move = wp.moveTool(wp, row, column, row2, column2, tools); break;

            case "BP": BPion bp = new BPion("BP", row2, column2);
                move = bp.moveTool(bp, row, column, row2, column2, tools); break;

            case "BR":
            case "WR": R r = new R("R", row2, column2);
                move = r.moveTool(r, row, column, row2, column2, tools); break;

            case "BN":
            case "WN": N n = new N("N", row2, column2);
                move = n.moveTool(n, row, column, row2, column2, tools); break;

            case "BB":
            case "WB": B b = new B("B", row2, column2);
                move = b.moveTool(b, row, column, row2, column2, tools); break;

            case "BK":
            case "WK": K k = new K("K", row2, column2);
                move = k.moveTool(k, row, column, row2, column2, tools); break;

            case "BQ":
            case "WQ": Q q = new Q("Q", row2, column2);
                move = q.moveTool(q, row, column, row2, column2, tools); break;

            case "--":
            case " ":
            case "A":
            case "B":
            case "C":
            case "D":
            case "E":
            case "F":
            case "G":
            case "H":
            case "1":
            case "2":
            case "3":
            case "4":
            case "5":
            case "6":
            case "7":
            case "8":
            case "9":

                Empty e = new Empty("--", row2, column2);
                move = e.moveTool(e, row, column, row2, column2, tools); break;
            }
            return(move);
        }
Пример #2
0
        static void Main(string[] args)
        {
            string[,] tools = new string[10, 10];
            tools           = setBoard(tools);

            string[,] tools2 = new string[10, 10]; //מערך לבדיקות

            string[][,] n = new string[1][, ];     //מערך לבדיקת לוח חוזר 3 פעמים
            n[0]          = toolsForCheking(tools);

            bool tie = false;
            bool move;
            bool whiteCoronation, blackCoronation;
            bool shahVsBlack, shahVsWhite;
            bool whiteMistake, blackMistake;
            bool math;
            bool hatzraha = false;
            bool wr1Moved = false, wr8Moved = false, br1Moved = false, br8Moved = false,
                 wkMoved = false, bkMoved = false;
            bool   goingRight;
            int    PionBlackRow = 0;
            int    PionBlackColumn = 0;
            bool   optionToHakhaVsBlack = false, optionToHakhaVsWhite = false;
            bool   attendToHakhaVsBlack, attendToHakhaVsWhite;
            int    PionWhiteRow = 0, PionWhiteColumn = 0;
            string messageRow = "whice row do you want to move your object ? ";
            string messageColumn = "whice column do you want to move your object ?";
            int    row, row2, column, column2;
            int    count       = 0;
            int    countTill50 = 0;
            bool   noPlayerCanDoMath;
            bool   sameBoardThreeTimes;
            Tools  tool;

            printBoard(tools);

            while (!tie)
            {
                Console.WriteLine("{0} turn, from " + messageRow
                                  , count % 2 == 0 ? "White" : "Black");//בחירת משבצת מוצא
                row = chekRowInput(Console.ReadLine());
                Console.WriteLine("From " + messageColumn);
                column = changeColumn(Console.ReadLine());
                move   = playOnlyWithYourTools(tools[row, column], count);//בדיקה
                if (!move)
                {
                    Console.WriteLine("You can't touce your opponent tools");
                    Console.WriteLine();
                    continue;
                }

                move = chekEmptySpace(tools[row, column]);//בדיקה
                if (!move)
                {
                    Console.WriteLine("You can not pick an empty space");
                    Console.WriteLine();
                    continue;
                }

                tool = new Tools(tools[row, column], row, column);

                Console.WriteLine("To " + messageRow); //בחירת משבצת תנועה
                row2 = chekRowInput(Console.ReadLine());
                Console.WriteLine("To " + messageColumn);
                column2 = changeColumn(Console.ReadLine());


                move = dontEatYoyrslef(tools[row, column], tools[row2, column2]);//בדיקה
                if (!move)
                {
                    Console.WriteLine("You can't eat yourself");
                    continue;
                }


                if (tools[row, column][1] == 'K' && row == row2 && (column2 == column + 2 ||
                                                                    column2 == column - 2))//האם נעשה ניסיון להצרחה?
                {
                    hatzraha = true;
                }

                if (tools[row, column] == "WP" && row2 == PionBlackRow &&
                    column2 == PionBlackColumn && optionToHakhaVsBlack)
                {
                    attendToHakhaVsBlack = true;//האם נעשה ניסיון להכאה דרך הילוכו?
                }
                else
                {
                    attendToHakhaVsBlack = false;
                }

                if (tools[row, column] == "BP" && row2 == PionWhiteRow &&
                    column2 == PionWhiteColumn && optionToHakhaVsWhite)
                {
                    attendToHakhaVsWhite = true;//האם נעשה ניסיון להכאה דרך הילוכו?
                }
                else
                {
                    attendToHakhaVsWhite = false;
                }


                if (!hatzraha && !attendToHakhaVsBlack && !attendToHakhaVsWhite)
                {                                                            //רק אם אין ניסיון  להצרחה או הכאה דרך הילוכו יש לבדוק את תזוזת הכלי
                    move = tool.moveTool(row, column, row2, column2, tools); //האם הכלי זז כחוק
                    if (!move)
                    {
                        Console.WriteLine("Illegal move ");
                        continue;
                    }
                }

                if (count % 2 == 0)//האם הלבן מכניס את עצמו לשח או לא יוצא משח?
                {
                    tools2       = toolsForCheking(tools);
                    tools2       = changeBoard(tools2, row, column, row2, column2);
                    whiteMistake = isThereShah(tools2, "WK", count);
                    if (whiteMistake)
                    {
                        Console.WriteLine("You can't get your self in to a Shah spot !");
                        continue;
                    }
                }

                else if (count % 2 != 0)//האם השחור מכניס את עצמו לשח או לא יוצא משח?
                {
                    tools2       = toolsForCheking(tools);
                    tools2       = changeBoard(tools2, row, column, row2, column2);
                    blackMistake = isThereShah(tools2, "BK", count);
                    if (blackMistake)
                    {
                        Console.WriteLine("You can't get your self in to a Shah spot !");
                        continue;
                    }
                }

                if (attendToHakhaVsBlack)//אכילת כלי שחור בהכאה דרך הילוכו
                {
                    tools[row2 - 1, column2] = "--";
                }


                if (attendToHakhaVsWhite)//אכילת כלי לבן בהכאה דרך הילוכו
                {
                    tools[row2 + 1, column2] = "--";
                }


                goingRight = column < column2 ? true : false; //משתנה עזר להצרחה


                if (hatzraha)//האם ההצרחה אפשרית בכלל
                {
                    bool king = count % 2 == 0 ? wkMoved : bkMoved;
                    bool r;
                    if (goingRight)
                    {
                        r = count % 2 == 0 ? wr8Moved : br8Moved;
                    }
                    else
                    {
                        r = count % 2 == 0 ? wr1Moved : br1Moved;
                    }

                    hatzraha = isHatzrahaPossible(tools, row, column, row2, column2, king,
                                                  r, count);

                    if (!hatzraha)
                    {
                        Console.WriteLine("Illegal move");
                        continue;
                    }
                    if (hatzraha)
                    {
                        tools = changeBoard(tools, row, column, row2, column2); //מזיז מלך

                        if (count % 2 == 0 && !wkMoved)                         //עדכון שהמלך זז
                        {
                            wkMoved = haveBeenMoved("WK", tools[row2, column2]);
                        }

                        else if (count % 2 != 0 && !bkMoved)//עדכון שהמלך זז
                        {
                            bkMoved = haveBeenMoved("BK", tools[row2, column2]);
                        }

                        if (goingRight)//עדכון טורים כדי שיזוז גם הצריח
                        {
                            column  = 8;
                            column2 = 5;
                        }
                        else
                        {
                            column  = 1;
                            column2 = 3;
                        }
                    }
                }


                if (tools[row2, column2] != "--" || tools[row, column][1] == 'P')//ספירת 50 מסעים
                {
                    countTill50 = 0;
                }
                else
                {
                    countTill50++;
                }


                tools = changeBoard(tools, row, column, row2, column2);//עידכון לוח והזזת כלי

                //בשלב זה הצעד עבר את כל הבדיקות ובוצע


                string[][,] m;
                m = new string[n.Length + 1][, ];//מערך שאוגר בתוכו את כל הלוחות

                for (int i = 0; i < n.Length; i++)
                {
                    m[i] = toolsForCheking(n[i]);
                }
                m[n.Length] = toolsForCheking(tools);

                n = new string[m.Length][, ];

                for (int i = 0; i < m.Length; i++) //מערך נוסף שאוגר בתוכו את כל הלוחות
                {
                    n[i] = toolsForCheking(m[i]);
                }
                Console.WriteLine(m.Length);

                sameBoardThreeTimes = sameBoard3Times(m, count);//האם אותו לוח מופיע פעם שלישית?
                if (sameBoardThreeTimes)
                {
                    printBoard(tools);
                    Console.WriteLine("Same board 3 times, its a tie");
                    break;
                }


                if (countTill50 == 50)//50 מסעים ללא הכאה או תזוזת רגלי המשחק נגמר בתיקו
                {
                    printBoard(tools);
                    Console.WriteLine("Its a tie (50)");
                    break;
                }

                optionToHakhaVsBlack = false; //ביטול אופציה להכאה דרך הילוכו
                optionToHakhaVsWhite = false; //ביטול אופציה להכאה דרך הילוכו


                if (count % 2 == 0 && !wkMoved)//האם צריחים ומלכים זזו
                {
                    wkMoved = haveBeenMoved("WK", tools[row2, column2]);
                }

                if (count % 2 != 0 && !bkMoved)
                {
                    bkMoved = haveBeenMoved("BK", tools[row2, column2]);
                }

                if (column == 1 && !wr1Moved)
                {
                    wr1Moved = haveBeenMoved("WR", tools[row2, column2]);
                }

                if (column == 1 && !br1Moved)
                {
                    br1Moved = haveBeenMoved("BR", tools[row2, column2]);
                }

                if (column == 8 && !wr8Moved)
                {
                    wr8Moved = haveBeenMoved("WR", tools[row2, column2]);
                }

                if (column == 8 && !br8Moved)
                {
                    br8Moved = haveBeenMoved("BR", tools[row2, column2]);
                }


                whiteCoronation = chekinCoronationWhite(tools);//הכתרה לבנה
                if (whiteCoronation)
                {
                    WPion wp = new WPion("WP", row2, column2);
                    tools[row2, column2] = wp.coronationWhite();
                }

                blackCoronation = chekinCoronationBlack(tools);//הכתרה שחורה
                if (blackCoronation)
                {
                    BPion bp = new BPion("BP", row2, column2);
                    tools[row2, column2] = bp.coronationBlack();
                }


                noPlayerCanDoMath = noPlayerCanDoMath1(tools);
                if (noPlayerCanDoMath)//תיקו - אף שחקן אינו יכול לבצע מט
                {
                    noPlayerCanDoMath = noPlayerCanDoMath2(tools);
                    if (noPlayerCanDoMath)
                    {
                        printBoard(tools);
                        Console.WriteLine("Its a tie, no player can do math");
                        break;
                    }
                }

                printBoard(tools);//הדפסת לוח


                count++;


                shahVsWhite = isThereShah(tools, "WK", count);//בדיקת שח נגד לבן
                if (shahVsWhite)
                {
                    math = isThereMath(tools, count);//מט
                    if (math)
                    {
                        Console.WriteLine("Game Over, Black Wins !");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Shah vs white king");
                    }
                }

                shahVsBlack = isThereShah(tools, "BK", count);//בדיקת שח נגד שחור
                if (shahVsBlack)
                {
                    math = isThereMath(tools, count);//מט
                    if (math)
                    {
                        Console.WriteLine("Game Over, White Wins !");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Shah vs black king");
                    }
                }

                tie = isThereMath(tools, count);//תיקו פט
                if (tie)
                {
                    Console.WriteLine("Game over, its a tie (pat)");
                    break;
                }

                hatzraha = false;

                if (tools[row2, column2] == "BP" && row2 - row == -2)//האם יש אופציה להכאה דרך הילוכו?
                {
                    PionBlackRow         = row - 1;
                    PionBlackColumn      = column;
                    optionToHakhaVsBlack = true;
                }
                if (tools[row2, column2] == "WP" && row2 - row == 2)//האם יש אופציה להכאה דרך הילוכו?
                {
                    PionWhiteRow         = row + 1;
                    PionWhiteColumn      = column;
                    optionToHakhaVsWhite = true;
                }
            }
        }