Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Blackjack{
- public static void main (String[] arg) {
- Deck d = new Deck(); d.shuffle();
- Hand h = new Hand();
- Score s = new Score(); //added for bonus
- System.out.println(d);
- System.out.println(d.hit());
- System.out.println( d.decode(d.hit()) );
- h.add(5);
- h.add(10);
- System.out.println(h.display());
- while(true) {
- System.out.print("Enter (n)ew hand, (h)it, or (q)uit: ");
- Scanner scan = new Scanner (System.in);
- // these variables are for collecting the information for the Score
- int [] line = new int [5]; //added for Bonus
- int handScore = 0; //added for Bonus
- char c = scan.next().charAt(0);
- if (c == 'n' || c == 'N') {
- System.out.println(c);
- h.reset();
- if (d.currentIndex < 51){
- int card= d.hit(); h.add(card);
- card= d.hit(); h.add(card);
- h.display();
- System.out.println();
- } else if (d.currentIndex > 51) {
- System.out.println("--- NO CARDS LEFT ON DECK ---");
- System.out.println();
- break;
- } else {
- System.out.println("-- NOT ENOUGH CARDS LEFT ON DECK--");
- System.out.println();
- break;
- }
- } else if (c == 'h' || c == 'H') {
- System.out.println(c);
- if (d.currentIndex < 52){ //must be 52
- int handValue = h.scoreInHand() ;
- if (handValue > 21) { //already busted
- System.out.println("--- GET A NEW HAND --- ");
- System.out.println();
- } else if (handValue == 21) {//already blackjack
- System.out.println("--- BlackJack --- ");
- System.out.println();
- } else if (h.firstIndex > 4 ) {
- System.out.println( "-- ALREADY 5 CARDS IN HAND ---");
- h.display();
- System.out.println( "--- GET A NEW HAND ---");
- System.out.println();
- } else if (h.firstIndex == 0) {
- System.out.println("--- GET A NEW HAND FIRST ---");
- System.out.println();
- } else {
- int card= d.hit(); h.add(card);
- h.display(); System.out.println();
- }
- } else { // > or = 52
- System.out.println("----- NO CARDS LEFT ON DECK -----");
- System.out.println();
- break;
- }//end if (d.firstIndex < 52
- } else if (c == 'q' || c == 'Q') {
- System.out.println(c);
- //same as the if(c == n), so it can collect the last game.
- break;
- }
- } //while
- //prints the scores of the games the user played while the game was run.
- //System.out.print(s.display());System.out.println(); //added for Bonus
- System.out.println("BYE!");
- }//end main
- }
- class Deck{
- private int[] cards;
- public int currentIndex;
- Deck(){
- currentIndex=0;
- cards=new int[52];
- for (int i=0; i<cards.length; i++)
- cards[i]=i;
- }
- public String toString(){
- String s="";
- for (int i=currentIndex;i<cards.length; i++)
- {
- /* s=s+" "+cards[i];*/
- String code=decode(cards[i]);
- s=s+" "+code;
- }
- return s;
- }
- public String decode(int value)
- {
- String code=""+"HCDS".charAt (value/13)
- +"23456789TJQKA".charAt(value%13);
- return code;
- }
- public void shuffle(){
- int j=0;
- for (int i=0;i<cards.length; i++)
- {
- j=(int)(52*Math.random());
- swap(i,j);
- }
- }
- public void swap(int i, int j){
- int temp=cards[i];
- cards[i]=cards[j];
- cards[j]=temp;
- }
- public int hit(){
- int i=cards[currentIndex];
- currentIndex++;
- return i;
- }
- }
- class Hand{
- private int []Hand;
- public int firstIndex;
- Hand(){
- Hand=new int[5];
- for (int i=0; i<Hand.length; i++)
- Hand[i]=-1;
- firstIndex=0;
- }
- public String display(){
- String s="";
- String empty="Hand is empty";
- for (int i=0; i<firstIndex;i++)
- s=s+" "+decode(Hand[i]);
- if (s.length()==0)
- s= empty;
- String sih="";
- int sihand= scoreInHand();
- if(sihand>0&&sihand<21)
- sih=""+sihand;
- else if(sihand==21&&firstIndex==2)
- sih="blackjack";
- else if (sihand==21&&firstIndex>2)
- sih=""+sihand;
- else
- sih="BUST";
- System.out.println(s+"n"+sih);
- return s.trim();
- }
- public void reset(){
- for (int i=0; i<Hand.length;i++)
- Hand[i]=-1;
- firstIndex=0;
- }
- public void add(int card){
- Hand[firstIndex]=card;
- firstIndex++;
- }
- public String decode2(int value){
- String code=""+"HCDS".charAt(value/13)
- +"23456789TJQA".charAt(value%13);
- return code;
- }
- public String decode(int value){
- String[] pattern={"Heart","Clubs","Diamond","Spades"};
- String[] num={"two","three","four","five","six","seven","eight","nine","ten","jack","queen","king","ace"};
- return num[value%13]+ " of " + pattern[value/13];
- }
- public int scoreInHand(){
- int[] s={2,3,4,5,6,7,8,9,10,10,10,10};
- boolean firstAce=true;
- int value=0;
- int sum=0;
- for (int i=0; i<firstIndex; i++)
- {
- value=Hand[i]%13;
- if (value==12)
- {
- if (firstAce)
- {
- value=11;
- firstAce=false;
- }
- else
- value=1;
- }
- else
- value=s[value];
- sum=sum+value;
- }
- return sum;
- }
- public String toString(){
- String s="";
- for (int i=0; i<5; i++)
- {
- if (i<firstIndex)
- s=s+" | " + decode2(Hand[i])+" ";
- else
- s=s+" ";
- }
- s=s+firstIndex;
- return s;
- }
- }//(2,3,4,5,6,7,8,9,10,10,10,10,11)
- class Score extends Deck{
- private String [] cuts=new String [26];
- private int gfirstIndex;
- Score(){
- cuts=new String[26];
- gfirstIndex=0;
- }
- public void markScore(Hand h, int score)
- {
- String s="";
- char fi;
- String hand=h.toString();
- int firstIndex;
- fi=hand.charAt(hand.length()-1);
- firstIndex=fi-'0';
- hand=hand.substring(0,hand.length()-1);
- s=s+" | "+(gfirstIndex+1)+(((gfirstIndex+1)<10)?" ":" ");
- s=s+hand+" ";
- s=s+"(score="+score+")";
- if (score>21)
- s=s+"(BUSTS)";
- else if (score==21&&firstIndex==2)
- s=s+"(BLACKJACK)";
- cuts[gfirstIndex]=s;
- gfirstIndex++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement