Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Hello!
I am trying to create a model class of a Crazy Eights game that uses abstraction from a Player class. The model follows these rules:

1. The game starts with each player being given some number of cards from the deck.

2. A single card is taken from the deck and placed on top of the discard pile (and is visible to the player).

3. A player's turn consists of taking zero or more cards from the deck (adding them to their hand) and then playing a card on the top of the discard pile (removing it from their hand).

4. A player can always play a card from their hand that has the same rank as the top card in the discard pile.

5. A player can always play a card from their hand that has the same suit as the top card in the discard pile.

6. A player can always play a card with rank 8. When a player plays an 8, they are allowed to change the suit of the card to any of the four suits of their choosing. (You will do this by removing the eight from your hand and then returning a new Card object with the desired suit.)

7. A player is allowed to (repeatedly) take a card from the deck before playing a card to the discard pile. You must play a card if you are able to. If the deck runs out of cards, the game ends.

8. The player that discards all their cards first is the winner. If the deck is exhausted before any player can play all their cards then there is no winner.

9. You are NOT implementing a crazy eights game. You are building the classes that would be needed for such a game. You are building the model for the game.

10. Player should NOT be "able to cheat" in the play method.

//This is my parent Player class that the CrazyEightsPlayer class works off of:

public abstract class Player{
  protected Hand hand;

  public Player(Hand hand){ this.hand = hand; }

  /* play a card from the player's hand */
  /* returns null if they cannot play a card and the deck becomes empty */
  /* do NOT return null unless you cannot play a card and the deck is empty 
  */
  public abstract Card play(Card top_of_discard_pile, Deck deck);

  public final int cardsLeft(){ return this.hand.numberOfCards(); }

 }



//This is my subclass CrazyEightsPlayer:

public class CrazyEightsPlayer extends Player {
  public CrazyEightsPlayer(Hand hand) {
    super(hand);
  }

  @Override
  public Card play(Card top_of_discard_pile, Deck deck) {
     while ((deck != null) && hand != null) {
        if ( ){

        } else {

        }
     }
     return ;
   }
 }


What I have tried:

I have just done a lot of typing and backspacing, inputting a bunch of different things to try to make it work. I am new to coding so I struggle a lot trying to come up with ways to make things work >.<
Please help!
Posted
Updated 30-Jul-18 10:05am
v4
Comments
CHill60 30-Jul-18 18:49pm    
Why have you removed the text of your question? You would have been better off just deleting your post. Now people will see your post and wonder what on earth the solution is about! Which means that member could end up with downvotes and possibly even reports of "Off Topic" or "Not an Answer" or "Unclear or Incomplete" or "Inaccurate / Misleading" ... all because you decided to delete the text of your question.
Just delete the post ... I see you are gathering downvotes anyway ... deleting your post will put an end to that

1 solution

Quote:
I have just done a lot of typing and backspacing, inputting a bunch of different things to try to make it work.
And there is your problem.

Stop typing.
No, seriously - stop typing. Start thinking instead.
Look at the rules, and work out what they require you to do when you play. Then get yourself a deck of cards and follow the rules manually. Write down - as a text description - what you need to actually do to follow the rules, what actions you need, what data you have to store.

When you are happy that you have that down pat, you will have a text description of the classes you need to implement to computerize the problem.

This is an analysis task, not a primarily coding task: Rule 9 makes that very, very clear. So analyse the problem instead of leaping into code!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900