The aim of Gue55ing Game is for a person and the computer to compete against oth
ID: 3702619 • Letter: T
Question
The aim of Gue55ing Game is for a person and the computer to compete against other to correctly guess a hidden number.
A game consists of four rounds. For each round a number between 1 and 100 (inclusive) is randomly generated and the players (person and computer) take turns to guess the number. The round ends when the correct guess is given or each player has had three guesses.
If a player guesses the number correctly then they are awarded points according to how many attempts were taken to guess the number. If the round ends without either player guessing correctly then the points are awarded to each player according to how close they were to the hidden number.
At the end of the four rounds the player with the highest cumulative score wins the game.
Game play
The Gue55ing Game game begins with a message inviting the human player to enter their name. The name can only contain characters (except all spaces, including leading and trailing spaces) and must be between 1 and 8 characters in length (inclusive). The other player will be the computer. A number with a value from 1 to 100 (inclusive) is randomly generated but hidden from the players. The player who will have the first turn at guessing the number is then randomly chosen by the computer. The round then progresses with the players taking turns until the correct number is guessed. Note that your program will generate a number guess for the computer player.
The following are the game rules:
• If the number is not guessed correctly then a message is displayed indicating whether the entered number was higher or lower than the hidden number and the other player takes a turn. Note this means that after each turn the range of possible numbers is reduced. For example: o When the game starts the original range is 1 – 100. The hidden number generated is 75
o First turn, the player guesses 50, this is lower. So now the new range for the computer to guess is between 51 and 100.
o Second turn, the other player guesses 90, this is higher. So now the new range for the player to guess is between 51 and 89.
o This goes on for each guess.
• If the player enters a number between 1 and 100 but not within the range of possible numbers (as explained above), then the player is given a warning message but is not given a chance to renter the number.
• If the player enters a number less than 1 or greater than 100, then a warning message is displayed and the player is invited to enter another number (with no penalty).
• If the player enters non-numeric characters, then a warning message is displayed and the player is invited to enter another number (with no penalty).
• If the human player enters 999 this indicates that they have decided to abandon the round. To simulate this for the computer player, at the start of each round, generate a random number between 1 and 20. This number will be known as the ‘abandon round indicator’. Then before each guess the computer player makes, generate a random number between 1 and 20. If this number is the same as the ‘abandon round indicator’ then the round is abandoned. If these numbers are not the same, the computer continues with its guess of the hidden number.
o Abandoning a round means no scores are assigned to either player for that round.
• If a player correctly guesses the number then the round ends, points are awarded to this player according to how many attempts have been made. Note the total number of attempts includes attempts by both players. The other player scores zero for the game.
Number of attempts
Score
1
20
2
15
3
11
4
8
5
6
6
5
If the round ends and no player has guessed the number then each player is awarded a score according to the proximity of their last guess to the hidden number, as follows:
score = 10 – proximity-of-last-guess (Note if proximity-of-last-guess is 10 or more then the score is 0 (zero))
For example, if the hidden number was 63 and the last guess for player1 was 53 and the last guess for player2 was 68, then player1 would score 0 (zero) and player2 would score 5.
Note: the ‘last guess’ here is referring to the third guess of each player in that particular round.
Program design
Your program should consist of at least three classes: Player, Game and RandomNumber. The following two sections give details of these classes. Students are advised to follow good programming practices and to use loops and appropriate field where required to ensure good program design.
Player class
The Player class will specify the attributes and behaviours of a player. An object of the Player class will have the following fields (at least):
Name – the name of the player.
Score – the cumulative game score
Guesses – the last number guessed for the current round
The data type of each field must be chosen carefully and you must be able to justify the choice of the data type of the fields. You may want to include comments in the class to state the assumption made. The class must also have a default constructor and a non-default constructor that accepts a value for the name of the player.
Player class no
The Player class should also have appropriate accessor and mutator methods for its fields. You should not allow an object of class Player to be set to an invalid state. There should be no input from the terminal or output to the screen. A Player object should also be able to return its state in the form of a String.
Game class
The Game class will be in the same BlueJ project that contains your Player class. The Game class will manage the playing of a game. It will have the following fields (at least):
Player1 (an object of type Player)
Player2 (an object of type Player)
Note that one of these players will be the computer.
The Game class will have methods to manage the playing of the game. These should include (at least) the following behaviours:
• Display a welcome message on the screen.
• Request the player to enter their name.
• Request the player to enter a number.
• Compare the number entered by a player with the hidden number.
• Display the result of the attempt at guessing the number.
• Display the result for the end of a round (including the value of the hidden number).
• Display the game result.
RandomNumber class
An object of the RandomNumber class will generate a random number from 1 to a maximum value specified.
Note: Please use iteration (or loop or repetition) for the Game method.
Number of attempts
Score
1
20
2
15
3
11
4
8
5
6
6
5
Explanation / Answer
The package for these class is “GuessingGame”.
Most of the sub parts are taken care of, baring a few. Please look at the comments in the classes to take it forward.
Please upvote if you find the answer satisfactory :)
PLAYER CLASS
package GuessingGame;
public class Player {
String name;
Integer score;
Integer guesses;
Integer upper;
Integer lower;
public Player(String name) {
this.name = name;
this.score=0;
this.guesses=0;
this.upper=100;
this.lower=1;
}
public Player() {
this.name = "";
this.score=0;
this.guesses=0;
this.upper=100;
this.lower=1;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public Integer getGuesses() {
return guesses;
}
public void setGuesses(Integer guesses) {
this.guesses = guesses;
}
@Override
public String toString() {
return "Player [Name=" + name + ", Score=" + score + "]";
}
public Integer getUpper() {
return upper;
}
public void setUpper(Integer upper) {
this.upper = upper;
}
public Integer getLower() {
return lower;
}
public void setLower(Integer lower) {
this.lower = lower;
}
}
RANDOM NUMBER CLASS
package GuessingGame;
import java.util.Random;
public class RandomNumber {
int randomNumber;
public RandomNumber() {
Random rand = new Random();
this.randomNumber=rand.nextInt(99)+1;
}
public int compareNumber(int i){
if(this.randomNumber==i)
return 0;
else if (this.randomNumber<i)
return 1;
else
return -1;
}
public int getRandomNumber() {
return randomNumber;
}
public void setRandomNumber(int randomNumber) {
this.randomNumber = randomNumber;
}
}
GAME CLASS
package GuessingGame;
import java.util.Random;
import java.util.Scanner;
public class Game {
public Game() {
this.Player1=new Player("Computer");
this.sc=new Scanner(System.in);
this.hiddenNumber=new RandomNumber();
}
Player Player1;
Player Player2;
RandomNumber hiddenNumber;
public static Scanner sc;
public void displayWelcomeMessage(){
System.out.println("----------------------------- Welcome to the Gue55ing game! ----------------------------- ");
}
public void enterName(){
System.out.println("Please enter your Name:");
String name;
while(true){
name=sc.next();
name=name.trim();
if(name.length()<=8 && name.length()>=1) //add number condition here
{
break;
}
else{
System.out.println("The name should be between 1 and 8 characters in length. Please enter your Name:");
}
}
System.out.println("Hello, "+name+"!");
this.setPlayer2(new Player(name));
}
public void enterNumber(){
//Abandon round indicator to be implemented
System.out.println("Please enter a number between "+this.getPlayer2().getLower()+" and "+this.getPlayer2().getUpper());
while(true){
int guess=sc.nextInt();
if(guess>=this.getPlayer2().getLower() && guess<=this.getPlayer2().getUpper()) //add number condition here - there should be no numbers in name
{
this.getPlayer2().setGuesses(guess);
break;
}
else{
System.out.println("The number should be between "+this.getPlayer2().getLower()+" and"
+ " "+this.getPlayer2().getUpper()+"."
+ " Please enter your Name:");
}
}
}
public int guessNumberForComputer(){
//Should the other player see the guess the computer made?? Modify the code accordingly
Random rand = new Random();
int random=rand.nextInt(this.getPlayer1().getUpper()-this.getPlayer1().getLower())+1;
this.getPlayer1().setGuesses(random);
System.out.println("Computer guessed : "+random);
int result=displayTheResultOfGuess(this.getPlayer1());
return result;
}
public int displayTheResultOfGuess(Player p){
if(hiddenNumber.compareNumber(p.guesses)==0)
{
System.out.println("Number guessed by "+p.getName()+" is correct");
return hiddenNumber.compareNumber(p.guesses);
}
else if(hiddenNumber.compareNumber(p.guesses)==1)
{
p.setUpper(p.getGuesses()-1);
System.out.println("Number guessed by "+p.getName()+" is higher");
return hiddenNumber.compareNumber(p.guesses);
}
else
{
p.setLower(p.getGuesses()+1);
System.out.println("Number guessed by "+p.getName()+" is lower");
return hiddenNumber.compareNumber(p.guesses);
}
}
public void displayEndOfRound(){
System.out.println("Round ended!");
System.out.println(Player1.toString());
System.out.println(Player2.toString());
System.out.println("Hidden number:" +this.hiddenNumber.getRandomNumber());
}
public void displayEndOfGame(){
System.out.println("Game ended!");
System.out.println(Player1.toString());
System.out.println(Player2.toString());
System.out.println(((Player1.getScore()>Player2.getScore())?Player1.getName():Player2.getName())+" Won!");
}
public int compareNumber(Player p){
return hiddenNumber.compareNumber(p.guesses);
}
public Player getPlayer1() {
return Player1;
}
public void setPlayer1(Player player1) {
Player1 = player1;
}
public Player getPlayer2() {
return Player2;
}
public void setPlayer2(Player player2) {
Player2 = player2;
}
}
GAME DRIVER CLASS
package GuessingGame;
import java.util.Random;
public class GameDriver {
public static void main(String[] args) {
Game game=new Game();
game.displayWelcomeMessage();
game.enterName();
Player currentPlayer=null;
Player otherPlayer=null;
int guess=0;
for(int i=1;i<5;i++){
System.out.println("Round "+i);
guess=0;
for(int j=1;j<7;j++)
{
if(j==1){
Random rand = new Random();
int random=rand.nextInt(10);
if(random%2==0){
currentPlayer=game.getPlayer1();
otherPlayer=game.getPlayer2();
System.out.println(game.getPlayer1().getName()+ " goes first!");
}else
{
currentPlayer=game.getPlayer2();
otherPlayer=game.getPlayer1();
System.out.println(game.getPlayer2().getName()+ " goes first!");
}
}
if(!currentPlayer.getName().equals("Computer"))
{
game.enterNumber();
guess++;
if(game.displayTheResultOfGuess(currentPlayer)==0){
currentPlayer.score=+guess;
i++;
break;
}
currentPlayer=otherPlayer;
otherPlayer=game.getPlayer2();
}
else{
//game.guessNumberForComputer();
guess++;
if(game.guessNumberForComputer()==0){
currentPlayer.score=+guess;
i++;
break;
}
currentPlayer=otherPlayer;
otherPlayer=game.getPlayer1();
}
}
game.displayEndOfRound();
}
game.displayEndOfGame();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.