Advertisement
Guest User

Nim

a guest
Dec 19th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.12 KB | None | 0 0
  1.  
  2. package baby_nim;
  3.  
  4. import java.util.Random;
  5. import java.util.Scanner;
  6.  
  7.  
  8. public class Baby_Nim {
  9.  
  10.    
  11.     public static void main(String[] args) {
  12.         int pile_A=3, pile_B=4,pile_C=5,remove,counter,rows,i,comppick;
  13.         String pile,player1,player2,versus;
  14.        
  15.         Scanner scan=new Scanner(System.in);
  16.        
  17.         System.out.print("Want to battle another \"Player\" or \"Computer\"? ");
  18.         versus=scan.next();
  19.        
  20.         if(versus.equals("Player")){
  21.        
  22.         System.out.print("Player 1, enter your name: ");
  23.         player1=scan.next();
  24.         System.out.print("Player 2, enter your name: ");
  25.         player2=scan.next();
  26.         System.out.println("");
  27.        
  28.         rows=pile_A;
  29.         if(pile_B>rows)
  30.             rows=pile_B;
  31.         if(pile_C>rows)
  32.             rows=pile_C;
  33.        
  34.         counter=1;
  35.        
  36.         while((pile_A>0 || pile_B>0 || pile_C>0) && (pile_A!=1 || pile_B!=0 || pile_C!=0) &&
  37.               (pile_A!=0 || pile_C!=0 || pile_B!=1) && (pile_A!=0 || pile_B!=0 || pile_C!=1)){
  38.            
  39.             i=0;
  40.              while(i<rows){
  41.            
  42.                 if(rows-i>pile_A)
  43.                     System.out.print("  ");
  44.                 else
  45.                     System.out.print("* ");
  46.            
  47.                 if(rows-i>pile_B)
  48.                     System.out.print("  ");
  49.                 else
  50.                     System.out.print("* ");
  51.            
  52.                 if(rows-i>pile_C)
  53.                     System.out.print("");
  54.                 else
  55.                     System.out.print("*");
  56.            
  57.                 System.out.println("");
  58.                 i++;
  59.              }
  60.        
  61.         System.out.println("A B C");
  62.        
  63.             //System.out.println("A: "+pile_A+"\tB: "+pile_B+"\tC: "+pile_C);
  64.            
  65.             if(counter%2!=0)
  66.                 System.out.print("\n"+player1+", choose a pile: ");
  67.             else
  68.                 System.out.print("\n"+player2+", choose a pile: ");
  69.             pile=scan.next();
  70.            
  71.             while((pile.equals("A")&&pile_A==0) || (pile.equals("B")&&pile_B==0) || (pile.equals("C")&&pile_C==0)){    
  72.             if(counter%2!=0)                                                                                          
  73.                 System.out.print("\nNice try, "+player1+". That pile is empty. Choose again: ");
  74.             else
  75.                 System.out.print("\nNice try, "+player2+". That pile is empty. Choose again: ");
  76.             pile=scan.next();
  77.             }
  78.            
  79.            
  80.             counter++;
  81.            
  82.             System.out.print("How many to remove from pile "+pile+": ");
  83.             remove=scan.nextInt();
  84.            
  85.             /*while(remove<0){
  86.                 System.out.print("\nYou must choose at least 1. How many? ");
  87.                 remove=scan.nextInt();
  88.             }*/
  89.            
  90.             while((pile.equals("A")&&remove>pile_A) || (pile.equals("B")&&remove>pile_B) || (pile.equals("C")&&remove>pile_C) || remove<=0){
  91.                                                                                                                                                
  92.                 if(remove>0){
  93.                     System.out.print("\nPile "+pile+" doesn't have that many. Try again: ");
  94.                     remove=scan.nextInt();
  95.                 }
  96.                 else{
  97.                     System.out.print("\nYou must choose at least 1. How many? ");
  98.                     remove=scan.nextInt();
  99.                 }
  100.                    
  101.             }
  102.            
  103.             if(pile.equals("A"))
  104.                 pile_A=pile_A-remove;
  105.             rows=pile_A;
  106.                
  107.             if(pile.equals("B"))
  108.                 pile_B=pile_B-remove;
  109.             if(pile_B>rows)
  110.                 rows=pile_B;
  111.            
  112.             if(pile.equals("C"))
  113.                 pile_C=pile_C-remove;
  114.             if(pile_C>rows)
  115.                 rows=pile_C;
  116.            
  117.             System.out.println("");
  118.            
  119.         }
  120.        
  121.         if((pile_A==0 && pile_B==0 && pile_C==1) || (pile_A==0 && pile_B==1 && pile_C==0) || (pile_A==1 && pile_B==0 && pile_C==0)){
  122.              
  123.             if(counter%2!=0)
  124.                 System.out.println(player1+", you must take the last remaining counter, so you lose. "+player2+" wins!");      
  125.              else
  126.                 System.out.println(player2+", you must take the last remaining counter, so you lose. "+player1+" wins!");      
  127.         }
  128.        
  129.         else{
  130.          
  131.            if(counter%2==0)
  132.                 System.out.println(player2+", there are no counters left, so you WIN!");
  133.            else
  134.                 System.out.println(player1+", there are no counters left, so you WIN!");
  135.         }
  136.        
  137.         }  
  138.        
  139.         else if(versus.equals("Computer")){
  140.            
  141.         Random r=new Random();
  142.            
  143.         System.out.print("Player 1, enter your name: ");
  144.         player1=scan.next();
  145.         System.out.println("");
  146.        
  147.         rows=pile_A;
  148.         if(pile_B>rows)
  149.             rows=pile_B;
  150.         if(pile_C>rows)
  151.             rows=pile_C;
  152.        
  153.         counter=1;
  154.        
  155.         while((pile_A>0 || pile_B>0 || pile_C>0) && (pile_A!=1 || pile_B!=0 || pile_C!=0) &&
  156.               (pile_A!=0 || pile_C!=0 || pile_B>1) && (pile_A!=0 || pile_B!=0 || pile_C!=1)){
  157.            
  158.             i=0;
  159.              while(i<rows){
  160.            
  161.                 if(rows-i>pile_A)
  162.                     System.out.print("  ");
  163.                 else
  164.                     System.out.print("* ");
  165.            
  166.                 if(rows-i>pile_B)
  167.                     System.out.print("  ");
  168.                 else
  169.                     System.out.print("* ");
  170.            
  171.                 if(rows-i>pile_C)
  172.                     System.out.print("");
  173.                 else
  174.                     System.out.print("*");
  175.            
  176.                 System.out.println("");
  177.                 i++;
  178.              }
  179.         System.out.println("A B C");
  180.        
  181.             //System.out.println("A: "+pile_A+"\tB: "+pile_B+"\tC: "+pile_C);
  182.             pile="something";
  183.             if(counter%2!=0){
  184.                 System.out.print("\n"+player1+", choose a pile: ");
  185.                 pile=scan.next();
  186.             }
  187.             else{
  188.                 if(pile_A>0 && pile_B>0 && pile_C>0){  
  189.                     comppick=1+r.nextInt(3);
  190.                     if(comppick==1)
  191.                         pile="A";
  192.                     if(comppick==2)
  193.                         pile="B";
  194.                     if(comppick==3)
  195.                         pile="C";
  196.                 }
  197.                 else if((pile_A>0 && pile_B>0 && pile_C==0) || (pile_A>0 && pile_B==0 && pile_C>0) || (pile_A==0 && pile_B>0 && pile_C>0)){
  198.                     comppick=1+r.nextInt(2);
  199.                     if(pile_C==0){
  200.                         if(comppick==1)
  201.                             pile="A";
  202.                         if(comppick==2)
  203.                             pile="B";
  204.                     }
  205.                      if(pile_B==0){
  206.                         if(comppick==1)
  207.                             pile="A";
  208.                         if(comppick==2)
  209.                             pile="C";
  210.                     }
  211.                       if(pile_A==0){
  212.                         if(comppick==1)
  213.                             pile="B";
  214.                         if(comppick==2)
  215.                             pile="C";
  216.                     }
  217.                        
  218.                 }
  219.                 else if(pile_A==0 && pile_B==0)
  220.                         pile="C";
  221.                 else if(pile_A==0 && pile_C==0)
  222.                         pile="B";
  223.                 else if(pile_B==0 && pile_C==0)
  224.                         pile="A";
  225.                 else
  226.                     pile="whatever";
  227.                    
  228.                 }
  229.                
  230.                 System.out.println("\nComputer chose a pile: "+pile);
  231.             }    
  232.                    
  233.             while((pile.equals("A")&&pile_A==0) || (pile.equals("B")&&pile_B==0) || (pile.equals("C")&&pile_C==0)){  
  234.             if(counter%2!=0)                                                                                          
  235.                 System.out.print("\nNice try, "+player1+". That pile is empty. Choose again: ");
  236.            
  237.             }
  238.            
  239.               if(counter%2!=0){
  240.                 System.out.print("How many to remove from pile "+pile+": ");  
  241.                 remove=scan.nextInt();
  242.               }
  243.               else{
  244.                 if(pile.equals("A"))
  245.                     remove=1+r.nextInt(pile_A-1);
  246.                 else if(pile.equals("B"))
  247.                     remove=1+r.nextInt(pile_B-1);
  248.                 else
  249.                     remove=1+r.nextInt(pile_C-1);
  250.                
  251.                 System.out.println("How many to remove from pile "+pile+": "+remove);    
  252.               }
  253.            
  254.              counter++;
  255.             /*while(remove<0){
  256.                 System.out.print("\nYou must choose at least 1. How many? ");
  257.                 remove=scan.nextInt();
  258.             }*/
  259.            
  260.             while((pile.equals("A")&&remove>pile_A) || (pile.equals("B")&&remove>pile_B) || (pile.equals("C")&&remove>pile_C) || remove<=0){
  261.                                                                                                                                              //trzeba wybrac wiecej niz 0  
  262.                 if(remove>0){
  263.                     System.out.print("\nPile "+pile+" doesn't have that many. Try again: ");
  264.                     remove=scan.nextInt();
  265.                 }
  266.                 else{
  267.                     System.out.print("\nYou must choose at least 1. How many? ");
  268.                     remove=scan.nextInt();
  269.                 }
  270.                    
  271.             }
  272.            
  273.             if(pile.equals("A"))
  274.                 pile_A=pile_A-remove;
  275.             rows=pile_A;
  276.                
  277.             if(pile.equals("B"))
  278.                 pile_B=pile_B-remove;
  279.             if(pile_B>rows)
  280.                 rows=pile_B;
  281.            
  282.             if(pile.equals("C"))
  283.                 pile_C=pile_C-remove;
  284.             if(pile_C>rows)
  285.                 rows=pile_C;
  286.            
  287.             System.out.println("");
  288.            
  289.        
  290.        
  291.         if((pile_A==0 && pile_B==0 && pile_C==1) || (pile_A==0 && pile_B==1 && pile_C==0) || (pile_A==1 && pile_B==0 && pile_C==0)){
  292.              
  293.             if(counter%2!=0)
  294.                 System.out.println(player1+", you must take the last remaining counter, so you lose. Computer wins!");      
  295.              else
  296.                 System.out.println("Computer, you must take the last remaining counter, so you lose. "+player1+" wins!");      
  297.         }
  298.        
  299.         else{
  300.          
  301.            if(counter%2==0)
  302.                 System.out.println("Computer, there are no counters left, so you WIN!");
  303.            else
  304.                 System.out.println(player1+", there are no counters left, so you WIN!");
  305.         }
  306.            
  307.         }  
  308.         }
  309.        
  310.        
  311.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement