Exemplo n.º 1
0
 protected override void Think(ThinkBox box)
 {
     if (box.Round == 0)
     MakeFirstMove(box);
      else
     MakeMove(box);
 }
Exemplo n.º 2
0
        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();
        }
Exemplo n.º 3
0
        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;
        }
Exemplo n.º 4
0
        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;
        }
Exemplo n.º 5
0
        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];
             }
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
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;
     }
      }
 }
Exemplo n.º 8
0
 internal override bool MakeMove(ThinkBox box)
 {
     return player.MakeMove(box);
 }
Exemplo n.º 9
0
 internal override bool MakeMove(ThinkBox box)
 {
     box.Result = box.Board;
      return view.MakeMove(box.Board);
 }
Exemplo n.º 10
0
 protected abstract void Think(ThinkBox box);
Exemplo n.º 11
0
 internal override bool MakeMove(ThinkBox box)
 {
     throw new InvalidOperationException("Null player can't make moves");
 }
Exemplo n.º 12
0
 internal abstract bool MakeMove(ThinkBox box);