Answered step by step
Verified Expert Solution
Link Copied!

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 Human(String name){
super(name);
scanner = new Scanner(System.in);
}
@Override
public int makeMove(int currentSticks){
System.out.println(name +", enter number of sticks to take (1 to "+ currentSticks /2+"): ");
int sticks = scanner.nextInt();
while (sticks 1|| sticks > currentSticks /2){
System.out.println("Invalid number of sticks. Try again: ");
sticks = scanner.nextInt();
}
return sticks;
}
}
abstract class Player {
protected String name;
public Player(String name){
this.name = name;
}
public abstract int makeMove(int currentSticks);
}
class Computer extends Player {
public Computer(String name){
super(name);
}
@Override
public int makeMove(int currentSticks){
int sticks =1+(int)(Math.random()*(currentSticks /2));
System.out.println(name +" takes "+ sticks +" sticks.");
return sticks;
}
}
class NimGame {
private int sticks;
private Player player1;
private Player player2;
public NimGame(int sticks){
this.sticks = sticks;
this.player1= new Computer("Computer");
this.player2= new Human("Human");
}
public void startGame(){
Player currentPlayer = player1;
while (sticks >1){
System.out.println("Current sticks: "+ sticks);
int takenSticks = currentPlayer.makeMove(sticks);
sticks -= takenSticks;
if (sticks =1){
System.out.println("No moves left. "+ currentPlayer.name +" wins!");
break;
}
currentPlayer =(currentPlayer == player1)? player2 : player1;
}
}
}
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the initial number of sticks: ");
int sticks = scanner.nextInt();
// if (args.length >0){
// int initialSticks = Integer.parseInt(args[0]);
// NimGame game = new NimGame(initialSticks);
// game.startGame();
//} else {
// System.out.println("Please provide the initial number of sticks as a command line argument." + sticks);
}
} $ java Nm 5
Welcome to Nm
Player 1: Computer
Player 2: Human
Remaining matches: 5
Computer removes 1 matches
Remaining matches: 4
Your move. There are 4 matches
1
Human removes 1 matches
Remaining matches: 3
Computer removes 1 matches
Remaining matches: 2
Your move. There are 2 matches
1
Human removes 1 matches
Remaining matches: 1
Computer loses, Human wins.
$ java Nm5
Welcome to Nm
Player 1: Computer
Player 2: Human
Remaining matches: 5
Computer removes 1 matches
Remaining matches: 4
Your move. There are 4 matches
2
Human removes 2 matches
Remaining matches: 2
Computer removes 1 matches
Remaining matches: 1
Human loses, Computer wins.
$ java Nm5
Welcome to Nm
Player 1: Computer
Player 2: Human
Remaining matches: 5
Computer removes 1 matches
Remaining matches: 4
Your move. There are 4 matches
9
Sorry, illegal move.
Please choose at least one match and at most 2
Your move. There are 4 matches
-1
Sorry, illegal move.
Please choose at least one match and at most 2
Your move. There are 4 matches
2
Human removes 2 matches
Remaining matches: 2
Computer removes 1 matches
Remaining matches: 1
Human loses, Computer wins.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logics For Databases And Information Systems

Authors: Jan Chomicki ,Gunter Saake

1st Edition

1461375827, 978-1461375821

More Books

Students also viewed these Databases questions

Question

6. Describe why communication is vital to everyone

Answered: 1 week ago