Docstoc

import java(8)

Document Sample
import java(8) Powered By Docstoc
					Blackjack
Quick Reference Guide
Note: 1. Not all code is complete 2. No code has been change, but may have been moved to fit the page

1

import java.util.Random; import javax.swing.*; public class Card { protected String face; protected ImageIcon cardpic; protected int value; protected String suit; public Card() { cardpic = null; value = 0; suit = null; face = null; } public Card(ImageIcon x, int val, String s, String f) { cardpic = x; value = val; face = f; suit = s; } public ImageIcon getimage() { return cardpic;} public int getvalue() { return value; } public void setvalue(int v) { value = v; } public String getsuit() { return suit; } public String getface() { return face; } public String toString() { return "Face: "+ face + " Suit"+ suit +" Value: "+ value; } }//end Card

2

import jss2.*; import jss2.exceptions.*; import java.util.Random; import javax.swing.*; import java.awt.*; public class Deck { ArraySet<Card> deckSet = new ArraySet<Card>(); /********************************************************** Constructs a deck of 52 Cards and puts them in the set. **********************************************************/ public Deck() { ImageIcon card49 = new ImageIcon("2s.jpg"); Card twos = new Card(card49, 2,"spade", "Two"); ImageIcon card45 = new ImageIcon("3s.jpg"); Card threes = new Card(card45, 3,"spade", "Three"); ImageIcon card1 = new ImageIcon("4s.jpg"); Card fours = new Card(card1, 4, "spade","Four"); ImageIcon card13 = new ImageIcon("5s.jpg"); Card fives = new Card(card13, 5, "spade", "Five"); //… deckSet.add(acec); deckSet.add(threeh); deckSet.add(aces); deckSet.add(jackh); deckSet.add(nineh); //… } /********************************************************** returns a single random Card from the deck **********************************************************/ public Card getCard() { Card result = new Card(); result = deckSet.removeRandom(); return result; } }//end deck
3

import jss2.*; import jss2.exceptions.*; import java.util.*; public class Hand { protected ArraySet<Card> inHand; protected int handvalue,count; public Hand() { inHand = new ArraySet<Card>(12); handvalue=0; count =0; } private void reduceHand(Card newCard) { if((handvalue) > 21) { if(aceInHand()) { handvalue -= 10; } } }//end reduceHand private boolean aceInHand() { boolean result = false; Card cardchk = null; Iterator<Card> scan = inHand.iterator(); while (scan.hasNext() && !result) { cardchk = scan.next(); if(cardchk.getvalue() == 11) { cardchk.setvalue(1); result = true; } } return result; } public Card newCard(Deck currentdeck) { Card result; result = currentdeck.getCard(); inHand.add(result); handvalue+=result.getvalue(); reduceHand(result); count++; return result; } public int getHandValue() { return handvalue; }

public Iterator<Card> iterator() { return inHand.iterator(); } public Card remove(Card crd) throws ElementNotFoundException { return(inHand.remove(crd)); } public String toString() { String result=""; Card cardstr = null; int i=0; Iterator<Card> scan = inHand.iterator(); while (scan.hasNext()) { cardstr= scan.next(); result += "card"+i+": "+cardstr.getvalue()+"\n"; i++; } return result; } }//end Hand

4

import jss2.exceptions.*; import java.util.*; public class Blackjack { Hand dealer; //to hold the dealer's cards Hand player; //to hold the player's cards Deck newdeck; //a set of cards public Blackjack(Hand dlr, Hand plr) { dealer = dlr; player = plr; newdeck = new Deck(); }//Blackjack constructor public void dealInitialCards() { dealer.newCard(newdeck); dealer.newCard(newdeck); player.newCard(newdeck); player.newCard(newdeck); }//end deal method public Card hit(Hand whohit) { Card result = whohit.newCard(newdeck); return result; }//end hit method public int handValue(Hand whohand) { int result = whohand.getHandValue(); return result; }// end handValue method public void discard(Hand whodis, Card discrd) throws ElementNotFoundException { Card card=null; boolean found = false; Iterator<Card> scan = whodis.iterator(); while (scan.hasNext() && !found) { card = scan.next(); if(discrd.equals(card)) { whodis.remove(card); found = true; } 5

} if(!found) throw new ElementNotFoundException("BlackJack"); }//end discard public boolean blackj() { boolean result = false; if(player.getHandValue() == 21) result = true; return result; }//end blackj public boolean bust(Hand whobust) { boolean result = false; if(whobust.getHandValue() > 21) result = true; return result; }//end bust public Hand dealerPlays() { Hand result = dealer; while(dealer.getHandValue() <= 16) { dealer.newCard(newdeck); } return result; }//end dealerPlays public String winner() { String result = ""; if((player.getHandValue() < dealer.getHandValue()) && dealer.getHandValue() <= 21 ) result = "Lose"; else if ((player.getHandValue() == dealer.getHandValue()) && dealer.getHandValue() <= 21 ) result = "Push"; else result = "Win"; return result; }//end winner }//end Blackjack 6


				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:28
posted:11/29/2009
language:English
pages:6
Jun Wang Jun Wang Dr
About Some of Those documents come from internet for research purpose,if you have the copyrights of one of them,tell me by mail vixychina@gmail.com.Thank you!