numCardsHand ++; getWorld().addObject(card, (numCardsHand*55) + (getWorld().getHeight()/8), 410);
numCardsHand ++; getWorld().addObject(card, (numCardsHand*55) + (getWorld().getHeight()/8), 410);
for (for i=0; i<playerTwoHand.size(); i++)
{
Card card = playerTwoHand.get(i);
card.setLocation(i*55+getWorld().getHeight()/8, card.getY());
}public void noPlay()
{
if (Greenfoot.isKeyDown("space") && noPlayDraw)
{
System.out.println("You Draw");
addCardOne();
updateCounter();
noPlayDraw = false;
allowPlay = false;
//If drawn card can be played
Card1 drawCard = playerOneHand.get(playerOneHand.size() - 1);
if (drawCard.getCardValue() == currentValue || drawCard.getCardColor() == currentColor)
{
clickedColor = drawCard.getCardColor();
clickedValue = drawCard.getCardValue();
if (drawCard.getCardValue().equals("+2"))
{
//If the clicked card is a +2 card
for (int i = 0; i < 2; i++)
{
addCardTwo();
}
skipPlayed = true;
}
else if (drawCard.getCardValue().equals("Skip"))
{
//If the clicked card is a skip card
skipPlayed = true;
}
else if (drawCard.getCardValue().equals("+4"))
{
//if the clicked card is a +4 card
for (int i = 0; i < 4; i++)
{
addCardTwo();
}
String inputColor = JOptionPane.showInputDialog("Choose a color");
currentColor = inputColor;
skipPlayed = true;
}
else if (drawCard.getCardValue().equals("Wild"))
{
//If the clicked card is a Wild card
String inputColor = JOptionPane.showInputDialog("Choose a color");
currentColor = inputColor;
}
drawCard.setLocation(350, 225);
numCardsHand --;
updateCounter();
Greenfoot.playSound("Placing Cards.mp3");
if (skipPlayed)
{
allowPlay = true;
noPlayDraw = true;
skipPlayed = false;
}
}
}
}if ("space".equals(Greenfoot.getKey()) && noPlayDraw)import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
import javax.swing.JOptionPane;
/**
* Main class for running the game. Create cards to make up the deck.
*
* @author Mark Lau
* @version v1 6/9/19
*/
public class Deck1 extends Actor
{
private String [] colors = {"Yellow", "Blue", "Red", "Green", "Yellow", "Blue", "Red", "Green"};
private String [] values = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "Reverse", "Skip", "+2"};
private ArrayList <Card1> cards = new ArrayList <Card1> ();
private ArrayList <Card1> playerOneHand = new ArrayList <Card1> ();
private ArrayList <Card1> playerTwoHand = new ArrayList <Card1> ();
private String clickedValue;
private String clickedColor;
private String currentValue;
private String currentColor;
private String imageName;
private int numCardsHand;
private int numCardsHand2;
private boolean allowPlay = true;
private boolean noPlayDraw = true;
private boolean noPlayDraw2;
private boolean skipPlayed;
private boolean skipPlayed2;
private boolean uno;
public Deck1()
{
generateCards();
}
/**
* Act - do whatever the Deck1 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
ifClicked();
uno();
noPlay();
cpuTurn();
checkWin();
}
/**
* ifClicked - When clicked, places cards in middle
*/
public void ifClicked()
{
if (Greenfoot.mouseClicked(null))
{
Object obj = Greenfoot.getMouseInfo().getActor();
if (obj != null && (obj instanceof Card1) && allowPlay)
{
Card1 clickedCard = (Card1)obj;
//Sets clicked color and value variables
clickedColor = clickedCard.getCardColor();
clickedValue = clickedCard.getCardValue();
//If user did not press enter key when had 1 card left
if (numCardsHand == 1)
{
if (uno == false)
{
System.out.println("You forgot to press the Enter key!\nNow you have to draw 2 cards!");
for (int i = 0; i < 2; i++)
{
addCardOne();
}
allowPlay = false;
noPlayDraw = false;
}
}
//Allows player to use a card if it is either the same number or color
if (clickedColor.equals(currentColor) || clickedValue.equals(currentValue) || clickedColor.equals("") && allowPlay)
{
System.out.println("You played " + clickedValue + " " + clickedColor);
//Sets the current color and value to what was played
currentColor = clickedColor;
currentValue = clickedValue;
if (clickedCard.getCardValue().equals("+2"))
{
//If the clicked card is a +2 card
for (int i = 0; i < 2; i++)
{
addCardTwo();
}
skipPlayed = true;
}
else if (clickedCard.getCardValue().equals("Skip"))
{
//If the clicked card is a skip card
skipPlayed = true;
}
else if (clickedCard.getCardValue().equals("+4"))
{
//if the clicked card is a +4 card
for (int i = 0; i < 4; i++)
{
addCardTwo();
}
inputColors();
skipPlayed = true;
}
else if (clickedCard.getCardValue().equals("Wild"))
{
//If the clicked card is a Wild card
inputColors();
}
//Allows CPU to play next
allowPlay = false;
noPlayDraw = false;
//Moves other cards to keep hand centered
for (int i = playerOneHand.indexOf(clickedCard)+ 1; i < playerOneHand.size(); i++)
{
Card1 card = playerOneHand.get(i);
card.setLocation(card.getX() - 55, card.getY());
}
//Makes new card on top of the existing card in the middle
Card1 card2 = new Card1(currentValue, clickedCard.getCardColor());
card2.setImage(currentValue + clickedCard.getCardColor() + ".png");
getWorld().addObject(card2, 350, 225);
playerOneHand.remove(clickedCard);
//Removes the previous counter value and updates it
numCardsHand --;
updateCounter();
Greenfoot.playSound("Placing Cards.mp3");
if (skipPlayed)
{
allowPlay = true;
noPlayDraw = true;
skipPlayed = false;
}
getWorld().removeObject(clickedCard);
}
}
}
}
/**
* cpuTurn- When it is the CPU's turn
*/
public void cpuTurn()
{
if (allowPlay == false)
{
for (int i = 0; i < playerTwoHand.size(); i++)
{
Card1 card = playerTwoHand.get(i);
if (card.getCardColor().equals(currentColor) || card.getCardValue().equals(currentValue) || card.getCardColor().equals(""))
{
System.out.println("CPU plays " + card.getCardValue() + " " + card.getCardColor());
//Changes current variables to color + value of played card
currentColor = card.getCardColor();
currentValue = card.getCardValue();
if (card.getCardValue().equals("+2"))
{
//If the chosen card is a +2 card
for (int j = 0; j < 2; i++)
{
addCardOne();
updateCounter();
}
skipPlayed2 = true;
}
else if (card.getCardValue().equals("Skip"))
{
skipPlayed2 = true;
}
else if (card.getCardValue().equals("+4"))
{
//if the chosen card is a +4 card
for (int j = 0; i < 4; i++)
{
addCardOne();
updateCounter();
}
setRandomColor();
System.out.println("Color is " + currentColor);
skipPlayed2 = true;
}
else if (card.getCardValue().equals("Wild"))
{
//If the chosen card is a Wild card
setRandomColor();
System.out.println("Color is " + currentColor);
}
//Sets variables to allow for player's turn
noPlayDraw2 = false;
allowPlay = true;
noPlayDraw = true;
//Moves other cards to keep hand centered
for (int j = playerTwoHand.indexOf(card)+ 1; j < playerTwoHand.size(); j++)
{
Card1 card1 = playerTwoHand.get(j);
card1.setLocation(card1.getX() - 55, card1.getY());
}
//Makes new card so image shows on top of discard pile
Card1 card2 = new Card1(currentValue, card.getCardColor());
card2.setImage(currentValue + card.getCardColor() + ".png");
getWorld().addObject(card2, 350, 225);
playerTwoHand.remove(card);
numCardsHand2 --;
Greenfoot.playSound("Placing Cards.mp3");
if (skipPlayed2)
{
allowPlay = false;
noPlayDraw = false;
skipPlayed2 = false;
}
//Removes card from hand
getWorld().removeObject(card);
//Stop for loop
break;
}
else
{
noPlayDraw2 = true;
}
}
if (noPlayDraw2 == true)
{
System.out.println("CPU Draws");
addCardTwo();
allowPlay = true;
noPlayDraw = true;
//If drawn card can be played
Card1 drawCard = playerTwoHand.get(playerTwoHand.size() - 1);
if (drawCard.getCardValue() == currentValue || drawCard.getCardColor() == currentColor)
{
System.out.println("CPU plays " + drawCard.getCardValue() + " " + drawCard.getCardColor());
drawCard.setImage(drawCard.getCardValue() + drawCard.getCardColor() + ".png");
currentColor = drawCard.getCardColor();
currentValue = drawCard.getCardValue();
if (drawCard.getCardValue().equals("+2"))
{
//If the drawn card is a +2 card
for (int i = 0; i < 2; i++)
{
addCardTwo();
}
skipPlayed = true;
}
else if (drawCard.getCardValue().equals("Skip"))
{
//If the drawn card is a skip card
skipPlayed = true;
}
else if (drawCard.getCardValue().equals("+4"))
{
//if the drawn card is a +4 card
for (int i = 0; i < 4; i++)
{
addCardTwo();
}
setRandomColor();
System.out.println("Color is " + currentColor);
skipPlayed = true;
}
else if (drawCard.getCardValue().equals("Wild"))
{
//If the drawn card is a Wild card
setRandomColor();
System.out.println("Color is " + currentColor);
}
drawCard.setLocation(350, 225);
numCardsHand2 --;
Greenfoot.playSound("Placing Cards.mp3");
if (skipPlayed)
{
allowPlay = false;
noPlayDraw = false;
skipPlayed = false;
}
}
}
}
}
/**
* noPlay - if the player cannot play any card
*/
public void noPlay()
{
if ("space".equals(Greenfoot.getKey()) && noPlayDraw)
{
addCardOne();
updateCounter();
noPlayDraw = false;
allowPlay = false;
//If drawn card can be played
Card1 drawCard = playerOneHand.get(playerOneHand.size() - 1);
System.out.println("You Draw " + drawCard.getCardValue() + " " + drawCard.getCardColor());
if (drawCard.getCardValue() == currentValue || drawCard.getCardColor() == currentColor)
{
System.out.println("You played " + drawCard.getCardValue() + " " + drawCard.getCardColor());
clickedColor = drawCard.getCardColor();
clickedValue = drawCard.getCardValue();
if (drawCard.getCardValue().equals("+2"))
{
//If the drawn card is a +2 card
for (int i = 0; i < 2; i++)
{
addCardTwo();
}
skipPlayed = true;
}
else if (drawCard.getCardValue().equals("Skip"))
{
//If the drawn card is a skip card
skipPlayed = true;
}
else if (drawCard.getCardValue().equals("+4"))
{
//if the drawn card is a +4 card
for (int i = 0; i < 4; i++)
{
addCardTwo();
}
inputColors();
skipPlayed = true;
}
else if (drawCard.getCardValue().equals("Wild"))
{
//If the drawn card is a Wild card
inputColors();
}
drawCard.setLocation(350, 225);
numCardsHand --;
updateCounter();
Greenfoot.playSound("Placing Cards.mp3");
if (skipPlayed)
{
allowPlay = true;
noPlayDraw = true;
skipPlayed = false;
}
}
}
}
/**
* uno - when player has 1 card left, they must press enter key or they will draw 2 cards
*/
public void uno()
{
if (numCardsHand == 1)
{
if (Greenfoot.isKeyDown("enter"))
{
System.out.println("Uno!");
uno = true;
}
else
{
uno = false;
}
}
}
/**
* setGameScreen - Adds card and deck objects when player buttons are clicked
*/
public void setGameScreen()
{
//Starts game with 7 cards in each hand
while (numCardsHand < 7)
{
addCardOne();
addCardTwo();
}
//Starts the game with a card to play on
Card1 currentCard = cards.get(Greenfoot.getRandomNumber(cards.size()));
while (currentCard.getCardValue().equals("Wild") || currentCard.getCardValue().equals("+4"))
{
currentCard = cards.get(Greenfoot.getRandomNumber(cards.size()));
}
cards.remove(currentCard);
currentCard.setImage(currentCard.getCardValue() + currentCard.getCardColor() + ".png");
getWorld().addObject(currentCard, 350, 225);
//Set current values to card that was added to the middle
currentValue = currentCard.getCardValue();
currentColor = currentCard.getCardColor();
}
/**
* setRandomColor - When wild or +4 card from cpu is played, chooses random color
*/
public void setRandomColor()
{
int randomNumber = Greenfoot.getRandomNumber(3); //0 = red, 1 = green, 2 = blue, 3 = yellow
if (randomNumber == 0)
{
currentColor = "Red";
}
else if (randomNumber == 1)
{
currentColor = "Green";
}
else if (randomNumber == 2)
{
currentColor = "Blue";
}
else if (randomNumber == 3)
{
currentColor = "Yellow";
}
}
/**
* inputColors - sets input colors depending on what user inputs into dialog
*/
public void inputColors()
{
String inputColor = JOptionPane.showInputDialog("Choose a color");
if (inputColor.equals("Red") || inputColor.equals("red"))
{
currentColor = "Red";
}
else if (inputColor.equals("Green") || inputColor.equals("green"))
{
currentColor = "Green";
}
else if (inputColor.equals("Blue") || inputColor.equals("blue"))
{
currentColor = "Blue";
}
else if (inputColor.equals("Yellow") || inputColor.equals("yellow"))
{
currentColor = "Yellow";
}
System.out.println("Color is " + currentColor);
}
/**
* addCardOne - Adds random card to player one
*/
public void addCardOne()
{
//Chooses a random card from the total card list, removes it from that list and adds it to the hand
Card1 card = cards.get(Greenfoot.getRandomNumber(cards.size()));
playerOneHand.add(card);
cards.remove(card);
numCardsHand ++;
//Creates the new card and sets its position
card.setImage(card.getCardValue() + card.getCardColor() + ".png");
getWorld().addObject(card, (numCardsHand*55) + (getWorld().getHeight()/8), 410);
}
/**
* addCardTwo - Adds random card to player two
*/
public void addCardTwo()
{
//Chooses a random card from the total card list, removes it from that list and adds it to the hand
Card1 card = cards.get(Greenfoot.getRandomNumber(cards.size()));
playerTwoHand.add(card);
cards.remove(card);
numCardsHand2 ++;
//Creates the new card and sets its position
card.setImage("Back.png");
getWorld().addObject(card, (numCardsHand2*55) + (getWorld().getHeight()/8), 40);
}
/**
* updateCounter - updates the counter
*/
public void updateCounter()
{
getWorld().removeObjects(getWorld().getObjects(Counter.class));
Counter counter = new Counter(numCardsHand);
getWorld().addObject(counter, 665, 350);
}
/**
* checkWin - Check if player has won
*/
public void checkWin()
{
if (numCardsHand == 0)
{
Instructions instruct = new Instructions("one win");
getWorld().addObject(instruct, 400, 225);
getWorld().removeObjects(getWorld().getObjects(Card1.class));
getWorld().removeObjects(getWorld().getObjects(Counter.class));
getWorld().removeObjects(getWorld().getObjects(Deck1.class));
Greenfoot.stop();
}
if (numCardsHand2 == 0)
{
Instructions instruct = new Instructions("one lose");
getWorld().addObject(instruct, 400, 225);
getWorld().removeObjects(getWorld().getObjects(Card1.class));
getWorld().removeObjects(getWorld().getObjects(Counter.class));
getWorld().removeObjects(getWorld().getObjects(Deck1.class));
Greenfoot.stop();
}
}
/**
* generateCards- Creates deck of cards and puts them in list
*/
public void generateCards()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 13; j++)
{
Card1 card = new Card1(values[j], colors[i]);
cards.add(card);
}
}
for (int k = 0; k < 4; k++)
{
Card1 card = new Card1("Wild", "");
Card1 card1 = new Card1("+4", "");
cards.add(card);
cards.add(card1);
}
}
}