SWEN221 Assignment 4- The Card Game Solved

30.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: zip solution files instantly, after Payment

Securely Powered by: Secure Checkout

Description

Rate this product

The Card Game

The CardGame system is a simple card game written in Java. The game implements some variations on the well-known trick-taking card game. For more on this style of game, see this:

http://en.wikipedia.org/wiki/Trick-taking_game

There are four players (North, East, South and West) who are dealt exactly 13 cards each (i.e. the whole deck). The game then proceeds in a series of tricks. In each trick the leader lays a card, and then the next player (following clockwise rotation) plays a card, and so on until four cards have been played. The following illustrates:

Here, we see that North and East have played and, hence, South is next to play. Since North lead with a Heart, and South has a Heart, then he/she must play one of the available hearts.

If a player has a card of the same suit as that played by the leader, then he/she must follow suit. The winner of a trick is the player who played the highest card in the same suit as the leader. The winner of the game is the player who, after every card is played, has won the most tricks.

Every round either has a single suit of trumps or has no trumps. The sequence of trumps is Hearts, Clubs, Diamonds, Spades, No Trumps and this is repeated for the duration. The current suit of trumps (if applicable) is always the highest suit with respect to the ordering of cards.

Part 1: Card Comparisons

The CardGame system is almost fully functioning! To start off, you should implement the methods

Card.equals(), Card.hashCode() and Card.compareTo. The latter method should sort cards by their suit and rank, such that Hearts < Clubs < Diamonds < Spades. In other words, any card in hearts is always less than any card in clubs, etc. However, the 6 of hearts is greater than the 2 of hearts. For picture cards we have that: Ace > King > Queen > Jack > 10.

There are several JUnit tests provided with the CardGame system for this part (testCardEquals(), testCardNotEquals() and testCardCompareTo()). You should ensure that all of these tests now pass correctly. You should also find that, having implemented the required classes and methods, you can now play the game by running the method cards.Main.main(), and choose all human players. If the Card.compareTo() method is implemented correctly, the hand of each player should be sorted by suit in increasing order, starting with hearts.

Part 2: Illegal Moves

This part is concerned with the method Trick.play(Player,Card). When a player plays a card, this method is called to update the current trick being played. Unfortunately, it is possible for players to play invalid cards (e.g. cards not present in their hand, or not following suit), or to try and play out of sequence. This happens when a human player does something out of sequence.

You should implement the method Trick.play(Player,Card) to ensure that any attempt to play an invalid card, or to play out of sequence results in an IllegalMove exception being raised.

There are several JUnit tests provided with the CardGame system for this part (testInvalidPlay 1(), …, testInvalidPlay 5(). You should ensure that all of these tests now pass correctly. You should also find that, when playing the game, trying to play an invalid card does not work (and, instead, an error is reported on the status bar).

Part 3: Cloning

This part is concerned with the method CardGame.clone() and its implementation. The “duplicate” button in the Graphical User Interface employs this method to duplicate the current game:

Unfortunately, this method is not currently implemented correctly. In particular, this method is implemented in AbstractCardGame using a shallow clone. However, in order to properly duplicate a game a deep clone is required. Therefore, you need to replace and/or override the method in AbstractCardGame with appropriate implementations in its subclass(es).

There are several JUnit tests provided with the CardGame system for this part (i.e. testValidClone 1(), …, testValidClone 5()). You should ensure that all of these tests now pass correctly.

Part 4: Artificial Intelligence

This part is concerned with the class SimpleComputerPlayer, which is currently mostly unimplemented. This player chooses what card to play based on the following rules:

  • If the AI player can potentially win the trick, then it plays the highest eligible card.
  • If the AI player cannot win the trick, then it discards the lowest eligible card.
  • In the special case that the AI player must win the trick, then it conservatively plays the least card needed to win.

An important concept for understanding these rules is the ordering of eligible cards. A card is eligible if it may be played according to the rules of the game (e.g. if the AI player can follow suit then it must, etc). The highest eligible card is then the card with the highest rank and suit, where the current suit of trumps (if applicable) is always the highest suit. In the case of two equally ranked cards of non-trump suit, then the underlying ordering implied by Card.compareTo() (as discussed above) should be used to chose.

There are several JUnit tests provided with the CardGame system for this part (testSimpleAI 1(), …, testSimpleAI 19()). You should ensure that all of these tests now pass correctly. You should also find that you’re now able to play the game against computer players.

  • SwenA4-3s1rpn.zip