当前位置: 首页 > 知识库问答 >
问题:

有两个玩家的骰子游戏,三个骰子选择和牌

仇正豪
2023-03-14

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

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

6:玩家移动到另一个玩家所在的位置(见下文)

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

  /*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!");    
       }
    }

    }




}

}

共有1个答案

石正信
2023-03-14

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

问题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;
}

我不确定模具辊到底出了什么问题,如果真的出了什么问题,但我注意到一些地方,你会想改变对p1和P2的操作:

  • 当玩家得到一张使他们领先的牌时,您将希望使用+=而不是=。即P1+=Cardmove5而不是P1=Cardmove5
  • 当玩家得到一张将他们移回的牌时,您似乎忘记添加p1-=cardmove语句。
  • 还有,确保p1和p2在正确的位置。例如,我认为在轮到计算机时,如果他们得到了把他们移到另一个玩家位置的牌,你应该做p2=p1,但你应该做p1=p2。同样,计算机返回到0。您有p1=0,但似乎需要p2=0。所以小心点。(还要小心复制粘贴。我猜这就是为什么会发生这种情况)

问题3:

if (spacesmoved >= (83) || spacesmoved <= (89))
if (spacesmoved >= (83) && spacesmoved <= (89))

“如果spacesmoved大于或等于83,小于或等于89”,这只适用于83至89之间的数字。

您还希望删除该块和其他类似块中“if”语句后面的分号。如果不这样做,这些条件中的代码将无法执行。当它发生时,这实际上是一个非常难找到的错误。

另一件需要知道的事情是,当您希望在“if”条件中执行多个内容时,必须将其括在花括号{}中,否则,该条件中只包含第一行,后面的任何行都将无条件执行。这是造成第三个问题的另一个事实。

最后一件事是,您应该尝试使用“else if”和“else”语句。它将帮助您的代码流更有意义。我不打算为您做所有的工作,但这个代码块可能更像这样:

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");
}

奖金小费

 类似资料:
  • 我是一个C++初学者,我需要创建一个骰子游戏模拟掷两个骰子。我对头文件的使用感到很困惑。但首先,为什么我需要返回骰子的票面号码?其次,int roll函数做什么?来重置价值观和面孔?如果是,默认值是多少?而最后一个函数骰子(int n),我是否使用这个函数来控制骰子值的最大总和?函数必须有一个具有以下函数的类头文件:

  • 有人能在这里给我指个正确的方向吗?我的游戏工作完美,但我想添加一些实际的互动/目标。谢谢

  • 提示:“写一个程序对着电脑玩猪游戏。在每个回合,当前玩家将掷出一对骰子并累积点数。目标是在你的对手之前达到100分或更多。(为了测试的目的,使用30点而不是100点)如果在任何回合中,玩家掷出一个1,则该回合积累的所有点数都将被没收,骰子的控制权将转移到另一个玩家身上。如果玩家在一个回合中滚动两个1,则该玩家失去迄今为止积累的所有点数,控制权转移到另一个玩家身上。玩家可以在每次掷骰后自愿交出骰子的

  • 我正在创建一个骰子游戏,其中2个玩家有他们自己的骰子,每个人都有自己的轮到扔他的骰子,玩家可以完全失去分数或获得分数取决于他们站在窗口的位置,游戏结束时,任何一个玩家首先站在终点车道上,获胜者是得分最高的玩家,我已经在游戏的设计到目前为止,但还没有在逻辑上工作。 这个链接是游戏应该是什么样子的图片: 我想知道我如何才能添加球员1和球员2在每个瓷砖上面的照片,所以基本上每次当一个球员玩他的回合,我希

  • 该程序应模拟滚动两个骰子,并计算和。添加一个循环,使程序可以玩10,000个游戏。添加计数器,计算玩家赢了多少次,输了多少次。在10,000场比赛结束时,计算获胜的概率[即赢/(赢+输)]并输出该值。从长远来看,谁会赢得最多的比赛,你还是房子?注意:要生成一个随机数x,其中0x≤<1,使用x=math.random();例如,乘以6并转换为一个整数,结果是一个介于0和5之间的整数。

  • 我试图为一个游戏的掷骰子程序,其中用户输入一个下注金额和2个六面骰子滚动,如果7或11是滚动,他们赢了。如果掷2,3或12就输了,如果掷其他任何一个数字,它就显示该数字为一个点。它会继续掷骰子,直到掷7或该点为止,如果掷7就输了,否则就赢了。出于某种原因,在掷骰子时,他会掷骰子,然后再掷一次,看看他们是否赢了或输了。我不知道如何解决这个问题,任何帮助都不会附带