Guessing Game coded in Java using a circularLinked List Requ

Guessing Game coded in Java using a circular-Linked List

Requirements

First, you will need to create a “Player” class. This class will contain a field that will hold the player’s name, a field that tracks the player’s number of wins (correct guesses), a field that tracks the number of incorrect guesses, and have methods to get, set, and construct the object appropriately.

Your game’s underlying structure will rely on a circular linked-list

You can perform the game logic in your main method or you can abstract some or all tasks behind a GuessingGame class. The choice is yours.

How does the game work?

Prompt the user for the number of players that want to play your game.

Prompt each player to enter their name. Create a new Player object for each player, add it to a Node, and then link it into the list.

Once all of the players are added choose a random number between 1 and 100 and start playing. Each player gets 1 guess and then the next player gets a turn. The player that guesses the number wins. The players continuing taking turns in a “circular” fashion until one of them wins. .

For each turn a player can guess a number, a player can choose to leave the game and be removed from the linked-list, or a player can choose to skip their turn and allow a new player to join in the game and be added to the linked-list. You’ll need a small menu system to handle this interaction. If there is only 1 player left in the list, that player is the winner.

Once a player is declared the winner, prompt them if they’d like to play again. If so, immediately start the next game with the existing players. The player that won the previous game will now go first. If they choose to not play again then show the statistics for each player during the run of games just played (total games played, wins, losses, etc.) and then exit the program.

Solution

Hi,

public class Player {
private String plyrNum;
private int number;

public Player(String name){
this.plyrNum = name;
}

public int gusNum() {
return Integer.parseInt(JOptionPane.showInputDialog(plyrNum
+\"\'s Turn\ Guess the Number\"));
JOptionPane.showMessageDialog(null, plyrNum+ \"\'s GUESS is \" + number);
}

public String getPlayerName() {
return plyrNum;
}
}

public class GusGam {

private int numtoGuss;
private Player ppp1;
private Player ppp2;
private Player ppp3;

  
   Player plyr1 = new Player();
Player plyr2 = new Player();

List<Player> players = new LinkedList<Player>();
players.add(plyr1);
players.add(plyr2);
  
public GusGam(String plyr1, String plyr2, String player3){
numtoGuss = (int) (Math.random()*10);
ppp1 = new Player(plyr1);
ppp2 = new Player(plyr2);
ppp3 = new Player(player3);
}

//Would probably split this up in submethods too.

public void sttGme(){
int flhgWinn = 0;
while(0==flhgWinn){
if (ppp1.gusNum()==numtoGuss){
flhgWinn = 1;
break;
}
JOptionPane.showMessageDialog(null, ppp1.getPlayerName()
+ \"\'s Guess is Wrong!\");
if (ppp2.gusNum()==numtoGuss){
flhgWinn = 2;
break;
}
JOptionPane.showMessageDialog(null,ppp2.getPlayerName()
+ \"\'s Guess is Wrong!\");
if (ppp3.gusNum()==numtoGuss){
flhgWinn = 3;
break;
}
JOptionPane.showMessageDialog(null,ppp3.getPlayerName()
+ \"\'s Guess is Wrong!\");
}
if (1 == flhgWinn){
JOptionPane.showMessageDialog(null,ppp1.getPlayerName()+ \" Wins!\");
} else if (2 == flhgWinn){
JOptionPane.showMessageDialog(null,ppp2.getPlayerName()+ \" Wins!\");
} else JOptionPane.showMessageDialog(null,ppp3.getPlayerName()+ \" Wins!\");
}

public static void main(String[] e){
GusGam gg = new GusGam(
JOptionPane.showInputDialog(\"Enter Player 1 Name: \"),
JOptionPane.showInputDialog(\"Enter Player 2 Name: \"),
JOptionPane.showInputDialog(\"Enter Player 3 Name: \")
);
gg.sttGme();
}
}

Guessing Game coded in Java using a circular-Linked List Requirements First, you will need to create a “Player” class. This class will contain a field that will
Guessing Game coded in Java using a circular-Linked List Requirements First, you will need to create a “Player” class. This class will contain a field that will

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site