java - This method must return a result of type int? Farkle Game -
this have far. reason, program won't let me return scoreone. method trying find repeated number , use points.
import java.util.scanner; public class farkle { // may not declare static fields in farkle // values must passed , methods (as described method // headers) /** * <p>users set number of players (2-6) , score needed win * game of farkle. * * <p>players take turns rolling dice , try accumulate required * number of points. game ends when first player accumulate * enough points declared winner. * * <p>program execution starts here. * * @param args unused */ public static void main(string[] args) { // todo scanner in = new scanner(system.in); /* * should put welcome message , main * program loop. make sure match output what's described * in project specification. */ system.out.println("welcome farkle."); // number of players game system.out.print("how many players? "); int numofplayers = in.nextint(); while (numofplayers > 6 || numofplayers < 2) { system.out.println("invalid answer. farkle may played 2-6 players. "); system.out.print("how many players? "); numofplayers = in.nextint(); } // user inputs points needed win system.out.print("how many points need win? "); int neededpoints = in.nextint(); system.out.println("game play until " + neededpoints + " points"); system.out.println("it player 1's turn. "); //user inputs command 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 dice"); system.out.println("\"finish\" - terminate current turn"); system.out.println("\"dice\" - print current dice"); system.out.println("\"turn\" - print current turn far"); system.out.println("\"scores\" - print scores"); system.out.println("\"help\" - print menu"); system.out.println("\"exit\" - terminates 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 2d-array 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 2d-array of dice (representing rolls * turn) printed console */ private static void printdice(int[][] alldice) { } /** * print record of dice rolled far during turn * , number of points earned far. * * <p>output should formatted follows (where x currentturnscore):<br /> * [turn1dice1, turn1dice2, turn1dice3, turn1dice4, turn1dice5, turn1dice6, ]<br /> * [turn2dice1, turn2dice2, turn2dice3, turn2dice4, turn2dice5, turn2dice6 ]<br /> * etc.<br /> * x points far turn.<br /> * * @param alldice 2d-array of dice (representing rolls * turn) * @param currentturnscore current score turn */ private static void printturn(int[][] alldice, int currentturnscore) { } /** * compute score rolled dice. * * dice should scored using following algorithm: * <ol> * <li>find groupings of dice of same number (die showing 0 not count) * <li>the largest group (> size 1) of dice of same number worth * points; number of points equal number of dice in group. * <li>if more 1 die number has same size group, smallest die * number should used. * <li>if no groups > size 1 exist, 1 point can earned if there die * number 1. * <li>otherwise, 0 points earned. * </ol> * * <p>dice found worth points should set 0, indicating * no longer available re-rolled later in turn. * * <p>for example:<br /> * [2, 3, 4, 3, 5, 6]<br /> * worth 2 points (for 2 3s)<br /> * resulting dice = [2, 0, 4, 0, 5, 6]<br /><br /> * [2, 3, 4, 3, 2, 6]<br /> * worth 2 points (for 2 2s)<br /> * resulting dice = [0, 3, 4, 3, 0, 6]<br /><br /> * [0, 3, 4, 2, 0, 6]<br /> * worth 0 points (because there no groups , no die showed 1) * * @param dice 1d-array of current dice. * @return score earned 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; ( 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 dice "unrolled" value of 1. * * @param dice 1d-array of dice reset */ private static void resetdice(int[] dice) { } /** * resets dice rolls uninitialized value of -1. * * @param alldice 2d-array of dice reset */ private static void resetdice(int[][] alldice) { } /** * checks how many dice still available rolled. die no longer * active if has value of 0. * * @param dice 1d-array of dice check * @return number of dice still available roll */ private static int numremainingdice(int[] dice) { } /** * print current scores console. * * <p>output should formatted follows:<br /> * player 1: x<br /> * player 2: x<br /> * etc.<br /> * player 1's score stored @ index 0 in array. * * @param scores 1d-array representing player's current scores. */ private static void printscores(int[] scores) { } /** * determines if 1 of players has accumulated * enough points (>= neededpoints) win. * * @param scores 1d-array of current scores * @return index of winning player if 1 exists, else -1 */ private static int findwinner(int[] scores, int neededpoints) { } }
if length of dice array 0 (if empty), loop never run. means there chance of not being able return not allowed. solve this, must add return statement possible code paths, including 1 loop never executes. this, add return 0
@ end of method, after loop.
Comments
Post a Comment