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);*/ }
public void Browse(Game g) { Console.WriteLine ("Current position with {0} to move:", c == 1 ? "Black" : "White"); Console.WriteLine (g.prettyPrint (position)); Console.WriteLine ("{0} wins", value == 1 ? "Black" : value == -1 ? "White" : "Nobody"); Console.WriteLine ("{0} possible moves:", children.Count); foreach (var child in children) { Console.WriteLine ("Value: " + child.value); Console.WriteLine (g.prettyPrint (child.position)); } Console.WriteLine ("{0} parents:", parents.Count); foreach (var parent in parents) { Console.WriteLine ("Value: " + parent.value); Console.WriteLine (g.prettyPrint (parent.position)); } var input = Console.ReadLine (); if (input.StartsWith ("c")) { input = input.Substring (1); int i = Int32.Parse (input); children [i - 1].Browse (g); } if (input.StartsWith ("p")) { input = input.Substring (1); int i = Int32.Parse (input); parents [i - 1].Browse (g); } if (input.StartsWith ("m")) { var plies = g.children(position, c); foreach (var ply in plies) { var s = new BNode(ply.Apply (position, g), (byte)(c ^ 1)); Console.WriteLine(g.prettyPrint(s.position)); } Browse (g); } }
public void Expand(Game g, Hashtable transposition, Queue<BNode> queue) { children = new List<BNode>(); var plies = g.children(position, c); foreach (var ply in plies) { var s = new BNode(ply.Apply (position, g), (byte)(c ^ 1)); if (transposition.ContainsKey(s)) { s = (BNode)transposition[s]; } else { s.Evaluate(g); transposition[s] = s; if(s.value == 0) queue.Enqueue(s); } s.parents.Add(this); children.Add(s); } }
public bool Equals(BNode other) { if (c != other.c) return false; if (position.Length != other.position.Length) return false; for (int i = 0; i < position.Length; ++i) if (!position[i].Equals(other.position[i])) return false; return true; }