Browse Source

Merge commit 'e861e03a1587583644dfee79ea0e2eddf3a56676' into HEAD

develop
Jenkins 3 years ago
parent
commit
9785162c83
  1. 19
      src/main/java/de/tims/fleetstorm/GameManager.java
  2. 114
      src/main/java/de/tims/leaderboard/Leaderboard.java
  3. 32
      src/main/java/de/tims/leaderboard/LeaderboardGUITest.java
  4. 21
      src/test/java/de/tims/fleetstorm/GameManagerTest.java
  5. 61
      src/test/java/de/tims/leaderboard/LeaderboardTest.java

19
src/main/java/de/tims/fleetstorm/GameManager.java

@ -0,0 +1,19 @@
package de.tims.fleetstorm;
public class GameManager {
private int gameState;
public static final int PREPARATION = 1;
public static final int RUNNING = 2;
public static final int OVER = 3;
public void start() {
this.gameState = GameManager.PREPARATION;
}
public int getGameState() {
return gameState;
}
}

114
src/main/java/de/tims/leaderboard/Leaderboard.java

@ -0,0 +1,114 @@
package de.tims.leaderboard;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;
import javax.swing.*;
import de.tims.player_management.Player;
public class Leaderboard {
private static final int NUM_PLAYERS_ON_LEADERBOARD = 10;
private List<Player> allPlayers;
String actualPlayer;
public Leaderboard(List<Player> allPlayers, String actualPlayer) {
this.allPlayers = allPlayers;
this.actualPlayer = actualPlayer;
}
public Player[] getTop10Players() {
Player[] top10Players = new Player[NUM_PLAYERS_ON_LEADERBOARD];
for (Player actualPlayer : allPlayers) {
for (int i = 0; i < NUM_PLAYERS_ON_LEADERBOARD; i++) {
if (top10Players[i] == null) {
top10Players[i] = actualPlayer;
break;
} else if (actualPlayer.getPoints() > top10Players[i].getPoints()) {
Player playerToInsert = actualPlayer;
Player tmp;
for (int j = i; j < NUM_PLAYERS_ON_LEADERBOARD; j++) {
tmp = top10Players[j];
top10Players[j] = playerToInsert;
playerToInsert = tmp;
}
break;
}
}
}
return top10Players;
}
public JPanel buildLeaderboard() {
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JLabel place;
JLabel name;
JLabel points;
Player[] top10Players = getTop10Players();
for (int i = -1; i < top10Players.length; i++) {
if (i < 0) {
place = new JLabel("Platzierung");
name = new JLabel("Name");
points = new JLabel("Punkte");
} else {
place = new JLabel((i + 1) + ".");
name = new JLabel(top10Players[i].getName());
points = new JLabel("" + top10Players[i].getPoints());
if (top10Players[i].getName().equals(actualPlayer)) {
Font actualPlayerFont = new Font(Font.DIALOG, Font.PLAIN, 20);
place.setForeground(Color.RED);
place.setFont(actualPlayerFont);
name.setForeground(Color.RED);
name.setFont(actualPlayerFont);
points.setForeground(Color.RED);
points.setFont(actualPlayerFont);
}
}
gbc.gridx = 0;
gbc.gridy = i + 1;
gbc.weightx = 0.5;
gbc.weighty = 0.1;
gbc.anchor = GridBagConstraints.LINE_END;
gbc.insets = new Insets(5, 0, 5, 5);
mainPanel.add(place, gbc);
gbc.gridx = 1;
gbc.gridy = i + 1;
gbc.weightx = 0.0;
gbc.weighty = 0.1;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.insets = new Insets(5, 5, 5, 10);
mainPanel.add(name, gbc);
gbc.gridx = 2;
gbc.gridy = i + 1;
gbc.weightx = 0.5;
gbc.weighty = 0.1;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.insets = new Insets(5, 10, 5, 0);
mainPanel.add(points, gbc);
}
return mainPanel;
}
}

32
src/main/java/de/tims/leaderboard/LeaderboardGUITest.java

@ -0,0 +1,32 @@
package de.tims.leaderboard;
import java.util.List;
import javax.swing.*;
import de.tims.player_management.Player;
public class LeaderboardGUITest {
private JFrame frame;
private JPanel panel;
Leaderboard leaderboard;
public LeaderboardGUITest() {
frame = new JFrame("LeaderboardGUITest");
leaderboard = new Leaderboard(List.of(new Player("Eins", 0), new Player("Zwei", 130), new Player("Drei", 20), new Player("Vier", 10),
new Player("Fuenf", 20), new Player("Sechs", 70), new Player("Sieben", 60), new Player("Acht", 20),
new Player("Neun", 60), new Player("Zehn", 50), new Player("Elf", 90), new Player("Zwoelf", 20),
new Player("Dreizehn", 40), new Player("Vierzehn", 80)), "Neun");
panel = leaderboard.buildLeaderboard();
frame.add(panel);
frame.setSize(640, 400);
frame.setVisible(true);
}
public static void main(String[] args) {
new LeaderboardGUITest();
}
}

21
src/test/java/de/tims/fleetstorm/GameManagerTest.java

@ -0,0 +1,21 @@
package de.tims.fleetstorm;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class GameManagerTest {
GameManager gameManager = new GameManager();
@Test
void testIfGameStateIsPreparationAfterStart() {
gameManager.start();
int expectedState = GameManager.PREPARATION;
int calculatedState = gameManager.getGameState();
assertEquals(expectedState, calculatedState);
}
}

61
src/test/java/de/tims/leaderboard/LeaderboardTest.java

@ -0,0 +1,61 @@
package de.tims.leaderboard;
import static org.assertj.core.api.Assertions.*;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import de.tims.player_management.Player;
class LeaderboardTest {
Leaderboard leaderboard;
@ParameterizedTest
@MethodSource("testCasesForGetTop10Players")
void getTop10PlayersTest(String testName, List<Player> allPlayers, String actualPlayer, Player[] expectedResult) {
leaderboard = new Leaderboard(allPlayers, actualPlayer);
Player[] calculatedResutl = leaderboard.getTop10Players();
assertThat(calculatedResutl).describedAs(testName).isEqualTo(expectedResult);
}
private static Stream<Arguments> testCasesForGetTop10Players() {
return Stream.of(Arguments.of("EmptyListReturnsEmptyArray", List.of(), "TestPlayer", new Player[] {null, null, null, null, null, null, null, null, null, null}),
Arguments.of("SortedListWithLessThan10ElementsReturnsListAsArray",
List.of(new Player("Eins", 80), new Player("Zwei", 70), new Player("Drei", 60), new Player("Vier", 50),
new Player("Fuenf", 40), new Player("Sechs", 30), new Player("Sieben", 20), new Player("Acht", 10)),
"TestPlayer",
new Player[] {new Player("Eins", 80), new Player("Zwei", 70), new Player("Drei", 60), new Player("Vier", 50),
new Player("Fuenf", 40), new Player("Sechs", 30), new Player("Sieben", 20), new Player("Acht", 10), null, null}),
Arguments.of("SortedListWithMoreThan10ElementsReturnsFirst10ElementsAsArray",
List.of(new Player("Eins", 140), new Player("Zwei", 130), new Player("Drei", 120), new Player("Vier", 110),
new Player("Fuenf", 100), new Player("Sechs", 90), new Player("Sieben", 80), new Player("Acht", 70),
new Player("Neun", 60), new Player("Zehn", 50), new Player("Elf", 40), new Player("Zwoelf", 30),
new Player("Dreizehn", 20), new Player("Vierzehn", 10)),
"TestPlayer",
new Player[] {new Player("Eins", 140), new Player("Zwei", 130), new Player("Drei", 120), new Player("Vier", 110),
new Player("Fuenf", 100), new Player("Sechs", 90), new Player("Sieben", 80), new Player("Acht", 70),
new Player("Neun", 60), new Player("Zehn", 50)}),
Arguments.of("UnsortedListWithLessThan10ElementsReturnsElementsSortedByPoints",
List.of(new Player("Eins", 20), new Player("Zwei", 30), new Player("Drei", 10), new Player("Vier", 60),
new Player("Fuenf", 50), new Player("Sechs", 90)),
"TestPlayer",
new Player[] {new Player("Sechs", 90), new Player("Vier", 60), new Player("Fuenf", 50), new Player("Zwei", 30),
new Player("Eins", 20), new Player("Drei", 10), null, null, null, null}),
Arguments.of("UnsortedListWithMoreThan10ElementsReturns10ElementsWithMostPointsSortedByPoints",
List.of(new Player("Eins", 0), new Player("Zwei", 130), new Player("Drei", 20), new Player("Vier", 10),
new Player("Fuenf", 20), new Player("Sechs", 70), new Player("Sieben", 60), new Player("Acht", 20),
new Player("Neun", 60), new Player("Zehn", 50), new Player("Elf", 90), new Player("Zwoelf", 20),
new Player("Dreizehn", 40), new Player("Vierzehn", 80)),
"TestPlayer",
new Player[] {new Player("Zwei", 130), new Player("Elf", 90), new Player("Vierzehn", 80), new Player("Sechs", 70),
new Player("Sieben", 60), new Player("Neun", 60), new Player("Zehn", 50), new Player("Dreizehn", 40),
new Player("Drei", 20), new Player("Fuenf", 20)}));
}
}
Loading…
Cancel
Save