diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e138c73 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +SnakeGame.iml +.idea/ +/out/ diff --git a/README.md b/README.md index 1d35272..f58e588 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # SnakeGame -This is a snake Game writing in Java using JetBrains IntelliJ Idea +This is a snake Game writing in Java using JetBrains IntelliJ Idea. + +Warning: history.txt is to record the score history of the game based on local computer. Delete this file will cause history lost. diff --git a/Snake/src/GamePanel.java b/Snake/src/GamePanel.java index 70b370a..4eda895 100644 --- a/Snake/src/GamePanel.java +++ b/Snake/src/GamePanel.java @@ -9,7 +9,7 @@ public class GamePanel extends JPanel implements ActionListener { static final int SCREEN_WIDTH = 600; static final int SCREEN_HEIGHT = 600; static final int UNIT_SIZE = 25; - static final int GAME_UNITS = (SCREEN_WIDTH * SCREEN_HEIGHT) / UNIT_SIZE; + static final int GAME_UNITS = (SCREEN_WIDTH * SCREEN_HEIGHT) / (UNIT_SIZE * UNIT_SIZE); static final int DELAY = 75; final int x[] = new int[GAME_UNITS]; final int y[] = new int[GAME_UNITS]; diff --git a/Snake/src/Ranking.java b/Snake/src/Ranking.java new file mode 100644 index 0000000..c3c683f --- /dev/null +++ b/Snake/src/Ranking.java @@ -0,0 +1,117 @@ +import java.io.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.concurrent.TimeUnit; + +/** + * @author SoongMingLiang (soongmingliang647@gmail.com) + * @version 1.0.0 + * + * This class is to provide the ranking system service to the SnakeGame. + * The methods that are available to other classes currently are + * getHistory(), insertScore(int new_score), and resetHistory() + * The main functionalities of these methods are return the game score history, insert a new score into the file, and + * reset the score history of the game respectively. + */ +public class Ranking { + private static final File historyScoreFile = new File ("history.txt"); + private static ArrayList scoreHistory = new ArrayList<>(); + private static ArrayList scoreHistory_write = new ArrayList<>(); + + private static void readHistory(){ + try(BufferedReader scoreReader = new BufferedReader(new FileReader(historyScoreFile))){ + String score = scoreReader.readLine(); + if(score == null){ + scoreHistory.add("0"); + } + else{ + while (score != null) { + scoreHistory.add(score); + score = scoreReader.readLine(); + } + } + } + catch(FileNotFoundException e){ + System.out.println("Caution!!!"); + System.out.println("File is not found! A new file will be generated for you."); + System.out.println("Note: All local history will be clear out!"); + try{ + if(historyScoreFile.createNewFile()){ + for(int i = 0; i < 3; i++){ + System.out.print("."); + TimeUnit.SECONDS.sleep(1); + } + System.out.println("\nYour history score file has been created successfully!"); + System.out.println("You are good to go now, kindly restart this program."); + } + } + catch(IOException | InterruptedException ioe){ + ioe.printStackTrace(); + } + } + catch (IOException ioe){ + ioe.printStackTrace(); + } + } + + private static void sortHistory(){ + Collections.fill(scoreHistory_write, 0); + for(var i : scoreHistory){ + scoreHistory_write.add(Integer.parseInt(i)); + } + Collections.sort(scoreHistory_write); + } + + public static ArrayList getHistory(){ + readHistory(); + return scoreHistory; + } + + public static void insertScore(int new_score){ + String newScore = String.valueOf(new_score); + scoreHistory.add(newScore); + sortHistory(); + try(FileWriter scoreWriter = new FileWriter(historyScoreFile, false)){ + for(var i : scoreHistory_write){ + if(i == 0){ + continue; + } + else{ + scoreWriter.write(i + "\n"); + } + } + } + catch (IOException e){ + e.printStackTrace(); + } + } + + public static void resetHistory(){ + try(FileWriter scoreWriter = new FileWriter(historyScoreFile, false)){ + scoreWriter.write(""); + } + catch (IOException e){ + e.printStackTrace(); + } + Collections.fill(scoreHistory, null); + Collections.fill(scoreHistory_write, 0); + } + + public static void main(String args[]){ + /*ArrayList history = getHistory(); + for(String s : history){ + System.out.println(s); + }*/ + + /*insertScore(10); + insertScore(300); + insertScore(100);*/ + + /*ArrayList history = getHistory(); + for(String s : history){ + System.out.println(s); + }*/ + + /*resetHistory();*/ + } +} \ No newline at end of file diff --git a/history.txt b/history.txt new file mode 100644 index 0000000..e69de29