Академический Документы
Профессиональный Документы
Культура Документы
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JOptionPane;
import com.sun.media.sound.ModelAbstractChannelMixer;
import jdk.internal.module.IllegalAccessLogger.Mode;
import java.util.*;
import java.text.*;
public ClockTimer()
{
setLayout(new BorderLayout());
timerButton.addActionListener(this);
timerPanel.add(timerButton);
add(timerPanel, "South");
public GameModel()
{
this.highCardGame = null;
this.computer = "";
this.human = "";
cardSelected = new ButtonGroup();
}
// deal cards
public void dealCards()
{
this.highCardGame.deal();
}
if (getLeftCard() == null)
{
setLeftCard(highCardGame.getCardFromDeck());
}
if (getRightCard() == null)
{
setRightCard(highCardGame.getCardFromDeck());
}
table.add(new JButton(GUICard.getIcon(getLeftCard())));
table.add(new JButton(GUICard.getIcon(getRightCard()))); //add 2 cards to
table
refreshButtons(player);
}
JOptionPane.showMessageDialog(null,
message, "Game Over",
JOptionPane.INFORMATION_MESSAGE);
}
}
//END class GameModel
int k;
//GUI buttons
public JButton
quit, //quit the game
cannotPlay; //when no card can be played
//games messages
public JTextArea message;
this.timer = timer;
cantPlayPress = false;
//computer panel
pnlComputerHand = new JPanel(new GridLayout(1,7));
pnlComputerHand.setBorder(BorderFactory.createTitledBorder("Computer Hand"));
//time panel
GridBagConstraints gbc = new GridBagConstraints();
//game panel
pnlGame = new JPanel(new GridBagLayout());
pnlGame.setBorder(BorderFactory.createTitledBorder("Game Panel"));
public GameControl()
{
model = new GameModel();
view = new GameView(new ClockTimer());
model.dealCards();
model.setTable(view.pnlComputerHand, view.pnlHumanHand, view.pnlPlayArea);
view.addQuitListener(new QuitListener());
view.addCannotPlayListener(new CantPlayListener());
view.setVisible();
}
view.pnlPlayArea.removeAll();
view.pnlHumanHand.removeAll();
view.pnlComputerHand.removeAll();
model.computerPlay(view);
model.setTable(view.pnlComputerHand, view.pnlHumanHand, view.pnlPlayArea);
view.addButtonListener(new ButtonListener());
}
}// END ButtonListener
}
//END class GameControl
/*****************************************************************************
* BELOW THIS LINE IS EXISTING CODE FROM MODULE 5:
* classes Card, Hand, Deck, GUICard and CardGameFramework
*****************************************************************************/
//START constructors
//Default constructor
public Card()
{
this.set('A', Suit.spades);
}
//Constructor
public Card(char value, Suit suit)
{
this.set(value, suit);
}
//END constructors
//START mutators
public boolean set(char value, Suit suit)
{
if(isValid(value, suit))
{
this.value = value;
this.suit = suit;
this.errorFlag = false;
}
else
{
this.errorFlag = true;
}
return this.errorFlag;
}
//END mutators
//START accessors
public Suit getSuit()
{
return this.suit;
}
/*
* public boolean equals(Card card) returns true if all the fields are
* identical and false otherwise.
*/
public boolean equals(Card card)
{
boolean isEqual = false;
/*
* private boolean isValid(char value, Suit suit) returns true if value is a
* valid card value and false otherwise.
*/
private boolean isValid(char value, Suit suit)
{
boolean isValid = false;
/*
* toString() concatenates the A thru 2 card value and the card suit into a
* single string "value of suit" example: "A of clubs"
*/
public String toString()
{
if(errorFlag == true)
{
return "[invalid]";
}
else
{
String card = Character.toUpperCase(value) + " of " + suit;
return card;
}
}
//START constructors
//Default constructor
public Hand()
{
this.myCards = new Card[MAX_CARDS];
this.numCards = 0;
}
//END constructors
/*
* public boolean takeCard(Card card) puts a new card in the players hand
* and also checks that taking a new card would not violate the MAX_CARDS
* allowed in the hand, which is set to MAX_CARDS = 56 by the Hand constructor.
*/
public boolean takeCard(Card card)
{
boolean newCard = false;
//checking if hand size plus card drawn will put us over max size
if (numCards + 1 <= MAX_CARDS)
{
myCards[numCards] = new Card(card.getValue(), card.getSuit());
this.numCards++;
newCard = true;
}
return newCard;
}
/*
* public Card playCard() returns and removes the card in the top occupied
* position of the hand array.
*/
public Card playCard(int cardIndex)
{
if ( numCards == 0 ) //error
{
//Creates a card that does not work
return new Card('M', Card.Suit.spades);
}
//Decreases numCards.
Card card = myCards[cardIndex];
numCards--;
for(int i = cardIndex; i < numCards; i++)
{
myCards[i] = myCards[i+1];
}
myCards[numCards] = null;
return card;
}
if (this.numCards > 0)
{
for (Card card:myCards)
{
if (card == null)
{
break;
}
hand.append(card.toString());
if(handCards < this.numCards - 1)
{
hand.append(", ");
}
cardCounter++;
handCards++;
//START constructors
//Default constructor
public Deck()
{
allocateMasterPack();
init(1);
}
//Constructor
public Deck(int numPacks)
{
allocateMasterPack();
init(numPacks);
}
/*
* shuffle method
* randomizes indices of existing cards in deck
*/
public void shuffle()
{
Random shuffle = new Random();
/*
* dealCard returns a card while topCard is not negative, otherwise return
* null
*/
public Card dealCard()
{
if (topCard != -1) //since a card is stored at 0, deck is empty at -1
return cards[topCard--];
return null;
}
/*
* allocateMasterPack generates proper card values for the pack
*/
private static void allocateMasterPack()
{
int k = 0; //for deck array number
if (masterPack[0] != null)
return;
int count = 0;
Card.arraySort(this.cards, this.getNumCards());
}
// turns 0 - 3 into c, d, h .s
static String turnIntIntoCardSuit(int j)
{
return Card.Suit.values()[j].toString();
}
}
//END class GUICard
// one of many ways to assure at least one full deal to all players
if (numCardsPerHand < 1 ||
numCardsPerHand > numPacks * (52 - numUnusedCardsPerPack)
/ numPlayers )
numCardsPerHand = numPacks * (52 - numUnusedCardsPerPack) / numPlayers;
// 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]) );
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());
}
}
//END class CardGameFramework ---------------------------------------------------