Вопрос игры Java Nim - компьютер забирает больше объектов, чем осталось в куче?

Я застрял на последних шагах моей программы Nim, и это сводит меня с ума. Мой цикл работал отлично, и я начал возиться с решением компьютера выбрать случайное целое число от 1 до 10 и запретить его способность брать больше, чем осталось в выбранной стопке. Теперь я получаю сообщение об ошибке: Исключение в потоке "основной" Компьютер берет 9 из кучи B (например)

java.lang.IllegalArgumentException: bound must be positive
    at java.util.Random.nextInt(Unknown Source)
    at Nim.computerMove(Nim.java:144)
    at Nim.playerMove(Nim.java:81)
    at Assign3.main(Assign3.java:17)

У меня есть два других класса, но я не думаю, что они здесь актуальны. Может кто-нибудь помочь мне исправить это?

import java.util.Random;
import java.util.Scanner;

public class Nim {

    private Pile pileA;                                 // first pile
    private Pile pileB;                                 // second pile
    private Pile pileC;                                 // third pile
    private Random rnd = new Random();                  // random number generator
    private Scanner input = new Scanner(System.in);     // scanner for all user input

    private int a, b, c, remove;
    char letter;
    boolean human = true;

    private int compLetter = (char)rnd.nextInt(3) + 'a';
    private int compRemove = rnd.nextInt(10) + 1;

    public Nim() {                                      // default constructor  
        System.out.println("Welcome to the NIM game");
        System.out.println("We play by the misère rules");

        a = 10;
        b = 10;
        c = 10;

        pileA = new Pile(10);
        pileA.getSize();

        pileB = new Pile(10);
        pileB.getSize();

        pileC = new Pile(10);
        pileC.getSize();

        printPiles();
    }

    public boolean playerMove() {                       // rules to handle user input

        while (true) {
            if (a == 0 && b == 0 && c == 0) {
                System.out.println("Sorry: you lose");
            }   
            if (a + b + c == 1) {
                System.out.println("Congrats: you win");
            }

        do {
            System.out.print("\nSelect a pile: ");
            letter = input.next().charAt(0);

            if (letter == 'a' || letter == 'b' || letter == 'c') {
                if (letter == 'a' && a == 0 || letter == 'b' && b == 0 || letter == 'c' && c == 0) {
                    System.err.println("Pile is empty, pick another");
                } else {
                    break;
                }
            } else {
                System.err.println("Invalid pile letter, select a, b, or c");
            }

        } while(true); 
            System.out.print("How many do you want to remove? ");
            remove = input.nextInt();

            if (a != 0 || b != 0 || c != 0) {
                }if (letter == 'a') {
                    a = a - remove;
                    human = false;
                    computerMove();
                }else if (remove >= a) {
                    System.out.println("Enter a number less than " + a);
                }else if (letter == 'b') {
                    b = b - remove;
                    human = false;
                    computerMove();
                }else if (remove >= b) {
                    System.out.println("Enter a number less than " + b);
                }else if (letter == 'c') {
                    c = c - remove;
                    human = false;
                    computerMove();
                }else if (remove >= c) {
                    System.out.println("Enter a number less than " + c);
                } else {
            }

        }

        }

    private void computerMove() {                       // rules to handle computer move

        printPiles();
        human = false;

        while (human == false) {

            compLetter = (char)rnd.nextInt(3) + 'a';
            compRemove = rnd.nextInt(10);

            while (compLetter == 'a') {

                if (a == 0) {
                //    compLetter = (char)rnd.nextInt(3) + 'a';
//                    System.err.println("Pile A is empty, pick another");
//                    printPiles();
//                    compLetter = (char)rnd.nextInt(3) + 'a';
                    break;
                } else {
                    a = a - compRemove;
                    System.out.println("Computer takes " + compRemove + " from pile A");
                    compRemove = rnd.nextInt(a);

                    human = true;
                    break;
                }
            }

            while (compLetter == 'b') {

                if (b == 0) {
             //     compLetter = (char)rnd.nextInt(3) + 'a';
//                    System.err.println("Pile B is empty, pick another");
//                    printPiles();
//                    compLetter = (char)rnd.nextInt(3) + 'a';
                    break;
                } else {
                    b = b - compRemove;
                    System.out.println("Computer takes " + compRemove + " from pile B");
                    compRemove = rnd.nextInt(b);

                    human = true;
                    break;
                }
            }

           while (compLetter == 'c') {

                if (c == 0) {
                //  compLetter = (char)rnd.nextInt(3) + 'a';
//                    System.err.println("Pile C is empty, pick another");
//                    printPiles();
//                    compLetter = (char)rnd.nextInt(3) + 'a';
                    break;
                } else {
                    c = c - compRemove;
                    System.out.println("Computer takes " + compRemove + " from pile C");

                    human = true;
                    break;
                }
           }
        }
            printPiles();
        } 

    public boolean done() {                     // is the game done?

    //  System.out.println("Game over.");

        return true;
    }

    public void printPiles() {                  // print the current state of the piles
        System.out.println("      A     B     C");
        System.out.println("      "+a+"    "+b+"    "+c);
    }
}

0 ответов

Другие вопросы по тегам