This repository was archived by the owner on Dec 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScoreBoard.java
More file actions
51 lines (40 loc) · 1.39 KB
/
ScoreBoard.java
File metadata and controls
51 lines (40 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.ematrix;
public class ScoreBoard {
private int scoreEntries = 0;
private GameEntry[] board;
public ScoreBoard(int boardSize){
board=new GameEntry[boardSize];
}
public GameEntry[] getBoard() {
return board;
}
public void addScoreToBoard(GameEntry newScoreEntry) {
int newScore = newScoreEntry.getPlayerScore( );
if (scoreEntries < board.length || newScore > board[scoreEntries-1].getPlayerScore( )) {
if (scoreEntries < board.length) // no score drops from the board
scoreEntries++;
int j = scoreEntries - 1;
while (j > 0 && this.board[j - 1].getPlayerScore() < newScore) {
board[j] = this.board[j - 1]; // shift entry from j-1 to j
j--; // and decrement j
}
board[j] = newScoreEntry;
}
}
public GameEntry removeScoreToBoard(int scoreIndex) throws IndexOutOfBoundsException {
if(scoreIndex>this.scoreEntries-1){
throw new IndexOutOfBoundsException("Invalid index: " + scoreIndex);
}
int j=scoreIndex;
GameEntry temp=this.board[j];
for(;j<scoreEntries-1;j++){
this.board[j] = this.board[j + 1];
}
this.board[j]=null;
scoreEntries--;
return temp;
}
public int getScoreEntries() {
return scoreEntries;
}
}