Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help to correct so this below Java code compile and ryn properly, and please send the code back with the changes in the code

Please help to correct so this below Java code compile and ryn properly, and please send the code back with the changes in the code in whole. 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);
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);
}
}
}

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_2

Step: 3

blur-text-image_3

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

Database And Expert Systems Applications 19th International Conference Dexa 2008 Turin Italy September 2008 Proceedings Lncs 5181

Authors: Sourav S. Bhowmick ,Josef Kung ,Roland Wagner

2008th Edition

3540856536, 978-3540856535

More Books

Students also viewed these Databases questions

Question

5. Develop a technique for gaining entry to a research site.

Answered: 1 week ago