lundi 12 novembre 2018

Nim game between computer and user using random

I have part of a code that currently takes users input and "suppose" to subtract the amount selected but it is doing the calculation part I desperately need help been wracking my brain on this for almost 3 days. It is able to compile and accept a letter and a number from user but when it reaches the part to calculate the program stops.

    import java.util.*;
import java.security.*;
import java.lang.*;

public class Nim {
private Pile pileA;
private Pile pileB;
private Pile pileC;
private String letter;
private int amt1,amt2,amt3;
private Random rnd = new Random();
private Scanner input = new Scanner(System.in);

public Nim() {
    System.out.println("Welcome to the NIM game");
    System.out.println("A\t"+"B\t"+"C");

    pileA = new Pile();
    amt1 = rnd.nextInt(10);
    pileA.getSize(amt1);

    pileB = new Pile();
    amt2 = rnd.nextInt(10);
    pileB.getSize(amt2);

    pileC = new Pile();
    amt3 = rnd.nextInt(10);
    pileC.getSize(amt3);


}
public boolean PlayerMove() {

if(amt1!=0 && amt2!=0 && amt3!=0) {
    System.out.println(amt1+"\t"+amt2+"\t"+amt3+"\n");
    System.out.println("Select a pile: ");
    letter = input.nextLine();
while(!Arrays.asList("A","a","B","b","C","c").contains(letter)) {   
    System.out.println("Invalid pile letter, select a, b or c");
    System.out.println("A\t"+"B\t"+"C\t");
    System.out.println(amt1+"\t"+amt2+"\t"+amt3);
    letter =input.nextLine();
}


    if(letter.equals("a")||letter.equals("A")){ 
        if(amt1!=0) {
    System.out.println("How many do you want to remove?");
        pileA.remove(amt1);
        }else {
        System.out.println("Pile is A empty, pick another");
        System.out.println("A\t"+"B\t"+"C");
        System.out.println(amt1+"\t"+amt2+"\t"+amt3+"\n");
        System.out.println("Select a pile: ");
        input.nextLine();
            }
    }


if(letter.equals("b")||letter.equals("B")){ 
        if(amt2!=0) {
        System.out.println("How many do you want to remove?");
        pileB.remove(amt2);
        }else {
            System.out.println("Pile is B empty, pick another");
            System.out.println("A\t"+"B\t"+"C");
            System.out.println(amt1+"\t"+amt2+"\t"+amt3+"\n");
            System.out.println("Select a pile: ");
            input.nextLine();
            }
}


if(letter.equals("c")||letter.equals("C")){ 
        if(amt3!=0) {
        System.out.println("How many do you want to remove?");
        System.out.println(amt1+"\t"+amt2+"\t"+amt3);
        pileC.remove(amt3);
        }else {
            System.out.println("Pile is C empty, pick another");
            System.out.println("A\t"+"B\t"+"C");
            System.out.println(amt1+"\t"+amt2+"\t"+amt3+"\n");
            System.out.println("Select a pile: ");
            input.nextLine();
                }
            }
    }
System.out.println("A\t"+"B\t"+"C");
System.out.println(amt1+"\t"+amt2+"\t"+amt3);

    return false;

}




public void computerMove() {


    int aiAmt = rnd.nextInt();
    String [] letters = {"A","a","B","b","C","c"};
    letter = letters[new Random().nextInt(letters.length)];

        if(letter.equals("a")||letter.equals("A")){ 
            if(amt1!=0) {   
            System.out.println("Computer takes "+aiAmt+" from pile "+letter);
            amt1 = amt1-aiAmt;
            }
        else {
            String lettersBC[] = {"B","b","C","c"};
            letter = lettersBC[new Random().nextInt(lettersBC.length)];
        }



        if(letter.equals("b")||letter.equals("B")){ 
            if(amt2!=0) {
            System.out.println("Computer takes "+aiAmt+" from pile "+letter);
            amt2 = amt2-aiAmt;
            }
            else {
                String lettersAC[] = {"A","a","C","c"};
                letter = lettersAC[new Random().nextInt(lettersAC.length)];
            }
        }

        if(letter.equals("c")||letter.equals("C")){ 
            System.out.println("Computer takes "+aiAmt+" from pile "+letter);
            amt3 = amt3-aiAmt;
            }   
            else {
                String lettersAB[] = {"A","a","B","b"};
                letter = lettersAB[new Random().nextInt(lettersAB.length)];
            }

        }
}


import java.util.*;
import java.text.*;


public class Pile {
private int size;
private Scanner input = new Scanner(System.in);

public Pile() {

}


public int getSize(int amount) {
    size =amount;
    return size;
}


public void remove(int num) {
    int remove;
    remove=input.nextInt();
    if (remove>num) {
        System.out.println("Pick a number between 1 and "+num);
        remove=input.nextInt();
    }else
    getSize(num-remove);

}




Aucun commentaire:

Enregistrer un commentaire