Question: This is part of a bigger code, I need to find the order of playing, by rolling the dice. if a tie happens, those players

This is part of a bigger code, I need to find the order of playing, by rolling the dice. if a tie happens, those players have to roll again (not all players)

Can you help me debug the playerOrder method? it works fine in cases, but it mostly gives error (specially for the number of players = 4)

import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; import java.util.Scanner; import java.util.TreeMap;

public class Order { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in);

List players = new ArrayList(); int numPlayers = 0; System.out.print("Please enter the number of players(between 2 and 4): "); numPlayers = keyboard.nextInt();

for(int index = 0 ; index < numPlayers ; index++) { Player player = new Player("P.No" + (index+1)); players.add(player); } System.out.println(" Let's decide in what order players will roll: "); Map order = new HashMap<>(); playerOrder(players, order); System.out.print(" The new order of playing is:"); for(Player player: players) { System.out.print( player.getName() + ", "); } keyboard.close(); } public static void playerOrder(List players, Map playerRoll){ for(Player player: players){ int roll = assignOrder(player); if(playerRoll.containsKey(roll)){ Player otherPlayer = playerRoll.get(roll); System.out.println(" A tie was achieved between " + otherPlayer.getName() + " and " + player.getName() + ". Rolling again: "); List newOrder = new ArrayList(); newOrder.add(otherPlayer); newOrder.add(player); playerOrder(newOrder, playerRoll); } else playerRoll.put(roll, player); } TreeMap tm = new TreeMap<>(playerRoll); players.clear(); for (int key : tm.keySet()) { players.add(playerRoll.get(key)); } Collections.reverse(players); } public static int assignOrder(Player player){ int roll = flipDice(); System.out.println( player.getName() + " got a dice value of " + roll);

return roll; } public static int flipDice() { Random random = new Random(); int dice = random.nextInt(6)+1; return dice; } }

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.Random; import java.util.Scanner;

public class Player { public String name; public Player(String name) { this.name = name; } public String getName(){ return name; } public int takeTurn() { int roll = flipDice(); System.out.println(name + " got dice value of " + roll + "."); return roll; } public int flipDice() { Random random = new Random(); int dice = random.nextInt(6)+1; return dice; } public String toString() { return name; }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!