Exemplo n.º 1
0
        private static void Main()
        {
            //RunTest();
            //return;
            var board = new BoardOfChess();


            board.ReadFromConsole();

            bool isShah = false, isMat = false, isPat = false;

            isMat  = board.CheckMat(ColorFigure.White);
            isPat  = board.CheckPat(ColorFigure.White);
            isShah = board.CheckShah(ColorFigure.White);


            if (isMat)
            {
                Console.Write("mate");
            }
            else if (isPat)
            {
                Console.Write("stalemate");
            }
            else
            {
                Console.Write(isShah ? "check" : "ok");
            }
        }
Exemplo n.º 2
0
        private static void RunTest()
        {
            var board           = new BoardOfChess();
            var tests           = new List <List <Figure> >();
            var correct         = new List <bool[]>();
            var outdate         = new List <bool[]>();
            var dontCorrectTest = new List <int>();

            //result :Mat,Pat,Shah

            //Test0
            tests.Add(new List <Figure>
            {
                new King(new Cell(0, 4), ColorFigure.White, board),
                new Rook(new Cell(0, 0), ColorFigure.Black, board),
                new Rook(new Cell(1, 1), ColorFigure.Black, board),
                new Rook(new Cell(7, 0), ColorFigure.White, board),
            });
            //Result
            correct.Add(new[] { false, false, true });

            //Test1
            tests.Add(new List <Figure>
            {
                new King(new Cell(0, 4), ColorFigure.White, board),
                new Rook(new Cell(0, 0), ColorFigure.Black, board),
                new Rook(new Cell(1, 1), ColorFigure.Black, board),
                new Rook(new Cell(7, 3), ColorFigure.White, board),
            });
            //Result
            correct.Add(new[] { false, false, true });

            //Test2
            tests.Add(new List <Figure>
            {
                new King(new Cell(0, 4), ColorFigure.White, board),
                new Rook(new Cell(0, 0), ColorFigure.Black, board),
            });
            //Result
            correct.Add(new[] { false, false, true });

            //Test3
            tests.Add(new List <Figure>
            {
                new Rook(new Cell(0, 0), ColorFigure.White, board),
                new Nag(new Cell(1, 0), ColorFigure.White, board),
                new Bishop(new Cell(2, 0), ColorFigure.White, board),
                new King(new Cell(3, 0), ColorFigure.White, board),
                new Quine(new Cell(4, 0), ColorFigure.White, board),
                new Bishop(new Cell(5, 0), ColorFigure.White, board),
                new Nag(new Cell(6, 0), ColorFigure.White, board),
                new Rook(new Cell(7, 0), ColorFigure.White, board),
                new Rook(new Cell(0, 7), ColorFigure.Black, board),
                new Nag(new Cell(1, 7), ColorFigure.Black, board),
                new Bishop(new Cell(2, 7), ColorFigure.Black, board),
                new King(new Cell(3, 7), ColorFigure.Black, board),
                new Quine(new Cell(4, 7), ColorFigure.Black, board),
                new Bishop(new Cell(5, 7), ColorFigure.Black, board),
                new Nag(new Cell(6, 7), ColorFigure.Black, board),
                new Rook(new Cell(7, 7), ColorFigure.Black, board),
            });
            //Result
            correct.Add(new[] { false, false, false });

            //Test4
            tests.Add(new List <Figure>
            {
                new King(new Cell(0, 0), ColorFigure.White, board),
                new Rook(new Cell(7, 0), ColorFigure.Black, board),
                new Rook(new Cell(6, 1), ColorFigure.Black, board),
                new King(new Cell(7, 7), ColorFigure.Black, board),
            });
            //Result
            correct.Add(new[] { true, false, true });

            //Test5
            tests.Add(new List <Figure>
            {
                new King(new Cell(0, 0), ColorFigure.White, board),
                new Rook(new Cell(7, 1), ColorFigure.Black, board),
                new Rook(new Cell(1, 7), ColorFigure.Black, board),
                //new King(new Cell(7, 7), ColorFigure.Black, board),
            });
            //Result
            correct.Add(new[] { false, true, false });

            //Test6
            tests.Add(new List <Figure>
            {
                new King(new Cell(0, 0), ColorFigure.White, board),
                new Quine(new Cell(1, 1), ColorFigure.White, board),
                new Rook(new Cell(7, 1), ColorFigure.Black, board),
                new Rook(new Cell(1, 7), ColorFigure.Black, board),
                //new King(new Cell(7, 7), ColorFigure.Black, board),
            });
            //Result
            correct.Add(new[] { false, false, false });

            //Test7
            tests.Add(new List <Figure>
            {
                new King(new Cell(0, 0), ColorFigure.White, board),
                new Quine(new Cell(1, 1), ColorFigure.Black, board),
                new Rook(new Cell(7, 1), ColorFigure.Black, board),
                new Rook(new Cell(1, 7), ColorFigure.Black, board),
                //new King(new Cell(7, 7), ColorFigure.Black, board),
            });
            //Result
            correct.Add(new[] { true, false, true });

            //Test8
            tests.Add(new List <Figure>
            {
                new King(new Cell(0, 0), ColorFigure.White, board),
                new Bishop(new Cell(2, 2), ColorFigure.White, board),
                new Quine(new Cell(1, 1), ColorFigure.Black, board),
                new Rook(new Cell(7, 1), ColorFigure.Black, board),
                new Rook(new Cell(1, 7), ColorFigure.Black, board),
                //new King(new Cell(7, 7), ColorFigure.Black, board),
            });
            //Result
            correct.Add(new[] { false, false, true });

            //Test9
            tests.Add(new List <Figure>
            {
                new King(new Cell(1, 0), ColorFigure.White, board),
                new Bishop(new Cell(1, 1), ColorFigure.White, board),
                new Quine(new Cell(0, 7), ColorFigure.Black, board),
                new Rook(new Cell(1, 7), ColorFigure.Black, board),
                new Rook(new Cell(2, 7), ColorFigure.Black, board),
                //new King(new Cell(7, 7), ColorFigure.Black, board),
            });
            //Result
            correct.Add(new[] { false, true, false });

            //Test10 Мат двумя слонами
            tests.Add(new List <Figure>
            {
                new King(new Cell(0, 0), ColorFigure.White, board),
                new King(new Cell(1, 2), ColorFigure.Black, board),
                new Bishop(new Cell(2, 2), ColorFigure.Black, board),
                new Bishop(new Cell(3, 2), ColorFigure.Black, board),
            });
            //Result
            correct.Add(new[] { true, false, true });

            //Test11 Мат двумя слонами
            tests.Add(new List <Figure>
            {
                new King(new Cell(0, 1), ColorFigure.White, board),
                new King(new Cell(2, 1), ColorFigure.Black, board),
                new Bishop(new Cell(1, 1), ColorFigure.Black, board),
                new Bishop(new Cell(2, 3), ColorFigure.Black, board),
            });
            //Result
            correct.Add(new[] { true, false, true });

            //Test12 Пат двумя слонами и королем
            tests.Add(new List <Figure>
            {
                new King(new Cell(0, 0), ColorFigure.White, board),
                new King(new Cell(1, 2), ColorFigure.Black, board),
                new Bishop(new Cell(3, 1), ColorFigure.Black, board),
                new Bishop(new Cell(3, 2), ColorFigure.Black, board),
            });
            //Result
            correct.Add(new[] { false, true, false });

            //Test13 Пат двумя слонами
            tests.Add(new List <Figure>
            {
                new King(new Cell(0, 1), ColorFigure.White, board),
                new King(new Cell(2, 1), ColorFigure.Black, board),
                new Bishop(new Cell(1, 1), ColorFigure.Black, board),
                new Bishop(new Cell(3, 2), ColorFigure.Black, board),
            });
            //Result
            correct.Add(new[] { false, true, false });

            //Test14 Пат двумя слонами
            tests.Add(new List <Figure>
            {
                new King(new Cell(2, 0), ColorFigure.White, board),
                new King(new Cell(0, 1), ColorFigure.Black, board),
                new Bishop(new Cell(2, 2), ColorFigure.Black, board),
                new Bishop(new Cell(1, 2), ColorFigure.Black, board),
            });
            //Result
            correct.Add(new[] { false, true, false });

            //Test14 Пат двумя слонами
            tests.Add(new List <Figure>
            {
                new King(new Cell(1, 0), ColorFigure.White, board),
                new King(new Cell(0, 2), ColorFigure.Black, board),
                new Bishop(new Cell(1, 1), ColorFigure.Black, board),
                new Bishop(new Cell(1, 2), ColorFigure.Black, board),
            });
            //Result
            correct.Add(new[] { false, true, false });
            //Test15 шах конем
            tests.Add(new List <Figure>
            {
                new King(new Cell(4, 5), ColorFigure.White, board),
                new Nag(new Cell(3, 3), ColorFigure.Black, board)
            });
            //Result
            correct.Add(new[] { false, false, true });

            //Test16 шах конем
            tests.Add(new List <Figure>
            {
                new King(new Cell(4, 5), ColorFigure.White, board),
                new Nag(new Cell(5, 7), ColorFigure.Black, board)
            });
            //Result
            correct.Add(new[] { false, false, true });
            //Test16 мат конем
            tests.Add(new List <Figure>
            {
                new King(new Cell(0, 0), ColorFigure.White, board),
                new Rook(new Cell(1, 0), ColorFigure.White, board),
                new Bishop(new Cell(0, 1), ColorFigure.White, board),
                new Bishop(new Cell(1, 1), ColorFigure.White, board),
                new Nag(new Cell(2, 1), ColorFigure.Black, board)
            });
            //Result
            correct.Add(new[] { true, false, true });

            //Тесторование
            for (int i = 0; i < tests.Count; i++)
            {
                Console.Out.WriteLine(String.Format("Test:{0}", i));
                board.AddFigures(tests[i]);
                Console.Out.WriteLine(board.ToString());


                bool isMat  = board.CheckMat(ColorFigure.White);
                bool isPat  = board.CheckPat(ColorFigure.White);
                bool isShah = board.CheckShah(ColorFigure.White);

                outdate.Add(new[] { isMat, isPat, isShah });

                bool result = (isMat == correct[i][0] && isPat == correct[i][1] && isShah == correct[i][2]);
                if (result == false)
                {
                    dontCorrectTest.Add(i);
                }
                board.RemoveAllFigures();
            }
            //Итог
            if (dontCorrectTest.Count == 0)
            {
                Console.WriteLine("OKEY!!!");
            }
            else
            {
                dontCorrectTest.ForEach(
                    er => Console.WriteLine(
                        "ErrorTest:{0}.Mat:{1}({2}).Pat{3}({4}).Shah{5}({6})",
                        er,
                        outdate[er][0],
                        correct[er][0],
                        outdate[er][1],
                        correct[er][1],
                        outdate[er][2],
                        correct[er][2]));
            }
            Console.ReadLine();
        }