2015-07-11 68 views
0

這是我迄今爲止。出於某種原因,該計劃不會讓我返回scoreOne。該方法試圖找到最重複的數字並將其用作點。此方法必須返回int類型的結果嗎? Farkle遊戲

import java.util.Scanner; 
public class Farkle { 

    // YOU MAY NOT DECLARE STATIC FIELDS in Farkle 
    // All values must be passed to and from methods (as described by method 
    // headers) 

    /** 
    * <p>Users set the number of players (2-6) and score needed to win 
    * for a game of Farkle. 
    * 
    * <p>Players take turns rolling dice and try to accumulate the required 
    * number of points. The game ends when the first player to accumulate 
    * enough points is declared the winner. 
    * 
    * <p>Program execution starts here. 
    * 
    * @param args UNUSED 
    */ 

    public static void main(String[] args) { 
     // TODO 
     Scanner in = new Scanner(System.in); 
     /* 
     * This is where you should put your welcome message and main 
     * program loop. Make sure to match your output to what's described 
     * in the project specification. 
     */ 

     System.out.println("Welcome to Farkle."); 
     // Number of players for this game 
     System.out.print("How many players? "); 
     int numOfPlayers = in.nextInt(); 
     while (numOfPlayers > 6 || numOfPlayers < 2) 
     { 
      System.out.println("Invalid answer. Farkle may be played by 2-6 players. "); 
      System.out.print("How many players? "); 
      numOfPlayers = in.nextInt(); 
     } 
     // User inputs points needed to win 
     System.out.print("How many points do you need to win? "); 
     int neededPoints = in.nextInt(); 

     System.out.println("Game will play until " + neededPoints + " points"); 
     System.out.println("It is now player 1's turn. "); 

     //User inputs command for game 
     System.out.print("Enter command: "); 
     String command = in.next(); 
     boolean commandLoop = true; 
     int dice [] = new int [6]; 
     int allDice [] [] = new int [10] [10]; 
     int currentRollNum = 0; 
     int scoreOne = 0; 
     int scoreTwo = 0; 
     while (commandLoop) { 
      if (command.equals("exit")) 
      { 
      System.out.println("Game over."); 
      break; 
      } 
      if (command.equals("help") || command.equals("HELP")) 
      { 
      printHelp(); 
      command = in.next(); 

      } 


      if (command.equals("roll")) 
      { 
       int arrayOne [] = rollDice(dice, allDice, currentRollNum); 
       System.out.println("Your roll: [" + arrayOne[0] + ", " + arrayOne[1] + ", " + arrayOne[2] + ", " + arrayOne[3] + ", " + arrayOne[4] + ", " +arrayOne[5] + "]"); 


       int var = scoreDice(arrayOne); 

       break; 
      } 


      if (command.equals("turn")) 
      { 
       int arrayTwo [] = rollDice(dice, allDice, currentRollNum); 
       System.out.print("Your roll: [" + arrayTwo[0] + ", " + arrayTwo[1] + ", " + arrayTwo[2] + ", " + arrayTwo[3] + ", " + arrayTwo[4] + ", " +arrayTwo[5] + "]"); 
       command = in.next(); 

      } 

      } 
    } 
    private static void printHelp() { 

     System.out.println("\"roll\" - roll the dice"); 
     System.out.println("\"finish\" - terminate the current turn"); 
     System.out.println("\"dice\" - print the current dice"); 
     System.out.println("\"turn\" - print the current turn so far"); 
     System.out.println("\"scores\" - print scores"); 
     System.out.println("\"help\" - print this help menu"); 
     System.out.println("\"exit\" - terminates the game"); 

    } 
    private static int[] rollDice(int[] dice, int[][] allDice, int currentRollNum) { 
     int dice1 = Dice.rollDie(true); 
     int dice2 = Dice.rollDie(true); 
     int dice3 = Dice.rollDie(true); 
     int dice4 = Dice.rollDie(true); 
     int dice5 = Dice.rollDie(true); 
     int dice6 = Dice.rollDie(true); 

     int [] newlyRolled= new int [6]; 
     newlyRolled [0] = dice1; 
     newlyRolled [1] = dice2; 
     newlyRolled [2] = dice3; 
     newlyRolled [3] = dice4; 
     newlyRolled [4] = dice5; 
     newlyRolled [5] = dice6; 

     return newlyRolled; 

    } 

    private static void printDice(int[] dice) { 





     } 

    /** 
    * Print a 2D-array to the console. 
    * [turn1dice1, turn1dice2, turn1dice3, turn1dice4, turn1dice5, turn1dice6, ]<br /> 
    * [turn2dice1, turn2dice2, turn2dice3, turn2dice4, turn2dice5, turn2dice6 ]<br /> 
    * [2, 4, 4, 3, 1, 6, ]<br /> 
    * [1, 3, 2, 5 ]<br /> 
ram allDice The 2D-array of dice (representing all rolls for this 
    * turn) to be printed to the console 
    */ 


    private static void printDice(int[][] allDice) { 




    } 

    /** 
    * Print a record of all dice rolled so far during this turn 
    * and the number of points earned so far. 
    * 
    * <p>Output should be formatted as follows (where X is the currentTurnScore):<br /> 
    * [turn1dice1, turn1dice2, turn1dice3, turn1dice4, turn1dice5, turn1dice6, ]<br /> 
    * [turn2dice1, turn2dice2, turn2dice3, turn2dice4, turn2dice5, turn2dice6 ]<br /> 
    * etc.<br /> 
    * X points so far this turn.<br /> 
    * 
    * @param allDice The 2D-array of dice (representing all rolls for this 
    * turn) 
    * @param currentTurnScore The current score for this turn 
    */ 



    private static void printTurn(int[][] allDice, int currentTurnScore) { 



    } 

    /** 
    * Compute the score for the currently rolled dice. 

    * 
    * Dice should be scored using the following algorithm: 
    * <ol> 
    * <li>Find all groupings of dice of the same number (die showing 0 do not count) 
    * <li>The largest group (> size 1) of dice of the same number will be worth 
    * points; the number of points is equal to the number of dice in the group. 
    * <li>If more than one die number has the same size group, the smallest die 
    * number should be used. 
    * <li>If no groups > size 1 exist, 1 point can be earned if there is a die 
    * with the number 1. 
    * <li>Otherwise, 0 points are earned. 
    * </ol> 
    * 
    * <p>Dice that are found to be worth points should be set to 0, indicating 
    * that they are no longer available to be re-rolled later in this turn. 
    * 
    * <p>For example:<br /> 
    * [2, 3, 4, 3, 5, 6]<br /> 
    * is worth 2 points (for the two 3s)<br /> 
    * resulting dice = [2, 0, 4, 0, 5, 6]<br /><br /> 
    * [2, 3, 4, 3, 2, 6]<br /> 
    * is worth 2 points (for the two 2s)<br /> 
    * resulting dice = [0, 3, 4, 3, 0, 6]<br /><br /> 
    * [0, 3, 4, 2, 0, 6]<br /> 
    * is worth 0 points (because there are no groups and no die showed 1) 
    * 
    * @param dice The 1D-array of current dice. 
    * @return The score earned by dice. 
    */ 
    private static int scoreDice(int[] dice) { 

     int numOne = 0; 
     int numTwo = 0; 
     int numThree = 0; 
     int numFour = 0; 
     int numFive = 0; 
     int numSix = 0; 

     for (int i=0; i<dice.length; i++) 
      { 
       switch (dice[i]){ 
       case 1: numOne++; break; 
       case 2: numTwo++; break; 
       case 3: numThree++; break; 
       case 4: numFour++; break; 
       case 5: numFive++; break; 
       case 6: numSix++; break; 
       } 
       int scoreOne = 0; 
       if (numOne > numTwo && numOne > numThree && numOne > numFour && numOne > numFive && numOne > numSix) 
       { 
       numOne = scoreOne; 

       } 

       else if (numTwo > numOne && numTwo > numFour && numTwo > numFive && numTwo > numSix && numTwo > numThree) 
       { 
       numTwo = scoreOne; 
       } 
       else if (numThree > numOne && numThree > numTwo && numThree > numFour && numThree> numFive && numThree > numSix){ 
       numThree = scoreOne; 
       } 
       else if (numFour > numOne && numFour > numTwo && numFour > numThree && numFour > numFive && numFour > numSix){ 
       numFour = scoreOne; 
       } 
       else if (numFive > numOne && numFive > numTwo && numFive > numThree && numFive > numFour){ 
       numFive = scoreOne; 
       } 

       return scoreOne; 

       } 

    } 

    /** 
    * Resets all dice to an "unrolled" value of 1. 
    * 
    * @param dice The 1D-array of dice to be reset 
    */ 
    private static void resetDice(int[] dice) { 

    } 

    /** 
    * Resets all dice rolls to an uninitialized value of -1. 
    * 
    * @param allDice The 2D-array of dice to be reset 
    */ 
    private static void resetDice(int[][] allDice) { 

    } 

    /** 
    * Checks how many dice are still available to be rolled. A die is no longer 
    * active if it has a value of 0. 
    * 
    * @param dice The 1D-array of dice to check 
    * @return The number of dice that are still available to roll 
    */ 
    private static int numRemainingDice(int[] dice) { 




    } 


    /** 
    * Print all current scores to the console. 
    * 
    * <p>Output should be formatted as follows:<br /> 
    * Player 1: X<br /> 
    * Player 2: X<br /> 
    * etc.<br /> 
    * where player 1's score is stored at index 0 in the array. 
    * 
    * @param scores The 1D-array representing player's current scores. 
    */ 
    private static void printScores(int[] scores) { 



    } 

    /** 
    * Determines if one of the players has accumulated 
    * enough points (>= neededPoints) to win. 
    * 
    * @param scores The 1D-array of current scores 
    * @return The index of the winning player if one exists, else -1 
    */ 


    private static int findWinner(int[] scores, int neededPoints) { 



    } 
} 
+0

findWinner鍵入爲返回一個「int」,但不返回int。同上numRemainingDice和scoreDice(in score)你在_some_代碼路徑中返回一個int,但是你必須在_all_代碼路徑中返回一個int。如果'dice'是一個空數組,那麼'for'循環的主體會發生什麼?從不運行?) – yshavit

+0

如果'dice.length'爲零,它將不會返回任何內容。 –

回答

1

如果骰子數組的長度爲0(如果它是空的),for循環將永遠不會運行。這意味着有可能無法返回任何不允許的東西。爲了解決這個問題,你必須向可能的代碼路徑添加一個return語句,包括for循環永遠不會執行的地方。爲此,在for循環之後,在方法的末尾添加return 0

相關問題