Example #1
0
 //getNextStep determines the step the computer will make given the current status
 //of the board and the difficulty level
 public static State getNextStep(State s, int diff)
 {
     List<State> possible = new List<State>();
     List<Tree> tree = new List<Tree>();
     for (int i = 8; i < 16; i++)
     {
         s.addStep(i, possible, false);
     }
     foreach (State st in possible)
     {
         Tree fresh = new Tree(st);
         tree.Add(fresh);
     }
     //for different difficulty level(lvl3 is disabled due to memory constraint)
     switch (diff)
     {
         case 1:
             foreach (Tree t in tree)
             {
                 t.populate(2);
             }
             break;
         case 2:
             foreach (Tree t in tree)
             {
                 t.populate(4);
             }
             break;
         case 3:
             foreach (Tree t in tree)
             {
                 t.populate(6);
             }
             break;
         default:
             break;
     }
     foreach (Tree t in tree)
     {
         t.calc();
     }
     int max = -2000;
     int refer = 0;
     int num = 0;
     int c = 0;
     foreach (Tree t in tree)
     {
         if (t.value > max)
         {
             max = t.value;
             refer = num;
         }
         if (t.value == max)
         {
             c++;
         }
         num++;
     }
     if (c != 1)
     {
         num = 0;
         foreach (Tree t in tree)
         {
             if(t.value == max)
             {
                 t.state.eval();
                 if (t.state.value > max)
                 {
                     max = t.state.value;
                     refer = num;
                 }
             }
             num++;
         }
     }
     if (possible.Count == 0) return null;
     //find the most yielding step and return its state
     return possible[refer];
 }
Example #2
0
 //To generate all the possible states in the future given the current state and how far into the future(count)
 public void populate(int count)
 {
     if (count == 0) return;
     List<State> temp = new List<State>();
     if (height % 2 != 0)
     {
         for (int i = 0; i < 8; i++)
         {
             state.addStep(i, temp, false);
         }
     }
     else
     {
         for (int i = 8; i < 16; i++)
         {
             state.addStep(i, temp, false);
         }
     }
     if (temp.Count == 0)
     {
         this.value = height % 2 == 0 ? -1000 : 1000;
     }
     foreach (State st in temp)
     {
         Tree fresh = new Tree(st);
         fresh.height = this.height + 1;
         next.Add(fresh);
     }
     foreach (Tree t in next)
     {
         t.populate(count - 1);
     }
 }