diff --git a/src/server/BaseGame.java b/src/server/BaseGame.java new file mode 100644 index 0000000..7d1b885 --- /dev/null +++ b/src/server/BaseGame.java @@ -0,0 +1,416 @@ +package server; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.List; + +import org.json.*; + +public abstract class BaseGame { + /* + * COLORS: + * GREEN + * RED + * YELLOW + * WHITE + * BLUE + * (COLORED) + */ + + public enum MSG_TYPES {ERROR, WARNING, INFORMATION, QUESTION}; + + final static int GREEN = 0; + final static int RED = 1; + final static int YELLOW = 2; + final static int WHITE = 3; + final static int BLUE = 4; + //final static int COLORED = 5; + final static int COLORS = 5; + final static int N_CARDS = 50; + final static int MAX_HINTS = 8; + final static int MAX_FLASHS = 3; + + int N_PLAYERS = 3; + static int CARDS_PER_PLAYER; + int CARDS_IN_DECK; + + int hints; + int flashs; + Card[][] deck; + int[] deckCounter; + ArrayList trash; + AbstractPlayer[] players; + int currentPlayer; + boolean won; + boolean lost; + + int port; + + List history; + + public BaseGame(){ + init(); + } + + protected void init(){ + history = new ArrayList<>(); + loadOptions(); + loadFont(); + //establishConnection(); + CARDS_PER_PLAYER = (N_PLAYERS <= 3) ? 5 : 4; + hints = MAX_HINTS; + flashs = 0; + roundsLeft = -1; + createPlayers(); + currentPlayer = 0; + resetMoveValues(); + addCards(); + CARDS_IN_DECK = cards.size(); + createUI(); + dealCards(); + } + + protected void createPlayers(){ + players = new AbstractPlayer[N_PLAYERS]; + } + + private void addCards(){ + cards = new ArrayList<>(); + deck = new Card[COLORS][5]; + deckCounter = new int[5]; + for(int i=0; i<5; ++i){ + deckCounter[i] = 0; + } + trash = 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 0){ + int index = random(CARDS_IN_DECK--); + card = cards.remove(index); + } else { + if(roundsLeft == -1) roundsLeft = N_PLAYERS; + card = new Card(3, -1, playerID, this); + } + players[playerID].setCard(cardID, card); + } + + protected void dealCards(){ + int dealtCards = 0; + for(; dealtCards < CARDS_PER_PLAYER; ++dealtCards){ + for(int i=0; i 0; + } + + void setWonOrLost(){ + boolean won = true; + boolean lost = false; + for(int i=0; i MAX_HINTS) hints = MAX_HINTS; + } + } + + protected void addThunder(){ + thunders++; + } + + protected void placeCard(Card c, int color, int value){ + deckCounter[color]++; + deck[color][value] = c; + if(value+1 == 5) addHint(); + } + + protected void onNext(){ + if(!checkValidMove()){ + printMessage("Please select all cards of the selected color/value", MSG_TYPES.ERROR); + return; + } + if(roundsLeft >= 0) roundsLeft--; + int selCardIndex = 0; + AbstractPlayer player = players[selectedPlayer]; + for(int i=0; i 0){ + printMessage("Please deselect all cards first", MSG_TYPES.ERROR); + return; + } + useColor = true; + useValue = false; + placeCard = false; + trashCard = false; + } + + protected void onValue(){ + if(!hintAvailable()){ + printMessage("No hints left!", MSG_TYPES.ERROR); + return; + } + if(selectedCards > 0){ + printMessage("Please deselect all cards first", MSG_TYPES.ERROR); + return; + } + useValue = true; + useColor = false; + placeCard = false; + trashCard = false; + } + + protected void onTrash(){ + if(selectedCards > 0){ + printMessage("Please deselect all cards first", MSG_TYPES.ERROR); + return; + } + useColor = false; + useValue = false; + placeCard = false; + trashCard = true; + } + + protected void onPlace(){ + if(selectedCards > 0){ + printMessage("Please deselect all cards first", MSG_TYPES.ERROR); + return; + } + useColor = false; + useValue = false; + placeCard = true; + trashCard = false; + } + + protected abstract void showTrash(); + + /** + * Stores the current state of the game in the history + * @param json the current state as json object + */ + protected void addToHistory(){ + String json = ""; + history.add(json); + } + + /** + * Returns the state of the game at a given @param move index + * @param move the index of the move you want to get. + * Positive values are interpreted as index from the start (0 is the initial game). + * Negative values are interpreted as index from the end (-1 is the last state). + */ + public List getHistory(int move){ + final int index = move >= 0 ? move : history.size()-1; + return history.subList(0, index); + } +} \ No newline at end of file diff --git a/src/server/ClientGame.java b/src/server/ClientGame.java new file mode 100644 index 0000000..19db676 --- /dev/null +++ b/src/server/ClientGame.java @@ -0,0 +1,107 @@ +package server; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.Socket; + +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONTokener; + +public class ClientGame extends BaseGame { + Socket socket; + + int selectedPlayer; + int selectedCards; + int chosenColor; + int chosenValue; + boolean useColor; + boolean useValue; + boolean placeCard; + boolean trashCard; + + //Options stuff + final File options = new File("options.json"); + JSONObject root; + int difficulty; + int fontSize; + + protected void loadOptions(){ + try { + JSONTokener tokener = new JSONTokener(options.toURI().toURL().openStream()); + root = new JSONObject(tokener); + difficulty = root.getInt("difficulty"); + fontSize = root.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(); + } + } + + public void resetMoveValues(){ + selectedPlayer = -1; + chosenColor = -1; + chosenValue = -1; + selectedCards = 0; + useColor = useValue = trashCard = placeCard = false; + } + + boolean checkValidMove(){ + if(selectedCards == 0) return false; + if(!hintAvailable() && (useColor || useValue)) return false; + if((trashCard || placeCard) && selectedCards == 1) return true; + int countCards = 0; + for(int i=0; i cards; + int roundsLeft; + + @Override + protected void createUI() { + // TODO Auto-generated method stub + + } + + @Override + protected void setNoCard(int index) { + // TODO Auto-generated method stub + + } + + @Override + public void loadFont() { + // TODO Auto-generated method stub + + } + + @Override + public Object getFont() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void printMessage(String msg, MSG_TYPES type) { + // TODO Auto-generated method stub + + } + + @Override + protected void showTrash() { + // TODO Auto-generated method stub + + } + +}