如何为两个玩家编程掷骰子

How to program dice rolls for two players

(处理中)代码没有返回我想要的结果。基本上,有两个玩家,每个玩家轮流掷骰子。这些值应分别存储在变量 "p1diceroll" 和 "p2diceroll" 中。它将比较这两个值,并根据谁滚得更高来发布谁先走。

void setup(){
    size (100,100);
    background(200,200,200); 
    println("press l to roll the die!");
}

void draw() {
      if(keyPressed)
          keyPressed();
      noLoop();
}

void keyPressed(){
    int p1diceroll=0;
    int p2diceroll=0;

    if (key == 'l') {
        double rand1 = Math.random();
        double rand2 = rand1*6;
        double rand3 = rand2 +1;
        p1diceroll = (int)rand3;

        println("You rolled a " + p1diceroll + "!"); 
        println("player 1! press 'a' to roll");
    }

    if (key == 'a') {
        double rand11 = Math.random();
        double rand22 = rand11*6;
        double rand33 = rand22 +1;
        p2diceroll = (int)rand33;

        println("You rolled a " + p2diceroll + "!");


        if (p2diceroll>p1diceroll) {
            System.out.println("player 2  rolled higher!. They go first. "); 
        } else if (p2diceroll==p1diceroll) {
            System.out.println("It's a tie! player 1 goes first by default." ); 
        } else {
          println("player 1 rolled higher! They go first.");
        }
    }
}

我希望输出为 "player 2 rolled higher!. They go first."、"It's a tie! player 1 goes first by default." 或 "player 1 rolled higher. They go first."

最简单的方法是:

import java.util.Random;

Random rand = new Random();


if (key == 'l'){
    // Obtain a number between [0 - 5].
    int p1diceroll = rand.nextInt(6) + 1;
    println("You rolled a " + p1diceroll + "!"); 
    println("player 2! press 'a' to roll");
  }
 if (key == 'a'){

   // Obtain a number between [0 - 5].
   int p2diceroll = rand.nextInt(6) + 1;

然后你就可以像之前那样比较了。

请注意,括号中的数字是[0-6)之间的区间,包括0不包括6我们在后面加上+1得到1-6

你也在说"player1 press a to roll",尽管轮到玩家2了。我在上面给定的代码中进行了调整。

除了 A.A 的回答之外,我还有几个处理选项:

  1. random()(例如 println((int)random(1,7));(转换为 int 相当于 println(floor(random(1,7)));,这会将浮点数降低到 1-6 之间的底数。
  2. randomGaussian() might be fun to play with the distribution is closer to a dice roll
  3. noise() will give you a lot of options, especially in tandem with noiseSeed() and noiseDetail()

我还注意到每次按下该键时每个玩家的 diceroll 值都会重置。 我不是 100% 确定这就是您想要的,因为这两个值之一将始终为 0。

这是使用 random() 和调试文本的代码调整版本:

int p1diceroll=0;
int p2diceroll=0;

String result = "";

void setup(){
    size (120,120);
    background(200,200,200); 
    println("press l to roll the die!");
}

void draw() {
  background(0);
  text("press 'l' for p1"
      +"\npress 'a' for p2"
      +"\n\np1diceroll: " + p1diceroll
      +"\np2diceroll: " + p2diceroll
      +"\n\n"+result,5,15);
}

void keyPressed(){
    if (key == 'l') {
        p1diceroll = (int)random(1,7);

        println("You(p1) rolled a " + p1diceroll + "!"); 
        println("player 2! press 'a' to roll");
    }

    if (key == 'a') {
        p2diceroll = (int)random(1,7);

        println("You(p2) rolled a " + p2diceroll + "!");

        if (p2diceroll > p1diceroll) {
          println("player 2  rolled higher(" + p2diceroll + " > " + p1diceroll + ")!. They go first. ");
          result = "player 2\ngoes first";
        } else if (p2diceroll == p1diceroll) {
          println("It's a tie! player 1 goes first by default." );
          result = "tie! player 1\ngoes first";
        } else {
          println("player 1 rolled higher(" + p1diceroll + " > " + p2diceroll + ")! They go first.");
          result = "player 1\ngoes first";
        }
    }
}