UnoGame, Can't figure out how to check for numbers on card and discard pile

Viewed 46

I'm making a Uno Game. I have a question in a method im making called playCard(Card card).

My question is how do I check the number of my card if I'm only allowed to have one parameter in my playCard which is card, I've already made a different class called NumberCard that I was able to implement Numbers into the card and a color as well. (NumberCard extends Card).

Is there something I need to type cast? and if there is what is it? I've been trying to figure out this one problem for more than 8 hours now.

Thanks!

Also I apologize if my question is ordered in a confusing way, it's my first time using StackOverFlow.

Here is the exactly what I'm asked to make

public void playCard(Card card):
This method is called when a player wishes to play a specific card from their hand. The method should validate that the card can be played, then update the game status based on the game rules.

Card Class

package model.cards;

abstract public class Card {
    private Color color;

    public Card(Color color){
        this.color = color;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }
}

NumberCard Class

package model.cards;

public class NumberCard extends ColorCard{
    private int number;

    public NumberCard(int number, Color color) {
        super(color);
        this.number = number;
    }
    public int getNumber() {
        return number;
    }
}

playCard method in Game Class

    public void playCard(Card card) //confused on how to add numbers here!
    {
            if (card.getColor() == getLatestDiscard().getColor()) {
                // confused on how to add the numbers.
            }
    }

Game Class

public class Game {
    //Attributes
    private ArrayList<Player> players = new ArrayList<Player>();
    private ArrayList<Card> deck;// shouldn't deck be 2d? how can I add the colors + the number at once?
    private ArrayList<Card> discardPile = new ArrayList<Card>();
    //ArrayList<>
    private int currentDrawn;
    private boolean playedCard;


    //Setters and Getters

    public ArrayList<Card> getDeck() {
        return deck;
    }
    public ArrayList<Player> getPlayers() {
        return players;
    }
    public ArrayList<Card> getDiscardPile() {
        return discardPile;
    }
    public int getCurrentDrawn() {
        return currentDrawn;
    }

    public boolean isPlayedCard() {
        return playedCard;
    }
    public Player getNextPlayer(){
        Collections.rotate(players, -1);
        /*
        players.add(players.remove(0));
        ^^ adds to the end of the array list the first player while removing them from first part of list

        thought it would be better to rotate, thinking if I remove and add the player it might affect the cards in hand
        making them reset or something.
         */
        return players.get(0);
    }
    public Card getLatestDiscard(){
        return discardPile.get(discardPile.size()-1);
        // size - 1 == amount of cards that exist?
    }
    public Player checkGameOver(){
        for (int i = 0; i != players.size(); i++) //for loop doesnt loop?
        {
            if (players.get(i).getHand().size() == 0)
                return players.get(i);
        }
        return null;
    }
    public void playCard(Card card) //confused on how to add numbers here!
    {
            if (card.getColor() == getLatestDiscard().getColor()) {
                // confused on how to add the numbers.
            }
    }

    public void playCard(Card card, Color color){
        card.setColor((color)); // Card sets color to the color picked?
    }


    public void draw() throws MaxCardsDrawnException {
        currentDrawn++ ;
        try {
            if(currentDrawn <= 2) {
                players.get(0).getHand().add(deck.get(0));
                deck.remove(0);
                if (deck.size() == 0)
                    rebuildDeck();
            }
            if (currentDrawn > 2)
                throw new MaxCardsDrawnException();
            } catch (MaxCardsDrawnException s) {
            System.out.println("You are only allowed to draw twice");
        }
    }
    private void rebuildDeck(){
        if (deck.size() == 0){
            deck.add(discardPile.get(discardPile.size() - 1));
            discardPile.remove(discardPile.size() - 1);
            Collections.shuffle(deck);
        }
    }
    public void endTurn() throws UnallowedCardException {

        if (isPlayedCard()) {
            if (Objects.equals(discardPile.get(discardPile.size() - 1), new SkipCard(Color.WILD)) ||
                    Objects.equals(discardPile.get(discardPile.size() - 1), new SkipCard(Color.BLUE)) ||
                    Objects.equals(discardPile.get(discardPile.size() - 1), new SkipCard(Color.RED)) ||
                    Objects.equals(discardPile.get(discardPile.size() - 1), new SkipCard(Color.GREEN)) ||
                    Objects.equals(discardPile.get(discardPile.size() - 1), new SkipCard(Color.YELLOW)))
            {
                getNextPlayer();
            }
        }
        else if (currentDrawn == 2) {
            getNextPlayer();
            currentDrawn = 0;
        }
        try{
            if (currentDrawn == 2) {
                getNextPlayer();
                currentDrawn = 0;
            }
            else
                throw new UnallowedCardException();

        }
        catch (UnallowedCardException e){
            System.out.println("You have to play a card or draw two cards!");
        }
    }



    public Game(ArrayList<Player> players)//9.2 description
    {
        this.deck = new ArrayList<Card>();
        this.players = new ArrayList<Player>();
        this.players = players;
        this.currentDrawn = 0;
        buildDeck();
        distribute();
        discardPile.add(deck.remove(deck.size() - 1));

    }

    private void buildDeck() {
        this.deck = new ArrayList<Card>(100);
        for(int x = 0; x != 5; x++) //The function mentioned in my explanation down bellow.
        {
            if (x <= 3)
            {
                for (int c = 0; c != 23; c++)
                {
                    if (x == 0) {
                        if (c < 10)
                            deck.add(new NumberCard(c, Color.RED));
                        else if (c <= 18)
                            deck.add(new NumberCard(c - 9, Color.RED));
                        else if (c <= 20)
                            deck.add(new HitCard(Color.RED));
                        else
                            deck.add(new SkipCard(Color.RED));
                    }
                    else if (x == 1){
                        if (c < 10)
                            deck.add(new NumberCard(c, Color.GREEN));
                        else if (c <= 18)
                            deck.add(new NumberCard(c - 9, Color.GREEN));
                        else if (c <= 20)
                            deck.add(new HitCard(Color.GREEN));
                        else
                            deck.add(new SkipCard(Color.GREEN));
                    }
                    else if (x == 2){
                        if (c < 10)
                            deck.add(new NumberCard(c, Color.BLUE));
                        else if (c <= 18)
                            deck.add(new NumberCard(c - 9, Color.BLUE));
                        else if (c <= 20)
                            deck.add(new HitCard(Color.BLUE));
                        else
                            deck.add(new SkipCard(Color.BLUE));
                        }
                    else if (x == 3) // why does it say that x is always == to  3;
                    {
                        if (c < 10)
                            deck.add(new NumberCard(c, Color.YELLOW));
                        else if (c <= 18)
                            deck.add(new NumberCard(c - 9, Color.YELLOW));
                        else if (c <= 20)
                            deck.add(new HitCard(Color.YELLOW));
                        else
                            deck.add(new SkipCard(Color.YELLOW));
                    }
                }
            }
            else
            {
                for (int c = 0; c != 4; c++)
                {
                    deck.add(new ChangeCard());
                    deck.add(new HitAndChangeCard());
                }
            }
        }
        Collections.shuffle(deck);
    }

    private void distribute(){
    for (int x = 0; x != players.size(); x++) {
        for (int i = 0; i < 7;i++ ) {
            players.get(x).getHand().add(deck.get(0));
            deck.remove(0);
        }
    }

    }


}

Player Class (incase needed)

package engine;
import model.cards.Card;
import java.util.ArrayList;
import java.util.Collections;

public class Player {
    private String name;
    private ArrayList<Card> hand;


    public Player(String name){
            //ArrayList<String> players= new ArrayList<String>(Collections.singleton(name));
            // I actually don't know what I did here ^
            //^ object ? is this what it means by "Constructor that initializes a Player object with the given name."
            this.name = name;
            hand =  new ArrayList<>();
       // players.stream().limit(4);//not needed or?
    }

    public String getName() {
        return name;
    }
0 Answers
Related