Ok so In my java programming class we were supposed to write a class pretty much that makes the "CheckersTable" class work. I am confused with the error message I am getting as I can't find any apparent errors in my program. The error isn't with the CheckersTable class, as we were instructed not to edit it, just to utilize it (though I will include it in my post anyway just in case it would be useful in helping me figure this out). The run-time error doesn't occur until after the program has gathered the data from the user.
If anyone has any pointers as to the error I'm getting I would me most grateful for your help.
Error message :
Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Random.java:265)
at Contestant.takeTurn(Contestant.java:50)
at CheckersTable.simulateTurn(CheckersTable.java:65)
at CheckersTable.simulateGame(CheckersTable.java:46)
at checkers.main(checkers.java:37)
Main class, this is what I have to execute to pass this off
import java.util.Scanner; public class checkers { public static void main (String[] args) { String name1, name2; int skilllevel1, skilllevel2, maxturntime1, maxturntime2; Scanner scan = new Scanner (System.in); System.out.println ("Player 1 Name"); name1 = scan.next(); System.out.println ("Player 1 Skill Level (1-10)"); skilllevel1 = scan.nextInt(); System.out.println ("Player 1 max time per turn"); maxturntime1 = scan.nextInt(); System.out.println ("Player 2 Name"); name2 = scan.next(); System.out.println ("Player 2 Skill Level (1-10)"); skilllevel2 = scan.nextInt(); System.out.println ("Player 2 max time per turn"); maxturntime2 = scan.nextInt(); Contestant player1 = new Contestant(name1, skilllevel1, maxturntime1); Contestant player2 = new Contestant(name2, skilllevel2, maxturntime2); int gametimeperplayer; gametimeperplayer = ((maxturntime1 + maxturntime2)/2); CheckersTable game = new CheckersTable(player1, player2, gametimeperplayer); game.simulateGame(); Contestant gamehistory; gamehistory = game.getWinner(); String gamewinner; gamewinner = game.getMoveHistory(); System.out.println (gamewinner + " won the game"); System.out.println (gamehistory); int clock1, clock2; clock1 = player1.getClockTime(); clock2 = player2.getClockTime(); System.out.println ("Player 1 had " + clock1 + " Seconds left"); System.out.println ("Player 2 had " + clock2 + " Seconds left"); int pieces1, pieces2; pieces1 = player1.getNumPieces(); pieces2 = player2.getNumPieces(); System.out.println ("Player 1 had " + pieces1 + " pieces left"); System.out.println ("Player 2 had " + pieces2 + " Pieces left"); } }
Contestant class, I had to write this as well.
import java.util.Random; public class Contestant { String Name; int skillValue, maxTurnTime, timeRemaining, piecesLost, numlost, seconds, remaining; public Contestant (String name, int Skillvalue, int maxturntime) { name = Name; Skillvalue = skillValue; maxturntime = maxTurnTime; } public int getClockTime() { return seconds; } public String getName() { return Name; } public int getNumPieces() { int remaining = 12; remaining = remaining - numlost; return remaining; } public int getSkillValue() { return skillValue; } public void initializeGameClock (int seconds) { seconds = timeRemaining; } public void losePieces (int numlost) { numlost = piecesLost; } public int takeTurn () { Random turnTime = new Random(); int turnDuration; turnDuration = turnTime.nextInt(maxTurnTime); timeRemaining = timeRemaining - turnDuration; return turnDuration; } }
This is the class I am not supposed to have to even look at
Also, this is the link to the API which we were supposed to design the Contestant class around and by which we were supposed to utilize the CheckersTable class./** * Checkers table is a class provided to you for lab 3. This class contains methods necessary for simulating a checkers game. * Remember, this class has been provided for you and your job is to study the methods so you can use them to complete the lab assignment. */ public class CheckersTable { private Contestant ad3f5d; private Contestant f21c3; private int c3dd3a; private String b3e342; private Contestant da329f; /** * * @param player1 The fist player in the checkers game * @param player2 The second player in the checkers game * @param timePerPlayer The number of seconds given to each player for their moves */ public CheckersTable(Contestant player1, Contestant player2, int timePerPlayer) { this.ad3f5d = player1; this.f21c3 = player2; this.c3dd3a = timePerPlayer; this.b3e342 = "MOVE HISTORY:\nName\t\tOpponent's Pieces Captured\t\tTime Left\tPieces Left\n-----------------------------------------------------------------------------------\n"; this.da329f = null; } /** * Simulates a game on the checkers table with the two players specified when the CheckersTable object was constructed. * After a game has been simulated, the getMoveHistory and getWinner can be used to obtain information about the simulated game. */ public void simulateGame() { this.ad3f5d.initializeGameClock(this.c3dd3a); this.f21c3.initializeGameClock(this.c3dd3a); int i = 0; do { simulateTurn(++i); } while (!(isGameOver())); } private void simulateTurn(int paramInt) { Contestant localContestant1; Contestant localContestant2; if (paramInt % 2 == 0) { localContestant1 = this.ad3f5d; localContestant2 = this.f21c3; } else { localContestant1 = this.f21c3; localContestant2 = this.ad3f5d; } int i = localContestant1.takeTurn(); int j = calcPiecesCaptured(localContestant1, localContestant2); localContestant2.losePieces(j); CheckersTable tmp57_56 = this; tmp57_56.b3e342 = tmp57_56.b3e342 + localContestant1.getName() + "\t\t" + j + " captured in " + i + " seconds" + (i < 10 ? "\t\t\t" : "\t\t") + localContestant1.getClockTime() + " seconds\t" + localContestant1.getNumPieces() + " pieces\n"; } private int calcPiecesCaptured(Contestant paramContestant1, Contestant paramContestant2) { int j; int i = (paramContestant1.getSkillValue() - paramContestant2.getSkillValue()) * 4; double d = Math.random() * 100.0D + i; if ((d < 45.0D) || (paramContestant1.getClockTime() <= 0)) j = 0; else { d -= 45.0D; if (d < 45.0D) j = 1; else j = 2; } if (paramContestant2.getNumPieces() < j) j = paramContestant2.getNumPieces(); return j; } private boolean isGameOver() { boolean i = true; if (this.ad3f5d.getClockTime() <= 0) this.da329f = this.f21c3; else if (this.f21c3.getClockTime() <= 0) this.da329f = this.ad3f5d; else if (this.ad3f5d.getNumPieces() <= 0) this.da329f = this.f21c3; else if (this.f21c3.getNumPieces() <= 0) this.da329f = this.ad3f5d; else i = false; return i; } /** * Gets the move history for the game after the game has been simulated. * * @return A string containing the whole move history for the game */ public String getMoveHistory() { return this.b3e342; } /** * Gets the winner of the game after the game has been simulated. * * @return A contestant object corresponding to the winner of the game */ public Contestant getWinner() { return this.da329f; } }
Generated Documentation (Untitled)