AdSense

Wednesday, December 24, 2014

The 100 Game

In "the 100 game," two players take turns adding, to a running 
total, any integer from 1..10. The player who first causes the running 
total to reach or exceed 100 wins. 
What if we change the game so that players cannot re-use integers? 
For example, if two players might take turns drawing from a common pool of numbers 
of 1..15 without replacement until they reach a total >= 100. This problem is 
to write a program that determines which player would win with ideal play. 

Write a procedure, "Boolean canIWin(int maxChoosableInteger, int desiredTotal)", 
which returns true if the first player to move can force a win with optimal play. 

Your priority should be programmer efficiency; don't focus on minimizing 
either space or time complexity. 
*/ 

Boolean canIWin(int maxChoosableInteger, int desiredTotal) { 
// Implementation here. Write yours 

}

This is a very interesting problem. Here is the heat discussion on Careercup. Overall, I don't think there is an optimal strategy, especially from the beginning of the game. So, just for fun, I include some randomness in to the game.

1. My code prints out all possible results of every start number from 1 to maxChoosableInteger assuming at the beginning of the game, there is no optimal strategy. 
2. There is no guarantee that starts from any number that P1 will win or P1 will lose. 
3. If the winnerNumber = (desiredTotal - currentSum) is in the range (1, maxChoosableInteger) and if  the winnerNumber hasn't been chosen, the current player will choose that number and she will win. 
4. If the winnerNumber has already been chosen, the current player will choose the smallest number available in order to prolong the game. 
5. If there is no winnerNumber, i.e., desiredTotal - currentSum isn't in the range (1, maxChoosableInteger), if the last player chose a small number (I classify "small" as number in the range (1, maxChoosableInteger/2)), the current player will choose a large number (range(maxChoosableInteger/2, maxChoosableInteger)), randomly (actually the assumption is not completely true). Otherwise she will choose a small number. Note if no such random number she can choose, she will choose the largest or smallest number available, respectively. 

I don't think my algorithm is the best one, some assumptions is not strict enough, but it's a little better than naive assumption of always choosing the largest number... I guess... 

Well, Merry Christmas. :)


import java.util.*;


public class The100Game {
 public void canIWin(int maxChooseableInteger, int desiredTotal) {
  if ((maxChooseableInteger*maxChooseableInteger + maxChooseableInteger) / 2 < desiredTotal) {
   throw new IllegalArgumentException("Wrong input!");
  }
  boolean[] pool = new boolean[maxChooseableInteger];
  for (int i = 0; i < maxChooseableInteger; i++) {
   System.out.println("If I start with: " + (i + 1));
   pool[i] = true;
   canP1Win(i + 1, i + 1, maxChooseableInteger, desiredTotal, pool, true);
    System.out.println("*******************"); 
   pool = new boolean[maxChooseableInteger];
  }
 }
 boolean canP1Win(int lastNumber, int currentSum, int maxChooseableInteger,
   int total, boolean[] pool, boolean isLastP1) {
  if (currentSum >= total) {
   if (isLastP1) 
    System.out.println("I can win!");
   else
    System.out.println("I will lose... :(");
   return isLastP1;
  }
  boolean isCurrentPlayerP1 = !isLastP1;
  int index;
  if (total - currentSum <= maxChooseableInteger) {
   index = maxChooseableInteger - 1;
   while (index >= 0 && pool[index]) {
    index--;
   }
   if (index < 0)
    return isCurrentPlayerP1;
   if ((currentSum + index + 1) >= total) {
    if (isCurrentPlayerP1) 
     System.out.println("I can win with: " + (index + 1));
    else
     System.out.println("I will lose with: " + (index + 1) + " :'(");
    return isCurrentPlayerP1;
   }
   //if the current player cannot win, she will choose a strategy not to let the opponent win
   index = 0;
   while (pool[index]) {
    index++;
   } 
  }
  else {
   Random r = new Random();
   if (lastNumber <= maxChooseableInteger / 2){
    index = r.nextInt(maxChooseableInteger / 2 + 1) + maxChooseableInteger/2;
    int count = 0;
    while ((pool[index] || index > maxChooseableInteger - 1 ) && count <= maxChooseableInteger) {
     index = r.nextInt(maxChooseableInteger / 2 + 1) + maxChooseableInteger/2;
     count++;
    }
    while (index >= 0 && pool[index]) {
     index--;
    }
    if (index < 0)
     return isCurrentPlayerP1;
   }
   else {
    index = r.nextInt(maxChooseableInteger / 2);
    int count = 0;
    while(pool[index] && count <= maxChooseableInteger) {
     index = r.nextInt(maxChooseableInteger / 2);
    }
    while (pool[index]) {
     index++;
    }
    //System.out.println("3 index: " + (index + 1));
   } 
  }
  if (isCurrentPlayerP1)
   System.out.println("I will choose " + (index + 1));
  else
   System.out.println("The oponent chooses: " + (index + 1));
  pool[index] = true;
  boolean p1CanWin = canP1Win(index + 1, currentSum + index + 1, maxChooseableInteger, total, 
    pool, isCurrentPlayerP1);
  if (isCurrentPlayerP1 && p1CanWin)
   return true;
  if (!isCurrentPlayerP1 && !p1CanWin)
   return false;
  pool[index] = false;
  return isCurrentPlayerP1 ? false : true;
 }
}

No comments:

Post a Comment