PDA

View Full Version : need help improving Blackjack program


mia_tech
03-06-2009, 09:35 PM
I'm still trying to get this working for a project at school, but now I face two major constraint, and one is that instead of having the big portion of my program decision making code in the main, I would've like to have it in a different class like "Game" or "Blackjack" and implement methods like "hitme", "deal", "stay", but I didn't find a way to do it.... could anyone make a suggestion?... also I can't put the result of the
cardDeck.get((int)(Math.random()*52)) inside a int variable so I can keep the score... also need help here!
here's what I've got
Card class
public class Cards {
String suit = "";
String cardType = "";

public Cards(String suit, String cardType)
{
this.suit = suit;
this.cardType = cardType;
}
public String getSuit()
{
return suit;
}
public String getcardType()
{
return cardType;
}
@Override
public String toString()
{
return cardType+" of "+suit;
}

}

Main class

import java.util.*;
import java.util.Scanner;
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner myscan = new Scanner(System.in);
//making the deck of cards
ArrayList<Cards> cardDeck = new ArrayList<Cards>();
String[] suit = {"Spade", "Hearts", "Clubs", "Diamonds"};
String[] number = {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
for(int i = 0; i < suit.length; i++)
{
for(int j = 0; j < number.length; j++)
{
cardDeck.add(new Cards(suit[i], number[j]));
}
}
//Starting the Game!
System.out.println("*****************************");
System.out.println("**** WELCOME ****");
System.out.println("**** TO ****");
System.out.println("**** BLACKJACK ****");
System.out.println("*****************************");

//User Playing
int userTotal = 0;
int total = 0;
System.out.println("\n");
System.out.println("*****User Playing******");
System.out.println("Press enter to start!"+"\n");
myscan.nextLine();
for(int i = 0; i < 2; i++)
{
//deal two cards
System.out.println(cardDeck.get((int)(Math.random()*52)));
}

while(userTotal < 21)
{
System.out.println("\n"+"Press 1(hit) or 2(stay)");
int key = myscan.nextInt();
if( (key == 1) && (userTotal < 21))
{
//hit me
System.out.println(cardDeck.get((int)(Math.random()*52)));
userTotal += total;
if(userTotal > 21)//if user goes over 21, tell him "busted"
{
System.out.println("you are busted!");
break;
}
}
else
{
//user stay--> give user total
System.out.println("you stay, and have a total of: "+userTotal);
break;
}
}


//computer playing!
int computerTotal = 0;
System.out.println("****** Computer Playing *********");
for(int i = 0; i < 2; i++)
{
System.out.println(cardDeck.get((int)(Math.random()*52)));//deal two card for computer
}

while(computerTotal < 21)
{
System.out.println("press enter to deal another card!");
myscan.next();
System.out.println(cardDeck.get((int)(Math.random()*52)));//hit computer until makes 21 or busted
if(computerTotal > 21)
{
System.out.println("Computer busted!"+computerTotal);//comptuer busted
break;
}
else if(computerTotal == 21)
{
System.out.println("Comptuer Wins!");//comptuer wins 21
}
}


if((userTotal < 21) && (userTotal > computerTotal))
{
System.out.println("User Wins!");
}
else System.out.println("Comptuer Wins!");

}
}