Fixed score sorting, added a place column in ScoreDialog

This commit is contained in:
Kai S. K. Engelbart 2019-05-15 20:41:34 +02:00
parent 7400795737
commit 8eab89a212
3 changed files with 20 additions and 9 deletions

1
.gitignore vendored
View File

@ -54,3 +54,4 @@ local.properties
.scala_dependencies .scala_dependencies
.worksheet .worksheet
/scores.ser /scores.ser
/scores old.ser

View File

@ -32,15 +32,16 @@ public class ScoreDialog extends JDialog {
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
getContentPane().setLayout(new BorderLayout(0, 0)); getContentPane().setLayout(new BorderLayout(0, 0));
String[] columnNames = {"Name", "Game duration", "Board Config", "Date"}; String[] columnNames = { "Place", "Name", "Game duration", "Board Config", "Date" };
String[][] data = new String[scores.size()][4]; String[][] data = new String[scores.size()][5];
Iterator<Score> iter = scores.iterator(); Iterator<Score> iter = scores.iterator();
for(int i = 0; i < data.length; i++) { for(int i = 0; i < data.length; i++) {
Score s = iter.next(); Score s = iter.next();
data[i][0] = s.getName(); data[i][0] = String.valueOf(i + 1);
data[i][1] = String.valueOf(s.getDuration()); data[i][1] = s.getName();
data[i][2] = boardConfigName; data[i][2] = String.valueOf(s.getDuration());
data[i][3] = new SimpleDateFormat().format(s.getDate()); data[i][3] = boardConfigName;
data[i][4] = new SimpleDateFormat().format(s.getDate());
} }
mtable = new JTable(data, columnNames); mtable = new JTable(data, columnNames);

View File

@ -39,18 +39,27 @@ public class ScoreManager {
if (config == EASY && (easy.size() < 10 || easy.get(9).getDuration() > evt.getDuration())) { if (config == EASY && (easy.size() < 10 || easy.get(9).getDuration() > evt.getDuration())) {
String name = JOptionPane.showInputDialog("Please enter your name"); String name = JOptionPane.showInputDialog("Please enter your name");
Score score = new Score(name, evt.getDuration(), new Date()); 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())) { } else if (config == MEDIUM && (medium.size() < 10 || medium.get(9).getDuration() > evt.getDuration())) {
String name = JOptionPane.showInputDialog("Please enter your name"); String name = JOptionPane.showInputDialog("Please enter your name");
Score score = new Score(name, evt.getDuration(), new Date()); 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())) { } else if (config == HARD && (hard.size() < 10 || hard.get(9).getDuration() > evt.getDuration())) {
String name = JOptionPane.showInputDialog("Please enter your name"); String name = JOptionPane.showInputDialog("Please enter your name");
Score score = new Score(name, evt.getDuration(), new Date()); Score score = new Score(name, evt.getDuration(), new Date());
hard.add(score); sortInsert(score, hard);
} }
} }
private void sortInsert(Score score, List<Score> 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() { public void displayEasy() {
new ScoreDialog(easy, "Easy").setVisible(true); new ScoreDialog(easy, "Easy").setVisible(true);
} }