help needed for simple java game - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

help needed for simple java game

LesTerrisLesTerris Posts: 1Member
I am having trouble with a JAVA blackjack game I have been making for a school project and it completely stupifies me.
When I run it and it comes to the point were it should calculate your winnings or loses it comes up with completely redicules numbers.

I want to appolagies for the messy programming but I'm new to this.

so if someone could please tel me what is happening.

source code:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class BlackJack_1 extends Applet
{
int cardtype = 0;
int cardnum = 0;
int i = 0;
int dealscr = 0;
int playscr = 0;
int geld = 250;
int inzet = 0;
int money = 0;
String card, cardtype1;
Button deal, kaartK, dubbel, staan ;
Image[] kaart = new Image[14];
boolean dealbj, playbj, dubbelb, dubwin, dubver, win, ver;
TextField InzetVak;

public void init()
{
deal = new Button(" Deal ");
deal.addActionListener(new KnopHandler());
add( deal );
kaartK = new Button(" Kaart ");
kaartK.addActionListener(new KaartHandler());
add( kaartK );
dubbel = new Button(" Dubbel ");
dubbel.addActionListener(new dubbelHandler());
add( dubbel );
staan = new Button(" Staan ");
staan.addActionListener(new staanHandler());
add( staan );
InzetVak = new TextField( 10 );
add( InzetVak );
setBackground( new Color(0,128,0) );
}
public void paint( Graphics g )
{
g.drawString( " geld: " + geld, 50, 50);
g.drawString( " inzet: " + inzet, 50, 60);
g.drawString( " " + i, 50, 70);
g.drawString( " " + dealscr, 50, 80);
g.drawString( " " + playscr, 50, 90);
if(dealbj == true)
{
g.drawString( "Dealer Black Jack", 150, 80);
geld = geld - inzet;
DisableKnop();
// WinVerlies(inzet, geld);
}
else if(playbj == true)
{
g.drawString( "Player Black Jack", 150, 90);
inzet = inzet * 2;
geld = geld + inzet;
DisableKnop();
// WinVerlies(inzet, geld);
}
else if(dubwin == true)
{
g.drawString( "Player Dubbel Win", 150, 90);
inzet = inzet * 2;
geld = geld + inzet;
DisableKnop();
}
else if(dubver == true)
{
g.drawString( "Player Dubbel Verlies", 150, 90);
inzet = inzet * 2;
geld = geld - inzet;
DisableKnop();
}
else if(win == true)
{
g.drawString( "Player Win", 150, 90);
geld =+ inzet;
DisableKnop();
}
else if(ver == true)
{
g.drawString( "Player Verlies", 150, 90);
geld = geld- inzet;
DisableKnop();
}
g.drawImage( kaart[0], 100, 100, 71, 96, this);
g.drawImage( kaart[1], 172, 100, 71, 96, this);
g.drawImage( kaart[2], 100, 200, 71, 96, this);
g.drawImage( kaart[3], 172, 200, 71, 96, this);
g.drawImage( kaart[4], 244, 200, 71, 96, this);
g.drawImage( kaart[5], 316, 200, 71, 96, this);
g.drawImage( kaart[6], 388, 200, 71, 96, this);
g.drawImage( kaart[7], 460, 200, 71, 96, this);
g.drawImage( kaart[8], 532, 200, 71, 96, this);
g.drawImage( kaart[9], 244, 100, 71, 96, this);
g.drawImage( kaart[10], 316, 100, 71, 96, this);
g.drawImage( kaart[11], 388, 100, 71, 96, this);
g.drawImage( kaart[12], 460, 100, 71, 96, this);
g.drawImage( kaart[13], 532, 100, 71, 96, this);
g.drawString( "dealbj " + dealbj, 50, 350);
g.drawString( "playbj " + playbj, 50, 360);
g.drawString( "dubwin " + dubwin, 50, 370);
g.drawString( "dubver " + dubver, 50, 380);
g.drawString( "win " + win, 50, 390);
g.drawString( "ver " + ver, 50, 400);
g.drawString( "dubbelb" + dubbelb, 50, 410);
}

class KnopHandler implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
String inzetString = InzetVak.getText();
inzet = Integer.parseInt( inzetString );
LeegRij();
i = 0;
dealscr = 0;
playscr = 0;
WinCond();
for(int p=0; p<=3; p++)
{
cardtype = (int) ( 4 * Math.random() + 1);
cardnum = (int) ( 13 * Math.random() + 1);
cardtype1 = "";
switch(cardtype)
{
case 1: cardtype1 = "s";
break;
case 2: cardtype1 = "h";
break;
case 3: cardtype1 = "d";
break;
case 4: cardtype1 = "c";
break;
}
card = cardtype1 + "" + cardnum;
kaart[p] = getImage( getDocumentBase(), "cards_gif/"+ card + ".gif" );
i=4;
if(dealscr <= 10 && cardnum == 1 && p <= 1)
{
cardnum = 11;
}
else if(cardnum > 10)
{
cardnum = 10;
}
else if(playscr <= 10 && cardnum == 1 && p>=2)
{
cardnum = 11;
}
else if(cardnum > 10)
{
cardnum = 10;
}
if(p<=1)
{
dealscr = dealscr + cardnum;
}
else
{
playscr = playscr + cardnum;
}
if(dealscr == 21)
{
dealbj = true;
}
else if(playscr == 21)
{
playbj = true;
}
kaartK.setEnabled( true );
dubbel.setEnabled( true );
staan.setEnabled( true );
repaint();
}
}
}
class KaartHandler implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
dubbel.setEnabled( false );
if( playscr <= 21 )
{
PlayTurn(i);
i++;
}
}
}
class dubbelHandler implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
if( playscr <= 21 )
{
PlayTurn(i);
i++;
DisableKnop();
DealTurn(i);
dubbelb = true;
}
}
}
class staanHandler implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
DisableKnop();
DealTurn(i);
}
}
public void DisableKnop()
{
kaartK.setEnabled( false );
dubbel.setEnabled( false );
staan.setEnabled( false );
}
public void DealTurn( int i )
{
while( i < 9 )
{
kaart[i] = getImage( getDocumentBase(), "cards_gif/blank.gif" );
i++;
}
while( dealscr < playscr && dealscr < 21 && playscr <= 21)
{
cardtype = (int) ( 4 * Math.random() + 1);
cardnum = (int) ( 13 * Math.random() + 1);
cardtype1 = "";
switch(cardtype)
{
case 1: cardtype1 = "s";
break;
case 2: cardtype1 = "h";
break;
case 3: cardtype1 = "d";
break;
case 4: cardtype1 = "c";
break;
}
card = cardtype1 + "" + cardnum;
if(dealscr <= 10 && cardnum == 1 && i >= 9)
{
cardnum = 11;
}
else if(cardnum > 10)
{
cardnum = 10;
}
dealscr = dealscr + cardnum;
kaart[i] = getImage( getDocumentBase(), "cards_gif/"+ card + ".gif" );
i++;
repaint();
}
if( dubbelb == true )
{
if(dealscr < playscr && playscr <= 21)
{
dubwin = true;
}
else if(dealscr > 21 && playscr <= 21)
{
dubwin = true;
}
else
{
dubver = true;
}
}
else if( dubbelb == false )
{
if(dealscr < playscr && playscr <= 21)
{
win = true;
}
else if(dealscr > 21 && playscr <= 21)
{
win = true;
}
else
{
ver = true;
}
}
repaint();

}
public void PlayTurn( int i )
{
cardtype = (int) ( 4 * Math.random() + 1);
cardnum = (int) ( 13 * Math.random() + 1);
cardtype1 = "";
switch(cardtype)
{
case 1: cardtype1 = "s";
break;
case 2: cardtype1 = "h";
break;
case 3: cardtype1 = "d";
break;
case 4: cardtype1 = "c";
break;
}
card = cardtype1 + "" + cardnum;
if(playscr <= 10 && cardnum == 1 && i <=8)
{
cardnum = 11;
}
else if(cardnum > 10)
{
cardnum = 10;
}
playscr = playscr + cardnum;
kaart[i] = getImage( getDocumentBase(), "cards_gif/"+ card + ".gif" );
if( playscr >= 21 )
{
ver = true;
DisableKnop();
}
repaint();
}
public void LeegRij()
{
for(int q=0; q<=13; q++ )
{
kaart[q] = getImage( getDocumentBase(), "cards_gif/blank.gif" );
}

}
public void WinCond()
{
dealbj = false;
playbj = false;
dubwin = false;
dubver = false;
win = false;
ver = false;
dubbelb = false;
}
}

Comments

  • BodkinBodkin Posts: 100Member
    Your problem lies primarily in your paint-method.

    Try testing how many times the paint-method gets called (increase a dummy variable on entry and write it by the other text). You will discover that the paint-method sometimes gets called 100 times or more.

    The reason for this is the drawImage-method: the ImageObserver argument is notified of any changes to the image as the image gets loaded. As the ImageObserver in this case is your Applet it will call the paint-method each time.

    The solution is to preload all images before you use them.
    The ImageIcon class will see to it that the image gets loaded before you use it.

    Use something like this:
    [code]
    // In your INIT method

    URL url = getCodeBase();
    String sSuit[] = {"h","s","d","c"};
    try {
    for(int value=0; value<13; value++)
    for(int suit=0; suit<4; suit++)
    cardimages[value][suit] = new ImageIcon(new URL(url, "cards_gif/"+(sSuit[suit]+(value+1))+".gif"));
    }
    catch (Exception ex) {
    }

    // In you PAINT method
    Card card;
    int index;
    for(index=0; index<dealerHand.getCardCount(); index++) {
    card = dealerHand.getCard(index);
    cardimages[card.getValue()][card.getSuit()].paintIcon(this, g, 100+36*index, 158);
    }
    for(index=0; index<playerHand.getCardCount(); index++) {
    card = playerHand.getCard(index);
    cardimages[card.getValue()][card.getSuit()].paintIcon(this, g, 100+36*index, 258);
    }
    [/code]

    It is a waste of time to load the images everytime you need them. Load all the images once, and then reuse.

    Furthermore, it's almost a crime to let the paint-method to anything else than paint. You should seriously consider moving your changes to 'geld' outside the paint-method.
Sign In or Register to comment.