Member
Posts: 277
Joined: Sun Mar 21, 2010
Location: Mississauga, Onatario
Division: Bad Company 2
Help with Java?
The cards
The code
- Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.net.URL;
public class blackjack extends JFrame implements ActionListener{
/////////////////////////////////////////Declerations///////////////////////////////////////////////////////////////////
///////////////////////All variables and gui components are declared here//////////////////////////////////////////////
String []cards,suits;
int w=0,a=0,l=0,playercardsheld=0,dealercardsheld=0;
int [][]checklist1,playercards,dealercards;
String[]checklist2;
int playertotal=0,value=0,dealertotal=0,playerace=0,dealerace=0;
String getchar,turn=("Player");
String getcardchar;
int getcharlength=0,searchchar=0 ;
boolean dealerdone=false;
int[]playercardvalue,playercardsuit;
int[]dealercardvalue,dealercardsuit;
MyDrawPanel drawpanel1;
JPanel panel1;
Image cardImages;
JButton buttonstart,buttondeal,buttonstop,buttonreset;
public static void main(String[ ] args)
{
new blackjack ();
}
//////////////////////////////////Main Routine//////////////////////////////////////////////////////////////////////////
///////////////Where gui components and arrays are created/prepared////////////////////////////////////////////////////
public blackjack (){
panel1=new JPanel();
panel1.setLayout(new GridLayout(2,2,4,4));
this.setLayout(new BorderLayout());
cards=new String [13];
suits=new String [4];
checklist1=new int [13][4];
playercards=new int [13][4];
dealercards=new int [13][4];
checklist2=new String [52];
playercardvalue=new int[52];
playercardsuit=new int[52];
dealercardvalue=new int[52];
dealercardsuit=new int[52];
buttonstart=new JButton("Start");
buttonstart.addActionListener(this);
panel1.add(buttonstart);
buttondeal=new JButton("Hit");
buttondeal.addActionListener(this);
panel1.add(buttondeal);
buttonstop=new JButton("Hold");
buttonstop.addActionListener(this);
panel1.add(buttonstop);
buttonreset=new JButton("Reset");
buttonreset.addActionListener(this);
panel1.add(buttonreset);
for (int a=0;a<13;a++)
cards[a]=(""+a);
cards[0]=("Ace");
cards[1]=("Jack");
cards[11]=("Queen");
cards[12]=("King");
suits[0]=("Clubs");
suits[1]=("Diamonds");
suits[3]=("Spades");
suits[2]=("Hearts");
drawpanel1=new MyDrawPanel();
this.add(panel1,BorderLayout.SOUTH);
this.add(drawpanel1,BorderLayout.CENTER);
this.setSize(900,800);
this.setVisible(true);
buttonreset.setEnabled (false);
buttondeal.setEnabled (false);
buttonstop.setEnabled (false);
randomize();
}
////////////////////////////////////Buttons/////////////////////////////////////////////////////////////////////////////
///////////////////////Where all button clicks are stored//////////////////////////////////////////////////////////////
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==buttonstart)
{
buttonreset.setEnabled (true);
buttondeal.setEnabled (true);
buttonstop.setEnabled (true);
buttonstart.setEnabled (false);
if (dealercardsheld==0||playercardsheld==0)
{
for(int r=0;r<2;r++)
{
changer();
playercardsheld++;
playertotal=playertotal+value;
System.out.println(checklist2[l]);
l++;
turn=("Dealer");
changer();
dealercardsheld++;
dealertotal=dealertotal+value;
System.out.println(checklist2[l]);
l++;
turn=("Player");
}
System.out.println("Player Total: "+playertotal);
System.out.println("Dealer Total: "+dealertotal);
}
}
if (e.getSource()==buttondeal)
{
give();
}
if (e.getSource()==buttonreset)
{
buttonreset.setEnabled (false);
buttondeal.setEnabled (false);
buttonstop.setEnabled (false);
buttonstart.setEnabled (true);
reset();
}
if (e.getSource()==buttonstop)
{
turn=("Dealer");
give();
win();
}
}
/////////////////////////Change Value Code/////changer();///////////////////////////////////////////////////////////////
////////////////////Used to change character values to values//////////////////////////////////////////////////////////
public void changer() {
getchar=""+checklist2[l].charAt(0);
getcharlength=checklist2[l].length();
searchchar=getcharlength-2;
getcardchar=""+checklist2[l].charAt(searchchar);
if (turn.equals("Player"))
{
if (getcardchar.equals("b"))
{
playercardsuit[l]=0;
}
else if (getcardchar.equals("d"))
{
playercardsuit[l]=1;
}
else if (getcardchar.equals("e"))
{
playercardsuit[l]=3;
}
else if (getcardchar.equals("t"))
{
playercardsuit[l]=2;
}
if (getchar.equals("1"))
{
getchar=""+getchar+checklist2[l].charAt(1);
value=Integer.parseInt(getchar);
playercardvalue[l]=value;
}
else if (getchar.equals("J"))
{
value=10;
playercardvalue[l]=1;
}
else if (getchar.equals("Q"))
{
value=10;
playercardvalue[l]=11;
}
else if (getchar.equals("K"))
{
value=10;
playercardvalue[l]=12;
}
else if (getchar.equals("A"))
{
ace();
playercardvalue[l]=0;
}
else
value=Integer.parseInt(getchar);
}
else if (turn.equals("Dealer"))
{
if (getcardchar.equals("b"))
{
dealercardsuit[l]=0;
}
else if (getcardchar.equals("d"))
{
dealercardsuit[l]=1;
}
else if (getcardchar.equals("e"))
{
dealercardsuit[l]=2;
}
else if (getcardchar.equals("t"))
{
dealercardsuit[l]=3;
}
System.out.println(getcardchar);
if (getchar.equals("1"))
{
getchar=""+getchar+checklist2[l].charAt(1);
value=Integer.parseInt(getchar);
dealercardvalue[l]=value;
}
else if (getchar.equals("J"))
{
value=10;
dealercardvalue[l]=1;
}
else if (getchar.equals("Q"))
{
value=10;
dealercardvalue[l]=11;
}
else if (getchar.equals("K"))
{
value=10;
dealercardvalue[l]=12;
}
else if (getchar.equals("A"))
{
ace();
dealercardvalue[l]=0;
}
else
value=Integer.parseInt(getchar);
}
}
/////////////////////////Reset Code////////reset();/////////////////////////////////////////////////////////////////////
/////////////////Used to reset the game/round//////////////////////////////////////////////////////////////////////////
public void reset (){
if (playercardsheld==0&&playertotal==0&&checklist1[1][1]==0)
{
randomize();
}
else
{
for (int w=0;w<13;w++)
{
for (int a=0;a<4;a++)
{
checklist1[w][a]=0;
}
}
for (int p=0;p<52;p++)
{
checklist2[p]=(null);
}
}
playertotal=0;
dealertotal=0;
dealercardsheld=0;
playercardsheld=0;
playerace=0;
dealerace=0;
turn=("Player");
randomize();
}
////////////////////////////Shuffle cards////////randomize();///////////////////////////////////////////////////////////
//////////////Used to shuffle the cards and reset if necessary/////////////////////////////////////////////////////////
public void randomize(){
l=0;
if (checklist1[1][1]==1)
reset();
else
{
for (int p=0;p<52;p++)
{
do
{
w=(int)(Math.random()*13);
a=(int)(Math.random()*4);
}while(checklist1[w][a]==1);
{
checklist1[w][a]++;
checklist2[p]=(cards[w]+" "+suits[a]);
}
}
}
}
//////////////////////////////Deal Code/////////give();/////////////////////////////////////////////////////////////////
////////////////////Used to deal/hit the cards to players//////////////////////////////////////////////////////////////
public void give(){
if (turn.equals("Player"))
{
System.out.println(checklist2[l]);
playercardsheld++;
changer();
playercardvalue[l]=value;
playertotal=playertotal+value;
l++;
System.out.println("Player Total: "+playertotal);
}
else if (turn.equals("Dealer"))
{
while (dealertotal<17)
{
System.out.println(checklist2[l]);
dealercardsheld++;
changer();
dealertotal=dealertotal+value;
l++;
System.out.println("Dealer Total: "+dealertotal);
}
dealerdone=true;
}
bust();
}
///////////////////////////////Win Checker/////////win();///////////////////////////////////////////////////////////////
///////////////////////////Checks win conditions///////////////////////////////////////////////////////////////////////
public void win(){
if (playertotal<dealertotal&&dealertotal<22||dealertotal==playertotal&&dealertotal<22||dealercardsheld==5&&dealertotal<22)
{
System.out.println("Win for Dealer");
}
else if (dealertotal<playertotal&&playertotal<22&&dealerdone==true||playercardsheld==5&&playertotal<22&&dealerdone==true)
{
System.out.println("Win for Player");
}
}
/////////////////////////////Bust Checker/////////bust();///////////////////////////////////////////////////////////////
///////////////////////////Checks for busts////////////////////////////////////////////////////////////////////////////
public void bust(){
if (playertotal>21)
{
System.out.println("Player busts");
System.out.println("Dealer Wins");
}
else if (dealertotal>21)
{
System.out.println("dealer busts");
System.out.println("Player wins");
}
}
/////////////////////////////ACE Checker//////////ace();////////////////////////////////////////////////////////////////
//////////////////////Used to check the Ace value//////////////////////////////////////////////////////////////////////
public void ace()
{
if (turn.equals("Player"))
{
if (playertotal>10||playerace>1)
{
value=1;
playerace++;
}
else if (playertotal<11||playerace==0)
{
value=11;
playerace++;
}
}
else if (turn.equals("Dealer"))
{
if (dealertotal>10||dealerace>1)
{
value=1;
}
else if (dealertotal<11||dealerace==0)
{
value=11;
}
}
}
class MyDrawPanel extends JPanel
{
public void paintComponent(Graphics g) {
//Converts the ordinay graphics object into a Graphics2d object
Graphics2D g2 = (Graphics2D)g;
//This line takes away the jaggedness and smooths out the lines
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(new Color(255,0,0));
g2.fillRect (1,1,500,500);
g2.drawImage(cardImages, 1,1, 20+79, 20+123,0,0 , 79, 123, drawpanel1);
}
private void loadImage() {
ClassLoader cl = blackjack.class.getClassLoader();
URL imageURL = cl.getResource("cards.png");
if (imageURL != null)
cardImages = Toolkit.getDefaultToolkit().createImage(imageURL);
}
}
}
I'm going to ask my teacher but want a second opinion
- 0