Guest User

Nim

a guest
Mar 24th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.13 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class Nim {
  6.  
  7.  
  8.     public static void main(String[] args) {
  9.        
  10.         Scanner sc = new Scanner(System.in);
  11.         Random r = new Random();
  12.         int pA = 5, pB = 5, pC = 5, turn = 1, pS, n, x;
  13.         String p1, p2, s1 = null, pile, m1, m2, m3;
  14.         //strings used more than once
  15.         m1 = "You must choose at least one and no more than what the stack has";
  16.         m2 = "Selected pile has no more stacks";
  17.         m3 = "Try again: ";
  18.         System.out.println("Player 1 enter your name ");
  19.         p1 = sc.next();
  20.         System.out.println("Player 2 enter your name or type \"computer\" to play against the computer");
  21.         p2 = sc.next();
  22.  
  23.         while ((pA + pB + pC) > 0) {
  24.             n = 0;
  25.             System.out.print("A: ");
  26.             while (n < pA) {
  27.                 System.out.print("*");
  28.                 n++;
  29.             }
  30.             n = 0;
  31.             System.out.print("\nB: ");
  32.             while (n < pB) {
  33.                 System.out.print("*");
  34.                 n++;
  35.             }
  36.             n = 0;
  37.             System.out.print("\nC: ");
  38.             while (n < pC) {
  39.                 System.out.print("*");
  40.                 n++;
  41.             }
  42.             //turn checker
  43.             if (turn == 1) {
  44.                 s1 = p1;
  45.                 turn--;
  46.             } else if (turn == 0 && (p2.equals("computer"))) {
  47.                 s1 = p1;
  48.                 turn++;
  49.             } else {
  50.                 s1 = p2;
  51.                 turn++;
  52.             }
  53.             System.out.print("\n" + s1 + " choose pile: ");
  54.             pile = sc.next();
  55.             //checks input to see if "A", "B" or "C" were selected
  56.             while (("A".compareTo(pile) + 3 <= 0)||("A".compareTo(pile)+3>3)) {
  57.                 System.out.print("Select a valid pile: ");
  58.                 pile = sc.next();
  59.             }
  60.             //checks whether the pile is empty or not
  61.             while ((pile.equals("A") && pA == 0) || (pile.equals("B") && pB == 0) || (pile.equals("C") && pC == 0)) {
  62.                 System.out.println(m2);
  63.                 System.out.print(m3);
  64.                 pile = sc.next();
  65.             }
  66.             System.out.print("How many to take from pile: ");
  67.             pS = sc.nextInt();
  68.             //cheat protection
  69.             while (pile.equals("A") && (pS <= 0 || pS > pA)) {
  70.                 System.out.println(m1);
  71.                 System.out.print(m3);
  72.                 pS = sc.nextInt();
  73.             }
  74.             while (pile.equals("B") && (pS <= 0 || pS > pB)) {
  75.                 System.out.println(m1);
  76.                 System.out.print(m3);
  77.                 pS = sc.nextInt();
  78.             }
  79.             while (pile.equals("C") && (pS <= 0 || pS > pC)) {
  80.                 System.out.println(m1);
  81.                 System.out.print(m3);
  82.                 pS = sc.nextInt();
  83.             }
  84.             if (pile.equals("A")) {
  85.                 pA -= pS;
  86.             } else if (pile.equals("B")) {
  87.                 pB -= pS;
  88.             } else if (pile.equals("C")) {
  89.                 pC -= pS;
  90.             }
  91.             //no idea how to make this shorter. Detects when theres 1 stack left to declare a winner before the computer's turn
  92.             if ((pA == 1 && pB == 0 && pC == 0) || (pA == 0 && pB == 1 && pC == 0) || (pA == 0 && pB == 0 && pC == 1)) {
  93.                 if (turn == 1 && (p2.equals("computer"))) {
  94.                     System.out.println("the computer lost, its pretty dumb.");
  95.                 } else if (turn == 1) {
  96.                     s1 = p1;
  97.                     System.out.println(s1 + " there is only one left, you lose");
  98.                 } else {
  99.                     s1 = p2;
  100.                     System.out.println(s1 + " there is only one left, you lose");
  101.                 }
  102.                 return;
  103.             }
  104.             //computer opponent, i dont know whats going on in here anymore
  105.             if (p2.equals("computer")) {
  106.                 x = 1 + r.nextInt(3);
  107.                 if (x == 1 && pA > 0) {
  108.                     x = 1 + r.nextInt(5);
  109.                     while (x > pA) {
  110.                         x = 1 + r.nextInt(5);
  111.                     }
  112.                     System.out.println("the computer took " + x + " from pile A");
  113.                     pA -= x;
  114.  
  115.                 } else if (x == 2 && pB > 0) {
  116.                     x = 1 + r.nextInt(5);
  117.                     while (x > pB) {
  118.                         x = 1 + r.nextInt(5);
  119.                     }
  120.                     System.out.println("the computer took " + x + " from pile B");
  121.                     pB -= x;
  122.                 } else if (x == 3 && pC > 0) {
  123.                     x = 1 + r.nextInt(5);
  124.                     while (x > pC) {
  125.                         x = 1 + r.nextInt(5);
  126.                     }
  127.                     System.out.println("the computer took " + x + " from pile C");
  128.                     pC -= x;
  129.  
  130.                 }
  131.                 //the more i read, the more confused i get but it works
  132.                 if (x == 1 && pA == 0) {
  133.                     if (pB > 0) {
  134.                         x = 1 + r.nextInt(5);
  135.                         while (x > pB) {
  136.                             x = 1 + r.nextInt(5);
  137.                         }
  138.                         System.out.println("the computer took " + x + " from pile B");
  139.                         pB -= x;
  140.                     } else {
  141.                         x = 1 + r.nextInt(5);
  142.                         while (x > pC) {
  143.                             x = 1 + r.nextInt(5);
  144.                         }
  145.                         System.out.println("the computer took " + x + " from pile C");
  146.                         pC -= x;
  147.                     }
  148.                 } else if (x == 2 && pB == 0) {
  149.                     if (pA > 0) {
  150.                         x = 1 + r.nextInt(5);
  151.                         while (x > pB) {
  152.                             x = 1 + r.nextInt(5);
  153.                         }
  154.                         System.out.println("the computer took " + x + " from pile A");
  155.                         pA -= x;
  156.                     } else {
  157.                         x = 1 + r.nextInt(5);
  158.                         while (x > pC) {
  159.                             x = 1 + r.nextInt(5);
  160.                         }
  161.                         System.out.println("the computer took " + x + " from pile C");
  162.                         pC -= x;
  163.                     }
  164.                 } else if (x == 3 && pC == 0) {
  165.                     if (pA > 0) {
  166.                         x = 1 + r.nextInt(5);
  167.                         while (x > pA) {
  168.                             x = 1 + r.nextInt(5);
  169.                         }
  170.                         System.out.println("the computer took " + x + " from pile A");
  171.                         pA -= x;
  172.                     } else {
  173.                         x = 1 + r.nextInt(5);
  174.                         while (x > pB) {
  175.                             x = 1 + r.nextInt(5);
  176.                         }
  177.                         System.out.println("the computer took " + x + " from pile B");
  178.                         pB -= x;
  179.                     }
  180.                 }
  181.             }
  182.             //computer player doesnt break any rules but doesnt really know how to play
  183.             if (pA==0&&pB==0&&pC==0){
  184.                 System.out.println("The computer probably lost by taking all remaining uhh things....");
  185.                 return;
  186.             }
  187.             //Detects when theres 1 stack left to declare a winner again at the end of the computer's turn in case it wins!
  188.             if ((pA == 1 && pB == 0 && pC == 0) || (pA == 0 && pB == 1 && pC == 0) || (pA == 0 && pB == 0 && pC == 1)) {
  189.                 if (turn == 1 && (p2.equals("computer"))) {
  190.                     System.out.println("the computer lost, its pretty dumb.");
  191.                 } else if (turn == 1) {
  192.                     s1 = p1;
  193.                     System.out.println(s1 + " there is only one left, you lose");
  194.                 } else {
  195.                     s1 = p2;
  196.                     System.out.println(s1 + " there is only one left, you lose");
  197.                 }
  198.                 return;
  199.             }
  200.         }
  201.        
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment