Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. public class Nim {
  2.  
  3.     public static boolean isValidEntry(int amountOfStones, int userInput) {
  4.         return userInput <= 3 && amountOfStones <= amountOfStones;
  5.     }
  6.  
  7.     public static int drawStones() {
  8.         int computerMove = (int) (3 * Math.random() + 1);
  9.         return computerMove;
  10.     }
  11.  
  12.     public static int humansTurn(int amountOfStones, Scanner input) {
  13.         int userInput;
  14.  
  15.         do {
  16.             System.out.print("\tThere are " + amountOfStones + " Stones. How many would you like?: ");
  17.             userInput = Integer.parseInt(input.nextLine());
  18.         } while (!isValidEntry(amountOfStones, userInput) && userInput != 0);
  19.         amountOfStones -= userInput;
  20.         System.out.println("\tThere are " + amountOfStones + " Stones Left.");
  21.        
  22.         if (amountOfStones == 1) {
  23.             System.out.println("\tYou Win!");
  24.         }
  25.         return amountOfStones;
  26.         }
  27.    
  28.  
  29.  
  30.    
  31.    
  32.  
  33.     public static int computersTurn(int amountOfStones) {
  34.         int newDraw = drawStones();
  35.         int newDraw1 = drawStones();
  36.         if (isValidEntry(amountOfStones, newDraw)) {
  37.             System.out.println("\tThe computer takes " + newDraw + " Stones.");
  38.             amountOfStones -= newDraw;
  39.             System.out.println("\tThere are now " + +amountOfStones + " Stones.");
  40.         } while (!isValidEntry(amountOfStones, newDraw));
  41.        
  42.         if (amountOfStones == 1) {
  43.             System.out.println("\tComputer wins!");
  44.            
  45.             } else {
  46.    
  47.                  newDraw1 = drawStones();
  48.             } while (newDraw1 <= 3 && newDraw1 > amountOfStones) {
  49.                
  50.             }
  51.    
  52.  
  53.    
  54.         return amountOfStones;
  55.     }
  56.  
  57.  
  58.  
  59.    
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.    
  67.  
  68.     public static void main(String[] args) {
  69.         Scanner input = new Scanner(System.in);
  70.         int bothTurns = (int) (16 * Math.random() + 15);
  71.         do {
  72.         bothTurns = humansTurn(bothTurns, input);
  73.         bothTurns = computersTurn(bothTurns);
  74.     } while (bothTurns != 0);
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement