Advertisement
Guest User

corn

a guest
Dec 9th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.64 KB | None | 0 0
  1. import java.util.*;
  2. /*
  3. * Danny Davis & Andrew Akiyama
  4. * Project 3b
  5. * ACM-ICPC live archive: 7127 Number Game
  6. *
  7. * Project Description: Alice and Bob are playing a game on a line of N
  8. *squares. The line is initially populated with one of
  9. *each of the numbers from 1 to N. Alice and Bob take
  10. *turns removing a single number from the line, subject
  11. *to the restriction that a number may only be removed
  12. *if it is not bordered by a higher number on either side. When the number is removed, the square that
  13. *contained it is now empty. The winner is the player who removes the 1 from the line. Given an initial
  14. *configuration, who will win, assuming Alice goes first and both of them play optimally?
  15. */
  16. public class Project_3b
  17. {
  18. public static void main(String[] args)
  19. {
  20. boolean isAlice = false;
  21. int a = Integer.MIN_VALUE;
  22. int b = Integer.MAX_VALUE;
  23. //int [] initState = { 2, 1, 3, 4 }; // Bob - Good
  24. //int [] initState = { 1, 3, 2, 4 }; // Alice - Good
  25. int [] initState = { 1, 3, 2 }; // Bob - Good
  26. //int [] initState = { 2, 5, 1, 6, 4, 3 }; // Alice - Good
  27.  
  28. int value = value(initState, isAlice, a, b);//returns the winner assuming optimality
  29.  
  30. ////////////////////////////////////////////////////////////////////////////////////
  31. ////Prints the winner //////////////////////////////////////////////////////////////
  32. ////////////////////////////////////////////////////////////////////////////////////
  33. if (value > 0)
  34. System.out.println("Alice");
  35. else if (value < 0)
  36. System.out.println("Bob");
  37.  
  38. }
  39.  
  40. //////////////////////////////////////////////////////////////////////////
  41. ////// Value function ////////////////////////
  42. //////////////////////////////////////////////////////////////////////////
  43. @SuppressWarnings("unused")
  44. public static int value ( int [] initState, boolean isAlice, int a, int b )
  45. {
  46. //returns an int greater than 0 if Alice wins or an int less than 0 if Bob wins
  47. ArrayList<int[]> succesors = getSuccesors( initState, a, b );
  48. for ( int i = 0; i < succesors.size(); i++)
  49. {
  50. //if the state is a terminal state
  51. if ( isGoalState(succesors.get(i)) )//return the state's utility
  52. return a;
  53. //else if the next agent is MAX:
  54. else if ( isAlice )
  55. {
  56. return maxValue( succesors.get(i), a, b );// return maxValue(state, a, b)
  57. }
  58. //else if the next agent is MIN:
  59. else
  60. {
  61. return minValue( succesors.get(i), a, b );// return minValue(state, a, b)
  62. }
  63. }
  64. return 1;
  65. }
  66.  
  67. /////////////////////////////////////////////////////////////////////////
  68. ///////// Max Value function //////////////////////
  69. /////////////////////////////////////////////////////////////////////////
  70. public static int maxValue ( int [] state, int a, int b )
  71. {
  72. boolean isAlice = false;
  73. int v = Integer.MIN_VALUE;// initialize v = negative infinity
  74. //for each successor of state:
  75. ArrayList<int[]> succesors = getSuccesors( state, a, b );
  76. for ( int i = 0; i < succesors.size(); i++ )
  77. {
  78. v = Math.max(v, value(succesors.get(i), isAlice, a, b));
  79. if (v >= b)
  80. return v;
  81. a = Math.max(a, v);
  82. }
  83. return v;
  84. }
  85.  
  86. /////////////////////////////////////////////////////////////////////////
  87. ///////// Min Value function //////////////////////
  88. /////////////////////////////////////////////////////////////////////////
  89. public static int minValue ( int [] state, int a, int b )
  90. {
  91. boolean isAlice = true;
  92. int v = Integer.MAX_VALUE;// initialize v = positive infinity
  93. //for each successor of state:
  94. ArrayList<int[]> succesors = getSuccesors( state, a, b );
  95. for ( int i = 0; i < succesors.size(); i++ )
  96. {
  97. v = Math.min(v, value(succesors.get(i), isAlice, a, b));
  98. if (v <= a)
  99. return v;
  100. b = Math.min(b, v);
  101. }
  102. return v;
  103. }
  104.  
  105. /////////////////////////////////////////////////////////////////////////
  106. ///////// isGoalState function /////////////////////
  107. /////////////////////////////////////////////////////////////////////////
  108. public static boolean isGoalState ( int [] state )
  109. {
  110. //Scan's through state array, if it has a # 1 left in it, false is returned for goal state
  111. // otherwise true is returned for goal state
  112. for ( int i = 0; i < state.length; i++ )
  113. {
  114. if ( state[i] == 1 )
  115. return false;
  116. }
  117. return true;
  118. }
  119.  
  120. /////////////////////////////////////////////////////////////////////////
  121. ////////// get Successor function ///////////////////
  122. /////////////////////////////////////////////////////////////////////////
  123. public static ArrayList<int[]> getSuccesors ( int [] state, int a, int b )
  124. {
  125. ArrayList<int[]> succesors = new ArrayList<int[]>();
  126. //loops through search space
  127. for ( int i = 0; i < state.length; i++ )
  128. {
  129. //if it is the first index of the array it only checks if the number is greater than the number to the right.
  130. //if it is larger than the number to the right then it is a possible successor.
  131. if ( i == 0)
  132. {
  133. if ( state[i] > state[i+1] )
  134. {
  135. int [] stateHold = new int [state.length];
  136. for ( int j = 0; j < state.length; j++ )
  137. {
  138. int z = state[j];
  139. stateHold[j] = z;
  140. }
  141. stateHold[i] = -1;//-1 represents an empty square
  142. succesors.add(stateHold);
  143. }
  144. }
  145. //if i is not the last index of the array it checks the number to the let and to the right.
  146. else if ( i != (state.length-1))
  147. {
  148. if ( state[i] > state[i-1] && state[i] > state[i+1] )
  149. {
  150. int [] stateHold = new int [state.length];
  151. for ( int j = 0; j < state.length; j++ )
  152. {
  153. int z = state[j];
  154. stateHold[j] = z;
  155. }
  156. stateHold[i] = -1;//-1 represents an empty square
  157. succesors.add(stateHold);
  158. }
  159. }
  160. //otherwise i is the last number of the array and it only checks the number to the left
  161. else
  162. {
  163. if ( state[i] > state[i-1] )
  164. {
  165. int [] stateHold = new int [state.length];
  166. for ( int j = 0; j < state.length; j++ )
  167. {
  168. int z = state[j];
  169. stateHold[j] = z;
  170. }
  171. stateHold[i] = -1;//-1 represents an empty square
  172. succesors.add(stateHold);
  173. }
  174. }
  175. }
  176. return succesors;
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement