Kniffel/fassade/KniffelSystem.java

179 lines
4.8 KiB
Java
Raw Normal View History

package fassade;
import domain.Game;
import domain.Player;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class KniffelSystem {
ArrayList<String> playerColors;
Game game;
public KniffelSystem(){
game = new Game();
playerColors = new ArrayList<>(Arrays.asList(
"\u001B[31m", // Quelle 2 Anfang
"\u001B[32m", //
"\u001B[34m", //
"\u001B[33m", //
"\u001B[36m")); // Quelle 2 Ende
}
public String LeaderBaordData(){
return "Leaderboard - TODO \n";
}
public String addPlayer(int playerNumber, String name) {
String playerColor = colorPicker(playerNumber);
Player playerToAdd = new Player(playerNumber, name, playerColor, 0);
game.addPlayer(playerToAdd);
return changePlayerNameColor(name, playerColor);
}
private String changePlayerNameColor(String name, String color){
String ANSI_RESET = "\u001B[0m";
return String.format(color + name + ANSI_RESET);
}
private String colorPicker(int playerNumber){
if (playerNumber == 1){
return "\u001B[35m"; // Quelle 2
}
Random rand = new Random(); // Quelle 1 Anfang
int randomIndex = rand.nextInt(playerColors.size()); //
return playerColors.remove(randomIndex); // Quelle 1 Ende
}
public String getCurrentPlayer(){
return game.getCurrentPlayer().toString();
}
public void nextPlayer(){
game.nextTurn();
}
// TEST
public void createTestPlayers(int amountPlayer){
String[] names = {"Vic", "Nastja", "Lilli", "Emelie", "Esra", "Oli"};
for (int i = 0; i < amountPlayer; i++){
addPlayer(i+1, names[i]);
}
}
public ArrayList<Integer> rollDices(ArrayList<Integer> rolls, String keptDice){
//? DEV TEST
if (keptDice.startsWith("dev")){
return createDevRoll(keptDice);
}
ArrayList<Integer> oldRolls = extractKeptDice(rolls, keptDice);
int amountNewRolls = oldRolls.size();
ArrayList<Integer> newRolls = rollMultipleDice(5 - amountNewRolls);
// --TEST
System.out.println("Old rolls:");
for(int oldRoll: oldRolls){
System.out.println(oldRoll);
}
System.out.println("New Rolls:");
for(int newRoll: newRolls){
System.out.println(newRoll);
}
System.out.println();
// TEST--
oldRolls.addAll(newRolls);
return oldRolls;
}
private ArrayList<Integer> extractKeptDice(ArrayList<Integer> previousRolls, String keptDice){
ArrayList<Integer> keptRolls = new ArrayList<Integer>();
if (keptDice.isEmpty()){
return keptRolls;
}
//? Remove whitespaces
keptDice = keptDice.replaceAll("\\s+","");
if (keptDice.length() == 1){
int singleIndex = Integer.parseInt(keptDice);
keptRolls.add(previousRolls.get(singleIndex - 1));
return keptRolls;
}
keptDice = keptDice.substring(1, keptDice.length()-1);
System.out.printf("Edited keptDice String: %s \n", keptDice); //! TEST
String[] keptDiceIndicesStrings = keptDice.split(",");
for (String keptDiceIndicesString : keptDiceIndicesStrings) {
int index = Integer.parseInt(keptDiceIndicesString);
keptRolls.add(previousRolls.get(index - 1));
}
return keptRolls;
}
private ArrayList<Integer> rollMultipleDice(int amountRolls){
System.out.printf("Amount rolls: %d \n", amountRolls); //! TEST
ArrayList<Integer> rolls = new ArrayList<>();
if (amountRolls == 0){
return rolls;
}
for (int i = 0; i < amountRolls; i++){
rolls.add(game.rollDice());
}
return rolls;
}
private ArrayList<Integer> createDevRoll(String keptDice){
// Format: dev(1,2,3,4,5)
// values aren't indices, they are the dice value
ArrayList<Integer> devRoll = new ArrayList<>();
keptDice = keptDice.replaceAll("\\s+","");
keptDice = keptDice.substring(4, keptDice.length()-1);
String[] rollStrings = keptDice.split(",");
for (String rollString : rollStrings){
devRoll.add(Integer.parseInt(rollString));
}
return devRoll;
}
//! TEST
public String[] getAllPlayerStrings(){
ArrayList<Player> players = game.getPlayers();
String[] returnStrings = new String[players.size()];
for (int i = 0; i < players.size(); i++){
returnStrings[i] = players.get(i).toString();
}
return returnStrings;
}
}