示例#1
0
 //выбирает ход для выбранного игрока с глубиной не более maxDeep
 public Step SelectMove(Player p, int maxDeep, Board board)
 {
     Count = 0;
     _maxDeep = maxDeep;
     _threadsResults.Clear();
     var nBoard = new Board(board);
     var game = new Game(nBoard);
     var moves = game.getAllLegalMoves(p);
     var b = moves.Batch(moves.Count/ThreadCount).ToList();
     var thraads = new Thread[b.Count];
     var n = 0;
     foreach (var curMoves in b)
     {
         var args = new TheadArg
         {
             board = new Board(board),
             startPlayer = p,
             steps = curMoves.ToList()
         };
         var thread = new Thread(Process) {IsBackground = true};
         thread.Start(args);
         thraads[n++] = thread;
     }
     while (thraads.Any(x => x.IsAlive))
     {
         Thread.Sleep(100);
     }
     var res = _threadsResults.MaxBy(x => x.Score);
     return res.Step;
 }
示例#2
0
 protected Figure(Player player, Board board, int cost, int x, int y)
 {
     _player = player;
     _board = board;
     Cost = cost;
     X = x;
     Y = y;
     _board[(x << 3) + y] = this;
 }
示例#3
0
 public Game(Board board)
 {
     _board = board;
     Player = Player.White;
     for (var i = 0; i < 8; i++)
         for (var j = 0; j < 8; j++)
             if (_board[(i << 3) + j] is King)
             {
                 if (_board[(i << 3) + j].Player == Player.White)
                     _whiteKing = _board[(i << 3) + j];
                 else _blackKing = _board[(i << 3) + j];
             }
 }
示例#4
0
        //посчитать счет черных (пока не самым оптимальный способ)
        static int calculateScoreBlack(Board board, Game game)
        {
            var sum = 0;
            var enemys = new List<Figure>();
            foreach (var figure in board.Figures)
            {
                if (figure != null && figure.Player == Player.White)
                    enemys.Add(figure);
            }
            foreach (var figure in board.Figures)
            {
                if (figure != null && figure.Player == Player.Black)
                {
                    sum += figure.Cost;
                    var attCount = 0;
                    for (int i = 0; i < enemys.Count; i++)
                    {
                        if (figure.AttackTarget(enemys[i]))
                            attCount++;
                    }
                    sum += (attCount - 1) * ((int)(figure.Cost * 0.3));
                    if (figure is Queen || figure is Bishop || figure is Knight)
                    {
                        if ((figure.X == 4 || figure.X == 5) && (figure.Y == 5 || figure.Y == 4))
                            sum += (int)(figure.X * 1.2);
                    }
                    if (figure is Pawn)
                    {

                    }
                }
            }
            var kingAtt = game.KingAlert(Player.Black);
            if (kingAtt == 1)
                sum -= 400;
            else if (kingAtt > 1)
                sum -= 800;
            return sum;
        }
示例#5
0
 public override void CopyOnOtherBoard(Board newBoard)
 {
     new Knight(Player, newBoard, X, Y);
 }
示例#6
0
 public Knight(Player p, Board board, int x, int y)
     : base(p, board, 300, x,y)
 {
 }
示例#7
0
 public Pawn(Player p, Board board, int x, int y)
     : base(p, board, 100, x, y)
 {
 }
示例#8
0
 public abstract void CopyOnOtherBoard(Board newBoard);
示例#9
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("Не коректный ход");
                }
            }
        }
示例#10
0
        static StepFromPosition[] pSteps = new StepFromPosition[64]; //препросчитанные шаги

        #endregion Fields

        #region Constructors

        public Rook(Player p, Board board, int x, int y)
            : base(p, board, 500, x, y)
        {
        }
示例#11
0
 //отменяет последний ход в стеке
 void BackMove(Board board)
 {
     board.CanselMove();
 }
示例#12
0
 //делает ход и заносит в стек информацию
 //о том, что необходимо для его отмены
 static void DoMove(Step step, Board board)
 {
     board.Move(step);
 }
示例#13
0
 public override void CopyOnOtherBoard(Board newBoard)
 {
     new Queen(Player, newBoard, X, Y);
 }
示例#14
0
        static StepFromPosition[] pSteps = new StepFromPosition[64]; //препросчитанные шаги

        #endregion Fields

        #region Constructors

        public Queen(Player p, Board board, int x, int y)
            : base(p, board, 900, x, y)
        {
        }
示例#15
0
        static HashSet<int>[] pSteps = new HashSet<int>[64]; //препросчитанные шаги

        #endregion Fields

        #region Constructors

        public King(Player p, Board board, int x, int y)
            : base(p, board, 10000, x, y)
        {
        }
示例#16
0
        static StepFromPosition[] pSteps = new StepFromPosition[64]; //препросчитанные шаги

        #endregion Fields

        #region Constructors

        public Bishop(Player p, Board board, int x, int y)
            : base(p, board, 300, x, y)
        {
        }