Title: Generic Cards Partial Solution
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Generic Cards

Here is a partial solution to the generic cards problem. It has lots of places with Stars where more work is needed. The following was sketched out without any compilation .... so expect some compile errors.

Card.java


abstract class Card implements Comparable
{
    String [] rank_name={"Two", "Three", "Four", "Five", "Six", "Seven",
               "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};

    String [] suit_name={"Clubs", "Diamonds", "Hearts", "Spades"};
    private int suit;
    private int rank;

    public int compareTo(Object obj)
    {
        Card played = (Card)obj;
        int order = suit - played.getSuit() ;
        if (order != 0) return order;
        order = rank - played.getRank();
        return order;
    }

    // If the Card we are in wins against Card c then return true, else false
    abstract boolean winningCard (Card c);

    Card(int suit, int rank)
    {
        this.suit = suit;
        this.rank = rank;
    }
    int getSuit()
    {
        return suit;
    }
    int getRank()
    {
        return rank;
    }
    public String toString()
    {
        return rank_name[rank] + " of " + suit_name[suit];
    }
 }



CardFactory.java


interface CardFactory
{
    Card create(int suit, int rank);
}


Deck.java


class Deck
{
    Card[] cards = new Card[52];
    int count=0;

    Deck(CardFactory cf)
    {
// See what happens if you try to replace cf.create with new Card
        for (int i=0; i < 4; i ++)
            for (int j=0; j< 13; j++)
                cards[count++] = cf.create(i,j);
        shuffle();
    }


    void shuffle()
    {
        //*********  Fill in logic *******
    }

    Card getNextCard()
    {
        //***********  Fill in logic *************
        return null;
    }

    Card[] getHand(int numCards)
    {
        //**********   Fill in logic *************
        return null;
    }
}




Game.java


import tools.*;
import java.util.*;

abstract class Game
{
    Card [] computerHand;
    Card [] userHand;
    Deck deck ;
    int handSize;
    MyInput in = new MyInput();

    // if c == null, then computer leads
    // if c!= null, c is the card played by the user,
    // The Card returned, is the computer decides to play.
    abstract Card computerCardPlay(Card c);


    Game(int handSize, CardFactory cf)
    {
        this.handSize = handSize;
        deck = new Deck(cf);
        computerHand = deck.getHand(handSize);
        userHand = deck.getHand(handSize);
        Arrays.sort(computerHand);
        Arrays.sort(userHand);
    }
    boolean playGame(boolean userGoesFirst)
    {
        int numUserWins = 0;
        int numComputerWins = 0;

        for (int i=0; i < handSize; i++)
        {

           if (userGoesFirst)
           {
              Card userCard = userCardPlay(null);
              Card computerCard = computerCardPlay(userCard);
              if (userCard.winningCard(computerCard))
                numUserWins += 1;
              else
              {
                userGoesFirst = false;
                numComputerWins += 1;
              }
           }
           else
           {
                Card computerCard = computerCardPlay(null);
                Card userCard = userCardPlay(computerCard);
                if (computerCard.winningCard(userCard))
                    numComputerWins += 1;
                else
                {
                    numUserWins +=1 ;
                    userGoesFirst = true;
                }
           }
        }
        if (numComputerWins > numUserWins) return false; // Lose
        return true;  // You win
    }


    void displayUserHand()
    {
        System.out.println(" ******  Your Hand Contains ******");

        for (int i=0; i < handSize; i++)
            if (userHand[i] != null)
                System.out.println("Card("+i+")=" + userHand[i]);
    }

    Card userCardPlay(Card c)
    {
        Card retCard = null;

        if (c == null)
            System.out.println("You lead ...");
        else
            System.out.println("Computer leads with: " + c);

        displayUserHand();
        for (;;)
        {
            System.out.print(" Enter the card number you select: ");
            int cardNum = in.getInt();
            if (cardNum < 0 || cardNum >=  handSize)
            {
                System.out.println("Card Number out of bounds ");
                continue;
            }
            retCard = userHand[cardNum];
            userHand[cardNum] = null;
            if (retCard != null) break;
            else System.out.println("Card already selected ");
         }
         return retCard;
    }
}


// Modified: March 8, 2004 10:16 AM