diff --git a/src/Card.java b/src/Card.java deleted file mode 100644 index 9a9299e..0000000 --- a/src/Card.java +++ /dev/null @@ -1,55 +0,0 @@ -import java.awt.Color; - -/* - * COLORS: - * GREEN - * RED - * YELLOW - * WHITE - * BLUE - * (COLORED) - */ -public class Card { - private int color; - private int value; - - public Card(int color, int value){ - this.color = color; - this.value = value; - } - - @Override - public String toString(){ - return "Color: " + color + "; Value: " + value; - } - - public Color getColor(){ - Color c = null; - switch(color){ - case 0: - c = Color.green; - break; - case 1: - c = Color.red; - break; - case 2: - c = Color.yellow; - break; - case 3: - c = Color.white; - break; - case 4: - c = Color.blue; - break; - } - return c; - } - - public int getColorInt(){ - return color; - } - - public int getValue(){ - return value; - } -} diff --git a/src/Game.java b/src/Game.java deleted file mode 100644 index 51e42d7..0000000 --- a/src/Game.java +++ /dev/null @@ -1,718 +0,0 @@ -import java.awt.BorderLayout; -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; -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; -import java.util.ArrayList; - -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; -import javax.swing.SwingConstants; -import javax.swing.UIManager; -import javax.swing.UnsupportedLookAndFeelException; - - -public class Game { - /* - * COLORS: - * GREEN - * RED - * YELLOW - * WHITE - * BLUE - * (COLORED) - */ - 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 COLORS = 5; - final static int NR_PLAYERS = 2; - static int CARDS_PER_PLAYER; - final static int NR_OF_CARDS = 50; - static int CARDS_IN_DECK; - final static int MAX_HINTS = 8; - final static int MAX_THUNDERS = 3; - int hints; - int thunders; - static ArrayList cards; - static Card[][] deck; - static int[] deckCounter; - static ArrayList trash; - static Player[] players; - static int currentPlayer; - - static int selectedPlayer; - static int selectedCards; - static int chosenColor; - 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; - - - public static void main(String[] args) { - Game game = new Game(); - System.out.println("Player " + currentPlayer + "'s turn"); - } - - public Game(){ - init(); - } - - private void init(){ - 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 resetMoveValues(){ - selectedPlayer = -1; - chosenColor = -1; - chosenValue = -1; - selectedCards = 0; - useColor = useValue = trashCard = placeCard = false; - } - - private void createPlayers(){ - players = new Player[NR_PLAYERS]; - playersUI = new JPanel[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; - } - //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()); - setWonOrLost(); - } else { - //TODO bad - thunderUI[thunders++].setText("1"); - moveCardToTrash(indexLabel, index, playerContainer); - setWonOrLost(); - if(lost) return; - } - dealCard(indexLabel, currentPlayer); - updateCards(indexLabel, index, playerContainer); - } - System.out.println("Player " + currentPlayer + " has finished his move!"); - currentPlayer++; - if(currentPlayer == NR_PLAYERS) currentPlayer = 0; - 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; - 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)); - updateCards(indexLabel, index, playerContainer); - } - - private void updateCards(int indexLabel, int index, JPanel playerContainer){ - dealCard(indexLabel, currentPlayer); - players[currentPlayer].setCardInfo(indexLabel, ""); - Card c = players[currentPlayer].getCard(indexLabel); - JLabel card = (JLabel) playerContainer.getComponent(index); - if(c.getValue() == -1){ //dummy card, no more cards - card.setForeground(Color.orange); - card.setText("No Cards left"); - MouseListener[] listeners = card.getMouseListeners(); - for(int i=0; i 0){ - int index = random(CARDS_IN_DECK--); - card = cards.remove(index); - } else { - card = new Card(3, -1); - } - players[playerID].setCard(cardID, card); - } - - private void dealCards(){ - int dealtCards = 0; - for(; dealtCards < CARDS_PER_PLAYER; ++dealtCards){ - for(int i=0; i select all 4's - } else if(chosenValue != clickedValue && chosenColor == clickedColor){ - //chosenValue = -2; - useColor = true; - //TODO same color => select all reds - } - }*/ - if(label.getBackground() == Color.black){ - if(useColor){ - if(chosenColor == clickedColor){ - //TODO add card - //selectedCards.add(clickedCard); - if(label.getBackground() != Color.pink){ - selectedCards++; - label.setBackground(Color.pink); - System.out.println("Added card #" + clickedCard + "(" + intColorToText(card.getColorInt()) + ", " + card.getValue() + ")"); - } - } - } else if(useValue){ - if(chosenValue == clickedValue){ - //TODO add card - //selectedCards.add(clickedCard); - if(label.getBackground() != Color.pink){ - selectedCards++; - label.setBackground(Color.pink); - System.out.println("Added card #" + clickedCard + "(" + intColorToText(card.getColorInt()) + ", " + card.getValue() + ")"); - } - } - } - } - } - - @Override - public void mousePressed(MouseEvent e) { - } - - @Override - public void mouseExited(MouseEvent e) { - } - - @Override - public void mouseEntered(MouseEvent e) { - } - - @Override - public void mouseClicked(MouseEvent e) { - } - }); - } - mainFrame.add(playerContainer, playerSide); - } - } - - private boolean hintAvailable(){ - return (hints > 0); - } - - private String intColorToText(int id){ - String color = ""; - switch(id){ - case 0: - color = "green"; - break; - case 1: - color = "red"; - break; - case 2: - color = "yellow"; - break; - case 3: - color = "white"; - break; - case 4: - color = "blue"; - break; - } - return color; - } - - private void setWonOrLost(){ - won = true; - lost = false; - for(int i=0; i cards; + static Card[][] deck; + static int[] deckCounter; + static ArrayList trash; + static AbstractPlayer[] players; + static int currentPlayer; + + static int selectedPlayer; + static int selectedCards; + static int chosenColor; + 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; + + + public static void main(String[] args) { + /*Game game = */new Game(); + System.out.println("Player " + currentPlayer + "'s turn"); + } + + public Game(){ + init(); + } + + private void init(){ + 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 resetMoveValues(){ + selectedPlayer = -1; + chosenColor = -1; + chosenValue = -1; + selectedCards = 0; + useColor = useValue = trashCard = placeCard = false; + } + + private void createPlayers(){ + players = new AbstractPlayer[NR_PLAYERS]; + playersUI = new JPanel[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: " + 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); + } + System.out.println("Player " + currentPlayer + " has finished his move!"); + currentPlayer++; + if(currentPlayer == NR_PLAYERS) currentPlayer = 0; + 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; + 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 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); + if(c.getValue() == -1){ //dummy card, no more cards + card.setForeground(Color.orange); + card.setText("No Cards left"); + MouseListener[] listeners = card.getMouseListeners(); + for(int i=0; i 0){ + int index = random(CARDS_IN_DECK--); + card = cards.remove(index); + } else { + card = new Card(3, -1); + } + players[playerID].setCard(cardID, card); + } + + private void dealCards(){ + int dealtCards = 0; + for(; dealtCards < CARDS_PER_PLAYER; ++dealtCards){ + for(int i=0; i 0); + } + + private String intColorToText(int id){ + String color = ""; + switch(id){ + case 0: + color = "green"; + break; + case 1: + color = "red"; + break; + case 2: + color = "yellow"; + break; + case 3: + color = "white"; + break; + case 4: + color = "blue"; + break; + } + return color; + } + + private void setWonOrLost(){ + won = true; + lost = false; + for(int i=0; i