Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help to correct the below Java code so output is like in the below pic. Please sent the code back with the changes in
Please help to correct the below Java code so output is like in the below pic. Please sent the code back with the changes in it in whole. Thanks! import java.util.Scanner;
class Human extends Player
private Scanner scanner;
public HumanString name
supername;
scanner new ScannerSystemin;
@Override
public int makeMoveint currentSticks
System.out.printlnname enter number of sticks to take to currentSticks : ;
int sticks scanner.nextInt;
while sticks sticks currentSticks
System.out.printlnInvalid number of sticks. Try again: ;
sticks scanner.nextInt;
return sticks;
abstract class Player
protected String name;
public PlayerString name
this.name name;
public abstract int makeMoveint currentSticks;
class Computer extends Player
public ComputerString name
supername;
@Override
public int makeMoveint currentSticks
int sticks intMathrandomcurrentSticks ;
System.out.printlnname takes sticks sticks.";
return sticks;
class NimGame
private int sticks;
private Player player;
private Player player;
public NimGameint sticks
this.sticks sticks;
this.player new ComputerComputer;
this.player new HumanHuman;
public void startGame
Player currentPlayer player;
while sticks
System.out.printlnCurrent sticks: sticks;
int takenSticks currentPlayer.makeMovesticks;
sticks takenSticks;
if sticks
System.out.printlnNo moves left. currentPlayer.name wins!";
break;
currentPlayer currentPlayer player player : player;
public class Main
public static void mainString args
Scanner scanner new ScannerSystemin;
System.out.printEnter the initial number of sticks: ;
int sticks scanner.nextInt;
if argslength
int initialSticks Integer.parseIntargs;
NimGame game new NimGameinitialSticks;
game.startGame;
else
System.out.printlnPlease provide the initial number of sticks as a command line argument." sticks;
$ java Nm
Welcome to
Player : Computer
Player : Human
Remaining matches:
Computer removes matches
Remaining matches:
Your move. There are matches
Human removes matches
Remaining matches:
Computer removes matches
Remaining matches:
Your move. There are matches
Human removes matches
Remaining matches:
Computer loses, Human wins.
$ java
Welcome to
Player : Computer
Player : Human
Remaining matches:
Computer removes matches
Remaining matches:
Your move. There are matches
Human removes matches
Remaining matches:
Computer removes matches
Remaining matches:
Human loses, Computer wins.
$ java
Welcome to
Player : Computer
Player : Human
Remaining matches:
Computer removes matches
Remaining matches:
Your move. There are matches
Sorry, illegal move.
Please choose at least one match and at most
Your move. There are matches
Sorry, illegal move.
Please choose at least one match and at most
Your move. There are matches
Human removes matches
Remaining matches:
Computer removes matches
Remaining matches:
Human loses, Computer wins.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started