Skip to content

Commit

Permalink
add Game Mode Logic
Browse files Browse the repository at this point in the history
  • Loading branch information
exuberant-duck committed Oct 24, 2020
1 parent 072d00d commit f3975ad
Show file tree
Hide file tree
Showing 15 changed files with 531 additions and 9 deletions.
37 changes: 37 additions & 0 deletions src/main/java/seedu/ecardnomics/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

import seedu.ecardnomics.command.Command;
import seedu.ecardnomics.command.deck.DoneEditCommand;
import seedu.ecardnomics.command.game.DoneGameCommand;
import seedu.ecardnomics.command.game.GameStartCommand;
import seedu.ecardnomics.command.normal.EditCommand;
import seedu.ecardnomics.command.ExitCommand;
import seedu.ecardnomics.command.normal.StartCommand;
import seedu.ecardnomics.deck.Deck;
import seedu.ecardnomics.deck.DeckList;
import seedu.ecardnomics.deck.FlashCard;
import seedu.ecardnomics.game.Game;
import seedu.ecardnomics.parser.DeckParser;
import seedu.ecardnomics.parser.GameParser;
import seedu.ecardnomics.parser.NormalParser;

/**
Expand Down Expand Up @@ -53,6 +59,20 @@ public static Command runDeckMode(Deck deck) {
return command;
}

/**
* Runs Game Mode for specified deck.
*
* @param deck
* @return Command used to exit Game Mode (either <code>done</code> or <code>exit</code>)
*/
public static Command runGameMode(Deck deck) {
GameStartCommand gameStartCommand = new GameStartCommand(deck);
executeCommand(gameStartCommand);

Game game = gameStartCommand.getGameInstance();
return game.run();
}

/**
* Runs Normal Mode in a loop until <code>exit</code> in input.
* Enters Deck Mode when <code>edit</code> is input.
Expand Down Expand Up @@ -80,6 +100,15 @@ public static void runNormalMode() {
}
}

if (StartCommand.isStart(command)) {
StartCommand startCommand = (StartCommand) command;
command = runGameMode(startCommand.getDeck());

if (command instanceof DoneGameCommand) {
Ui.printNormalWelcome();
}
}

} while (!ExitCommand.isExit(command));

Ui.printExitLine();
Expand All @@ -94,6 +123,14 @@ public static void main(String[] args) {
// TEMP FOR TESTING
Deck pokemon = new Deck("Pokemon");
deckList.addDeck(pokemon);
pokemon.add(new FlashCard("Who's that Pokemon?", "It's Pikachu!"));
pokemon.add(new FlashCard("Who's that Digimon?", "It's Agumon!"));
pokemon.add(new FlashCard("Who's that Ben 10 alien?", "It's Grey Matter!"));
pokemon.add(new FlashCard("Who's that Dog?", "It's Scooby-Doo!"));
// pokemon.add(new FlashCard("A Question 5", "It's Question 5"));
// pokemon.add(new FlashCard("B Question 6", "It's Question 6"));
// pokemon.add(new FlashCard("C Question 7", "It's Question 7"));
// pokemon.add(new FlashCard("D Question 8", "It's Question 8"));
runNormalMode();
// runDeckMode(pokemon);
}
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/seedu/ecardnomics/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ public class Ui {
"You are back in Normal Mode";
public static final String DECK_WELCOME_LINE =
"You are now in Deck Mode, editing: ";
public static final String GAME_WELCOME_MESSAGE =
"Welcome to Game Mode!\n" + System.lineSeparator() +
"In this mode, you test your knowledge against the flash cards in the deck.\n" +
"Questions will be displayed in a randomised order. At each question, you can\n" +
" 1. Try to attempt an answer at the question, by typing at the prompt\n" +
" 2. Press <enter> (with an empty attempt if you want to do it in your head)\n" +
"Then, our 'advanced' algorithms will check your answer and score your answer \n" +
"(if any), and display the correct answer for you to check your answer against.\n" +
"Finally, we will ask if you think you got it right. If you did not, the\n" +
"question will be inserted back into the question pool, and you will get a \n" +
"chance to attempt it again!\n" + System.lineSeparator() +
"Press <enter> to begin. Type `help` for help. Have fun!\n" + System.lineSeparator() +
"Game Mode is started for: ";
public static final String BYE_LINE =
"Bye. Hope to see you again soon!";
public static final String NOT_RECOGNISED_LINE =
Expand Down Expand Up @@ -60,9 +73,18 @@ public class Ui {
"Question and answer updated.";
private static final String NO_UPDATE_LINE =
"Original question and answer retained";
private static final String INCLUDE_EXCLUDE_LINE =
"Do you want to re-attempt this question later? ";
private static final String ATTEMPT_FEEDBACK_LINE =
"The % match between your answer and the actual answer is:";
private static final String ENTER_ATTEMPT_LINE =
" Enter your attempt below (or `done`, `exit`, `help`):";
private static final String DONE_GAME_LINE =
"You have completed all the flash cards in this deck! Returning to Normal Mode...";

public static final String EXIT = "exit";
public static final String EDIT = "edit";
public static final String START = "start";
public static final String DONE = "done";
public static final String ADD = "add";
public static final String CREATE = "create";
Expand Down Expand Up @@ -124,6 +146,14 @@ public static void printDeckPrompt(Deck deck) {
System.out.print(" > ");
}

/**
* Displays the prompt for user input in Game Mode.
*/
public static void printGamePrompt(Deck deck) {
System.out.println("[Game - " + deck.getName() + "]");
System.out.print(" > ");
}

/**
* Displays the prompt for user input without specifying current mode.
*/
Expand All @@ -145,6 +175,13 @@ public static void printDeckWelcome(int index, Deck deck) {
printMessage(DECK_WELCOME_LINE + "[" + index + "] " + deck.getName());
}

/**
* Displays the welcome message for Game Mode.
*/
public static void printGameWelcome(int index, Deck deck) {
printMessage(GAME_WELCOME_MESSAGE + "[" + index + "] " + deck.getName());
}

/**
* Displays the greeting message.
*/
Expand Down Expand Up @@ -327,4 +364,28 @@ public static void printInvalidYorNResponse() {
public static void printVersionNumber() {
printMessage("Version: " + VERSION_NUMBER);
}

public static void printIncludeExcludeLine() {
System.out.print(INCLUDE_EXCLUDE_LINE + YN_LINE + "\n");
printPrompt();
}

public static void printAttemptFeedback(double matchPercentage) {
System.out.println(String.format("%s %.2f", ATTEMPT_FEEDBACK_LINE, matchPercentage));
}

public static void printGameQuestion(String question) {
System.out.println("Q: " + question);
System.out.println(ENTER_ATTEMPT_LINE);
printPrompt();
}

public static void printAnswerGameMode(String answer) {
System.out.println("A: " + answer);
}

public static void printDoneGameMessage() {
System.out.println(DASH_LINES);
System.out.println(DONE_GAME_LINE);
}
}
20 changes: 20 additions & 0 deletions src/main/java/seedu/ecardnomics/command/game/DoneGameCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package seedu.ecardnomics.command.game;

import seedu.ecardnomics.command.Command;
import seedu.ecardnomics.deck.Deck;

public class DoneGameCommand extends GameCommand {
/** Constructor. */
public DoneGameCommand(Deck deck) {
super(deck);
}

@Override
public void execute() {
}

/** Returns if command given is an instance of <code>DoneEditCommand</code>. */
public static boolean isDoneGame(Command command) {
return command instanceof DoneGameCommand;
}
}
19 changes: 19 additions & 0 deletions src/main/java/seedu/ecardnomics/command/game/GameCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package seedu.ecardnomics.command.game;

import seedu.ecardnomics.command.Command;
import seedu.ecardnomics.deck.Deck;

public abstract class GameCommand extends Command {
protected Deck currentDeck;

public GameCommand() {
currentDeck = null;
}

public GameCommand(Deck currentDeck) {
assert currentDeck != null : "Command must operate on a deck.";
this.currentDeck = currentDeck;
}

public abstract void execute();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package seedu.ecardnomics.command.game;

public class GameResponseCommand extends GameCommand {
String attempt;

public GameResponseCommand(String userInput) {
super();
attempt = userInput;
}

@Override
public void execute() {
//
}

public String getAttempt() {
return attempt;
}
}
22 changes: 22 additions & 0 deletions src/main/java/seedu/ecardnomics/command/game/GameStartCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package seedu.ecardnomics.command.game;

import seedu.ecardnomics.deck.Deck;
import seedu.ecardnomics.game.Game;

public class GameStartCommand extends GameCommand {
Game game;

/** Constructor. */
public GameStartCommand(Deck deck) {
super(deck);
}

@Override
public void execute() {
game = new Game(currentDeck);
}

public Game getGameInstance() {
return game;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class HelpCommand extends NormalCommand {
+ " create <nm> Creates a new deck of flash cards, named <nm>.\n"
+ " decks Lists all available decks.\n"
+ " edit <ix> Enter Deck Mode for editing the deck at list index <ix>.\n"
+ " start <ix> Enter Game Mode for deck at list index <ix>! Do your best!\n"
+ " delete <ix> Deletes the deck at list index <ix> from list of decks.\n"
+ " exit Exits the program.\n"
+ " help Show this output.\n"
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/seedu/ecardnomics/command/normal/StartCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package seedu.ecardnomics.command.normal;

import seedu.ecardnomics.Ui;
import seedu.ecardnomics.command.Command;
import seedu.ecardnomics.command.game.GameStartCommand;
import seedu.ecardnomics.deck.Deck;
import seedu.ecardnomics.deck.DeckList;

public class StartCommand extends NormalCommand {
Deck deck;

/** Constructor. */
public StartCommand(DeckList deckList, Deck deck) {
super(deckList);
assert deck != null : "Do not operate on a null reference.";
this.deck = deck;
}

@Override
public void execute() {
Ui.printGameWelcome(deckList.getIndexOf(deck) + 1, deck);
new GameStartCommand(deck);
}

/** Returns Deck whose Game Mode is in progress. */
public Deck getDeck() {
return deck;
}

/** Returns if command given is an instance of <code>StartCommand</code>. */
public static boolean isStart(Command command) {
return command instanceof StartCommand;
}
}
11 changes: 10 additions & 1 deletion src/main/java/seedu/ecardnomics/deck/Deck.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,20 @@ public void setName(String name) {
this.name = name;
}

/**
* Retrieves the inherent ArrayList data structure of the deck.
*
* @return ArrayList of FlashCards
*/
public ArrayList<FlashCard> getDeck() {
return deck;
}

/**
* Retrieves the flashcard at specified index.
*
* @param index Index of flashcard to be found
* @return
* @return FlashCard at index
*/
public FlashCard get(int index) {
assert (index >= 0 && index < deck.size()) : "Index should be within range";
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/seedu/ecardnomics/game/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package seedu.ecardnomics.game;

import seedu.ecardnomics.command.Command;
import seedu.ecardnomics.deck.Deck;

public class Game {
GameStorage storage;
GameEngine engine;

public Game(Deck deck) {
storage = new GameStorage(deck);
engine = new GameEngine(storage);
}

public Command run() {
return engine.runGameLoop();
}
}
Loading

0 comments on commit f3975ad

Please sign in to comment.