Documente Academic
Documente Profesional
Documente Cultură
java 7/31/2018
import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
///* Timer
======================================================================
class Assig6
{
//Added here so it could be used in a function and modified directly.
//In func placeNewCards()
public static Model buildGame;
public static Thread mainThread = Thread.currentThread();
public static void main(String[] args)
{
Timer timer = new Timer();
Random randomizer = new Random();
Assig6.buildGame = new Model();
ViewPlusController myView = new ViewPlusController("BUILD");
int input = -2;
Clickable.type inputType;
buildGame.framework.deal();
//System.out.print(buildGame.framework.getHand(0));
myView.update(buildGame);
while (true)
{
//Get inputs
Card playedCard = null;
Card stackCard = null;
Boolean passed = false;
int stackIndex = -1;
int cardIndex = -1;
;
while (true)
{
getInput();
input = Clickable.getInputValue();
inputType = Clickable.getInputType();
if (inputType == Clickable.type.PASS)
{
System.out.print("Cannot play pressed by human\n");
//Index 1 refers to player, index 0 is computer
buildGame.cannotPlayCount[1] += 1;
passed = true;
buildGame.consecutivePasses++;
break;
}
else if(inputType == Clickable.type.HAND)
{
//System.out.printf("Hand input: %d\n", input);
//Set card value
cardIndex = input;
playedCard = buildGame.framework.getHand(1).inspectCard(input);
}
else if(inputType == Clickable.type.STACK)
{
//System.out.printf("Stack input: %d\n", input);
//Set stack value
stackIndex = input;
stackCard = buildGame.stack[input];
}
1
Assig6.java 7/31/2018
//Use input
if (passed == false)
{
//Card is played, play the card and update the model accordingly.
System.out.println("Played card "+playedCard.toString());
buildGame.framework.getHand(1).playCard(cardIndex);
buildGame.stack[stackIndex] = playedCard;
//Draw card for player
if (buildGame.framework.takeCard(1) == false)
{
//Card draw failed.
}
buildGame.consecutivePasses = 0;
}
else
{
//Player passed, check if computer passed too
if (buildGame.consecutivePasses >= 2)
{
placeNewCards();
}
}
2
Assig6.java 7/31/2018
validValues[2] = Card.valuRanks[(((valueIndex-1)%13)+13)%13];
validValues[3] = Card.valuRanks[(valueIndex+1)%13];
if (maxPlayableIndexes > 0)
{
int randomInt = randomizer.nextInt(maxPlayableIndexes);
int selectedIndex = playableIndexes[randomInt];
int selectedStack = playableToStack[randomInt];
3
Assig6.java 7/31/2018
Card computerPlayedCard =
buildGame.framework.getHand(0).playCard(selectedIndex);
buildGame.stack[selectedStack] = computerPlayedCard;
System.out.println("Computer played "+computerPlayedCard.toString
());
if (buildGame.framework.takeCard(0) == false)
{
//Card draw failed.
}
}
else
{
//No playable moves for computer, must pass
buildGame.consecutivePasses++;
buildGame.cannotPlayCount[0]++;
//If other player also passed, set down new cards if possible
if (buildGame.consecutivePasses >= 2)
{
placeNewCards();
}
}
//Update view
myView.update(buildGame);
//if buildGame.break;
}
buildGame.gameOver = true;
System.out.print("Game ended\n");
System.out.printf("Player passes: %d\n", buildGame.cannotPlayCount[1]);
System.out.printf("Computer passes: %d\n", buildGame.cannotPlayCount[0])
;
myView.update(buildGame);
return;
}
4
Assig6.java 7/31/2018
5
Assig6.java 7/31/2018
t1=0;
remainingDuration -= (t1-t0)/1000000;
sleepDuration = Long.MAX_VALUE;
}
else
{
System.out.println("Resuming");
//Sleep resumed
sleepDuration = remainingDuration;
}
}
}
public void run()
{
System.out.print("Running\n");
timerLabel.setFont(textSize);
timePanel.add(timerLabel);
timerLabel.setText("Time elapsed: " + timeElasped);
while(true)
{
sleepSuccess = true;
t0 = System.nanoTime();
System.out.printf("Attempting to sleep for %d\n", sleepDuration);
6
Assig6.java 7/31/2018
doNothing(sleepDuration);
}
}
//
==============================================================================
*/
7
Assig6.java 7/31/2018
if (enabled)
{
returnedIndex = this.returnVal;
returnedType = this.returnType;
Assig6.mainThread.interrupt();
}
}
setTitle(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout (3, 1));
pnlHumanHand.setBorder(BorderFactory.createTitledBorder("Player"));
pnlComputerHand.setBorder(BorderFactory.createTitledBorder("Computer"));
pnlPlayArea.setLayout(new GridLayout(1,3));
labelStackA.setBorder(BorderFactory.createTitledBorder("Stack A"));
labelStackB.setBorder(BorderFactory.createTitledBorder("Stack B"));
labelStackA.setHorizontalAlignment(JLabel.CENTER);
labelStackA.setIcon(GUICard.getOutlineCardIcon());
labelStackA.addMouseListener(new Clickable(0,Clickable.type.STACK));
labelStackB.setHorizontalAlignment(JLabel.CENTER);
labelStackB.setIcon(GUICard.getOutlineCardIcon());
labelStackB.addMouseListener(new Clickable(1,Clickable.type.STACK));
pnlPlayArea.add(labelStackA);
pnlPlayArea.add(labelStackB);
add(pnlComputerHand);
add(pnlPlayArea);
add(pnlHumanHand);
this.setVisible(true);
}
8
Assig6.java 7/31/2018
{
int computerCards = m.framework.getHand(0).getNumCards();
int humanCards = m.framework.getHand(1).getNumCards();
pnlComputerHand.setLayout(new GridLayout(1, computerCards));
pnlHumanHand.setLayout(new GridLayout(1, humanCards));
JLabel tempLabel;
Card tempCard;
pnlHumanHand.removeAll();
for (int i = 0; i < humanCards; i++)
{
tempLabel = new JLabel();
tempCard = m.framework.getHand(1).inspectCard(i);
tempLabel.addMouseListener(new Clickable(i,Clickable.type.HAND));
tempLabel.setHorizontalAlignment(JLabel.CENTER);
pnlHumanHand.add(tempLabel);
tempLabel.setIcon(GUICard.getIcon(tempCard));
}
tempCard = m.stack[0];
if (tempCard != null)
{
labelStackA.setIcon(GUICard.getIcon(tempCard));
}
tempCard = m.stack[1];
if (tempCard != null)
{
labelStackB.setIcon(GUICard.getIcon(tempCard));
}
if (m.gameOver)
{
String winner = "Nobody";
if (m.cannotPlayCount[0] < m.cannotPlayCount[1])
{
winner = "Computer";
}
else if (m.cannotPlayCount[0] > m.cannotPlayCount[1])
{
winner = "Player";
}
String endMessage = "<html>";
9
Assig6.java 7/31/2018
pnlPlayArea.removeAll();
pnlPlayArea.add(new JLabel(endMessage));
pnlPlayArea.setLayout(new FlowLayout(FlowLayout.CENTER));
pnlPlayArea.updateUI();
}
System.out.print("Update Complete\n");
//Add and update labels based off model.
}
}
class Model
{
public CardGameFramework framework;
public int[] cannotPlayCount;
public int consecutivePasses = 0;
public Card stack[];
public boolean gameOver = false;
Model()
{
int numPlayers = 2;
int numPacks = 1;
int numJokersPerPack = 0;
int numUnusedCardsPerPack = 4;
int numCardsPerHand = 5;
Card[] unusedCardsPerPack = {new Card('X', Card.Suit.DIAMONDS),
new Card('X', Card.Suit.CLUBS),
new Card('X', Card.Suit.HEARTS),
new Card('X', Card.Suit.SPADES)};
10
Assig6.java 7/31/2018
// 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];
}
11
Assig6.java 7/31/2018
// 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;
12
Assig6.java 7/31/2018
return hand[playerIndex].takeCard(deck.dealCard());
}
class GUICard
{
private static Icon[][] iconCards = new ImageIcon[14][4]; // 14 = A thru
K + joker
private static Icon iconBack;
private static Icon iconOutline;
static boolean iconsLoaded = false;
13
Assig6.java 7/31/2018
{
case 0:
return "2";
case 1:
return "3";
case 2:
return "4";
case 3:
return "5";
case 4:
return "6";
case 5:
return "7";
case 6:
return "8";
case 7:
return "9";
case 8:
return "T";
case 9:
return "A";
case 10:
return "K";
case 11:
return "Q";
case 12:
return "J";
case 13:
return "X";
default:
return "0";
}
}
14
Assig6.java 7/31/2018
}
static int valueAsInt(Card card)
{
char k = card.getValue();
switch (k)
{
case '2':
return 0;
case '3':
return 1;
case '4':
return 2;
case '5':
return 3;
case '6':
return 4;
case '7':
return 5;
case '8':
return 6;
case '9':
return 7;
case 'T':
return 8;
case 'A':
return 9;
case 'K':
return 10;
case 'Q':
return 11;
case 'J':
return 12;
case 'X':
return 13;
default:
return -1;
}
15
Assig6.java 7/31/2018
iconsLoaded = true;
}
return iconBack;
}
class Card
{
enum Suit {CLUBS, DIAMONDS, HEARTS, SPADES};
private char value;
private Suit suit;
private boolean errorFlag;
public static char[] valuRanks = new char[]{'2', '3', '4', '5', '6', '7',
'8', '9', 'T', 'J', 'Q', 'K', 'A', 'X'};
16
Assig6.java 7/31/2018
17
Assig6.java 7/31/2018
else if (b == valuRanks[i])
{
return b;
}
}
return b;
}
}
class Hand
{
public static final int MAX_CARDS = 52;
18
Assig6.java 7/31/2018
{
numCards = 0; //intialize the numCards variable to Zero.
}
return true;
}
else
{
return false;
}
}
public Card playCard(int i)//Removes the card at the index and returns it.
{
if (i >= 0 && i < myCards.length)
{
Card card = myCards[i];
Card selectCard = new Card();
selectCard.setValue(card.getValue(), card.getSuit());
myCards[i] = myCards[numCards - 1];
myCards[numCards - 1] = null;
numCards = numCards - 1;
return selectCard;
}
return null;
}
19
Assig6.java 7/31/2018
}
}
else
{
str = "You have an empty hand.";
}
return str;
}
class Deck
{
//public data members
public final static int MAX_CARDS = 336; //MAX_PACK equal 6 decks
20
Assig6.java 7/31/2018
21
Assig6.java 7/31/2018
{
int r = rand.nextInt(x);
//replace cards incrementally randomly, it's possible for cards to
randomize more than once
Card temp = cards[r];
cards[r] = cards[i];
cards[i] = temp;
}
}
//Card dealCard() - returns and removes the card in the top occupied
position of cards[].
//An accessor for the int topCard (no mutator).
public Card dealCard()
{
if (topCard >= 0)
{ //if there are still cards in deck
Card dealed = inspectCard(topCard);
cards[topCard] = null;
topCard = topCard - 1;
return dealed;
}
else
return new Card('0', Card.Suit.SPADES); //invalid card response from
Card Class
}
if (cardInDeck)
{
return false;
}
else
{
22
Assig6.java 7/31/2018
topCard = topCard++;
cards[topCard] = new Card(card.getValue(), card.getSuit());
return true;
}
}
if (cardInDeck)
return true;
else
return false;
}
23