Kalah AI

Hi people,
my assignment is to create a simple AI for the game kalah (also known as mancala). this AI composed of making a min/max 'tree' (the tree is never created in the memory) it is all done by recursion. the game will send my AI class a gamestate, and then the AI will apply moves to this game state and keep doing this until the tree gets to depth d, where it will return a value that ranks the gamestate. it will return several values, so i just search for the max and then return that one. ultimately, the AI returns which position (pit on the board) to take the beans from, should be the the one that returned the highest rank.
here is my horrendous and ugly code...
// Roarke Randall
// 10.24.08
// CSE 23
// end of the game project #2
public class KalahAI73833446 implements KalahAI
     Position[] positions;
     int[] evaluations;
     GameState clone;
     GameState clone2;
     Boolean player;
     static final int DEPTH = 4;
     int maxevaluation;
     public Position chooseMove(GameState state)
          //System.out.println("choosing a move");
          player = false;
          clone = (GameState)state.clone();
          clone2 = (GameState)state.clone();
          positions = new Position[clone.getHolesPerPlayer()+1];
          evaluations = new int[clone.getHolesPerPlayer()+1];
          maxevaluation = 0;
          Position bestPos = null;
          if(clone.isTopPlayerTurn()) player = true;
          for(int i = 1; i <= clone.getHolesPerPlayer(); i++)
               try
                    clone.checkValidMove(clone.createPosition(player, i));
                    positions[i] = clone.createPosition( player, i);               
               }catch(InvalidMoveException e){
                    positions[i] = null;
          for( int i = 1; i <= clone.getHolesPerPlayer(); i++)
               clone = (GameState)state.clone();
               if(positions[i] != null)
                    try
                         clone.applyMove(clone.createPosition(
                                   clone.isTopPlayerTurn(), positions.getHoleNumber()));
                         evaluations[i] = search( clone, DEPTH);
                         if(evaluations[i] >= maxevaluation)
                              //System.out.println(evaluations[i]);
                              maxevaluation = evaluations[i];
                              bestPos = positions[i];
                    }catch(InvalidMoveException e){
                         //System.out.println("not a valid place");
                         evaluations[i] = -1;
          if( bestPos == null)
               for(int i = 1; i < state.getHolesPerPlayer()+1; i++)
                    try{
                         //System.out.println("something got fucked up");
                         clone.checkValidMove(clone.createPosition(clone.isTopPlayerTurn(), i));
                         bestPos = clone.createPosition(clone.isTopPlayerTurn(), i);
                    }catch (Exception e){}
          return state.createPosition(player, bestPos.getHoleNumber());
     public int search(GameState g, int depth)
          GameState gprime;
          int[] validpositions = new int[g.getHolesPerPlayer()+1];
          int[] evaluations2 = new int[g.getHolesPerPlayer()+1];
          int maxevaluation2 = 0;
          // leaf of the 'tree'
          if(depth == 0 )
               //System.out.println(evaluate(g));
               return evaluate(g);
          else
               validpositions = validPositions(g);
               // this computer's 'virtual' turn
               if( g.isTopPlayerTurn() && player
                    || !g.isTopPlayerTurn() && !player)
                    for(int i = 1; i < g.getHolesPerPlayer()+1; i++)
                         if(validpositions[i] != 0 )
                              gprime = (GameState) g.clone();
                              try
                                   gprime.applyMove(
                                             gprime.createPosition(gprime.isTopPlayerTurn(), i));
                                   evaluations2[i] = search(gprime, depth-1);
                                   if(evaluations2[i] > maxevaluation2)
                                        maxevaluation2 = evaluations2[i];
                              }catch(InvalidMoveException e)
                                   System.out.println("some how this 'valid position' wasnt valid");
                    //System.out.println(maxevaluation2);
                    return maxevaluation2;
               else // opponent 'virtual' turn
                    validpositions = validPositions(g);
                    int minevaluation2 = 0;
                    for(int i = 1; i < g.getHolesPerPlayer()+1; i++)
                         if(validpositions[i] != 0 )
                              gprime = (GameState) g.clone();
                              try
                                   gprime.applyMove(
                                             gprime.createPosition(gprime.isTopPlayerTurn(), i));
                                   evaluations2[i] = search(gprime, depth-1);
                                   if(evaluations2[i] < minevaluation2)
                                        minevaluation2 = evaluations2[i];
                              }catch(InvalidMoveException e)
                                   System.out.println("some how this 'valid position' wasnt valid");
                    return minevaluation2;
     public int evaluate( GameState g)
          return g.getBeanCount(g.createPosition(player, Position.POT)) -
               g.getBeanCount(g.createPosition(!player, Position.POT));
     public int[] validPositions(GameState g)
          int[] pos = new int[g.getHolesPerPlayer()+1];
          for(int i = 1; i < g.getHolesPerPlayer()+1; i++)
               try
                    g.checkValidMove(g.createPosition(g.isTopPlayerTurn(), i));
                    pos[i] = i;
               }catch (InvalidMoveException e)
                    pos[i] = 0;
          return pos;
http://www.ics.uci.edu/~goodrich/teach/ics23/LabManual/EndOfTheGame/ thats the project.
my problem is that some reason my search(...) returns 0, even though if i print the value that is supposed to be returned, it isnt 0. can anyone see something in here that would make that happen? please ask questions, i know my code is ugly and hard to follow probably.

// Roarke Randall
// 10.24.08
// CSE 23
// end of the game project #2
public class KalahAI73833446 implements KalahAI
     Position[] positions;
     int[] evaluations;
     GameState clone;
     GameState clone2;
     Boolean player;
     static final int DEPTH = 4;
     int maxevaluation;
     public Position chooseMove(GameState state)
          //System.out.println("choosing a move");
          player = false;
          clone = (GameState)state.clone();
          clone2 = (GameState)state.clone();
          positions = new Position[clone.getHolesPerPlayer()+1];
          evaluations = new int[clone.getHolesPerPlayer()+1];
          maxevaluation = 0;
          Position bestPos = null;
          if(clone.isTopPlayerTurn()) player = true;
          for(int i = 1; i <= clone.getHolesPerPlayer(); i++)
               try
                    clone.checkValidMove(clone.createPosition(player, i));
                    positions[i] = clone.createPosition( player, i);               
               }catch(InvalidMoveException e){
                    positions[i] = null;
          for( int i = 1; i <= clone.getHolesPerPlayer(); i++)
               clone = (GameState)state.clone();
               if(positions[i] != null)
                    try
                         clone.applyMove(clone.createPosition(
                                   clone.isTopPlayerTurn(), positions.getHoleNumber()));
                         evaluations[i] = search( clone, DEPTH);
                         if(evaluations[i] >= maxevaluation)
                              //System.out.println(evaluations[i]);
                              maxevaluation = evaluations[i];
                              bestPos = positions[i];
                    }catch(InvalidMoveException e){
                         //System.out.println("not a valid place");
                         evaluations[i] = -1;
          if( bestPos == null)
               for(int i = 1; i < state.getHolesPerPlayer()+1; i++)
                    try{
                         //System.out.println("something got fucked up");
                         clone.checkValidMove(clone.createPosition(clone.isTopPlayerTurn(), i));
                         bestPos = clone.createPosition(clone.isTopPlayerTurn(), i);
                    }catch (Exception e){}
          return state.createPosition(player, bestPos.getHoleNumber());
     public int search(GameState g, int depth)
          GameState gprime;
          int[] validpositions = new int[g.getHolesPerPlayer()+1];
          int[] evaluations2 = new int[g.getHolesPerPlayer()+1];
          int maxevaluation2 = 0;
          // leaf of the 'tree'
          if(depth == 0 )
               //System.out.println(evaluate(g));
               return evaluate(g);
          else
               validpositions = validPositions(g);
               // this computer's 'virtual' turn
               if( g.isTopPlayerTurn() && player
                    || !g.isTopPlayerTurn() && !player)
                    for(int i = 1; i < g.getHolesPerPlayer()+1; i++)
                         if(validpositions[i] != 0 )
                              gprime = (GameState) g.clone();
                              try
                                   gprime.applyMove(
                                             gprime.createPosition(gprime.isTopPlayerTurn(), i));
                                   evaluations2[i] = search(gprime, depth-1);
                                   if(evaluations2[i] > maxevaluation2)
                                        maxevaluation2 = evaluations2[i];
                              }catch(InvalidMoveException e)
                                   System.out.println("some how this 'valid position' wasnt valid");
                    //System.out.println(maxevaluation2);
                    return maxevaluation2;
               else // opponent 'virtual' turn
                    validpositions = validPositions(g);
                    int minevaluation2 = 0;
                    for(int i = 1; i < g.getHolesPerPlayer()+1; i++)
                         if(validpositions[i] != 0 )
                              gprime = (GameState) g.clone();
                              try
                                   gprime.applyMove(
                                             gprime.createPosition(gprime.isTopPlayerTurn(), i));
                                   evaluations2[i] = search(gprime, depth-1);
                                   if(evaluations2[i] < minevaluation2)
                                        minevaluation2 = evaluations2[i];
                              }catch(InvalidMoveException e)
                                   System.out.println("some how this 'valid position' wasnt valid");
                    return minevaluation2;
     public int evaluate( GameState g)
          return g.getBeanCount(g.createPosition(player, Position.POT)) -
               g.getBeanCount(g.createPosition(!player, Position.POT));
     public int[] validPositions(GameState g)
          int[] pos = new int[g.getHolesPerPlayer()+1];
          for(int i = 1; i < g.getHolesPerPlayer()+1; i++)
               try
                    g.checkValidMove(g.createPosition(g.isTopPlayerTurn(), i));
                    pos[i] = i;
               }catch (InvalidMoveException e)
                    pos[i] = 0;
          return pos;

Similar Messages

  • NLS patch for EBS 12.0.6

    Hi ,
    I have a EBS 12.0.6 fresh install. Now I wanted to install japanese language and i am looking for the NLS patch number. I am able to find the 6394500 for 12.0.4. Should I use the same for 12.0.6 also. Please help.
    thanks in advance
    Kala

    Hi,
    Can you help where can I find NLS patch for Japanese Language for R12.0.4 Check Oracle e-Delivery website and/or MOS. If you could not locate the files then log a SR.
    Thanks,
    Hussein

  • Table to find the list of shopping carts

    Hi SRM Gurus,
               could you please guide me is there any table if i want to find the shoppingcart cart list for the particular catagory,particular vendor lke that.
    Correct answer will be apprecitable.
    Thanks In Advance
    Kala

    Hi,
    How about following table-field ?
    BBP_PDIGP-CATEGORY_ID
    CRMD_ORDERADM_I-HEADER
    CRMD_PARTNER-PARTNER_NO
    SRMD_LINK-GUID_SET
    SRMD_LINK-GUID_HI
    CRMD_ORDERADM_I-HEADER
    Regards,
    Masa

  • Disable create button in ME23N

    Hi Gurus,
    I have a requirement to disable 'create' button in ME23N screen.
    Can we acheive this by transaction variant?
    Appreciate if some body can explain the steps.
    Thanks
    Kala

    Hi sasi kala,
               Ofcourse it possible by SHDO,
    Try like this..
    Goto SHD0 and enter ME23N in Transaction code field
    and then enter e.g. ZME23N in Transaciton variant filed then hit create button
    it will take you to the PO create screen.
    here you enter all the value and and create the PO.
    every popup screen hit enter
    and which ever value you want to defulat to the transaction
    those values you have to check the check box of
    W.content and output only
    then save. it will ask you the object where you wan to save
    if you want to migrate to another system then use the real package other wise use $TMP for each screen (until the popup goes away)
    and save it
    Then go back to main SHD0 screen and hit goto button and select create variant transaciton and create the tcode for this variant which can be used by user
    Regards
    Kiran

  • ESS - Adequacy of electronic signature

    Hi All
    We are in the process of implementing ESS for a county project.
    The question is regarding W4 withholdings, does anyone have any suggestions with regard to best practices when documenting the withholding change by the employee.
    Is electronic signature enough to address both IRS and Local Gov regulations or will a hard copy of the employees elections still be required.
    I will be eager to have any body's inputs on what their company elected to do when implementing ESS .
    Thanks in advance.
    Kala

    Hi Kiran
    Can we substantiate with any documentary proof for this that IRS is OK with electronic signatures for this purpose ?
    If there is any thing like that it will be of great help really in making my client happy.
    Thanks,
    Sasi

  • Stock & Non-stock material

    Hi experts,
      How to find which material is stock and nonstock from the internal goods/services list in SRM frontend
    Thanks in advance
    Kala

    Hi,
    The non stock items no need of material no ,
    Possibility to track tgroug Query , so the query is as per text u have given in the po.
    Plz try this one,
    regards,
    andra.

Maybe you are looking for