Java骰子游戏 - 选择谁基于掷骰子和轮流开始游戏

时间:2017-11-03 07:42:43

标签: java

提示:“写一个程序来对着电脑玩猪游戏。每一个回合,当前玩家都会 滚动一对骰子并累积积分。目标是在你之前达到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");
     } 
   }
}

1 个答案:

答案 0 :(得分: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) );

设计不是很好,但希望能为您提供下一步操作的提示。