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

时间:2019-05-13 19:58:07

标签: java processing dice

(正在处理)代码未返回我想要的内容。基本上,有两个玩家,每个玩家轮流掷骰子。这些值应分别存储在变量“ 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.");
        }
    }
}

我希望输出结果是“玩家2滚得更高!他们排名第一。”,“这是平局!玩家1默认情况下排名第一。”或“玩家1得更高。他们排名第一”。

2 个答案:

答案 0 :(得分:1)

最简单的方法是:

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-6后加+1

即使这是玩家2的回合,您也说“玩家1按下以滚动”。我在上面的给定代码中进行了调整。

答案 1 :(得分:0)

除了A.A的答案外,我还有一些处理选项:

  1. random()(例如println((int)random(1,7));(向int的广播等效于println(floor(random(1,7)));,它将浮点数降至1-6之间的下限)。
  2. randomGaussian()distribution is closer to a dice roll一起玩可能很有趣
  3. noise()将为您提供很多选择,尤其是与noiseSeed()noiseDetail()
  4. 一起使用时

我还注意到每次按键时都会重置每个玩家的骰子值。 我不是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";
        }
    }
}