diff --git a/src/card/Card.java b/src/card/Card.java index 10cd0ee..f35f9bc 100644 --- a/src/card/Card.java +++ b/src/card/Card.java @@ -1,5 +1,6 @@ package card; -import game.AbstractGame; +import game.BaseGame; +import game.BaseGame.MSG_TYPES; import java.awt.Color; @@ -16,23 +17,81 @@ protected int color; protected int value; protected int playerId; - protected AbstractGame g; + protected boolean isSelected; + protected BaseGame g; - public Card(int color, int value, int playerId, AbstractGame g){ + public Card(int color, int value, int playerId, BaseGame g){ this.color = color; this.value = value; this.playerId = playerId; this.g = g; + isSelected = false; } public Card clone(){ - Card c = null; - try { - c = (Card) super.clone(); - } catch (CloneNotSupportedException e) { - e.printStackTrace(); + Card card = new Card(color, value, playerId, g); + return card; + } + + protected void onCardClick(){ + if(isSelected){ + g.removeSelectedCards(); + isSelected = false; + if(g.getSelectedCards() == 0){ + g.resetMoveValues(); + } } - return c; + if(!g.isUseColor() && !g.isUseValue() && !g.isTrashCard() && !g.isPlaceCard()){ + g.printMessage("Please choose move type first", MSG_TYPES.ERROR); + return; + } + if(playerId == g.getCurrentPlayerIndex()){ + if(g.isUseColor() || g.isUseValue()){ + g.printMessage("Please don't select your own cards!", MSG_TYPES.ERROR); + return; + } else if((g.isTrashCard() || g.isPlaceCard()) && g.getSelectedCards() == 1){ + g.printMessage("You already selected a card!", MSG_TYPES.ERROR); + return; + } + } else if(playerId != g.getCurrentPlayerIndex() && (g.isTrashCard() || g.isPlaceCard())){ + g.printMessage("Please select your own cards", MSG_TYPES.ERROR); + return; + } + if(g.getSelectedPlayer() == -1){ + g.setSelectedPlayer(playerId); + g.setChosenColor(color); + g.setChosenValue(value); + g.addSelectedCards(); + isSelected = true; + return; + } + if(g.getSelectedPlayer() != playerId){ + g.printMessage("Please choose a card of the selected player or unselect all cards and choose another player.", MSG_TYPES.ERROR); + return; + } + if(g.getChosenColor() == -1){ + g.setChosenColor(color); + } + if(g.getChosenValue() == -1){ + g.setChosenValue(value); + } + if(!isSelected){ + if(g.isUseColor()){ + if(g.getChosenColor() == color){ + if(!isSelected){ + g.addSelectedCards(); + isSelected = true; + } + } + } else if(g.isUseValue()){ + if(g.getChosenValue() == value){ + if(!isSelected){ + g.addSelectedCards(); + isSelected = true; + } + } + } + } } @Override @@ -84,6 +143,14 @@ return color; } + public boolean isSelected(){ + return isSelected; + } + + public void setSelected(boolean set){ + isSelected = set; + } + public int getColorInt(){ return color; } diff --git a/src/card/SwingCard.java b/src/card/SwingCard.java index 1079316..379224b 100644 --- a/src/card/SwingCard.java +++ b/src/card/SwingCard.java @@ -1,7 +1,6 @@ package card; import game.SwingGame; -import game.AbstractGame.MSG_TYPES; import java.awt.Color; import java.awt.Font; @@ -33,67 +32,7 @@ face.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { - if(face.getBackground() == Color.pink){ - g.removeSelectedCards(); - face.setBackground(Color.black); - //System.out.println("Removed card #" + clickedCard + "(" + card.getColorInt() + ", " + card.getValue() + ")"); - if(g.getSelectedCards() == 0){ - g.resetMoveValues(); - } - } - if(!g.isUseColor() && !g.isUseValue() && !g.isTrashCard() && !g.isPlaceCard()){ - g.printMessage("Please choose move type first", MSG_TYPES.ERROR); - return; - } - if(playerId == g.getCurrentPlayerIndex()){ - if(g.isUseColor() || g.isUseValue()){ - g.printMessage("Please don't select your own cards!", MSG_TYPES.ERROR); - return; - } else if((g.isTrashCard() || g.isPlaceCard()) && g.getSelectedCards() == 1){ - g.printMessage("You already selected a card!", MSG_TYPES.ERROR); - return; - } - } else if(playerId != g.getCurrentPlayerIndex() && (g.isTrashCard() || g.isPlaceCard())){ - g.printMessage("Please select your own cards", MSG_TYPES.ERROR); - return; - } - if(g.getSelectedPlayer() == -1){ - g.setSelectedPlayer(playerId); - g.setChosenColor(color); - g.setChosenValue(value); - g.addSelectedCards(); - face.setBackground(Color.pink); - return; - } - if(g.getSelectedPlayer() != playerId){ - g.printMessage("Please choose a card of the selected player or unselect all cards and choose another player.", MSG_TYPES.ERROR); - return; - } - if(g.getChosenColor() == -1){ - g.setChosenColor(color); - } - if(g.getChosenValue() == -1){ - g.setChosenValue(value); - } - if(face.getBackground() == Color.black){ - if(g.isUseColor()){ - if(g.getChosenColor() == color){ - if(face.getBackground() != Color.pink){ - g.addSelectedCards(); - face.setBackground(Color.pink); - //System.out.println("Added card #" + clickedCard + "(" + intColorToText(card.getColorInt()) + ", " + card.getValue() + ")"); - } - } - } else if(g.isUseValue()){ - if(g.getChosenValue() == value){ - if(face.getBackground() != Color.pink){ - g.addSelectedCards(); - face.setBackground(Color.pink); - //System.out.println("Added card #" + clickedCard + "(" + intColorToText(card.getColorInt()) + ", " + card.getValue() + ")"); - } - } - } - } + onCardClick(); } @Override @@ -113,4 +52,21 @@ } }); } + + @Override + protected void onCardClick(){ + super.onCardClick(); + if(isSelected){ + face.setBackground(Color.pink); + } else { + face.setBackground(Color.black); + } + } + + @Override + public void setSelected(boolean set){ + super.setSelected(set); + if(isSelected) face.setBackground(Color.pink); + else face.setBackground(Color.black); + } } diff --git a/src/game/AbstractGame.java b/src/game/AbstractGame.java deleted file mode 100644 index f381a7b..0000000 --- a/src/game/AbstractGame.java +++ /dev/null @@ -1,392 +0,0 @@ -package game; -import player.AbstractPlayer; - -import java.awt.Point; -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 org.json.*; - -import card.Card; - -public abstract class AbstractGame { - /* - * 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; - int N_PLAYERS = 2; - static int CARDS_PER_PLAYER; - final static int N_CARDS = 50; - int CARDS_IN_DECK; - final static int MAX_HINTS = 8; - final static int MAX_THUNDERS = 3; - int hints; - int thunders; - ArrayList cards; - Card[][] deck; - int[] deckCounter; - ArrayList trash; - AbstractPlayer[] players; - int currentPlayer; - int roundsLeft; - - int selectedPlayer; - int selectedCards; - int chosenColor; - int chosenValue; - boolean useColor; - boolean useValue; - boolean placeCard; - boolean trashCard; - boolean won; - boolean lost; - - //Options stuff - final File options = new File("options.json"); - JSONObject root; - int difficulty; - int fontSize; - - //Network stuff - ServerSocket server; - Socket s; - boolean isHost; - - /*public static void main(String[] args) { - AbstractGame game = new AbstractGame(); - game.getPlayer(game.currentPlayer).deactiveCards(); - System.out.println("Player " + game.currentPlayer + "'s turn"); - }*/ - - public AbstractGame(){ - init(); - } - - protected void init(){ - loadOptions(); - loadFont(); - //establishConnection(); - CARDS_PER_PLAYER = (N_PLAYERS < 3) ? 5 : 4; - hints = MAX_HINTS; - thunders = 0; - roundsLeft = -1; - createPlayers(); - currentPlayer = 0; - resetMoveValues(); - addCards(); - CARDS_IN_DECK = cards.size(); - createUI(); - dealCards(); - } - - protected void loadOptions(){ - try { - JSONTokener tokener = new JSONTokener(options.toURI().toURL().openStream()); - root = new JSONObject(tokener); - difficulty = root.getInt("difficulty"); - fontSize = root.getInt("fontSize"); - isHost = root.getBoolean("host"); - if(difficulty < 0) difficulty = 0; - if(fontSize <= 0) fontSize = 20; - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (MalformedURLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - protected void establishConnection(){ - try { - if(isHost){ - server = new ServerSocket(8765); - printMessage("Your server is running on port 8765", MSG_TYPES.INFORMATION); - s = server.accept(); - DataInputStream inStream = new DataInputStream(s.getInputStream()); - String str = inStream.readUTF(); - System.out.println("Message: " + str); - s.close(); - server.close(); - } else { - s = new Socket("localhost", 8765); - DataOutputStream outStream = new DataOutputStream(s.getOutputStream()); - outStream.writeUTF("Hello"); - outStream.flush(); - outStream.close(); - s.close(); - } - } catch(UnknownHostException uhe){ - printMessage("Unknown host '???'", MSG_TYPES.ERROR); - } catch (IOException e) { - printMessage(e.getLocalizedMessage(), MSG_TYPES.ERROR); - System.exit(0); //TODO status code - } - - } - - public void resetMoveValues(){ - selectedPlayer = -1; - chosenColor = -1; - chosenValue = -1; - selectedCards = 0; - useColor = useValue = trashCard = placeCard = false; - } - - protected void createPlayers(){ - players = new AbstractPlayer[N_PLAYERS]; - /*for(int i=0; i(); - deck = new Card[COLORS][5]; - deckCounter = new int[5]; - 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(){ - won = true; - lost = false; - for(int i=0; i 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(); - - /*private void printArray(int[][] arr){ - for(int j=0; j cards; + Card[][] deck; + int[] deckCounter; + ArrayList trash; + AbstractPlayer[] players; + int currentPlayer; + int roundsLeft; + + int selectedPlayer; + int selectedCards; + int chosenColor; + int chosenValue; + boolean useColor; + boolean useValue; + boolean placeCard; + boolean trashCard; + boolean won; + boolean lost; + + //Options stuff + final File options = new File("options.json"); + JSONObject root; + int difficulty; + int fontSize; + + //Network stuff + ServerSocket server; + Socket s; + boolean isHost; + + /*public static void main(String[] args) { + AbstractGame game = new AbstractGame(); + game.getPlayer(game.currentPlayer).deactiveCards(); + System.out.println("Player " + game.currentPlayer + "'s turn"); + }*/ + + public BaseGame(){ + init(); + } + + protected void init(){ + loadOptions(); + loadFont(); + //establishConnection(); + CARDS_PER_PLAYER = (N_PLAYERS < 3) ? 5 : 4; + hints = MAX_HINTS; + thunders = 0; + roundsLeft = -1; + createPlayers(); + currentPlayer = 0; + resetMoveValues(); + addCards(); + CARDS_IN_DECK = cards.size(); + createUI(); + dealCards(); + } + + protected void loadOptions(){ + try { + JSONTokener tokener = new JSONTokener(options.toURI().toURL().openStream()); + root = new JSONObject(tokener); + difficulty = root.getInt("difficulty"); + fontSize = root.getInt("fontSize"); + isHost = root.getBoolean("host"); + if(difficulty < 0) difficulty = 0; + if(fontSize <= 0) fontSize = 20; + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (MalformedURLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + protected void establishConnection(){ + try { + if(isHost){ + server = new ServerSocket(8765); + printMessage("Your server is running on port 8765", MSG_TYPES.INFORMATION); + s = server.accept(); + DataInputStream inStream = new DataInputStream(s.getInputStream()); + String str = inStream.readUTF(); + System.out.println("Message: " + str); + s.close(); + server.close(); + } else { + s = new Socket("localhost", 8765); + DataOutputStream outStream = new DataOutputStream(s.getOutputStream()); + outStream.writeUTF("Hello"); + outStream.flush(); + outStream.close(); + s.close(); + } + } catch(UnknownHostException uhe){ + printMessage("Unknown host '???'", MSG_TYPES.ERROR); + } catch (IOException e) { + printMessage(e.getLocalizedMessage(), MSG_TYPES.ERROR); + System.exit(0); //TODO status code + } + + } + + public void resetMoveValues(){ + selectedPlayer = -1; + chosenColor = -1; + chosenValue = -1; + selectedCards = 0; + useColor = useValue = trashCard = placeCard = false; + } + + protected void createPlayers(){ + players = new AbstractPlayer[N_PLAYERS]; + /*for(int i=0; i(); + deck = new Card[COLORS][5]; + deckCounter = new int[5]; + 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(){ + won = true; + 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; + } + + 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; + 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(); + + /*private void printArray(int[][] arr){ + for(int j=0; j cards; -// Card[][] deck; -// int[] deckCounter; -// ArrayList trash; -// AbstractPlayer[] players; -// int currentPlayer; -// -// int selectedPlayer; -// int selectedCards; -// int chosenColor; -// int chosenValue; -// boolean useColor; -// boolean useValue; -// boolean placeCard; -// boolean trashCard; -// boolean won; -// boolean lost; -// -// //UI STUFF -// JFrame mainFrame; -// JPanel gamePanel; -// JPanel hintPanel; -// JPanel thunderPanel; -// JPanel buttonPanel; -// JPanel deckPanel; -// JLabel[][] deckUI; -// JButton colorButton; -// JButton valueButton; -// JButton nextMove; -// JButton placeCardButton; -// JButton trashCardButton; -// JLabel[] hintUI; -// JLabel[] thunderUI; -// JPanel[] playersUI; -// Font font; -// -// //Options stuff -// final File options = new File("options.json"); -// JSONObject root; -// int difficulty; -// int fontSize; -// -// public static void main(String[] args) { -// Game game = new Game(); -// game.getPlayer(game.currentPlayer).deactiveCards(); -// System.out.println("Player " + game.currentPlayer + "'s turn"); -// } -// -// public Game(){ -// init(); -// } -// -// private void init(){ -// loadOptions(); -// font = new Font("Shanghai", Font.PLAIN, fontSize); -// CARDS_PER_PLAYER = (NR_PLAYERS < 3) ? 5 : 4; -// hints = MAX_HINTS; -// thunders = 0; -// createPlayers(); -// currentPlayer = 0; -// resetMoveValues(); -// addCards(); -// CARDS_IN_DECK = cards.size(); -// createUI(); -// dealCards(); -// } -// -// private 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; -// System.out.println("FontSize: " + fontSize); -// System.out.println("Difficulty: " + difficulty); -// } catch (JSONException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } catch (MalformedURLException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } catch (IOException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// } -// -// public void resetMoveValues(){ -// selectedPlayer = -1; -// chosenColor = -1; -// chosenValue = -1; -// selectedCards = 0; -// useColor = useValue = trashCard = placeCard = false; -// } -// -// private void createPlayers(){ -// players = new AbstractPlayer[NR_PLAYERS]; -// for(int i=0; i(); -// deck = new Card[COLORS][5]; -// deckCounter = new int[5]; -// 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){ -// System.out.println("Please deselect all cards first"); -// return; -// } -// useColor = true; -// useValue = false; -// placeCard = false; -// trashCard = false; -// } -// @Override -// public void mousePressed(MouseEvent e) { -// } -// @Override -// public void mouseExited(MouseEvent e) { -// } -// @Override -// public void mouseEntered(MouseEvent e) { -// } -// @Override -// public void mouseClicked(MouseEvent e) { -// } -// }); -// valueButton.addMouseListener(new MouseListener(){ -// @Override -// public void mouseReleased(MouseEvent e) { -// if(!hintAvailable()){ -// System.out.println("No hints left!"); -// return; -// } -// if(selectedCards > 0){ -// System.out.println("Please deselect all cards first"); -// return; -// } -// useValue = true; -// useColor = false; -// placeCard = false; -// trashCard = false; -// } -// @Override -// public void mousePressed(MouseEvent e) { -// } -// @Override -// public void mouseExited(MouseEvent e) { -// } -// @Override -// public void mouseEntered(MouseEvent e) { -// } -// @Override -// public void mouseClicked(MouseEvent e) { -// } -// }); -// nextMove.addMouseListener(new MouseListener(){ -// @Override -// public void mouseReleased(MouseEvent e) { -// if(!checkValidMove()){ -// System.out.println("Please select all cards of the selected color/value"); -// return; -// } -// int selCardIndex = 0; -// for(int i=0; i MAX_HINTS) hints = MAX_HINTS; -// } -// } else if(placeCard){ -// Card c = players[currentPlayer].getCard(selCardIndex); -// int color = c.getColorInt(); -// int value = c.getValue(); -// int deckValue = deckCounter[color] + 1; -// if(deckValue == value){ //place card possible -// deckCounter[color]++; -// deck[color][value-1] = c; -// deckUI[color][value-1].setText(value + ""); -// deckUI[color][value-1].setForeground(c.getColor()); -// setWonOrLost(); -// } else { //card too high or too low -// thunderUI[thunders++].setText("1"); -// moveCardToTrash(selCardIndex); -// setWonOrLost(); -// if(lost) return; -// } -// //dealCard(selCardIndex, currentPlayer); -// updateCards(selCardIndex); -// } -// players[currentPlayer].activeCards(); -// System.out.println("Player " + currentPlayer + " has finished his move!"); -// currentPlayer++; -// if(currentPlayer == NR_PLAYERS) currentPlayer = 0; -// players[currentPlayer].deactiveCards(); -// resetMoveValues(); -// System.out.println(CARDS_IN_DECK + " cards left."); -// System.out.println(trash.size() + " cards in trash"); -// System.out.println("Player " + currentPlayer + "'s turn"); -// } -// @Override -// public void mousePressed(MouseEvent e) { -// } -// @Override -// public void mouseExited(MouseEvent e) { -// } -// @Override -// public void mouseEntered(MouseEvent e) { -// } -// @Override -// public void mouseClicked(MouseEvent e) { -// } -// }); -// trashCardButton.addMouseListener(new MouseListener(){ -// @Override -// public void mouseReleased(MouseEvent e) { -// if(selectedCards > 0){ -// System.out.println("Please deselect all cards first"); -// return; -// } -// useColor = false; -// useValue = false; -// placeCard = false; -// trashCard = true; -// } -// @Override -// public void mousePressed(MouseEvent e) { -// } -// @Override -// public void mouseExited(MouseEvent e) { -// } -// @Override -// public void mouseEntered(MouseEvent e) { -// } -// @Override -// public void mouseClicked(MouseEvent e) { -// } -// }); -// placeCardButton.addMouseListener(new MouseListener(){ -// @Override -// public void mouseReleased(MouseEvent e) { -// if(selectedCards > 0){ -// System.out.println("Please deselect all cards first"); -// return; -// } -// useColor = false; -// useValue = false; -// placeCard = true; -// trashCard = false; -// } -// @Override -// public void mousePressed(MouseEvent e) { -// } -// @Override -// public void mouseExited(MouseEvent e) { -// } -// @Override -// public void mouseEntered(MouseEvent e) { -// } -// @Override -// public void mouseClicked(MouseEvent e) { -// } -// }); -// } -// -// private void moveCardToTrash(int index){ -// trash.add(players[currentPlayer].getCard(index)); -// updateCards(index); -// } -// -// private void updateCards(int index){ -// dealCard(index, currentPlayer); -// players[currentPlayer].setCardInfo(index, ""); -// Card c = players[currentPlayer].getCard(index); -// //JLabel card = ((SwingPlayer) players[currentPlayer]).getUiCard(index); -// AbstractPlayer card = players[currentPlayer]; -// if(c.getValue() == -1){ //dummy card, no more cards -// card.setColor(index, 255, 122, 0); -// card.setText(index, "No Cards left"); -// card.unsetListeners(index); -// } -// } -// -// private void dealCard(int cardID, int playerID){ -// SwingCard card; -// if(CARDS_IN_DECK > 0){ -// int index = random(CARDS_IN_DECK--); -// card = (SwingCard) cards.remove(index); -// } else { -// card = new SwingCard(3, -1, playerID, this); -// } -// players[playerID].setCard(cardID, card); -// } -// -// private void dealCards(){ -// int dealtCards = 0; -// for(; dealtCards < CARDS_PER_PLAYER; ++dealtCards){ -// for(int i=0; i 0); -// } -// -// private void setWonOrLost(){ -// won = true; -// lost = false; -// for(int i=0; i= 0) roundsLeft--; - int selCardIndex = 0; - for(int i=0; i MAX_HINTS) hints = MAX_HINTS; - } - } else if(placeCard){ - Card c = players[currentPlayer].getCard(selCardIndex); - int color = c.getColorInt(); - int value = c.getValue(); - int deckValue = deckCounter[color] + 1; - if(deckValue == value){ //place card possible - deckCounter[color]++; - deck[color][value-1] = c; - deckUI[color][value-1].setText(value + ""); - deckUI[color][value-1].setForeground(c.getColor()); - } else { //card too high or too low - thunderUI[thunders++].setText("1"); - moveCardToTrash(selCardIndex); - } - setWonOrLost(); - if(lost) return; - //dealCard(selCardIndex, currentPlayer); - updateCards(selCardIndex); - } - players[currentPlayer].activeCards(); - printMessage("Player " + currentPlayer + " has finished his move!", MSG_TYPES.INFORMATION); - currentPlayer++; - if(currentPlayer == N_PLAYERS) currentPlayer = 0; - players[currentPlayer].deactiveCards(); - resetMoveValues(); } @Override protected void onColor(){ super.onColor(); - if(!hintAvailable()){ - printMessage("No hints left!", MSG_TYPES.ERROR); - return; - } - if(selectedCards > 0){ - printMessage("Please deselect all cards first", MSG_TYPES.ERROR); - return; - } - useColor = true; - useValue = false; - placeCard = false; - trashCard = false; } @Override protected void onValue(){ super.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; } @Override @@ -423,6 +358,7 @@ @Override protected void showTrash(){ JPanel panel = new JPanel(); + panel.setBackground(Color.black); for(Card c : trash){ JLabel label = new JLabel(c.getValue() + ""); label.setForeground(c.getColor()); diff --git a/src/player/AbstractPlayer.java b/src/player/AbstractPlayer.java index 51895ec..13d55ff 100644 --- a/src/player/AbstractPlayer.java +++ b/src/player/AbstractPlayer.java @@ -1,15 +1,15 @@ package player; import card.Card; -import game.AbstractGame; +import game.BaseGame; public abstract class AbstractPlayer { protected Card[] cards; protected String[] cardInfos; protected int Id; - protected AbstractGame g; + protected BaseGame g; - public AbstractPlayer(int nrOfCards, AbstractGame g, int Id){ + public AbstractPlayer(int nrOfCards, BaseGame g, int Id){ cards = new Card[nrOfCards]; cardInfos = new String[nrOfCards]; this.g = g; diff --git a/src/player/SwingPlayer.java b/src/player/SwingPlayer.java index 5977f4c..92462a4 100644 --- a/src/player/SwingPlayer.java +++ b/src/player/SwingPlayer.java @@ -14,7 +14,6 @@ private final static String HTML_BEGIN = ""; private final static String HTML_END = ""; private final static String HTML_NEWLINE = "
"; - //private Card[] swingCards; private JLabel[] labels; private JPanel container; public SwingPlayer(int nrOfCards, SwingGame swingGame, int Id){ @@ -88,15 +87,18 @@ ((SwingCard) cards[index]).getFace().setForeground(new Color(r, g, b)); } - @Override - public int[] getColor(int index) { + private int[] swingColorToInt(Color c){ int[] color = new int[3]; - Color c = ((SwingCard) cards[index]).getFace().getForeground(); color[0] = c.getRed(); color[1] = c.getGreen(); color[2] = c.getBlue(); return color; } + + @Override + public int[] getColor(int index) { + return swingColorToInt(((SwingCard) cards[index]).getFace().getForeground()); + } @Override public void setBackground(int index, int r, int g, int b) { @@ -105,12 +107,7 @@ @Override public int[] getBackground(int index) { - int[] color = new int[3]; - Color c = ((SwingCard) cards[index]).getFace().getBackground(); - color[0] = c.getRed(); - color[1] = c.getGreen(); - color[2] = c.getBlue(); - return color; + return swingColorToInt(((SwingCard) cards[index]).getFace().getBackground()); } @Override @@ -143,7 +140,6 @@ public void activeCards(){ for(int i=0; i