From 8eab89a2121aaa59dade6c72cacf1e12ba0fff6f Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 15 May 2019 20:41:34 +0200 Subject: [PATCH] Fixed score sorting, added a place column in ScoreDialog --- .gitignore | 1 + src/dev/kske/minesweeper/ScoreDialog.java | 13 +++++++------ src/dev/kske/minesweeper/ScoreManager.java | 15 ++++++++++++--- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 1f514cf..48dd617 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,4 @@ local.properties .scala_dependencies .worksheet /scores.ser +/scores old.ser diff --git a/src/dev/kske/minesweeper/ScoreDialog.java b/src/dev/kske/minesweeper/ScoreDialog.java index ea685ea..be8e53d 100644 --- a/src/dev/kske/minesweeper/ScoreDialog.java +++ b/src/dev/kske/minesweeper/ScoreDialog.java @@ -32,15 +32,16 @@ public class ScoreDialog extends JDialog { setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); getContentPane().setLayout(new BorderLayout(0, 0)); - String[] columnNames = {"Name", "Game duration", "Board Config", "Date"}; - String[][] data = new String[scores.size()][4]; + String[] columnNames = { "Place", "Name", "Game duration", "Board Config", "Date" }; + String[][] data = new String[scores.size()][5]; Iterator iter = scores.iterator(); for(int i = 0; i < data.length; i++) { Score s = iter.next(); - data[i][0] = s.getName(); - data[i][1] = String.valueOf(s.getDuration()); - data[i][2] = boardConfigName; - data[i][3] = new SimpleDateFormat().format(s.getDate()); + data[i][0] = String.valueOf(i + 1); + data[i][1] = s.getName(); + data[i][2] = String.valueOf(s.getDuration()); + data[i][3] = boardConfigName; + data[i][4] = new SimpleDateFormat().format(s.getDate()); } mtable = new JTable(data, columnNames); diff --git a/src/dev/kske/minesweeper/ScoreManager.java b/src/dev/kske/minesweeper/ScoreManager.java index 60d2879..3f70de3 100644 --- a/src/dev/kske/minesweeper/ScoreManager.java +++ b/src/dev/kske/minesweeper/ScoreManager.java @@ -39,18 +39,27 @@ public class ScoreManager { if (config == EASY && (easy.size() < 10 || easy.get(9).getDuration() > evt.getDuration())) { String name = JOptionPane.showInputDialog("Please enter your name"); Score score = new Score(name, evt.getDuration(), new Date()); - easy.add(score); + sortInsert(score, easy); } else if (config == MEDIUM && (medium.size() < 10 || medium.get(9).getDuration() > evt.getDuration())) { String name = JOptionPane.showInputDialog("Please enter your name"); Score score = new Score(name, evt.getDuration(), new Date()); - medium.add(score); + sortInsert(score, medium); } else if (config == HARD && (hard.size() < 10 || hard.get(9).getDuration() > evt.getDuration())) { String name = JOptionPane.showInputDialog("Please enter your name"); Score score = new Score(name, evt.getDuration(), new Date()); - hard.add(score); + sortInsert(score, hard); } } + private void sortInsert(Score score, List list) { + for (int i = 0; i < list.size(); i++) + if (list.get(i).getDuration() > score.getDuration()) { + list.add(i, score); + return; + } + list.add(score); + } + public void displayEasy() { new ScoreDialog(easy, "Easy").setVisible(true); }