diff --git a/src/server/BaseGame.java b/src/server/BaseGame.java index 4e0c5b4..b0fe67e 100644 --- a/src/server/BaseGame.java +++ b/src/server/BaseGame.java @@ -33,8 +33,10 @@ public static final byte MOVE_PLACE_CARD = 2; public static final byte MOVE_TRASH_CARD = 3; public static final byte MOVE_HINT = 4; + public static final byte HINT_COLOR = 0; public static final byte HINT_VALUE = 1; + public static final byte CONNECTION_VALID = 8; public static final byte CONNECTION_INVALID = 9; @@ -49,14 +51,14 @@ final static int MAX_HINTS = 8; final static int MAX_FLASHS = 3; - int nrOfPlayers = 0; + int nPlayers = 0; static int CARDS_PER_PLAYER; - int cardsInDeck; + int nCardsInDeck; int hints; int flashs; - Card[][] deck; - int[] deckCounter; + Card[][] playingDeck; + int[] playingDeckCounter; List trash; BasePlayer[] players; int currentPlayer; @@ -73,27 +75,27 @@ } protected void init(){ - createDeck(); + createPlayingDeck(); createHistory(); createTrash(); - CARDS_PER_PLAYER = (nrOfPlayers <= 3) ? 5 : 4; + CARDS_PER_PLAYER = (nPlayers <= 3) ? 5 : 4; hints = MAX_HINTS; flashs = 0; - cardsInDeck = N_CARDS; + nCardsInDeck = N_CARDS; } protected abstract void createHistory(); protected abstract void createTrash(); - protected void createDeck(){ - deck = new Card[COLORS][5]; - deckCounter = new int[5]; - for (int i=0; i 0; } - protected void createPlayers() { - players = new BasePlayer[nrOfPlayers]; - for(int i=0; i=0 ? move : history.size()+move; return history.subList(0, index); } - + protected abstract void receive(DataInputStream inStream); public static void Log(String msg){ @@ -152,7 +153,7 @@ throw new RuntimeException(e); // TODO wer faengt das? } } - + @Override public void run() { while(keepRunning){ diff --git a/src/server/ClientGame.java b/src/server/ClientGame.java index 4e3bd48..be62df1 100644 --- a/src/server/ClientGame.java +++ b/src/server/ClientGame.java @@ -3,8 +3,8 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; -import java.net.MalformedURLException; import java.net.Socket; import java.net.UnknownHostException; import java.util.ArrayList; @@ -22,7 +22,7 @@ //Options stuff final File options = new File("options.json"); - JSONObject root; + JSONObject optionsRoot; int difficulty; int fontSize; @@ -35,7 +35,7 @@ public ClientGame(){ preinit(); } - + protected void preinit(){ port = 1337; hostName = "localhost"; @@ -70,13 +70,14 @@ Log("Client " + ownId + " waiting for game..."); } } - + @Override protected void init() { loadOptions(); loadFont(); super.init(); } + @Override protected void createHistory() { history = new ArrayList<>(); @@ -89,16 +90,14 @@ protected void loadOptions(){ try { - JSONTokener tokener = new JSONTokener(options.toURI().toURL().openStream()); - root = new JSONObject(tokener); - difficulty = root.getInt("difficulty"); - fontSize = root.getInt("fontSize"); + JSONTokener tokener = new JSONTokener(new FileInputStream(options)); + optionsRoot = new JSONObject(tokener); + difficulty = optionsRoot.getInt("difficulty"); + fontSize = optionsRoot.getInt("fontSize"); if(difficulty < 0) difficulty = 0; if(fontSize <= 0) fontSize = 20; } catch (JSONException e) { e.printStackTrace(); - } catch (MalformedURLException e) { - e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } @@ -107,7 +106,7 @@ protected void movePlaceCard(Card c){ movePlaceCard(c, false); } - + protected void movePlaceCard(Card c, boolean isDry) { byte msgType = isDry ? MOVE_PLACE_CARD | MOVE_DRY : MOVE_PLACE_CARD; send(msgType, c.toJson().toString()); @@ -136,7 +135,7 @@ msg.put("type", HINT_COLOR); } msg.put("value", value); - msg.put("player_id", playerId); + msg.put("playerId", playerId); send(msgType, msg.toString()); } @@ -174,19 +173,19 @@ //public abstract void printMessage(String msg, MSG_TYPES type); - protected abstract void createUI(); - protected abstract void updateUI(); + protected abstract void createUi(); + protected abstract void updateUi(); public int getDifficulty(){ return difficulty; } - + public abstract void loadFont(); public int getFontSize(){ return fontSize; } - + public Object getFont() { // TODO return null; @@ -208,18 +207,18 @@ // TODO Auto-generated catch block e.printStackTrace(); } - + Log("Received magic: " + magic[0] + magic[1] + magic[2] + magic[3]); Log("Received proto: " + proto); Log("Received msgType: " + msgType); Log("Received useless: " + useless[0] + useless[1]); - + // TODO check Protocol // switch(proto){ // case VIBE_DUMB: // break; // } - + String msg = null; JSONObject jo = null; try { @@ -246,33 +245,33 @@ Log("first update!"); init(); updateGame(jo); - createUI(); + createUi(); initGameReceived = true; break; } updateGame(jo); - updateUI(); + updateUi(); break; } } protected int getPlayersSightId(int id){ // TODO get the id of a player with ownId as 0-based index - return (id + (nrOfPlayers - ownId)) % nrOfPlayers; + return (id + (nPlayers - ownId)) % nPlayers; } protected int getRealId(int id){ // TODO get the real id of a player - return (id + ownId) % nrOfPlayers; + return (id + ownId) % nPlayers; } protected void updateGame(JSONObject jo){ - nrOfPlayers = jo.getInt("nrOfPlayers"); + nPlayers = jo.getInt("nPlayers"); if(!initGameReceived){ createPlayers(); } // TODO movesLeft = jo.getInt("movesLeft"); - cardsInDeck = jo.getInt("cardsInDeck"); + nCardsInDeck = jo.getInt("nCardsInDeck"); hints = jo.getInt("hints"); flashs = jo.getInt("flashs"); JSONArray trashArray = jo.getJSONArray("trash"); @@ -283,17 +282,17 @@ trash.add(card); } - JSONObject deckRow = jo.getJSONObject("deck"); - for(int i=0; i cards; + List cardsInDeck; long seed; Random rand; @@ -54,14 +54,14 @@ sockets = new ArrayList<>(); try { server = new ServerSocket(port); - while(nrOfPlayers < 2){ //TODO + while(nPlayers < 2){ //TODO Socket socket = server.accept(); sockets.add(socket); JSONObject connectionJson = new JSONObject(); - connectionJson.put("id", nrOfPlayers); + connectionJson.put("id", nPlayers); sendToClient(CONNECTION_VALID, connectionJson.toString(), socket); - nrOfPlayers++; - Log(nrOfPlayers + " players connected!"); + nPlayers++; + Log(nPlayers + " players connected!"); // TODO do this somewhere else Log("Creating new Socket Listener for #" + (sockets.size()-1)); Log("Socket id: " + socket); @@ -90,7 +90,7 @@ movesLeft = -1; createPlayers(); currentPlayer = 0; - createDeck(); + createPlayingDeck(); createTrash(); // shuffleCards(); // TODO use this instead of addCards() addCards(); @@ -98,7 +98,7 @@ } protected void shuffleCards() { - cards = new ArrayList<>(); + cardsInDeck = new ArrayList<>(); //TODO add cards //TODO shuffle } @@ -218,7 +218,7 @@ case MOVE_HINT: int hintType = jo.getInt("type"); int value = jo.getInt("value"); - int player = jo.getInt("player_id"); + int player = jo.getInt("playerId"); isValid = giveHint(hintType, value, player); wasMove = true; break; @@ -232,7 +232,7 @@ if(wasMove){ Log("was VALID move"); currentPlayer++; - if(currentPlayer == nrOfPlayers) currentPlayer = 0; + if(currentPlayer == nPlayers) currentPlayer = 0; // TODO notify players if(movesLeft >= 0) movesLeft--; String moveMsg = toJson().toString(); @@ -263,10 +263,10 @@ int value = card.getValue(); - int deckValue = deckCounter[color]; + int deckValue = playingDeckCounter[color]; if(deckValue == value){ //place card possible - deckCounter[color]++; - deck[color][value] = card; + playingDeckCounter[color]++; + playingDeck[color][value] = card; if (value + 1 == 5) addHint(); } else { //card too high or too low addFlash(); @@ -308,17 +308,17 @@ } protected void createPlayers() { - players = new BasePlayer[nrOfPlayers]; - for(int i=0; i(); - int[] dist = new int[] { 3, 2, 2, 2, 1 }; - int[][] tmp_cards = new int[][] { dist.clone(), dist.clone(), dist.clone(), dist.clone(), dist.clone() }; + cardsInDeck = new ArrayList<>(); + int[] dist = new int[]{ 3, 2, 2, 2, 1 }; + int[][] tmp_cards = new int[][]{ dist.clone(), dist.clone(), dist.clone(), dist.clone(), dist.clone() }; for (int i = 0; i < N_CARDS; ++i) { int[] p = new int[2]; do { p = randomPosition(); } while (tmp_cards[p[0]][p[1]] == 0); tmp_cards[p[0]][p[1]]--; - cards.add(new Card(p[0], p[1], i)); + cardsInDeck.add(new Card(p[0], p[1], i)); } } @@ -353,25 +353,25 @@ return rand.nextInt(max); } - private void dealCard(int cardPos, int playerID) { + private void dealCard(int cardPos, int playerId) { Card card; - if (cardsInDeck > 0) { - int index = random(cardsInDeck--); - card = cards.remove(index); + if (nCardsInDeck > 0) { + int index = random(nCardsInDeck--); + card = cardsInDeck.remove(index); // TODO Use this code as soon as shuffle is implemented // card = getCardFromDeck(); } else { - if (movesLeft == -1) movesLeft = nrOfPlayers; + if (movesLeft == -1) movesLeft = nPlayers; card = Card.DUMMY_CARD; } - players[playerID].setCard(cardPos, card); + players[playerId].setCard(cardPos, card); } void setWonOrLost() { boolean won = true; boolean lost = false; for (int i = 0; i < COLORS; ++i) { - if (deckCounter[i] != 5) { + if (playingDeckCounter[i] != 5) { won = false; break; } @@ -457,7 +457,7 @@ protected JSONObject toJson(){ JSONObject root = new JSONObject(); - root.put("cardsInDeck", cardsInDeck); + root.put("nCardsInDeck", nCardsInDeck); root.put("hints", hints); root.put("flashs", flashs); root.put("movesLeft", movesLeft); @@ -468,21 +468,21 @@ root.put("trash", trashArray); JSONObject deckRow = new JSONObject(); - for(int i=0; i