package prak;

// Minimax.java
//yang dirandom 0  - 20
class Minimax2
{
   public static void main (String [] args)
   {
      // Build a game tree to keep track of all possible game configurations
      // that could occur during games of Nim with an initial pile of four
      // matches. The first move is made by player A.

      Node root = buildGameTree (4, 'A',0);

      // Use the minimax algorithm to determine if player A's optimal move is
      // the child node to the left of the current root node, the child node
      // directly below the current root node, or the child node to the right
      // of the current root node.

      int v1 = computeMinimax (root.left);
      int v2 = computeMinimax (root.center);
      int v3 = computeMinimax (root.right);

      if (v1 > v2 && v1 > v3)
          System.out.println ("Move to the left node.");
      else
      if (v2 > v1 && v2 > v3)
          System.out.println ("Move to the center node.");
      else
      if (v3 > v1 && v3 > v2)
          System.out.println ("Move to the right node.");
      else
          System.out.println ("?");
   }

   static Node buildGameTree (int nmatches, char player,int level)
   {
      Node n = new Node ();
      n.nmatches = nmatches;
      n.player = player;
      n.level = level ;
      level++ ;
      if (nmatches >= 1)
          n.left = buildGameTree (nmatches-1, (player == 'A') ? 'B' : 'A',level);
      if (nmatches >= 2)
          n.center = buildGameTree (nmatches-2, (player == 'A') ? 'B' : 'A',level);
      if (nmatches >= 3)
          n.right = buildGameTree (nmatches-3, (player == 'A') ? 'B' : 'A',level);

      return n;
   }

   static int computeMinimax (Node n)
   {
      int ans=0;
      int r = 0 ;
      if (n.nmatches == 0)
         if (n.player == 'A'){
              r = (int)(Math.random()* 20) ;
              System.out.println("Player A Level " + n.level+ " nmatches : " + n.nmatches+ " : " + r );
              return r ;
          }
          else{
              r = (int)(Math.random()* 20) ;
              System.out.println("Player B Level " + n.level+ " nmatches : " + n.nmatches+ " : " + r);
              return r ;
          }
          //return (n.player == 'A') ? 1 : -1;
      else
      if (n.player == 'A')
      {
          ans = Math.max (-10, computeMinimax (n.left));
          System.out.println("Player A Level " + n.level+ " nmatches : " + n.nmatches+ " ans : " + ans);         
          if (n.center != null)
          {
              ans = Math.max (ans, computeMinimax (n.center));
              System.out.println("Player A Level " + n.level+ " nmatches : " + n.nmatches+ " ans : " + ans);             
              if (n.right != null){
                  ans = Math.max (ans, computeMinimax (n.right));
                  System.out.println("Player A Level " + n.level+ " nmatches : " + n.nmatches+ " ans : " + ans);
              }
          }
      }
      else
      {
          ans = Math.min (100, computeMinimax (n.left));
          System.out.println("Player B Level " + n.level+ " nmatches : " + n.nmatches+ " ans : " + ans);
        
          if (n.center != null)
          {
              ans = Math.min (ans, computeMinimax (n.center));
              System.out.println("Player B Level " + n.level+ " nmatches : " + n.nmatches+ " ans : " + ans);
          
              if (n.right != null){
                  ans = Math.min (ans, computeMinimax (n.right));
                  System.out.println("Player B Level " + n.level+ " nmatches : " + n.nmatches+ " ans : " + ans);
              }
          }
      }

      return ans;
   }
}

