diff --git a/src/Card.java b/src/Card.java index 99c50f9..9a9299e 100644 --- a/src/Card.java +++ b/src/Card.java @@ -18,6 +18,11 @@ this.value = value; } + @Override + public String toString(){ + return "Color: " + color + "; Value: " + value; + } + public Color getColor(){ Color c = null; switch(color){ diff --git a/src/Game.java b/src/Game.java index 2e1a580..b238162 100644 --- a/src/Game.java +++ b/src/Game.java @@ -2,6 +2,7 @@ import java.awt.Color; import java.awt.Dimension; import java.awt.Font; +import java.awt.GridLayout; import java.awt.Point; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; @@ -12,6 +13,7 @@ import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; +import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; @@ -50,7 +52,6 @@ static ArrayList trash; static Player[] players; static int currentPlayer; - static boolean won; static int selectedPlayer; static int selectedCards; @@ -58,12 +59,24 @@ static int chosenValue; static boolean useColor; static boolean useValue; + static boolean placeCard; + static boolean trashCard; + static boolean won; + static 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; static JPanel[] playersUI; @@ -84,7 +97,6 @@ thunders = 0; createPlayers(); currentPlayer = 0; - won = false; resetMoveValues(); addCards(); CARDS_IN_DECK = cards.size(); @@ -97,7 +109,7 @@ chosenColor = -1; chosenValue = -1; selectedCards = 0; - useColor = useValue = false; + useColor = useValue = trashCard = placeCard = false; } private void createPlayers(){ @@ -133,6 +145,9 @@ } private 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 0){ + System.out.println("Please deselect all cards first"); + return; + } + useColor = true; + useValue = 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(selectedCards > 0){ + System.out.println("Please deselect all cards first"); + return; + } + useValue = true; + useColor = 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; + } + //TODO get cards, reset background, update tooltips + JPanel playerContainer = playersUI[selectedPlayer]; + int labels = 0; + int indexLabel = 0; + int index = 0; + for(int i=0; i MAX_HINTS) hints = MAX_HINTS; + } + } else if(placeCard){ + //trash.add(players[currentPlayer].getCard(indexLabel)); + Card c = players[currentPlayer].getCard(indexLabel); + int color = c.getColorInt(); + int value = c.getValue(); + int deckValue = deckCounter[color] + 1; + if(deckValue == value){ + //TODO good + deckCounter[color]++; + deck[color][value-1] = c; + deckUI[color][value-1].setText("Value: " + value); + deckUI[color][value-1].setForeground(c.getColor()); + } else { + //TODO bad + thunderUI[thunders++].setText("1"); + setWonOrLost(); + if(lost) return; + moveCardToTrash(indexLabel, index, playerContainer); + } + dealCard(indexLabel, currentPlayer); + players[currentPlayer].setCardInfo(indexLabel, ""); + c = players[currentPlayer].getCard(indexLabel); + JLabel card = (JLabel) playerContainer.getComponent(index); + card.setText("Value: " + c.getValue()); + card.setForeground(c.getColor()); + } + System.out.println("Player " + currentPlayer + " has finished his move!"); + currentPlayer++; + if(currentPlayer == NR_PLAYERS) currentPlayer = 0; + resetMoveValues(); + 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; + trashCard = false; + placeCard = 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) { + } + }); + } + + private void moveCardToTrash(int indexLabel, int index, JPanel playerContainer){ + trash.add(players[currentPlayer].getCard(indexLabel)); + dealCard(indexLabel, currentPlayer); + players[currentPlayer].setCardInfo(indexLabel, ""); + Card c = players[currentPlayer].getCard(indexLabel); + JLabel card = (JLabel) playerContainer.getComponent(index); + card.setText("Value: " + c.getValue()); + card.setForeground(c.getColor()); + } + + private void dealCard(int cardID, int playerID){ + int index = random(CARDS_IN_DECK--); + Card card = cards.remove(index); + players[playerID].setCard(cardID, card); } private void dealCards(){ @@ -343,9 +546,25 @@ System.out.println("No hints left!"); return; } + if(!useColor && !useValue && !trashCard && !placeCard){ + System.out.println("Please choose move type first"); + return; + } JLabel label = (JLabel) e.getComponent(); String name = label.getName(); int clickedPlayer = Integer.parseInt(name.substring(0, 1)); + if(clickedPlayer == currentPlayer){ + if(useColor || useValue){ + System.out.println("Please don't select your own cards, asshole!"); + return; + } else if((trashCard || placeCard) && selectedCards == 1){ + System.out.println("You already selected a card!"); + return; + } + } else if(clickedPlayer != currentPlayer && (trashCard || placeCard)){ + System.out.println("Please select your own cards"); + return; + } int clickedCard = Integer.parseInt(name.substring(name.length()-1)); Card card = players[clickedPlayer].getCard(clickedCard); int clickedValue = card.getValue(); @@ -371,7 +590,7 @@ if(chosenValue == -1){ chosenValue = clickedValue; } - if(!useColor && !useValue){ + /*if(!useColor && !useValue){ if(chosenValue == clickedValue && chosenColor == clickedColor){ //return; //TODO nothing to do here... } else if(chosenValue == clickedValue && chosenColor != clickedColor){ @@ -383,7 +602,7 @@ useColor = true; //TODO same color => select all reds } - } + }*/ if(label.getBackground() == Color.pink){ selectedCards--; label.setBackground(Color.black); @@ -445,6 +664,23 @@ return (hints > 0); } + private void setWonOrLost(){ + won = true; + lost = false; + for(int i=0; i