【问题标题】:Java Dice Game - Choosing who starts a game based on dice roll and taking turnsJava Dice Game - 根据掷骰和轮流选择谁开始游戏
【发布时间】:2017-11-03 16:06:38
【问题描述】:

提示:“写一个程序来和电脑玩猪游戏。每回合,当前玩家会 掷一对骰子并累积积分。目标是在你之前达到 100 分或更多分 对手会。 (出于测试目的,使用 30 而不是 100 分)如果在任何回合,玩家 掷出 1,该轮累积的所有分数都将被没收并控制骰子 移动到另一个玩家。如果玩家在一回合中掷出两个 1,则玩家将失去所有分数 到目前为止累积的积分将被没收,控制权转移给其他玩家。玩家可以 每次掷骰后自愿交出对骰子的控制权。因此玩家必须决定滚动 再次(成为猪)并冒着失分的风险,或者放弃对骰子的控制,可能会让 其他玩家获胜。电脑要掷硬币来选择第一个玩家”

我的问题:我让程序根据掷硬币的结果输出计算机或玩家先走。但是,我将如何真正提示程序运行选择首先启动的人的方法,然后在每回合结束时如何在计算机和玩家之间切换?顺便说一句,我知道这段代码不完整,但我希望我的问题有意义。

到目前为止的代码:

import java.util.*;
public class NavaiPigGame
{
   public static final int POINT = 30;
   public static final int FORFEIT_POINTS = 20;
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);
      Random rand = new Random();
      play(rand,input);
   }
   // desription of game
   public static void description()
   {
      System.out.println("***********************************************************************************");
      System.out.println("Write a program to play the pig game against the computer. At each turn, the current player will");
      System.out.println("roll a pair of dice and accumulates points. The goal is to reach to 100 or more points before your");
      System.out.println("opponent does. (For the testing purposes use 30 instead of 100 points) If, on any turn, the player");
      System.out.println("rolls a 1, all the points accumulated for that round are forfeited and the control of the dice");
      System.out.println("moves to the other player. If the player rolls two 1s in one turn, the player loses all the points");
      System.out.println("accumulated thus far are forfeited and the control moves to the other player. The player may");
      System.out.println("voluntarily turn over the control of the dice after each roll. Therefore player must decide to roll");
      System.out.println("again (be a pig) and risk losing points, or relinquish control of the dice, possibly allowing the");
      System.out.println("other player to win. Computer is going to flip a coin to choose the first player");
      System.out.println("***********************************************************************************");
      System.out.println("lets start the fun");
   }
   //flips a coin and decides who starts the game
   public static String flipCoin(Random rand)
   {
      int coin = rand.nextInt(2);
      String comp = "";
      switch (coin)
      {
         case 0: comp = "heads";
                 break;
         case 1: comp = "tails";   
                 break;
      }
      return comp;
   }
   public static int rollDice(Random rand)
   {
      int dice1 = rand.nextInt(6)+1;
      int dice2 = rand.nextInt(6)+1;
      System.out.println("Dice 1: " +dice1);
      System.out.println("Dice 2: " +dice2);
      return dice1+dice2;     
   }
   // select a random name of the computer via arrays
   public static String nameComputer(Random rand)
   {
      int name = rand.nextInt(10);
      String compName = "";
      switch (name)
      {
         case 0: compName = "Lisa";
                 break;
         case 1: compName = "Kathy";
                 break;
         case 2: compName = "Hali";
                 break;
         case 3: compName = "Jack";
                 break;
         case 4: compName = "Alex";
                 break;
         case 5: compName = "Max";
                 break;
         case 6: compName = "Jill";
                 break;
         case 7: compName = "James";
                 break;
         case 8: compName = "Martha";
                 break;
         case 9: compName = "Lauren";
                 break;
      }
      return compName;
   }
   public static void play(Random rand, Scanner input)
   {
      int playerScores = 0;
      int playerTotal = 0;
      int computerScores = 0;
      int computerTotal = 0;
      boolean gameOver = false
      boolean turnOver = false
      description();
      String compName = nameComputer(rand);
      System.out.println("Hi my name is " + compName);
      System.out.print("What is your name? ");
      String name = input.nextLine();
      System.out.println("Hi " + name + ", I am flipping the coin to determine who goes first");
      System.out.print("press any key to start the game. ");
     input.nextLine(); 
     String flip = flipCoin(rand);
     int turn;
     if (flip.equals("heads"))
     {
        turn = 1;
        System.out.println("You are going to start the game");
     }
     else
     {
        turn = 0;
        System.out.println(compName + " is going to start the game");
     } 
   }
}

【问题讨论】:

标签: java


【解决方案1】:

创建:

  • 一个playTurn(int turn) 函数(1 代表玩家,0 代表计算机)处理轮次(掷骰子、计算点等)
  • 检查是否有获胜者的布尔函数。示例:isWinner(int player)(同样,1 代表玩家,0 代表计算机)

第一次在你的if() else 语句中使用这个函数:

if (flip.equals("heads"))
{
    turn = 1;
    System.out.println("You are going to start the game");
    playTurn(turn);
 }
 else
 {
    turn = 0;
    System.out.println(compName + " is going to start the game");
    playTurn(turn);
 } 

然后你可以添加:

 do {
     if(turn == 1){
         turn = 0;
         playTurn(turn);
     }else{
         turn == 1;
         playTurn(turn);
     }
  while ( !isWinner(1)|| !isWinner(0) );

这不是很好的设计,但希望它能给你一个关于下一步做什么的提示。

【讨论】:

  • 感谢您的回复。我的 computerTurn 和 playerTurn 方法也有问题。我有一种方法可以为每个骰子,int die1 和 int die2 掷骰子。但是,我不能将这些变量调用到 computerTurn 或 playerTurn 中,因为那样我必须将它们作为参数放在 main 方法中,并且我不允许在 main 方法中启动两个骰子值,只能在 dice 方法中.对此有何建议?
  • 你听说过 Java 类吗?也许将所有内容分成类会很容易。
猜你喜欢
  • 1970-01-01
  • 2015-12-24
  • 2012-02-29
  • 2018-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多