Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 两人骰子游戏,3个骰子选择和卡_Java_Dice - Fatal编程技术网

Java 两人骰子游戏,3个骰子选择和卡

Java 两人骰子游戏,3个骰子选择和卡,java,dice,Java,Dice,我正在尝试为一个游戏编写代码,这个游戏有一个玩家和一台电脑掷骰子,直到其中一个或两个骰子都达到250(他们可能打成平局)。播放器和计算机可以从3个模具选择中选择一个。单面24面扎带、双面10面模具或三面6面模具。如果10面和6面模具都相同,则有额外奖励。有两个“湖”,如果玩家在其中着陆,玩家必须在湖开始之前返回较低的数字,还有一个泥泞的沼泽,玩家在沼泽中的每一个动作都被切成两半。每10个点(10、20、30、40等)玩家随机抽一张牌。玩家可以随机获得11张不同的牌: 1-4:玩家从1-6向前移动

我正在尝试为一个游戏编写代码,这个游戏有一个玩家和一台电脑掷骰子,直到其中一个或两个骰子都达到250(他们可能打成平局)。播放器和计算机可以从3个模具选择中选择一个。单面24面扎带、双面10面模具或三面6面模具。如果10面和6面模具都相同,则有额外奖励。有两个“湖”,如果玩家在其中着陆,玩家必须在湖开始之前返回较低的数字,还有一个泥泞的沼泽,玩家在沼泽中的每一个动作都被切成两半。每10个点(10、20、30、40等)玩家随机抽一张牌。玩家可以随机获得11张不同的牌:

1-4:玩家从1-6向前移动随机数量

5:玩家从4-11随机向前移动(随机8+4)

6:玩家移动到其他玩家所在的位置(见下文)

7:玩家移回起始位置(移动到位置0)

8-9:玩家从1-6随机向后移动一个数量

10-11:玩家从4-11向后移动随机数量

我有几个问题。我的第一个问题是,模具辊不会在每次转动后改变,它们将保持不变。所以如果我选择3个骰子,我可能会得到3个随机数,如果我再次选择这些骰子,我会得到相同的3个数

我似乎也无法正确更新玩家的死亡人数。如果玩家总共掷出18分,下一回合他掷出14分,计数将从18分增加到14分

我的第三个问题是,似乎无论我做什么,湖,泥泞补丁和获奖公告的打印声明总是打印出来。我尝试了一些不同的方法,但似乎没有任何效果

我是新的代码编写(这是我写的第四个程序),并没有广泛的知识,知道什么是错误的。代码不需要熟练地完成,我只希望它能正常工作。非常感谢您的任何帮助

  /*This program will create a "Board" game. Each player can choose 
  from several different types of die. The computer and user will take 
  turns "rolling" a dice. There are several obstacles that can send one 
  of the players back. The goal is to get above 250*/

  import java.util.*;

  public class Project4 {

public static void main(String[] args) {
    Scanner in=new Scanner(System.in);
    //assigning variables
    int p1, p2;
    p1=p2=0;
    int spacesmoved  = 0;

    //Setting up the randomization of the 24 sided die
    int minimum1 = 1;
    int maximum1 = 24;
    Random rn1 = new Random();
    int range1 = maximum1 - minimum1 + 1;
    int die1 =  rn1.nextInt(range1) + minimum1;

    //Setting up the randomization of the 10 sided die
    int minimum2 = 1;
    int maximum2 = 10;
    Random rn2 = new Random();
    int range2 = maximum2 - minimum2+ 1;
    int die2 = rn2.nextInt(range2) + minimum2;
    int die22 = rn2.nextInt(range2) + minimum2;
    int die222 = rn2.nextInt(range2) + minimum2;

    //Setting up the randomization of the 6 sided die
    int minimum3 = 1;
    int maximum3 = 10;
    Random rn3 = new Random();
    int range3 = maximum3 - minimum3+ 1;
    int die3 = rn3.nextInt(range3) + minimum3;
    int die33 = rn3.nextInt(range3) + minimum3;
    int die333 = rn3.nextInt(range3) + minimum3;

    //Setting a loop for the players to take turns until one, or both, reach > 250
    while (p1 <= 250 && p2 <= 250) {
        {System.out.println(" Current positions.  Player: " + p1 + " Computer: " + p2);
            System.out.println("Which die would you like to roll? die1(1) = one 24-sided die, die2(2) = two 10-sided dice, die3(3) = three 6-sided dice: ");
       String diechoice = in.nextLine().toLowerCase();

       //Getting the die roll if the player chooses the 24 sided die
       if (diechoice.equals ("1")) {
           spacesmoved = (die1);
       System.out.println("Player rolled a " + die1);
       System.out.println("Player moves forward " + die1 +" spaces");
       p1+=spacesmoved;

       }

       //Getting the die roll if the player chooses the two 10 sided die
       if (diechoice.equals ("2")) { spacesmoved = (die2 + die22);
       System.out.println("First die is " + die2);//TESTTTT
       System.out.println("Second die is a " + die22);//TEST
       System.out.println(die2 + die22);//TESTTTTtttt
            if (die2 == die22); {
            spacesmoved = (die2 + die22 + die222);
            System.out.println("Player rolled doubles, player gets to roll a 3rd 10 sided die");
            System.out.println("Players 3rd dice roll is " + die222);
            System.out.println("Player moves forward a total of " + spacesmoved + " spots");
            p1 += spacesmoved;
        }
     //  player1spot = (currentspot + spacesmoved);
        }

       //Getting the die roll if the player chooses three 6 sided die
       if (diechoice.equals("3")) { spacesmoved = (die3 + die33 + die333);
       System.out.println("die 1 is " + die3);
       System.out.println("die 2 is " + die33);
       System.out.println("die 3 is " + die333);
       System.out.println("Player 1 moves forward a total of " + spacesmoved + " spots");
       { if (die3 == die33)
           if (die33 == die333)
           spacesmoved = ( spacesmoved * 2);

       p1 += spacesmoved;
       }}
       /*Setting up the lakes and muddy patch. If the player lands in a lake he goes back
       to the lower edge of the lake. If in the mud his moves are cut in half ONLY while in the mud */

       {if (spacesmoved >= (83) || spacesmoved <= (89)); spacesmoved = (82);
         System.out.println("Player landed in a lake, player goes back to space " + spacesmoved);
       if (spacesmoved >= (152) || spacesmoved <= (155)); spacesmoved = (151);
         System.out.println("Player landed in a lake, player goes back to space " + spacesmoved);
       if (spacesmoved >= (201) || spacesmoved <= (233)); spacesmoved = (spacesmoved / 2);
         System.out.println("Player landed in mud, players turns are cut in half until player gets out");
       }
       //Setting up the random cards if the player lands on a 10
       if (p1 % 10==0);
       { int minimum4 = 0;
       int maximum4 = 11;
       Random rn4 = new Random();
       int range4 = maximum4 - minimum4 + 1;
       int card =  rn4.nextInt(range4) + minimum4;

       //if player gets a card that moves them ahead a random number between 1-6
        if (card >=4);
           int minimum = 0;
           int maximum = 6;
           Random rn = new Random();
           int range = maximum - minimum + 1;
           int cardmove =  rn.nextInt(range) + minimum;
            p1 = cardmove;

       //if player gets a card that moves them ahead a random number between 4-11
        if (card == 5);
           int minimum5 = 4;
           int maximum5 = 11;
           Random rn5 = new Random();
           int range5 = maximum5 - minimum5 + 1;
           int cardmove5 =  rn5.nextInt(range5) + minimum5;
            p1 = cardmove5;
        //if player gets a card that moves them to the spot of the other player
        if (card == 6);
           p2 = p1;


        //if player gets a card that moves them back to 0 (moves location to 0)
        if (card ==7);
           p1 = 0;

        //if player gets a card that moves them back between 1-6 spaces
        if (card == (8) || card == 9);
           int minimum6 = 1;
           int maximum6 = 6;
           Random rn6 = new Random();
           int range6 = maximum6 - minimum6 + 1;
           int cardmove6 =  rn6.nextInt(range6) + minimum6;


        //if player gets a card that moves them back between 4-11 spaces
         if (card == (10) || card == 11);
           int minimum7 = 4;
           int maximum7 = 11;
           Random rn7 = new Random();
           int range7 = maximum7 - minimum7 + 1;
           int cardmove7 =  rn7.nextInt(range7) + minimum7;
       }
          //Setting up the computers turn

       System.out.println("Computers turn");
       {
         int minimum = 0;
         int maximum = 2;
         Random rn = new Random();
         int range = maximum - minimum + 1;
         int computersturn =  rn.nextInt(range) + minimum;

       //If computer randomly chooses a 24 sided die
         spacesmoved = (die1);
         System.out.println("Computer rolled a " + die1);
         System.out.println("Computer moved " + die1 +" spaces");
         p2+=spacesmoved;

       }

       //If the computer randomly chooses the two 10 sided die
         if (diechoice.equals ("die2")) { spacesmoved = (die2 + die22);
         System.out.println("First die is " + die2);//TESTTTT
         System.out.println("Second die is a " + die22);//TEST
         System.out.println(die2 + die22);//TESTTTTtttt
            if (die2 == die22); {
            spacesmoved = (die2 + die22 + die222);
            System.out.println("Computer rolled doubles, player gets to roll a 3rd 10 sided die");
            System.out.println("Computer 3rd dice roll is " + die222);
            System.out.println("Computer moves a total of " + spacesmoved + " spots");
            p2 += spacesmoved;
        }

        }

       //If the computer randomly chooses three 6 sided die
       if (diechoice.equals("die3")) { spacesmoved = (die3 + die33 + die333);
       System.out.println("die 1 is " + die3);
       System.out.println("die 2 is " + die33);
       System.out.println("die 3 is " + die333);
       System.out.println("Computer 1 moves a total of " + spacesmoved + " spots");
       { if (die3 == die33)
           if (die33 == die333)
           spacesmoved = ( spacesmoved * 2);

       p2 += spacesmoved;


       }




       //Setting the lakes and mud for the computer 

       if (spacesmoved >= (83) || spacesmoved <= (89)); spacesmoved = (82);
       System.out.println("Computer landed in a lake, player goes back to space " + spacesmoved);
       if (spacesmoved >= (152) || spacesmoved <= (155)); spacesmoved = (151);
       System.out.println("Computer landed in a lake, player goes back to space " + spacesmoved);
       if (spacesmoved >= (201) || spacesmoved <= (233)); spacesmoved = (spacesmoved / 2);
       System.out.println("Computer landed in mud, players turns are cut in half until player gets out");

       //Setting up the cards for the computer
       if (p1 % 10==0);
       { int minimum4 = 0;
       int maximum4 = 11;
       Random rn4 = new Random();
       int range4 = maximum4 - minimum4 + 1;
       int card =  rn4.nextInt(range4) + minimum4;

       //if computer gets a card that moves them ahead a random number between 1-6
        if (card >=4);
           int minimum = 0;
           int maximum = 6;
           Random rn = new Random();
           int range = maximum - minimum + 1;
           int cardmove =  rn.nextInt(range) + minimum;

       //if computer gets a card that moves them ahead a random number between 4-11
        if (card == 5);
           int minimum5 = 4;
           int maximum5 = 11;
           Random rn5 = new Random();
           int range5 = maximum5 - minimum5 + 1;
           int cardmove5 =  rn5.nextInt(range5) + minimum5;

        //if computer gets a card that moves them to the spot of the other player
        if (card == 6);
           p1 = p2;


        //if computer gets a card that moves them back to 0 (moves location to 0)
        if (card ==7);
           p1 = 0;

        //if computer gets a card that moves them back between 1-6 spaces
        if (card == (8) || card == 9);
           int minimum6 = 1;
           int maximum6 = 6;
           Random rn6 = new Random();
           int range6 = maximum6 - minimum6 + 1;
           int cardmove6 =  rn6.nextInt(range6) + minimum6;


        //if computer gets a card that moves them back between 4-11 spaces
         if (card == (10) || card == 11);
           int minimum7 = 4;
           int maximum7 = 11;
           Random rn7 = new Random();
           int range7 = maximum7 - minimum7 + 1;
           int cardmove7 =  rn7.nextInt(range7) + minimum7;
       }
       }    
      //Writing a final statment showing the winner, or if both tied.
       {     if (p1 > p2);
         System.out.println("Player 1 wins! Good job!");

       if (p2 >p1);
          System.out.println("Computer wins! Better luck next time!");

       if (p2 == p1);
          System.out.println("The game ends in a tie!");    
       }
    }

    }




}
/*此程序将创建一个“棋盘”游戏。每个玩家都可以选择
从几个不同类型的模具。计算机和用户将
变成“掷”骰子。有几个障碍可以发送一个
所有的球员都回来了。目标是达到250以上*/
导入java.util.*;
公共类项目4{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
//分配变量
int p1,p2;
p1=p2=0;
int spacesmoved=0;
//设置24面模具的随机化
int最小值1=1;
int最大值1=24;
随机rn1=新随机();
int range1=最大值1-最小值1+1;
int die1=rn1.nextInt(范围1)+最小值1;
//设置10面模具的随机化
int最小值2=1;
int最大值2=10;
随机rn2=新随机();
int range2=最大值2-最小值2+1;
int die2=rn2.nextInt(范围2)+最小值2;
int die22=rn2.nextInt(范围2)+最小值2;
int die222=rn2.nextInt(范围2)+最小值2;
//设置六面模具的随机化
int最小值3=1;
int最大值3=10;
随机rn3=新随机();
int range3=最大值3-最小值3+1;
int die3=rn3.nextInt(范围3)+最小值3;
int die33=rn3.nextInt(范围3)+最小值3;
int die333=rn3.nextInt(范围3)+最小值3;
//设置一个循环,让玩家轮流,直到其中一个或两个达到>250
而(p1=(83)| | spacesmoved=(152)| | spacesmoved=(201)| | spacesmoved=4);
int最小值=0;
int最大值=6;
Random rn=新的Random();
整数范围=最大值-最小值+1;
int cardmove=rn.nextInt(范围)+最小值;
//若计算机得到一张卡片,将它们向前移动一个4-11之间的随机数
如果(卡片==5);
int最小值5=4;
int最大值5=11;
随机rn5=新随机();
int range5=最大值5-最小值5+1;
int cardmove5=rn5.nextInt(范围5)+最小值5;
//如果计算机得到一张卡片,将他们移动到另一个玩家的位置
如果(卡片==6);
p1=p2;
//如果计算机得到一张将其移回0的卡(将位置移回0)
如果(卡片==7);
p1=0;
//如果计算机得到一张卡片,将它们移回1-6个空格
如果(卡片==(8)| |卡片==9);
int最小值6=1;
int最大值6=6;
随机rn6=新随机();
int range6=最大值6-最小值6+1;
int cardmove6=rn6.nextInt(范围6)+最小值6;
//如果计算机得到一张卡片,可以将它们移回4-11个空格
如果(卡片==(10)| |卡片==11);
int最小值7=4;
int最大值7=11;
随机rn7=新随机();
整数范围7=最大值7-最小值7+1;
int cardmove7=rn7.nextInt(范围7)+最小值7;
}
}    
//写一份最后的陈述,展示胜利者,或者如果双方平手。
{if(p1>p2);
System.out.println(“玩家1赢了!干得好!”);
if(p2>p1);
System.out.println(“电脑赢了!下次好运!”);
如果(p2==p1);
System.out.println(“游戏以平局结束!”);
}
}
}
}

}

以下是我注意到的与您提到的三个问题有关的事情:

问题1:

您正在代码执行的最开始设置骰子的值。从那时起,你根本不会改变它们。这就是问题的原因,每次旋转都是相同的数字。您可能认为,每次使用
die1
或任何其他die变量时,它都在重新执行文件顶部的代码,但事实并非如此

文件顶部的代码只执行一次,然后存储在该变量中的值将用于程序执行的其余部分。直到你改变
//Getting the die roll if the player chooses the 24 sided die
if (diechoice.equals ("1")) {
    die1 = rn1.nextInt(range1) + minimum1;
    System.out.println("Player rolled a " + die1);
    System.out.println("Player moves forward " + die1 +" spaces");
    p1+=die1;
}
if (spacesmoved >= (83) || spacesmoved <= (89))
if (spacesmoved >= (83) && spacesmoved <= (89))
if (p1 >= (83) && p1 <= (89))
{
    p1 = (82);
    System.out.println("Player landed in a lake, player goes back to space " + p1);
}
else if (p1 >= (152) && p1 <= (155))
{
    p1 = (151);
    System.out.println("Player landed in a lake, player goes back to space " + p1);
}
else if (p1 >= (201) && p1 <= (233))
{
    spacesmoved = (spacesmoved / 2);
    p1 -= spacesmoved;
    System.out.println("Player landed in mud, players turns are cut in half until player gets out");
}