Question

In: Computer Science

Objective: Write a program which simulates a hot potato game. In this version of a classic...

Objective:

Write a program which simulates a hot potato game. In this version of a classic game, two or more players compete to see who can hold onto a potato the longest without getting caught. First the potato is assigned a random value greater than one second and less than three minutes both inclusive. This time is the total amount of time the potato may be held in each round. Next players are put into a circular list. Then each person gets possession of the potato in order. The player with the potato indicates how long they wish to hold on to it by entering a number from 1-10 seconds. If the player’s time is less than the remaining potato possession time then it moves on to the next player. However, if the time is larger, then the player is removed from the circular list and the potato’s time is reset. This is done until there is one player remaining.

Notes:

  • The number of players has to be greater than 1 and specified by the user.
  • Player’s then may enter their names.
  • If a player picks a value outside of 1-10 then their pick is defaulted to 10.
  • Once a game is over the user is prompted whether or not to play again.
  • You must create your own circular linked list. This is very similar to the structure taught in class except the last element links to the first element.
  • You may either choose to do a single or double linked list.
  • Using multiple objects may make this the solution easier and extendable.

Example Dialog:

Welcome to the Hot Potato Game!

Enter the number of players (2 or more required).

3

Enter the player 1's name

Human H. Human

Enter the player 2's name

Person H. Person

Enter the player 3's name

NotLizard H. NotLizard

Human H. Human Enter a number from 1-10 corresponding to the number of seconds to hold the potato

4

Human H. Human is safe for now.

Person H. Person Enter a number from 1-10 corresponding to the number of seconds to hold the potato

6

Person H. Person is safe for now.

NotLizard H. NotLizard Enter a number from 1-10 corresponding to the number of seconds to hold the potato

10

NotLizard H. NotLizard is safe for now.

Human H. Human Enter a number from 1-10 corresponding to the number of seconds to hold the potato

44

The number must be between 0 and 10. We will assume you meant 10

Human H. Human is safe for now.

Person H. Person Enter a number from 1-10 corresponding to the number of seconds to hold the potato

10

HOT POTATO!!! Person H. Person has been eliminated!

NotLizard H. NotLizard Enter a number from 1-10 corresponding to the number of seconds to hold the potato

3

NotLizard H. NotLizard is safe for now.

Human H. Human Enter a number from 1-10 corresponding to the number of seconds to hold the potato

10

Human H. Human is safe for now.

NotLizard H. NotLizard Enter a number from 1-10 corresponding to the number of seconds to hold the potato

5

NotLizard H. NotLizard is safe for now.

Human H. Human Enter a number from 1-10 corresponding to the number of seconds to hold the potato

10

HOT POTATO!!! Human H. Human has been eliminated!

NotLizard H. NotLizard WINS!

Would you like to continue? Press ENTER to continue or enter "quit" to quit

quit

Goodbye

//I'm a beginner level coder and have a hard time understanding it. If the code could include a note for what each method is doing I would really appreciate it. Plus I would appreciate it if the code stayed as simple as possible with no advanced coding within it. Typically the language I use while coding and what the professor recommended is Java Eclipse

Solutions

Expert Solution

Java Code:

import java.util.*; //for Scanner and Random classes
class Node //class Node which represents Player
{
protected String name; //to store player name
protected Node link; //links to next player

// non-argument Constructor
public Node()
{
link = null;
name = "";
}   
// Parameterized Constructor  
public Node(String s,Node n)
{
link = n;
name = s;
}   
// Function to set link to next Player
public void setLink(Node n)
{
link = n;
}   
// Function to set Name
public void setName(String s)
{
name = s;
}   
// Function to get link to next Player
public Node getLink()
{
return link;
}   
// Function to get name
public String getName()
{
return name;
}
}

class HotPotato{ //driver class named HotPotato
//deleteNode method takes a node and removes the node from list
public static Node deleteNode(Node del){
Node node = del.getLink(), prev = null;
while(node!=del){ //loops until node mathces with del
prev = node;
node = node.getLink();
}
prev.setLink(node.getLink());
return prev.getLink(); //returns the linked node to prev
}
public static void main(String[] args) { //main method.
Scanner keyboard = new Scanner(System.in); //created object for scanner
System.out.print("Welcome to the Hot Potato Game!");
String chose; //to store the quit the game or play input
do{
System.out.println("Enter the number of players (2 or more required). ");
int n = keyboard.nextInt(); //stores the number of players into n
keyboard.nextLine(); //escapes the nextline character
  
int i=1;

Node start=null, node=null, end=null;
while(i<=n){ //generates the singly circular linked list
System.out.println("Enter the player "+i+"'s name");
String name = keyboard.nextLine();//keyboard.nextLine();
node = new Node(name,start);
if(start==null){
start = node;
node.setLink(start);
end = start;
}   
else{
end.setLink(node);
end = node;
}
i += 1;
}

Random rand = new Random();
int potatoTime = rand.nextInt(121)+60; //generates random number between 60 and 180
node = start;
int seconds = potatoTime; //assings the potatoTime to seconds
  
while(n>1){ //loops until the only one player is remaining in list
System.out.println(node.getName()+" Enter a number from 1-10 corresponding to the number of seconds to hold the potato");
int input = keyboard.nextInt();
if(input>10){
System.out.println("The number must be between 0 and 10. We will assume you meant 10");
input = 10;
}
if(input>=seconds){ //if input is greater than seconds then
n--; //decrements the number of players
seconds = potatoTime; //resets the potatoTime
System.out.println("HOT POTATO!!! "+node.getName()+" has been eliminated! ");
node = deleteNode(node); //deletes the node from list
continue; //and contiunues without execuitng further loop block
}
else{ //else decrements input from seconds
seconds -= input;
System.out.println(node.getName()+" is safe for now. ");
}
node = node.getLink(); //takes next node into node
}
System.out.println(node.getName()+" WINS!");
System.out.println("Would you like to continue? Press ENTER to continue or enter 'quit' to quit");  
chose = keyboard.next(); //takes the users choice
}while(!chose.equals("quit")); //loops until user enters quit
System.out.println("Goodbye"); //prints good bye statement
}
}

Output Screenshots:


Related Solutions

Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
Objective: Write a program that simulates a robot running a queue of commands to move around...
Objective: Write a program that simulates a robot running a queue of commands to move around a board with obstacles. Requirements: The board is composed of spaces that are either empty (“_”) or have an obstacle (“X”). Also the board is assumed to be 10x10 spaces. The robot (“O”) has an x and y position corresponding to its location on the board, and four commands: move up, move down, move left, and move right. Both the board and the robot’s...
Write a program in Matlab where the program plays a hot and cold game. The user...
Write a program in Matlab where the program plays a hot and cold game. The user answers two inputs: x=input('what is the x location of the object?') y=input('what is the y location of the object?') You write the program so that the computer plays the game. Pick a starting point. Program Calculates the distance to the object. Program picks another point, calculates the distance to the object. Program knows its at the right spot when the distance is less than...
Implement Hot Potato game A group of people, numbered 1 to N, are sitting in a...
Implement Hot Potato game A group of people, numbered 1 to N, are sitting in a circle. Starting at person 1, a hot potato is passed. After X passes, the person holding the hot potato is eliminated, the circle closes ranks, and the game continues with the person who was sitting after the eliminated person picking up the hot potato. The last remaining person wins. For example: if X = 0 and N = 5, players are eliminated in order,...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as “I don’t think so”, “Yes, of course!”, “I’m not sure”, and so forth. The program should read the responses from the file into a list. It should prompt the user to ask...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as “I don’t think so,” “Yes, of course!,” “I’m not sure,” and so forth. The program should read the responses from the file into an array or ArrayList object. It should prompt the...
The following is a C program that is a video game version of Connect 4. The...
The following is a C program that is a video game version of Connect 4. The code works fine as is now but I want to change the way you input which column you drop a disk into. Currently, you type in 1 and it goes into column 1. If you type in 6, it goes into column 6 and so on. But I want to make it so you input A or a, then it goes into column 1...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
using matlab Write a script that simulates a card game that works as follows: A dealer...
using matlab Write a script that simulates a card game that works as follows: A dealer places 5 cards face down on the table and flips the first card. The player goes down the line, one at a time, and guesses if the next card is higher or lower than the card displayed, and then the next card is revealed. In the end, the player is awarded a point for each correct guess. In terms of coding, your script should...
Write a program where a user of this program will play a game in which he/she...
Write a program where a user of this program will play a game in which he/she needs to guess a target number, which is a number that the program has randomly picked in the range that the user chooses. The program will repeatedly prompt for the guessed number and provide a clue whether the guessed number is bigger or smaller than the target number, until the guessed number equals the target number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT