Skip to content

Commit f3975ad

Browse files
committed
add Game Mode Logic
1 parent 072d00d commit f3975ad

15 files changed

Lines changed: 531 additions & 9 deletions

src/main/java/seedu/ecardnomics/Main.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
import seedu.ecardnomics.command.Command;
44
import seedu.ecardnomics.command.deck.DoneEditCommand;
5+
import seedu.ecardnomics.command.game.DoneGameCommand;
6+
import seedu.ecardnomics.command.game.GameStartCommand;
57
import seedu.ecardnomics.command.normal.EditCommand;
68
import seedu.ecardnomics.command.ExitCommand;
9+
import seedu.ecardnomics.command.normal.StartCommand;
710
import seedu.ecardnomics.deck.Deck;
811
import seedu.ecardnomics.deck.DeckList;
12+
import seedu.ecardnomics.deck.FlashCard;
13+
import seedu.ecardnomics.game.Game;
914
import seedu.ecardnomics.parser.DeckParser;
15+
import seedu.ecardnomics.parser.GameParser;
1016
import seedu.ecardnomics.parser.NormalParser;
1117

1218
/**
@@ -53,6 +59,20 @@ public static Command runDeckMode(Deck deck) {
5359
return command;
5460
}
5561

62+
/**
63+
* Runs Game Mode for specified deck.
64+
*
65+
* @param deck
66+
* @return Command used to exit Game Mode (either <code>done</code> or <code>exit</code>)
67+
*/
68+
public static Command runGameMode(Deck deck) {
69+
GameStartCommand gameStartCommand = new GameStartCommand(deck);
70+
executeCommand(gameStartCommand);
71+
72+
Game game = gameStartCommand.getGameInstance();
73+
return game.run();
74+
}
75+
5676
/**
5777
* Runs Normal Mode in a loop until <code>exit</code> in input.
5878
* Enters Deck Mode when <code>edit</code> is input.
@@ -80,6 +100,15 @@ public static void runNormalMode() {
80100
}
81101
}
82102

103+
if (StartCommand.isStart(command)) {
104+
StartCommand startCommand = (StartCommand) command;
105+
command = runGameMode(startCommand.getDeck());
106+
107+
if (command instanceof DoneGameCommand) {
108+
Ui.printNormalWelcome();
109+
}
110+
}
111+
83112
} while (!ExitCommand.isExit(command));
84113

85114
Ui.printExitLine();
@@ -94,6 +123,14 @@ public static void main(String[] args) {
94123
// TEMP FOR TESTING
95124
Deck pokemon = new Deck("Pokemon");
96125
deckList.addDeck(pokemon);
126+
pokemon.add(new FlashCard("Who's that Pokemon?", "It's Pikachu!"));
127+
pokemon.add(new FlashCard("Who's that Digimon?", "It's Agumon!"));
128+
pokemon.add(new FlashCard("Who's that Ben 10 alien?", "It's Grey Matter!"));
129+
pokemon.add(new FlashCard("Who's that Dog?", "It's Scooby-Doo!"));
130+
// pokemon.add(new FlashCard("A Question 5", "It's Question 5"));
131+
// pokemon.add(new FlashCard("B Question 6", "It's Question 6"));
132+
// pokemon.add(new FlashCard("C Question 7", "It's Question 7"));
133+
// pokemon.add(new FlashCard("D Question 8", "It's Question 8"));
97134
runNormalMode();
98135
// runDeckMode(pokemon);
99136
}

src/main/java/seedu/ecardnomics/Ui.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ public class Ui {
1717
"You are back in Normal Mode";
1818
public static final String DECK_WELCOME_LINE =
1919
"You are now in Deck Mode, editing: ";
20+
public static final String GAME_WELCOME_MESSAGE =
21+
"Welcome to Game Mode!\n" + System.lineSeparator() +
22+
"In this mode, you test your knowledge against the flash cards in the deck.\n" +
23+
"Questions will be displayed in a randomised order. At each question, you can\n" +
24+
" 1. Try to attempt an answer at the question, by typing at the prompt\n" +
25+
" 2. Press <enter> (with an empty attempt if you want to do it in your head)\n" +
26+
"Then, our 'advanced' algorithms will check your answer and score your answer \n" +
27+
"(if any), and display the correct answer for you to check your answer against.\n" +
28+
"Finally, we will ask if you think you got it right. If you did not, the\n" +
29+
"question will be inserted back into the question pool, and you will get a \n" +
30+
"chance to attempt it again!\n" + System.lineSeparator() +
31+
"Press <enter> to begin. Type `help` for help. Have fun!\n" + System.lineSeparator() +
32+
"Game Mode is started for: ";
2033
public static final String BYE_LINE =
2134
"Bye. Hope to see you again soon!";
2235
public static final String NOT_RECOGNISED_LINE =
@@ -60,9 +73,18 @@ public class Ui {
6073
"Question and answer updated.";
6174
private static final String NO_UPDATE_LINE =
6275
"Original question and answer retained";
76+
private static final String INCLUDE_EXCLUDE_LINE =
77+
"Do you want to re-attempt this question later? ";
78+
private static final String ATTEMPT_FEEDBACK_LINE =
79+
"The % match between your answer and the actual answer is:";
80+
private static final String ENTER_ATTEMPT_LINE =
81+
" Enter your attempt below (or `done`, `exit`, `help`):";
82+
private static final String DONE_GAME_LINE =
83+
"You have completed all the flash cards in this deck! Returning to Normal Mode...";
6384

6485
public static final String EXIT = "exit";
6586
public static final String EDIT = "edit";
87+
public static final String START = "start";
6688
public static final String DONE = "done";
6789
public static final String ADD = "add";
6890
public static final String CREATE = "create";
@@ -124,6 +146,14 @@ public static void printDeckPrompt(Deck deck) {
124146
System.out.print(" > ");
125147
}
126148

149+
/**
150+
* Displays the prompt for user input in Game Mode.
151+
*/
152+
public static void printGamePrompt(Deck deck) {
153+
System.out.println("[Game - " + deck.getName() + "]");
154+
System.out.print(" > ");
155+
}
156+
127157
/**
128158
* Displays the prompt for user input without specifying current mode.
129159
*/
@@ -145,6 +175,13 @@ public static void printDeckWelcome(int index, Deck deck) {
145175
printMessage(DECK_WELCOME_LINE + "[" + index + "] " + deck.getName());
146176
}
147177

178+
/**
179+
* Displays the welcome message for Game Mode.
180+
*/
181+
public static void printGameWelcome(int index, Deck deck) {
182+
printMessage(GAME_WELCOME_MESSAGE + "[" + index + "] " + deck.getName());
183+
}
184+
148185
/**
149186
* Displays the greeting message.
150187
*/
@@ -327,4 +364,28 @@ public static void printInvalidYorNResponse() {
327364
public static void printVersionNumber() {
328365
printMessage("Version: " + VERSION_NUMBER);
329366
}
367+
368+
public static void printIncludeExcludeLine() {
369+
System.out.print(INCLUDE_EXCLUDE_LINE + YN_LINE + "\n");
370+
printPrompt();
371+
}
372+
373+
public static void printAttemptFeedback(double matchPercentage) {
374+
System.out.println(String.format("%s %.2f", ATTEMPT_FEEDBACK_LINE, matchPercentage));
375+
}
376+
377+
public static void printGameQuestion(String question) {
378+
System.out.println("Q: " + question);
379+
System.out.println(ENTER_ATTEMPT_LINE);
380+
printPrompt();
381+
}
382+
383+
public static void printAnswerGameMode(String answer) {
384+
System.out.println("A: " + answer);
385+
}
386+
387+
public static void printDoneGameMessage() {
388+
System.out.println(DASH_LINES);
389+
System.out.println(DONE_GAME_LINE);
390+
}
330391
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package seedu.ecardnomics.command.game;
2+
3+
import seedu.ecardnomics.command.Command;
4+
import seedu.ecardnomics.deck.Deck;
5+
6+
public class DoneGameCommand extends GameCommand {
7+
/** Constructor. */
8+
public DoneGameCommand(Deck deck) {
9+
super(deck);
10+
}
11+
12+
@Override
13+
public void execute() {
14+
}
15+
16+
/** Returns if command given is an instance of <code>DoneEditCommand</code>. */
17+
public static boolean isDoneGame(Command command) {
18+
return command instanceof DoneGameCommand;
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package seedu.ecardnomics.command.game;
2+
3+
import seedu.ecardnomics.command.Command;
4+
import seedu.ecardnomics.deck.Deck;
5+
6+
public abstract class GameCommand extends Command {
7+
protected Deck currentDeck;
8+
9+
public GameCommand() {
10+
currentDeck = null;
11+
}
12+
13+
public GameCommand(Deck currentDeck) {
14+
assert currentDeck != null : "Command must operate on a deck.";
15+
this.currentDeck = currentDeck;
16+
}
17+
18+
public abstract void execute();
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package seedu.ecardnomics.command.game;
2+
3+
public class GameResponseCommand extends GameCommand {
4+
String attempt;
5+
6+
public GameResponseCommand(String userInput) {
7+
super();
8+
attempt = userInput;
9+
}
10+
11+
@Override
12+
public void execute() {
13+
//
14+
}
15+
16+
public String getAttempt() {
17+
return attempt;
18+
}
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package seedu.ecardnomics.command.game;
2+
3+
import seedu.ecardnomics.deck.Deck;
4+
import seedu.ecardnomics.game.Game;
5+
6+
public class GameStartCommand extends GameCommand {
7+
Game game;
8+
9+
/** Constructor. */
10+
public GameStartCommand(Deck deck) {
11+
super(deck);
12+
}
13+
14+
@Override
15+
public void execute() {
16+
game = new Game(currentDeck);
17+
}
18+
19+
public Game getGameInstance() {
20+
return game;
21+
}
22+
}

src/main/java/seedu/ecardnomics/command/normal/HelpCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class HelpCommand extends NormalCommand {
1212
+ " create <nm> Creates a new deck of flash cards, named <nm>.\n"
1313
+ " decks Lists all available decks.\n"
1414
+ " edit <ix> Enter Deck Mode for editing the deck at list index <ix>.\n"
15+
+ " start <ix> Enter Game Mode for deck at list index <ix>! Do your best!\n"
1516
+ " delete <ix> Deletes the deck at list index <ix> from list of decks.\n"
1617
+ " exit Exits the program.\n"
1718
+ " help Show this output.\n"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package seedu.ecardnomics.command.normal;
2+
3+
import seedu.ecardnomics.Ui;
4+
import seedu.ecardnomics.command.Command;
5+
import seedu.ecardnomics.command.game.GameStartCommand;
6+
import seedu.ecardnomics.deck.Deck;
7+
import seedu.ecardnomics.deck.DeckList;
8+
9+
public class StartCommand extends NormalCommand {
10+
Deck deck;
11+
12+
/** Constructor. */
13+
public StartCommand(DeckList deckList, Deck deck) {
14+
super(deckList);
15+
assert deck != null : "Do not operate on a null reference.";
16+
this.deck = deck;
17+
}
18+
19+
@Override
20+
public void execute() {
21+
Ui.printGameWelcome(deckList.getIndexOf(deck) + 1, deck);
22+
new GameStartCommand(deck);
23+
}
24+
25+
/** Returns Deck whose Game Mode is in progress. */
26+
public Deck getDeck() {
27+
return deck;
28+
}
29+
30+
/** Returns if command given is an instance of <code>StartCommand</code>. */
31+
public static boolean isStart(Command command) {
32+
return command instanceof StartCommand;
33+
}
34+
}

src/main/java/seedu/ecardnomics/deck/Deck.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,20 @@ public void setName(String name) {
3535
this.name = name;
3636
}
3737

38+
/**
39+
* Retrieves the inherent ArrayList data structure of the deck.
40+
*
41+
* @return ArrayList of FlashCards
42+
*/
43+
public ArrayList<FlashCard> getDeck() {
44+
return deck;
45+
}
46+
3847
/**
3948
* Retrieves the flashcard at specified index.
4049
*
4150
* @param index Index of flashcard to be found
42-
* @return
51+
* @return FlashCard at index
4352
*/
4453
public FlashCard get(int index) {
4554
assert (index >= 0 && index < deck.size()) : "Index should be within range";
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package seedu.ecardnomics.game;
2+
3+
import seedu.ecardnomics.command.Command;
4+
import seedu.ecardnomics.deck.Deck;
5+
6+
public class Game {
7+
GameStorage storage;
8+
GameEngine engine;
9+
10+
public Game(Deck deck) {
11+
storage = new GameStorage(deck);
12+
engine = new GameEngine(storage);
13+
}
14+
15+
public Command run() {
16+
return engine.runGameLoop();
17+
}
18+
}

0 commit comments

Comments
 (0)