Skip to content

Commit

Permalink
Finishing touches and link final version (#129)
Browse files Browse the repository at this point in the history
* tidied up the codebase and added javadoc where needed

* Added version link in readme

* added extra javadoc
  • Loading branch information
shyke0611 authored Oct 7, 2024
1 parent ea4ff33 commit 3da3571
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 220 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ You will need the following software installed:
* [Stacked-Success Versions](https://github.com/Stacked-Success/Softeng310/releases)

**View individual versions**:
* [Stacked-Success v1.0.4](https://github.com/Stacked-Success/Softeng310/releases/tag/v.1.0.4) (Latest)
* [Stacked-Success Final v1.0.5](https://github.com/Stacked-Success/Softeng310/releases/tag/v.1.0.5) (Latest)
* [Stacked-Success v1.0.4](https://github.com/Stacked-Success/Softeng310/releases/tag/v.1.0.4)
* [Stacked-Success v1.0.3](https://github.com/Stacked-Success/Softeng310/releases/tag/v1.0.3)
* [Stacked-Success v1.0.2](https://github.com/Stacked-Success/Softeng310/releases/tag/v1.0.2)
* [Stacked-Success v1.0.1](https://github.com/Stacked-Success/Softeng310/releases/tag/v1.0.1)
Expand Down
2 changes: 0 additions & 2 deletions marathon_score.txt
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
10|20|300
0|21|5
1 change: 0 additions & 1 deletion src/main/java/com/stackedsuccess/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.stackedsuccess.managers.SceneManager.AppUI;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
Expand Down
17 changes: 4 additions & 13 deletions src/main/java/com/stackedsuccess/ScoreRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,22 @@ private ScoreRecorder() {
* @throws IOException if an I/O error occurs
*/
public static void saveScore(String score) throws IOException {
HashMap<String, Integer> scores = getAllScores(); // Get current scores as a HashMap
HashMap<String, Integer> scores = getAllScores();
String playerName;
if(NameEntryController.name == null || NameEntryController.name.equals("")){
playerName = "Anonymous";
} else {
playerName = NameEntryController.name;
}
// Add or update the score for the player
scores.put(playerName, Integer.parseInt(score));

// Sort the scores in descending order and keep only the top MAX_SCORES entries
List<Map.Entry<String, Integer>> sortedScores = new ArrayList<>(scores.entrySet());
sortedScores.sort((entry1, entry2) -> entry2.getValue().compareTo(entry1.getValue()));

// Clear the map and add back only the top scores
scores.clear();
for (int i = 0; i < Math.min(MAX_SCORES, sortedScores.size()); i++) {
Map.Entry<String, Integer> entry = sortedScores.get(i);
scores.put(entry.getKey(), entry.getValue());
}

// Write the sorted scores back to the file
writeScores(scores);
}

Expand All @@ -64,8 +58,6 @@ public static String getHighScore() throws IOException {
if (scores.isEmpty()) {
return "No high score available.";
}

// Find the player with the highest score
return Collections.max(scores.entrySet(), Map.Entry.comparingByValue()).toString();
}

Expand All @@ -85,7 +77,7 @@ public static HashMap<String, Integer> getAllScores() throws IOException {
String line;
while ((line = reader.readLine()) != null) {
if (!line.trim().isEmpty()) {
String[] parts = line.split(" "); // Assuming scores are formatted as "Name Score"
String[] parts = line.split(" ");
if (parts.length == 2) {
String playerName = parts[0];
Integer playerScore = Integer.parseInt(parts[1]);
Expand All @@ -106,7 +98,7 @@ public static HashMap<String, Integer> getAllScores() throws IOException {
static void writeScores(HashMap<String, Integer> scores) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(SCOREFILE))) {
for (HashMap.Entry<String, Integer> entry : scores.entrySet()) {
writer.write(entry.getKey() + " " + entry.getValue()); // Write name and score
writer.write(entry.getKey() + " " + entry.getValue());
writer.newLine();
}
}
Expand All @@ -125,7 +117,6 @@ public static void createScoreFile() {
try {
boolean isFileCreated = scoreFile.createNewFile();
if (!isFileCreated) {
// Retry creating the file
isFileCreated = scoreFile.createNewFile();
if (!isFileCreated) {
throw new IOException("Failed to create score file after retrying.");
Expand All @@ -151,7 +142,7 @@ public static void saveMarathonScore(int linesCleared, int targetLines, int time
scores.add(newScore);

if (scores.size() > MAX_SCORES) {
scores.remove(scores.size() - 1); // Keep only the top MAX_SCORES entries
scores.remove(scores.size() - 1);
}
writeMarathonScores(scores);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ public class GameBoardController implements GameStateManager {

private static final int SOLID_BLOCK_VALUE = -2;

private int targetLines; // Stores the target number of lines in Marathon Mode
private boolean isMarathonMode; // Stores whether Marathon Mode is active
private int targetLines;
private boolean isMarathonMode;

private GameInstance gameInstance;
private final TetriminoImageManager imageManager;

private Timeline gameTimer; // JavaFX Timeline to schedule timer updates
private int elapsedSeconds; // Track the total number of elapsed seconds
private Timeline gameTimer;
private int elapsedSeconds;

public GameBoardController() {
imageManager = new TetriminoImageManager();
Expand All @@ -109,8 +109,8 @@ public GameBoardController() {
*/
public void setGameInstance(GameInstance gameInstance) {
this.gameInstance = gameInstance;
this.isMarathonMode = gameInstance.getIsMarathonMode(); // Store whether it is Marathon Mode
this.targetLines = gameInstance.getTargetLines(); // Get the target lines if Marathon Mode is active
this.isMarathonMode = gameInstance.getIsMarathonMode();
this.targetLines = gameInstance.getTargetLines();
}

private TutorialController tutorialController;
Expand All @@ -136,25 +136,19 @@ public void setGameInstance(GameInstance gameInstance) {
*/
@FXML
public void initialize() {
// resets score, line, and level to initial state
resetLabels();
basePane.requestFocus();

Platform.runLater(() -> {
if (gameInstance != null) {
gameInstance.start();

// Update next piece view
nextPieceView
.setImage(imageManager.getTetriminoImage(gameInstance.getGameBoard().getNextTetrimino().getClass()));

// Set window close handler
setWindowCloseHandler(getStage());

// Initialize lineLabel for Marathon Mode
if (isMarathonMode) {
lineLabel.setText("0/" + targetLines);
// start timer
startTimer();
} else {
timerVbox.setVisible(false);
Expand All @@ -163,7 +157,6 @@ public void initialize() {
}
});

//sets the tutorial controller to return to the game
tutorialController = new TutorialController();
tutorialController.setDestinationAppUI(AppUI.GAME);
tutorialController.setHasTutorialBeenViewed(true);
Expand Down Expand Up @@ -225,27 +218,23 @@ public void updateDisplay(int[][] board) {
@FXML
@Override
public void gameOver() throws IOException {
// Stop the game timer if it is running
if (gameTimer != null) {
gameTimer.stop();
}
int linesCleared = gameInstance.getGameBoard().getTotalLinesCleared();

try {
if (gameInstance.getIsMarathonMode()) {
// Save Marathon Mode Score
int targetLines = gameInstance.getTargetLines();
int timeTakenInSeconds = elapsedSeconds; // Assuming elapsedSeconds is tracking the timer
int timeTakenInSeconds = elapsedSeconds;

ScoreRecorder.saveMarathonScore(linesCleared, targetLines, timeTakenInSeconds);
} else {
// Save Basic Mode Score
int finalScore = Integer.parseInt(scoreLabel.getText());
ScoreRecorder.saveScore(String.valueOf(finalScore));

}
} catch (IOException e) {
// If there is an issue saving the score, throw an exception
}


Expand Down Expand Up @@ -280,7 +269,6 @@ public void onKeyPressed(KeyEvent event) {
if (action == Action.PAUSE) {
togglePauseScreen();
}
// otherwise passes the players input to the game instance
gameInstance.handleInput(event);
}

Expand Down Expand Up @@ -364,6 +352,7 @@ public void onClickMainMenu(ActionEvent event) throws IOException {
elapsedSeconds = 0;
updateTimerLabel();
}
Platform.runLater(() -> SoundManager.getInstance().playBackgroundMusic("mainmenu"));
}

/**
Expand Down Expand Up @@ -604,7 +593,6 @@ private void playGameOverAnimation() {
});
animationTimeline.getKeyFrames().add(actionsKeyFrame);

// Remove solid blocks
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
int delay = 2000 + (row * 50);
Expand Down Expand Up @@ -658,12 +646,10 @@ private void enableGameOverElements() {
int targetLines = gameInstance.getTargetLines();

if (linesCleared >= targetLines) {
// Player won Marathon Mode
gameOverLabel.setText("Victory!");
gameOverScoreLabel.setText("Score: " + linesCleared + "/" + targetLines);
gameOverHighScoreLabel.setVisible(false); // Hide high score for winning Marathon Mode
gameOverHighScoreLabel.setVisible(false);
} else {
// Player lost Marathon Mode
gameOverLabel.setText("Game Over");
gameOverScoreLabel.setText("Score: " + linesCleared + "/" + targetLines);
try {
Expand All @@ -674,7 +660,6 @@ private void enableGameOverElements() {
}
}
} else {
// Basic Mode
gameOverLabel.setText("Game Over");
gameOverScoreLabel.setText("Score: " + scoreLabel.getText());
try {
Expand Down
Loading

0 comments on commit 3da3571

Please sign in to comment.