Skip to content
Snippets Groups Projects
Commit 53e1a299 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Extended game to be playable with 7-sided dice (or arbitrary-sided dice)

parent 9986b501
Branches
Tags
No related merge requests found
......@@ -7,6 +7,10 @@ import edu.unl.cse.csce361.yatzy.controller.ScoreController;
import edu.unl.cse.csce361.yatzy.view.GameBoard;
import edu.unl.cse.csce361.yatzy.view.textview.TextGameBoard;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
/**
* <p>The main class for the game of Yatzy.</p>
*
......@@ -15,7 +19,68 @@ import edu.unl.cse.csce361.yatzy.view.textview.TextGameBoard;
*/
public class Game {
public static final int NUMBER_OF_DICE = 5;
public static final int NUMBER_OF_DIE_SIDES = 6;
public static final int NUMBER_OF_DIE_SIDES;
static {
NUMBER_OF_DIE_SIDES = promptForNumberOfDieSides();
}
enum MenuOption {
SIX_SIDES("Play traditional Yatzy with 6-sided dice"),
SEVEN_SIDES("Play Ya7zy with 7-sided dice"),
EXIT("Exit program");
private String description;
MenuOption(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
/**
* Prompts the player to select the number of die faces and returns that value or exits if no value is chosen.
* @return the number of die sides the player wants
*/
private static int promptForNumberOfDieSides() {
int numberOfSides = 0;
int intOption = 0;
MenuOption menuOption = MenuOption.EXIT;
Scanner scanner = new Scanner(System.in);
try {
System.out.println("Welcome to Ya7zy by Procrastination Pastimes!");
for (MenuOption option : MenuOption.values()) {
System.out.println((option.ordinal() + 1) + " -- " + option.getDescription());
}
System.out.println();
System.out.print("Please select your game preference: ");
intOption = scanner.nextInt();
scanner.nextLine(); // consume the newline
menuOption = MenuOption.values()[intOption - 1];
} catch (InputMismatchException | ArrayIndexOutOfBoundsException ignored) {
System.out.println("Please select a valid, in-range integer.");
menuOption = MenuOption.EXIT;
} finally {
switch (menuOption) {
case SIX_SIDES:
numberOfSides = 6;
break;
case SEVEN_SIDES:
numberOfSides = 7;
break;
case EXIT:
System.out.println("Goodbye.");
System.exit(0);
default:
System.err.println("Somehow you selected an impossible option.");
System.exit(1);
}
}
return numberOfSides;
}
public static void main(String[] args) {
boolean isVT100 = ((args.length > 0) && args[0].equalsIgnoreCase("vt100"));
......
......@@ -77,8 +77,9 @@ public class ScoreController extends Controller {
addRightCategory(new TwoPairCommand(), categoryModels);
addRightCategory(new OfAKindCommand(3), categoryModels);
addRightCategory(new OfAKindCommand(4), categoryModels);
addRightCategory(new SequenceCommand(1), categoryModels);
addRightCategory(new SequenceCommand(2), categoryModels);
for (int i = 0; i < Game.NUMBER_OF_DIE_SIDES - 4; i++) {
addRightCategory(new SequenceCommand(i + 1), categoryModels);
}
addRightCategory(new FullHouseCommand(), categoryModels);
addRightCategory(new ChanceCommand(), categoryModels);
addRightCategory(new OfAKindCommand(5), categoryModels);
......
......@@ -12,10 +12,13 @@ import edu.unl.cse.csce361.yatzy.model.CategoryModel;
*/
public abstract class AbstractDieBasedScoringCommand implements ScoringCommand {
protected final static RuleBasedNumberFormat numberFormat;
protected final static RuleBasedNumberFormat sequenceFormat;
static {
numberFormat = new RuleBasedNumberFormat(RuleBasedNumberFormat.SPELLOUT);
numberFormat.setContext(DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU);
sequenceFormat = new RuleBasedNumberFormat(RuleBasedNumberFormat.ORDINAL);
sequenceFormat.setContext(DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU);
}
protected final CategoryModel categoryModel;
......
......@@ -15,13 +15,6 @@ public class SequenceCommand extends AbstractDieBasedScoringCommand {
@Override
public String toString() {
switch (sequenceStart) {
case 1:
return "Small Straight";
case 2:
return "Large Straight";
default:
return "Straight from " + numberFormat.format(sequenceStart);
}
return sequenceFormat.format(sequenceStart) + " Straight";
}
}
package edu.unl.cse.csce361.yatzy.view.textview;
import edu.unl.cse.csce361.yatzy.Game;
import edu.unl.cse.csce361.yatzy.view.AbstractDieView;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
......@@ -15,7 +16,7 @@ import java.util.Objects;
*/
public class TextDieView extends AbstractDieView {
public static final String dieFacesFile = "d6.json";
public static final String dieFacesFile = "d" + Game.NUMBER_OF_DIE_SIDES + ".json";
public static final String[] faces;
private static final String plainBackground;
......
{
"faces": [
"+-------+%n| |%n| |%n| |%n+-------+",
"+-------+%n| |%n| * |%n| |%n+-------+",
"+-------+%n| * |%n| |%n| * |%n+-------+",
"+-------+%n| * |%n| * |%n| * |%n+-------+",
"+-------+%n| * * |%n| |%n| * * |%n+-------+",
"+-------+%n| * * |%n| * |%n| * * |%n+-------+",
"+-------+%n| * * |%n| * * |%n| * * |%n+-------+",
"+-------+%n| * * |%n| * * * |%n| * * |%n+-------+"
],
"plainBackground": " %n %n %n %n ",
"highlightedBackground": "X X%nX X%nX X%nX X%nX X"
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment