public class Farkle
extends java.lang.Object
See https://en.wikipedia.org/?title=Farkle for more details.
| Constructor and Description |
|---|
Farkle() |
| Modifier and Type | Method and Description |
|---|---|
private static int |
findWinner(int[] scores,
int neededPoints)
Determines if one of the players has accumulated
enough points (>= neededPoints) to win.
|
static void |
main(java.lang.String[] args)
Users set the number of players (2-6) and score needed to win
for a game of Farkle.
|
private static int |
numRemainingDice(int[] dice)
Checks how many dice are still available to be rolled.
|
private static void |
printDice(int[] dice)
Print a 1D-array to the console.
|
private static void |
printDice(int[][] allDice)
Print a 2D-array to the console.
|
private static void |
printHelp()
Print a help menu to the console.
|
private static void |
printScores(int[] scores)
Print all current scores to the console.
|
private static void |
printTurn(int[][] allDice,
int currentTurnScore)
Print a record of all dice rolled so far during this turn
and the number of points earned so far.
|
private static void |
resetDice(int[] dice)
Resets all dice to an "unrolled" value of 1.
|
private static void |
resetDice(int[][] allDice)
Resets all dice rolls to an uninitialized value of -1.
|
private static int[] |
rollDice(int[] dice,
int[][] allDice,
int currentRollNum)
Rolls the dice for the current turn.
|
private static int |
scoreDice(int[] dice)
Compute the score for the currently rolled dice.
|
public static void main(java.lang.String[] args)
Users set the number of players (2-6) and score needed to win for a game of Farkle.
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.
Program execution starts here.
args - UNUSEDprivate static void printHelp()
private static int[] rollDice(int[] dice,
int[][] allDice,
int currentRollNum)
Each die MUST BE ROLLED using the Dice.rollDie() method. To debug your code with a predictable sequence of dice rolls, invoke Dice.rollDie(true). To generate a random sequence of dice rolls for better testing, invoke Dice.rollDie(false). See the Dice class Javadoc for more information.
dice - The 1D-array of dice to be rolledallDice - The 2D-array of dice, representing all rolls for this
turn. The current roll will be stored into this array.currentRollNum - The number of the current roll, used to store
rolled dice into allDiceprivate static void printDice(int[] dice)
Output should be formatted as follows:
[dice1, dice2, dice3, dice4, dice5, dice6, ]
e.g., [2, 4, 4, 3, 1, 6, ]
Do not print dice that are no longer available, e.g., dice whose value is 0.
dice - The 1D-array of dice to be printed to the consoleprivate static void printDice(int[][] allDice)
Output should be formatted as follows:
[turn1dice1, turn1dice2, turn1dice3, turn1dice4, turn1dice5, turn1dice6, ]
[turn2dice1, turn2dice2, turn2dice3, turn2dice4, turn2dice5, turn2dice6 ]
etc.
e.g.,
[2, 4, 4, 3, 1, 6, ]
[1, 3, 2, 5 ]
where each 1D array enclosed in [] is a row in the 2D-array.
Do not print dice that are no longer available, e.g., dice whose value is 0.
Do not print uninitialized dice, e.g., dice whose value is -1.
allDice - The 2D-array of dice (representing all rolls for this
turn) to be printed to the consoleprivate static void printTurn(int[][] allDice,
int currentTurnScore)
Output should be formatted as follows (where X is the currentTurnScore):
[turn1dice1, turn1dice2, turn1dice3, turn1dice4, turn1dice5, turn1dice6, ]
[turn2dice1, turn2dice2, turn2dice3, turn2dice4, turn2dice5, turn2dice6 ]
etc.
X points so far this turn.
allDice - The 2D-array of dice (representing all rolls for this
turn)currentTurnScore - The current score for this turnprivate static int scoreDice(int[] dice)
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.
For example:
[2, 3, 4, 3, 5, 6]
is worth 2 points (for the two 3s)
resulting dice = [2, 0, 4, 0, 5, 6]
[2, 3, 4, 3, 2, 6]
is worth 2 points (for the two 2s)
resulting dice = [0, 3, 4, 3, 0, 6]
[0, 3, 4, 2, 0, 6]
is worth 0 points (because there are no groups and no die showed 1)
dice - The 1D-array of current dice.private static void resetDice(int[] dice)
dice - The 1D-array of dice to be resetprivate static void resetDice(int[][] allDice)
allDice - The 2D-array of dice to be resetprivate static int numRemainingDice(int[] dice)
dice - The 1D-array of dice to checkprivate static void printScores(int[] scores)
Output should be formatted as follows:
Player 1: X
Player 2: X
etc.
where player 1's score is stored at index 0 in the array.
scores - The 1D-array representing player's current scores.private static int findWinner(int[] scores,
int neededPoints)
scores - The 1D-array of current scores