Skip to content

Commit

Permalink
add death mode
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Dec 23, 2023
1 parent bccf733 commit 2becc2b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.github.creme332.model.Model;
import com.github.creme332.utils.Calculator;
import com.github.creme332.utils.SettingsManager;
import com.github.creme332.view.GameOver.GameOverScreen;

/**
Expand Down Expand Up @@ -57,29 +58,39 @@ public void addTabAction(Action tabAction) {
* Display game statistics: game duration, WPM, accuracy
*/
public void showStats() {
SettingsManager settings = new SettingsManager();

// display chart
gameOverScreen.drawChart(model.getTimeArray(),
model.getWPMArray());

// display game duration
gameOverScreen.setTimeTaken(model.getGameDuration());

// get char count depending on game mode
long charCount = 0;
if (settings.getData("Mode").equals("death")) {
charCount = model.getCursorPos() + 1;
} else {
charCount = model.getTypeText().length();
}

// display cpm
double cpm = calc.cpm(
model.getTypeText().length(),
charCount,
model.getGameDuration());
gameOverScreen.setCPM((long) cpm);

// display final wpm
double wpm = calc.wpm(
model.getTypeText().length(),
charCount,
model.getGameDuration());
gameOverScreen.setWPM((long) wpm);

// display accuracy
gameOverScreen.setAccuracy(
(long) calc.accuracy(
model.getTypeText().length(),
charCount,
model.getTotalMistakes()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ private void dispatchKeyEvent(String keyCommand) {
}

// incorrect character pressed

// increment number of mistakes made by 1
model.incrementMistakes();

// highlight incorrectly typed character red, if it is not already red
Expand All @@ -152,6 +154,11 @@ private void dispatchKeyEvent(String keyCommand) {
err.printStackTrace();
}

// check if game mode is death mode and end game immediately
if (settings.getData("Mode").equals("death")) {
handleGameOver();
}

}

/**
Expand Down
34 changes: 15 additions & 19 deletions src/main/java/com/github/creme332/utils/SettingsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,40 @@ public class SettingsManager {
public SettingsManager() {
// initialize all settings and their possible options
FontIcon icon = FontIcon.of(BootstrapIcons.SPEEDOMETER, 20, Color.white);
Setting s = new Setting("Mode", new String[] { "word", "quote" },
"In word mode you must type a list random words whereas in quote mode, you are given a random quote. The number of words in each mode is determined by the difficulty setting.",
Setting setting = new Setting("Mode", new String[] { "word", "death" },
"In word mode you must type a limited number of random words with no time limit. Death mode is the same as word mode with the exception that the game ends on your first mistake.",
icon);
settingDict.put(
"Mode",
s);
setting);

icon = FontIcon.of(BootstrapIcons.STAR_FILL, 20, Color.white);
s = new Setting("Difficulty", new String[] { "easy", "medium", "hard" },
"Normal is the classic type test experience. Expert fails the test if you submit (press space) an incorrect word. Master fails if you press a single incorrect key (meaning you have to achieve 100% accuracy).",
setting = new Setting("Difficulty", new String[] { "easy", "medium", "hard" },
"In the word game mode, the higher the difficulty, the more words you have to type with no time limit. In time mode, the higher the difficulty, the less time you have to type as many words as you can.",
icon);
settingDict.put("Difficulty", s);
settingDict.put("Difficulty", setting);

icon = FontIcon.of(BootstrapIcons.SPEEDOMETER, 20, Color.white);
s = new Setting("Live speed", new String[] { "hide", "show" },
setting = new Setting("Live speed", new String[] { "hide", "show" },
"Displays a live speed during the test. Updates once every second.", icon);
settingDict.put("Live speed", s);
settingDict.put("Live speed", setting);

icon = FontIcon.of(BootstrapIcons.CURSOR, 20, Color.white);
s = new Setting("Live accuracy", new String[] { "hide", "show" }, "Displays live accuracy during the test.",
setting = new Setting("Live accuracy", new String[] { "hide", "show" },
"Displays live accuracy during the test.",
icon);
settingDict.put("Live accuracy", s);
settingDict.put("Live accuracy", setting);

icon = FontIcon.of(BootstrapIcons.STOPWATCH, 20, Color.white);
s = new Setting("Live timer", new String[] { "hide", "show" },
setting = new Setting("Live timer", new String[] { "hide", "show" },
"Displays a live timer for timed tests and word count for word based tests (word, quote or custom mode).",
icon);
settingDict.put("Live timer", s);

icon = FontIcon.of(BootstrapIcons.MOON, 20, Color.white);
s = new Setting("Lazy mode", new String[] { "off", "on" },
"Replaces accents / diacritics / special characters with their normal letter equivalents.", icon);
settingDict.put("Lazy mode", s);
settingDict.put("Live timer", setting);

icon = FontIcon.of(BootstrapIcons.SPEAKER, 20, Color.white);
s = new Setting("Typing sound", new String[] { "off", "on" },
setting = new Setting("Typing sound", new String[] { "off", "on" },
"Plays a short sound when you press a key.", icon);
settingDict.put("Typing sound", s);
settingDict.put("Typing sound", setting);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ public void addHomeButtonListener(ActionListener newActionListener) {
}

public void drawChart(double[] timeData, double[] wpmData) {
if (timeData.length == 0 || wpmData.length == 0) {
timeData = new double[] { 0 };
wpmData = new double[] { 0 };
}

chart.updateSeries(timeData, wpmData);
double average = Arrays.stream(wpmData).average().orElse(Double.NaN);
chart.updateAverageWPM(average);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/github/creme332/view/GameOver/WPMChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ public void saveImage() {
* @param wpmData array of WPM data.
*/
public void updateSeries(double[] timeData, double[] wpmData) {
assert (timeData.length > 0 && wpmData.length > 0);

chart.removeSeries(seriesName);
XYSeries series = chart.addSeries(seriesName, timeData, wpmData);

Expand Down

0 comments on commit 2becc2b

Please sign in to comment.