Card Game
Card Game
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// initialize controller
public TBGTimer(TBGController controller)
{
this.controller = controller;
}
void startTimer()
{
gameTimer.start();
}
void stopTimer()
{
gameTimer.stop();
}
}
class TBGModel
{
// member data
static int NUM_CARDS_PER_HAND = 7;
static int NUM_PLAYERS = 2;
static int numPacksPerDeck = 1;
static int numJokersPerPack = 2;
static int numUnusedCardsPerPack = 0;
static Card[] unusedCardsPerPack = null;
class CardGameOutline
{
private static final int MAX_PLAYERS = 50;
// allocate
this.unusedCardsPerPack = new Card[numUnusedCardsPerPack];
this.hand = new Hand[numPlayers];
for (k = 0; k < numPlayers; k++)
this.hand[k] = new Hand();
deck = new Deck(numPacks);
// assign to members
this.numPacks = numPacks;
this.numJokersPerPack = numJokersPerPack;
this.numUnusedCardsPerPack = numUnusedCardsPerPack;
this.numPlayers = numPlayers;
this.numCardsPerHand = numCardsPerHand;
for (k = 0; k < numUnusedCardsPerPack; k++)
this.unusedCardsPerPack[k] = unusedCardsPerPack[k];
return hand[k];
}
// add jokers
for (k = 0; k < numPacks; k++)
for ( j = 0; j < numJokersPerPack; j++)
deck.addCard( new Card('X', Card.Suit.values()
[j]) );
// shuffle the cards
deck.shuffle();
}
enoughCards = true;
for (k = 0; k < numCardsPerHand && enoughCards ; k++)
{
for (j = 0; j < numPlayers; j++)
if (deck.getNumCards() > 0)
hand[j].takeCard( deck.dealCard() );
else
{
enoughCards = false;
break;
}
}
return enoughCards;
}
void sortHands()
{
int k;
return hand[playerIndex].takeCard(deck.dealCard());
}
}
// instance variables
private char value;
private Suit suit;
private boolean cardError;
// constructor
public Card(char value, Suit suit)
{
set(value, suit);
}
// accessors
public Suit getSuit()
{
return suit;
}
// equals method
public boolean equals(Card card)
{
return ((this.value == card.value) &&
(this.suit.equals(card.suit))
&& (this.getCardError() == card.getCardError()));
}
//Rank from A to X
public static char[] valueRanks()
{
return "A123456789TJQKX".toCharArray();
}
// default constructor
public Hand()
{
myCards = new Card[MAX_HAND_CARDS];
numCards = 0;
}
// accesor method
public int getNumCards()
{
return numCards;
}
numCards--;
for(int i = cardIndex; i < numCards; i++)
{
myCards[i] = myCards[i + 1];
}
myCards[numCards] = null;
return card;
}
}
// makes sure that there are not too many instances of a card
public boolean addCard(Card card)
{
if (topCard + 1 >= Hand.MAX_HAND_CARDS)
{
return false;
}
else
{
topCard++;
cards[topCard] = card;
return true;
}
}
// buttons, labels
static JButton[] humanButtons = new
JButton[TBGModel.NUM_CARDS_PER_HAND];
JButton stopButton = new JButton("Stop");
JButton startButton = new JButton("Start");
JButton cannotPlayButton = new JButton("I Cannot Play");
JLabel cardsLeftInDeckStat = new JLabel();
JLabel humanCannotPlayStat = new JLabel();
JLabel compCannotPlayStat = new JLabel();
// timer variables
static int counter = 0;
TBGTimer gameTimer;
JLabel timer;
// listeners
public void addStartListener(ActionListener startListener)
{
startButton.addActionListener(startListener);
}
public void addStopListener(ActionListener stopListener)
{
stopButton.addActionListener(stopListener);
}
// setting booleans
// cannoyPlayRequired booleans will be true unless proven
otherwise
humanCannotPlay = false;
cannotPlayRequired = true;
compCannotPlay = false;
compCannotPlayRequired = true;
if (cardToAdd != leftCard
&& (cardToAdd == leftCard + 1
|| cardToAdd == leftCard - 1))
{
leftCardsIndex++;
leftCards[leftCardsIndex] = new
TBGModel.Card(card.getValue(),
card.getSuit());
}
else if (cardToAdd != middleCard
&& (cardToAdd == middleCard + 1
|| cardToAdd == middleCard - 1))
{
middleCardsIndex++;
middleCards[middleCardsIndex] = new
TBGModel.Card(card.getValue(),
card.getSuit());
}
else if (cardToAdd != rightCard
&& (cardToAdd == rightCard + 1
|| cardToAdd == rightCard - 1))
{
rightCardsIndex++;
rightCards[rightCardsIndex] = new
TBGModel.Card(card.getValue(),
card.getSuit());
}
else
return false;
return true;
}
pnlComputerHand.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Computer Hand"));
add(pnlComputerHand, BorderLayout.NORTH);
pnlComputerHand.setBackground(lightOrange);
// accessors
public int getNumCardsPerHand()
{
return numCardsPerHand;
}
public int getNumPlayers()
{
return numPlayers;
}
class TBGController
{
TBGModel gameModel;
TBGView gameView;
TBGTimer gameTimer;
TBGModel.CardGameOutline game;
TBGView.CardTable cardTable;
this.gameView.addStartListener(new StartListener());
this.gameView.addStopListener(new StopListener());
this.gameView.addCannotPlayListener(new CannotPlayListener());
}