protected override void Think(ThinkBox box) { if (box.Round == 0) MakeFirstMove(box); else MakeMove(box); }
public IEnumerable<Player> Play() { var loop = new GameLoop( Board.Players, player => { using (Board.Flip(player)) return Heuristic.GetPossibleMoves(Board).Any(); }); var history = new List<IBoard>(); foreach (var player in loop) { Board.Player = player; var box = new ThinkBox(loop.Round, Board.Clone(), history.ToArray()); if (!player.MakeMove(box)) break; Board.Commit(box.Result); history.Add(box.Result); yield return player; } yield return GetResult(); }
internal override bool MakeMove(ThinkBox box) { var timeout = new CancellationTokenSource(); var task = new Task(() => Think(box), timeout.Token); task.Start(); if (!task.Wait(Timeout)) timeout.Cancel(); return null != box.Result; }
private void MakeFirstMove(ThinkBox box) { var moves = Heuristic .GetPossibleMoves(box.Board) .ToArray(); var random = new Random(); var index = random.Next(moves.Length - 1); foreach (var node in moves[index].Where(node => node.State.ToUnit(box.Board.Player) > 0)) box.Board[node.X, node.Y] = node.State; box.Result = box.Board; }
protected override void Think(ThinkBox box) { var moves = Heuristic .GetPossibleMoves(box.Board) .ToArray(); if (moves.Length > 0) { var random = new Random(); var index = random.Next(moves.Length - 1); box.Result = moves[index]; } }
private void MakeMove(ThinkBox box) { Func<IBoard, float, float, int, float> makeMove = null; var baseline = box.Board; makeMove = (position, a, b, depth) => { if (depth == maxDepth) return Score(position); var moves = Heuristic.GetPossibleMoves(position); if (!moves.Any()) return Score(position); foreach (var move in moves) { var score = move .Players .Where(player => player != move.Player) .Sum(player => { using (move.Flip(player)) return -makeMove(move, -b, -a, depth + 1); }); if (score > a) { #if DEBUG Trace.WriteLine(new BoardTraceMessage( string.Format( "a: {0} \n\r" + "s: {1} \n\r" + "b: {2} ", a, score, b), move)); #endif a = score; if (depth == 0) box.Result = move; if (b <= a) break; } } return a; }; makeMove(box.Board, float.NegativeInfinity, float.PositiveInfinity, 0); }
private void MakeMove(ThinkBox box) { var moves = Heuristic.GetPossibleMoves(box.Board); var a = float.NegativeInfinity; foreach (var move in moves) { var score = Score(move); if (score > a) { #if DEBUG Trace.WriteLine(new BoardTraceMessage( string.Format( "a: {0} \n\r" + "s: {1} \n\r", a, score), move)); #endif a = score; box.Result = move; } } }
internal override bool MakeMove(ThinkBox box) { return player.MakeMove(box); }
internal override bool MakeMove(ThinkBox box) { box.Result = box.Board; return view.MakeMove(box.Board); }
protected abstract void Think(ThinkBox box);
internal override bool MakeMove(ThinkBox box) { throw new InvalidOperationException("Null player can't make moves"); }
internal abstract bool MakeMove(ThinkBox box);