示例#1
0
 public override Bits[] Apply(Bits[] position, Game g)
 {
     var result = new Bits[position.Length];
     Array.Copy (position, result, position.Length);
     // Remove the piece from initial position
     var pI = g.PieceIndex (c, movedIndex);
     result[pI] ^= square;
     // Put it on new position, possibly promoting
     if (promo) {
         var pPI = g.PromotedIndex (c, movedIndex);
         result [pPI] ^= move;
     }
     else
         result [g.PieceIndex (c, movedIndex)] ^= move;
     // If a piece is captured update hand and enemy piece
     if (capturedIndex >= 0) {
         // Get the correct indices
         var pEI = g.PieceIndex (c^1, capturedIndex);
         var hI = g.HandIndex (c);
         // Update the bitboards
         result [pEI] ^= (move);
         var mask = g.handMask[g.demote[capturedIndex]];
         B.PushMasked(ref result[hI], mask);
     }
     return result;
 }
示例#2
0
        public override Bits[] Apply(Bits[] position, Game g)
        {
            //Create a new set of bits
            var result = new Bits[position.Length];
            Array.Copy (position, result, position.Length);

            // Drop the piece in question by setting the location bit
            result [g.PieceIndex  (c, dropIndex)] |= location;

            // Remove the piece from the player's hand
            Bits mask = g.handMask[dropIndex];
            var handIndex = g.HandIndex (c);
            result[handIndex] = result[handIndex];
            B.PopMasked(ref result[handIndex], mask);

            return result;
        }
示例#3
0
 public static void Main(string[] args)
 {
     GameSetup setup = new GameSetup (4, 3);
     setup.SetPromotionRanks (1);
     //setup.AddWhitePiece (0, 0, Type.Bishop);
     setup.AddWhitePiece (1, 0, Type.King);
     setup.AddWhitePiece (2, 0, Type.Rook);
     //setup.AddWhitePiece (1, 1, Type.Pawn);
     //setup.AddBlackPiece (2, 3, Type.Bishop);
     setup.AddBlackPiece (1, 3, Type.King);
     setup.AddBlackPiece (0, 3, Type.Rook);
     //setup.AddBlackPiece (1, 2, Type.Pawn);
     Game g = new Game(setup);
     Console.WriteLine(g.prettyPrint(g.startingPos));
     var pn = new PNSearch (false, 15);
     pn.Prove (g);
     var bestgame = pn.BestGame ();
     foreach (var pos in bestgame)
     {
         Console.WriteLine(g.prettyPrint(pos));
     }
 }
示例#4
0
        public void Prove(Game g)
        {
            // Clear the transposition table and queue
            transposition.Clear();
            queue.Clear();

            // Evaluate root and add it to transposition table
            root = new BNode(g.startingPos, 1);
            root.Evaluate (g);
            transposition[root] = root;
            queue.Enqueue(root);
            BNode next;
            int count = 0;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            while (root.value == 0 && queue.Count > 0)
            {
                next = queue.Dequeue();
                next.Expand(g, transposition, queue);
                BNode.InitiateVisiting();
                next.Update(g);

                if (sw.ElapsedMilliseconds > timeLimit * 60000)
                    break;

                /*if(count % 1000 == 0) {
                    Console.WriteLine("Value:         " + n.value);
                    Console.WriteLine("Transposition: " + BNode.transposition.Count);
                    Console.WriteLine("Queue:         " + BNode.queue.Count);
                }*/
                count++;
            }
            sw.Stop();
            timeSpent = (int) sw.ElapsedMilliseconds;

            /*Console.WriteLine((root.value==1?"Black":root.value==-1?"White":"Nobody") + " won!");
            Console.WriteLine("Transposition: " + transposition.Count);
            Console.WriteLine("Queue:         " + queue.Count);*/
        }
示例#5
0
 public void Evaluate(Game g)
 {
     int pos = g.gamePosition(position, c);
     if (pos > 0)
     {
         pn = -1 * (pos - 2) * Int32.MaxValue;
         dn = (pos - 1) * Int32.MaxValue;
     }
     else
     {
         pn = 1;
         dn = 1;
     }
 }
示例#6
0
 public void Show(Game g)
 {
     Console.WriteLine(g.prettyPrint(position));
     if (children != null)
         foreach (var child in children)
             child.Show(g);
 }
示例#7
0
        public void ExpandTree(Game g, ref int nodeCount, ref int collisionCount, Hashtable transposition)
        {
            if(children != null)
                return;
            children = new List<Node>();
            var plies = g.children(position, c);
            foreach (var ply in plies)
            {
                var s = new Node(ply.Apply(position, g), (byte)(c ^ 1));
                s.Evaluate(g);
                nodeCount++;

                if (transposition.ContainsKey(s))
                    collisionCount++;
                else
                    transposition.Add(s, s);

                s.parents.Add(this);
                children.Add(s);
            }
        }
示例#8
0
 public void Expand(Game g, Hashtable transposition)
 {
     if(children != null)
         return;
     children = new List<Node>();
     var plies = g.children(position, c);
     foreach (var ply in plies)
     {
         var s = new Node(ply.Apply(position, g), (byte)(c ^ 1));
         if (transposition.ContainsKey(s))
             s = (Node)transposition[s];
         else
         {
             s.Evaluate(g);
             transposition[s] = s;
         }
         s.parents.Add(this);
         children.Add(s);
     }
 }
示例#9
0
        public void Prove(Game g)
        {
            root = new Node (g.startingPos, 1);
            root.Evaluate (g);
            transposition [root] = root;
            nodeCount++;
            Node lastExpanded = null;
            int lastpn = 0, lastdn = 0;

            int count = 0;

            Stopwatch sw = new Stopwatch ();
            sw.Start ();
            while (root.pn != 0 && root.dn != 0) {
                var mpn = MostProving (root);

                if (graph) {
                    if (System.Object.ReferenceEquals (mpn, lastExpanded) && lastpn == mpn.pn && lastdn == mpn.dn) {
                        sw.Stop ();
                        timeSpent = (int)sw.ElapsedMilliseconds;
                        throw new Exception ("Loop in finding most proving");
                    }
                    mpn.Expand (g, transposition);
                    mpn.StartUpdate ();

                    lastExpanded = mpn;
                    lastpn = mpn.pn;
                    lastdn = mpn.dn;
                } else {
                    mpn.ExpandTree (g, ref  nodeCount, ref collisionCount, transposition);
                    mpn.StartUpdateTree (firstRun);

                    if (count % 10000 == 0)
                    {
                        Console.WriteLine("PN:         {0}\nDN:         {1}", root.pn, root.dn);
                        Console.WriteLine("Collisions: " + collisionCount);
                        Console.WriteLine("Nodes:      " + nodeCount);
                    }

                    count++;
                }

                if (sw.ElapsedMilliseconds > timeLimit * 60000) {
                    sw.Stop ();
                    timeSpent = (int)sw.ElapsedMilliseconds;
                    throw new Exception ("Time limit exceeded");
                }
            }

            sw.Stop ();
            timeSpent = (int)sw.ElapsedMilliseconds;

            // If this is the first run of a graph search where black loses, run again to check for draw
            if (!graph && firstRun && root.dn == 0) {
                second = new PNSearch (false, timeLimit);
                second.firstRun = false;
                second.Prove (g);
            }
        }
示例#10
0
 /// <summary>
 /// Apply this ply to the <para>position</para> according to game <para>g</para>.
 /// </summary>
 /// <param name='position'>
 /// The position from which this ply is possible
 /// </param>
 /// <param name='g'>
 /// The game containing all the rules.
 /// </param>
 public abstract Bits[] Apply(Bits[] position, Game g);