Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.83 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class GameOfNim{
  3.   public static void main(String[]args){
  4.  
  5.  
  6.     Scanner in = new Scanner(System.in);
  7.     System.out.println("The game of Nim. This is a well known game with a number of variants. The following variant has an interesting winning strategy. Two players alternately take marbles from a pile. In each move, a player chooses how many marbles to take. The player must take at least one but at most half of the marbles. Then the other player takes a turn. The player who takes the last marble loses.");
  8.  
  9.  
  10.     //GENERATING TURN:
  11.     System.out.println("-----------------------------------------------");
  12.     boolean turn = false;
  13.     double turnNbr = Math.random();
  14.     if(turnNbr>0.5){
  15.       turn = true;
  16.       System.out.println("The user starts");
  17.     }else{
  18.       turn = false;
  19.       System.out.println("The computer starts");
  20.      }
  21.  
  22.     //GENERATING PILE:
  23.     int pileSize = (int)(Math.random() * ((100 - 10) + 1)) + 10;
  24.     System.out.println("The pile size is: " + pileSize);
  25.  
  26.     //GENERATING GAMEDIFF:
  27.     boolean difficulty = false;
  28.      double gameDifficulty = Math.random();
  29.      if(gameDifficulty>0.5){
  30.        difficulty = true;
  31.        System.out.println("The computer plays stupid");
  32.      }else{
  33.        difficulty = false;
  34.        // The computer plays "smart"
  35.        System.out.println("The computer plays smart");
  36.      }
  37.      System.out.println("-----------------------------------------------");
  38.  
  39.  
  40.  
  41.  
  42.      int subComp = 0;
  43.      int counter = 0;
  44.  
  45.      if(turn){
  46.        while(pileSize>0){
  47.  
  48.          // The user is playing;
  49.         System.out.println("How many marbles do you wanna take?: ");
  50.         int sub = in.nextInt();
  51.         pileSize = pileSize - sub;
  52.         System.out.println("Marbles left: " + pileSize);
  53.         if(pileSize == 1){
  54.           System.out.println("The user won!");
  55.         }
  56.         else if(pileSize-sub == 1){
  57.           System.out.println("You lost!");
  58.         }
  59.         System.out.println("The computer is playing ...");
  60.  
  61.  
  62.         if(difficulty){
  63.            subComp = (int)(Math.random() * ((pileSize/2 - 1) + 1)) + 1;
  64.           pileSize = pileSize - subComp;
  65.         }
  66.         else{
  67.           if(pileSize == Math.pow(subComp,2)-1){
  68.             subComp = (int)(Math.random() * ((pileSize/2 - 1) + 1)) + 1;
  69.           }
  70.  
  71.           }
  72.           System.out.println("The computer took: " + subComp + " marbles off!");
  73.           System.out.println("Marbles left: " + pileSize);
  74.           System.out.println("Your turn!");
  75.           System.out.println("-----------------------------------------------");
  76.           counter++;
  77.         }
  78.  
  79.  
  80.      }
  81.      else{
  82.        while(pileSize>0){
  83.  
  84.         System.out.println("The computer is playing ...");
  85.         // The computer is playing;
  86.  
  87.         if(difficulty){
  88.            subComp = (int)(Math.random() * ((pileSize/2 - 1) + 1)) + 1;
  89.           pileSize = pileSize - subComp;
  90.         }
  91.         else{
  92.           if(pileSize == Math.pow(subComp,2)-1){
  93.             subComp = (int)(Math.random() * ((pileSize/2 - 1) + 1)) + 1;
  94.           }
  95.  
  96.           }
  97.           System.out.println("The computer took: " + subComp + " marbles off!");
  98.           System.out.println("Marbles left: " + pileSize);
  99.           System.out.println("Your turn!");
  100.         }
  101.  
  102.  
  103.         // The user is playing;
  104.        System.out.println("How many marbles do you wanna take?: ");
  105.        int sub = in.nextInt();
  106.        pileSize = pileSize - sub;
  107.        System.out.println("Marbles left: " + pileSize);
  108.        if(pileSize == 1){
  109.          System.out.println("The user won!");
  110.        }
  111.        else if(pileSize-sub == 1){
  112.          System.out.println("You lost!");
  113.        }
  114.  
  115.         System.out.println("-----------------------------------------------");
  116.         counter++;
  117.      }
  118.  
  119.  
  120.  
  121.  
  122.      }
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.      /**
  139.      //TESTING SETTINGS:
  140.      for(int i = 1; i<=30;i++){
  141.        //GENERATING TURN:
  142.        boolean turn = false;
  143.        double turnNbr = Math.random();
  144.        if(turnNbr>0.5){
  145.          turn = true;
  146.          System.out.println("The user starts");
  147.        }else{
  148.          turn = false;
  149.          System.out.println("The computer starts");
  150.         }
  151.  
  152.        //GENERATING PILE:
  153.        int pileSize = (int)(Math.random() * ((100 - 10) + 1)) + 10;
  154.        System.out.println("The pile size is: " + pileSize);
  155.  
  156.        //GENERATING GAMEDIFF:
  157.        boolean difficulty = false;
  158.         double gameDifficulty = Math.random();
  159.         if(gameDifficulty>0.5){
  160.           difficulty = true;
  161.           System.out.println("The computer plays stupid");
  162.         }else{
  163.           difficulty = false;
  164.           // The computer plays "smart"
  165.           System.out.println("The computer plays smart");
  166.         }
  167.         System.out.println("-----------------------------------------------");
  168.       }
  169.  
  170.       **/
  171.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement