diff --git a/src/game/AbstractGame.java b/src/game/AbstractGame.java index 248e22b..f381a7b 100644 --- a/src/game/AbstractGame.java +++ b/src/game/AbstractGame.java @@ -207,7 +207,7 @@ protected abstract void createUI(); void moveCardToTrash(int index){ - trash.add(players[currentPlayer].getCard(index)); + trash.add(players[currentPlayer].getCard(index).clone()); updateCards(index); } @@ -356,6 +356,31 @@ public abstract void printMessage(String msg, MSG_TYPES type); + protected void onNext(){} + protected void onColor(){} + protected void onValue(){} + 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 0){ - printMessage("Please deselect all cards first", MSG_TYPES.ERROR); - return; - } - useColor = true; - useValue = false; - placeCard = false; - trashCard = false; + onColor(); } @Override public void mousePressed(MouseEvent e) { @@ -152,18 +141,7 @@ valueButton.addMouseListener(new MouseListener(){ @Override public void mouseReleased(MouseEvent e) { - 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; + onValue(); } @Override public void mousePressed(MouseEvent e) { @@ -181,74 +159,7 @@ nextMove.addMouseListener(new MouseListener(){ @Override public void mouseReleased(MouseEvent e) { - 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 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(); - //System.out.println(CARDS_IN_DECK + " cards left."); - //System.out.println(trash.size() + " cards in trash"); - //System.out.println("Player " + currentPlayer + "'s turn"); + onNext(); } @Override public void mousePressed(MouseEvent e) { @@ -266,14 +177,8 @@ trashCardButton.addMouseListener(new MouseListener(){ @Override public void mouseReleased(MouseEvent e) { - if(selectedCards > 0){ - printMessage("Please deselect all cards first", MSG_TYPES.ERROR); - return; - } - useColor = false; - useValue = false; - placeCard = false; - trashCard = true; + if(e.isControlDown()) showTrash(); + else onTrash(); } @Override public void mousePressed(MouseEvent e) { @@ -291,14 +196,7 @@ placeCardButton.addMouseListener(new MouseListener(){ @Override public void mouseReleased(MouseEvent e) { - if(selectedCards > 0){ - printMessage("Please deselect all cards first", MSG_TYPES.ERROR); - return; - } - useColor = false; - useValue = false; - placeCard = true; - trashCard = false; + onPlace(); } @Override public void mousePressed(MouseEvent e) { @@ -409,6 +307,132 @@ } @Override + protected void onNext(){ + super.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 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 + protected void onTrash(){ + super.onTrash(); + } + + @Override + protected void onPlace(){ + super.onPlace(); + } + + @Override + protected void showTrash(){ + JPanel panel = new JPanel(); + for(Card c : trash){ + JLabel label = new JLabel(c.getValue() + ""); + label.setForeground(c.getColor()); + panel.add(label); + } + panel.setVisible(true); + JOptionPane.showMessageDialog(mainFrame, panel); + } + + @Override public Object getFont() { return font; }