//выбирает ход для выбранного игрока с глубиной не более 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; }
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; }
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]; } }
//посчитать счет черных (пока не самым оптимальный способ) 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; }
public override void CopyOnOtherBoard(Board newBoard) { new Knight(Player, newBoard, X, Y); }
public Knight(Player p, Board board, int x, int y) : base(p, board, 300, x,y) { }
public Pawn(Player p, Board board, int x, int y) : base(p, board, 100, x, y) { }
public abstract void CopyOnOtherBoard(Board newBoard);
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("Не коректный ход"); } } }
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) { }
//отменяет последний ход в стеке void BackMove(Board board) { board.CanselMove(); }
//делает ход и заносит в стек информацию //о том, что необходимо для его отмены static void DoMove(Step step, Board board) { board.Move(step); }
public override void CopyOnOtherBoard(Board newBoard) { new Queen(Player, newBoard, X, Y); }
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) { }
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) { }
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) { }