Please tell me how to decide the Java dice game draw.

Asked 2 years ago, Updated 2 years ago, 27 views

We use Eclipse to create a game in which the person with the highest total wins by rolling the dice.

The rules are as follows:

However, I don't know how to write down the draw decision.
Could you give me some advice?Thank you for your cooperation.

Current Code:

package test;

public class SaikoroGames {

  public static void main(String[]args) {

    // Replace the total value of each player
    intokan [] = new int [3];
    
    // determine the number of players
    int player=3;
    int sum = 0;
    intdice = 0;
    
    int max = Integer.MIN_VALUE;
    // loop for the number of people
    for(int j=0;j<player;j++){
        System.out.printf("[%d]:",j+1);
      sum = 0;
      for(inti=1;i<=player;i++){
        dice=(int)(Math.random()*6)+1;
        System.out.print(dice+"");
        sum+=dice;
        
      }
      System.out.println();
      System.out.print("Total value:"+sum);
      max = Math.max (max, sum);
      hokan[j] = sum;
      System.out.println();
      System.out.println();
    }
    // winner's decision
    for(int j=0;j<player;j++){
      if(hokan[j]==max){
          System.out.println();
        System.out.printf("Winner is [%d], ",j+1);
        System.out.print("Total value is" + hokan[j]);
      }
    }
  }
}

java

2022-09-29 22:32

1 Answers

First extract the player who got the maximum value (possibly multiple players) and
Why don't you take care of them later and output them all together outputting them?

// Winner Decision
// Pick up the player who got the maximum value (max)
List<Integer>winners=new ArrayList<>();
for(int j=0;j<player;j++){
    if(hokan[j]==max){
        winners.add(j);
    }
}
// Outputs the player who got max and max value
System.out.println();
System.out.printf("Winner is");
for (int winner: winners) {
    System.out.printf("[%d]", winner+1);
}
// If there are more than two players who get max, draw.
if(winners.size()>=2){
    System.out.print("(draw)");
}
System.out.print(", total value is "+max);


2022-09-29 22:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.