From 2becc2b35757ff804b13383589a12779932bea5a Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Sat, 23 Dec 2023 18:48:21 +0400 Subject: [PATCH] add death mode --- .../controller/GameOverScreenController.java | 17 ++++++++-- .../controller/PlayScreenController.java | 7 ++++ .../creme332/utils/SettingsManager.java | 34 ++++++++----------- .../view/GameOver/GameOverScreen.java | 5 +++ .../creme332/view/GameOver/WPMChart.java | 2 ++ 5 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/github/creme332/controller/GameOverScreenController.java b/src/main/java/com/github/creme332/controller/GameOverScreenController.java index 9426110..5c9eb0a 100644 --- a/src/main/java/com/github/creme332/controller/GameOverScreenController.java +++ b/src/main/java/com/github/creme332/controller/GameOverScreenController.java @@ -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; /** @@ -57,6 +58,8 @@ 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()); @@ -64,22 +67,30 @@ public void showStats() { // 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())); } diff --git a/src/main/java/com/github/creme332/controller/PlayScreenController.java b/src/main/java/com/github/creme332/controller/PlayScreenController.java index 200e61a..bda65e6 100644 --- a/src/main/java/com/github/creme332/controller/PlayScreenController.java +++ b/src/main/java/com/github/creme332/controller/PlayScreenController.java @@ -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 @@ -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(); + } + } /** diff --git a/src/main/java/com/github/creme332/utils/SettingsManager.java b/src/main/java/com/github/creme332/utils/SettingsManager.java index f57d211..c114048 100644 --- a/src/main/java/com/github/creme332/utils/SettingsManager.java +++ b/src/main/java/com/github/creme332/utils/SettingsManager.java @@ -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); } /** diff --git a/src/main/java/com/github/creme332/view/GameOver/GameOverScreen.java b/src/main/java/com/github/creme332/view/GameOver/GameOverScreen.java index 997138a..74cd4dc 100644 --- a/src/main/java/com/github/creme332/view/GameOver/GameOverScreen.java +++ b/src/main/java/com/github/creme332/view/GameOver/GameOverScreen.java @@ -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); diff --git a/src/main/java/com/github/creme332/view/GameOver/WPMChart.java b/src/main/java/com/github/creme332/view/GameOver/WPMChart.java index 8fed0ca..e543938 100644 --- a/src/main/java/com/github/creme332/view/GameOver/WPMChart.java +++ b/src/main/java/com/github/creme332/view/GameOver/WPMChart.java @@ -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);