Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package baby_nim;
- import java.util.Random;
- import java.util.Scanner;
- public class Baby_Nim {
- public static void main(String[] args) {
- int pile_A=3, pile_B=4,pile_C=5,remove,counter,rows,i,comppick;
- String pile,player1,player2,versus;
- Scanner scan=new Scanner(System.in);
- System.out.print("Want to battle another \"Player\" or \"Computer\"? ");
- versus=scan.next();
- if(versus.equals("Player")){
- System.out.print("Player 1, enter your name: ");
- player1=scan.next();
- System.out.print("Player 2, enter your name: ");
- player2=scan.next();
- System.out.println("");
- rows=pile_A;
- if(pile_B>rows)
- rows=pile_B;
- if(pile_C>rows)
- rows=pile_C;
- counter=1;
- while((pile_A>0 || pile_B>0 || pile_C>0) && (pile_A!=1 || pile_B!=0 || pile_C!=0) &&
- (pile_A!=0 || pile_C!=0 || pile_B!=1) && (pile_A!=0 || pile_B!=0 || pile_C!=1)){
- i=0;
- while(i<rows){
- if(rows-i>pile_A)
- System.out.print(" ");
- else
- System.out.print("* ");
- if(rows-i>pile_B)
- System.out.print(" ");
- else
- System.out.print("* ");
- if(rows-i>pile_C)
- System.out.print("");
- else
- System.out.print("*");
- System.out.println("");
- i++;
- }
- System.out.println("A B C");
- //System.out.println("A: "+pile_A+"\tB: "+pile_B+"\tC: "+pile_C);
- if(counter%2!=0)
- System.out.print("\n"+player1+", choose a pile: ");
- else
- System.out.print("\n"+player2+", choose a pile: ");
- pile=scan.next();
- while((pile.equals("A")&&pile_A==0) || (pile.equals("B")&&pile_B==0) || (pile.equals("C")&&pile_C==0)){
- if(counter%2!=0)
- System.out.print("\nNice try, "+player1+". That pile is empty. Choose again: ");
- else
- System.out.print("\nNice try, "+player2+". That pile is empty. Choose again: ");
- pile=scan.next();
- }
- counter++;
- System.out.print("How many to remove from pile "+pile+": ");
- remove=scan.nextInt();
- /*while(remove<0){
- System.out.print("\nYou must choose at least 1. How many? ");
- remove=scan.nextInt();
- }*/
- while((pile.equals("A")&&remove>pile_A) || (pile.equals("B")&&remove>pile_B) || (pile.equals("C")&&remove>pile_C) || remove<=0){
- if(remove>0){
- System.out.print("\nPile "+pile+" doesn't have that many. Try again: ");
- remove=scan.nextInt();
- }
- else{
- System.out.print("\nYou must choose at least 1. How many? ");
- remove=scan.nextInt();
- }
- }
- if(pile.equals("A"))
- pile_A=pile_A-remove;
- rows=pile_A;
- if(pile.equals("B"))
- pile_B=pile_B-remove;
- if(pile_B>rows)
- rows=pile_B;
- if(pile.equals("C"))
- pile_C=pile_C-remove;
- if(pile_C>rows)
- rows=pile_C;
- System.out.println("");
- }
- 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)){
- if(counter%2!=0)
- System.out.println(player1+", you must take the last remaining counter, so you lose. "+player2+" wins!");
- else
- System.out.println(player2+", you must take the last remaining counter, so you lose. "+player1+" wins!");
- }
- else{
- if(counter%2==0)
- System.out.println(player2+", there are no counters left, so you WIN!");
- else
- System.out.println(player1+", there are no counters left, so you WIN!");
- }
- }
- else if(versus.equals("Computer")){
- Random r=new Random();
- System.out.print("Player 1, enter your name: ");
- player1=scan.next();
- System.out.println("");
- rows=pile_A;
- if(pile_B>rows)
- rows=pile_B;
- if(pile_C>rows)
- rows=pile_C;
- counter=1;
- while((pile_A>0 || pile_B>0 || pile_C>0) && (pile_A!=1 || pile_B!=0 || pile_C!=0) &&
- (pile_A!=0 || pile_C!=0 || pile_B>1) && (pile_A!=0 || pile_B!=0 || pile_C!=1)){
- i=0;
- while(i<rows){
- if(rows-i>pile_A)
- System.out.print(" ");
- else
- System.out.print("* ");
- if(rows-i>pile_B)
- System.out.print(" ");
- else
- System.out.print("* ");
- if(rows-i>pile_C)
- System.out.print("");
- else
- System.out.print("*");
- System.out.println("");
- i++;
- }
- System.out.println("A B C");
- //System.out.println("A: "+pile_A+"\tB: "+pile_B+"\tC: "+pile_C);
- pile="something";
- if(counter%2!=0){
- System.out.print("\n"+player1+", choose a pile: ");
- pile=scan.next();
- }
- else{
- if(pile_A>0 && pile_B>0 && pile_C>0){
- comppick=1+r.nextInt(3);
- if(comppick==1)
- pile="A";
- if(comppick==2)
- pile="B";
- if(comppick==3)
- pile="C";
- }
- 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)){
- comppick=1+r.nextInt(2);
- if(pile_C==0){
- if(comppick==1)
- pile="A";
- if(comppick==2)
- pile="B";
- }
- if(pile_B==0){
- if(comppick==1)
- pile="A";
- if(comppick==2)
- pile="C";
- }
- if(pile_A==0){
- if(comppick==1)
- pile="B";
- if(comppick==2)
- pile="C";
- }
- }
- else if(pile_A==0 && pile_B==0)
- pile="C";
- else if(pile_A==0 && pile_C==0)
- pile="B";
- else if(pile_B==0 && pile_C==0)
- pile="A";
- else
- pile="whatever";
- }
- System.out.println("\nComputer chose a pile: "+pile);
- }
- while((pile.equals("A")&&pile_A==0) || (pile.equals("B")&&pile_B==0) || (pile.equals("C")&&pile_C==0)){
- if(counter%2!=0)
- System.out.print("\nNice try, "+player1+". That pile is empty. Choose again: ");
- }
- if(counter%2!=0){
- System.out.print("How many to remove from pile "+pile+": ");
- remove=scan.nextInt();
- }
- else{
- if(pile.equals("A"))
- remove=1+r.nextInt(pile_A-1);
- else if(pile.equals("B"))
- remove=1+r.nextInt(pile_B-1);
- else
- remove=1+r.nextInt(pile_C-1);
- System.out.println("How many to remove from pile "+pile+": "+remove);
- }
- counter++;
- /*while(remove<0){
- System.out.print("\nYou must choose at least 1. How many? ");
- remove=scan.nextInt();
- }*/
- while((pile.equals("A")&&remove>pile_A) || (pile.equals("B")&&remove>pile_B) || (pile.equals("C")&&remove>pile_C) || remove<=0){
- //trzeba wybrac wiecej niz 0
- if(remove>0){
- System.out.print("\nPile "+pile+" doesn't have that many. Try again: ");
- remove=scan.nextInt();
- }
- else{
- System.out.print("\nYou must choose at least 1. How many? ");
- remove=scan.nextInt();
- }
- }
- if(pile.equals("A"))
- pile_A=pile_A-remove;
- rows=pile_A;
- if(pile.equals("B"))
- pile_B=pile_B-remove;
- if(pile_B>rows)
- rows=pile_B;
- if(pile.equals("C"))
- pile_C=pile_C-remove;
- if(pile_C>rows)
- rows=pile_C;
- System.out.println("");
- 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)){
- if(counter%2!=0)
- System.out.println(player1+", you must take the last remaining counter, so you lose. Computer wins!");
- else
- System.out.println("Computer, you must take the last remaining counter, so you lose. "+player1+" wins!");
- }
- else{
- if(counter%2==0)
- System.out.println("Computer, there are no counters left, so you WIN!");
- else
- System.out.println(player1+", there are no counters left, so you WIN!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement