Exemple #1
0
        static void Main(string[] args)
        {
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
               var board = new Board();
            //Создаем пешек
            //for (int i = 0; i < 8; i++)
            //{
            //    new Pawn(Player.White, 1, i);
            //    new Pawn(Player.Black, 6, i);
            //}
            //new Rook(Player.White, 0, 0);
            //new Rook(Player.Black, 7, 0);
            //new Knight(Player.White, 0, 1);
            //new Knight(Player.Black, 7, 1);
            //new Bishop(Player.White, 0, 2);
            //new Bishop(Player.Black, 7, 2);
            //new Queen(Player.White, 0, 3);
            //new Queen(Player.Black, 7, 3);
            //new King(Player.White, 0, 4);
            //new King(Player.Black, 7, 4);
            //new Bishop(Player.White, 0, 5);
            //new Bishop(Player.Black, 7, 5);
            //new Knight(Player.White, 0, 6);
            //new Knight(Player.Black, 7, 6);
            //new Rook(Player.White, 0, 7);
            //new Rook(Player.Black, 7, 7);

            var g = new Game(board);
            var ai = new AI();
            var s = State.Calm;

            while (s != State.Checkmate)
            {
                PrintBoard(board.Figures);
                Console.WriteLine();

                Console.Write("Введите ваш ход({0}): ", g.Player);
                Step st;
                if (g.Player == Player.White)
                {
                    var step = Console.ReadLine();
                    st = Step.StringToStep(step);
                }
                else
                {
                    var tim = new Stopwatch();
                    tim.Start();
                    st = ai.SelectMove(g.Player, 6, board);
                    tim.Stop();
                    Console.WriteLine(tim.ElapsedMilliseconds);
                    Console.WriteLine();
                }
                try
                {
                        g.doMove(st);
                    s = g.calcState();
                }
                catch (ErrorStepExveption)
                {
                    Console.WriteLine("Не коректный ход");
                }
            }
        }
Exemple #2
0
 private int AlphaBetaWhiteWS(int alpha, int beta, int depth, Game game)
 {
     Count++;
     var max = Int32.MinValue;
     if (depth <= 0) return calculateScoreWhite(game._board, game) - calculateScoreBlack(game._board, game);
     var moves = game.getAllLegalMoves(Player.White);
     if (moves.Count() != 0)
         foreach (var x in moves)
         {
             DoMove(x, game._board);
             var tmp = AlphaBetaBlackWS(alpha, beta, depth - 1, game);
             BackMove(game._board);
             if (tmp > max) max = tmp;
             if (tmp > alpha) alpha = tmp;
             if (max >= beta) return max;
         }
     else
     {
         var state = game.calcState();
         if (state == State.Checkmate)
             return 0;
         else return -10000;
     }
     return max;
 }
Exemple #3
0
 private int AlphaBetaWhiteBS(int alpha, int beta, int depth, Game game)
 {
     Count++;
     var min = Int32.MaxValue;
     if (depth <= 0)
     {
         var state = game.calcState(Player.White);
         switch (state)
         {
             case State.Calm:
                 return calculateScoreWhite(game._board, game) - calculateScoreBlack(game._board, game);
             case State.Check:
                 return calculateScoreWhite(game._board, game) - calculateScoreBlack(game._board, game) - 300 ;
             case State.Checkmate:
                 return Int32.MinValue;
             case State.Draw:
                 return 0;
         }
     }
     var moves = game.getAllLegalMoves(Player.White);
     foreach (var x in moves)
     {
         DoMove(x, game._board);
         var tmp = AlphaBetaBlackBS(alpha, beta, depth - 1, game);
         BackMove(game._board);
         if (tmp < min) min = tmp;
         if (tmp < beta) beta = tmp;
         if (min <= alpha) return min;
     }
     return min;
 }
Exemple #4
0
 private int AlphaBetaBlackWS(int alpha, int beta, int depth, Game game)
 {
     Count++;
     var min = int.MaxValue;
     if (depth <= 0) return calculateScoreBlack(game._board, game) - calculateScoreWhite(game._board, game);
     var moves = game.getAllLegalMoves(Player.Black);
     if (moves.Count() != 0)
         foreach (var x in moves)
         {
             DoMove(x, game._board);
             var tmp = AlphaBetaWhiteWS(alpha, beta, depth - 1, game);
             BackMove(game._board);
             if (tmp < min) min = tmp;
             if (tmp < beta) beta = tmp;
             if (min <= alpha) return min;
         }
     else
     {
         var state = game.calcState();
         return state == State.Checkmate ? 0 : 10000;
     }
     return min;
 }
Exemple #5
0
 private int AlphaBetaBlackBS(int alpha, int beta, int depth, Game game)
 {
     Interlocked.Increment(ref Count);
     var max = Int32.MinValue;
     if (depth <= 0)
     {
         var state = game.calcState(Player.White);
         switch (state)
         {
             case State.Calm:
                 return calculateScoreBlack(game._board, game) - calculateScoreWhite(game._board, game);
             case State.Check:
                 return calculateScoreBlack(game._board, game)- 300-calculateScoreWhite(game._board, game);
             case State.Checkmate:
                 return Int32.MaxValue;
             case State.Draw:
                 return 0;
         }
     }
     var moves = game.getAllLegalMoves(Player.Black);
     foreach (var x in moves)
     {
         DoMove(x, game._board);
         var tmp = AlphaBetaWhiteBS(alpha, beta, depth - 1, game);
         BackMove(game._board);
         if (tmp > max) max = tmp;
         if (tmp > alpha) alpha = tmp;
         if (max >= beta) return max;
     }
     return max;
 }
Exemple #6
0
        public void FirstStepState()
        {
            var board = new Board(true);
            Queen.PrecalcStep();
            Bishop.PrecalcStep();
            Rook.PrecalcStep();
            King.preaCalc();
            Knight.preaCalc();
            //Создаем пешек
            for (var i = 0; i < 8; i++)
            {
                new Pawn(Player.White, board, 1, i);
                new Pawn(Player.Black, board, 6, i);
            }
            new Rook(Player.White, board, 0, 0);
            new Rook(Player.Black, board, 7, 0);
            new Knight(Player.White, board, 0, 1);
            new Knight(Player.Black, board, 7, 1);
            new Bishop(Player.White, board, 0, 2);
            new Bishop(Player.Black, board, 7, 2);
            new Queen(Player.White, board, 3, 3);
            new Queen(Player.Black, board, 4, 3);
            new King(Player.White, board, 0, 4);
            new King(Player.Black, board, 7, 4);
            new Bishop(Player.White, board, 0, 5);
            new Bishop(Player.Black, board, 7, 5);
            new Knight(Player.White, board, 0, 6);
            new Knight(Player.Black, board, 7, 6);
            new Rook(Player.White, board, 2, 7);
            new Rook(Player.Black, board, 5, 7);

            for (long  i = 0; i < 100000; i++)
            {
                var g = new Game(board);
                var s = g.calcState();
            }

            //            Assert.AreEqual(s, State.Calm);
        }