# Need help with my code.

• 07-12-2007, 03:18 PM
stormviper
Need help with my code.
Well, its a blackjack game, a very simple one i made for my college assignment, but im having several problems on adding and atm, im having such a headache with it. The game doesnt loopback to the start and I dont know if my "logical" thinking is good enough for the computer though.

Ive done the basics of it, so if anyone could help me "add on" to it, your help is greatly appreciated. If its not too much trouble, could you also make it crash proof.

These are the leftover requirements that i cant figure out yet on how to do.

1)The computer must be able to deal as good as you can. In other words, it will try its best to reach to 21. Implement this “thinking” logic.

2) There is a initial cash balance of 500 for each player, when the game starts.

3) Either players(i.e. you or the computer) can place a bet for each deal, and the program ends when a player has won the game. (remember to display the outcome of the game)

4) A way to repeat the game process till either player or computer loses all his cash.

Code:
Quote:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BlackJack {
public static void main(String[] args) {
int total[] = new int[3];
boolean valid = true;

int Player1 = 9+(int)(Math.random()*2);
int Player2 =6+(int)(Math.random()*5);
total[0] = Player1 + Player2;

int number = Integer.parseInt(JOptionPane.showInputDialog("Ente r the number of player : Maximum 2 players"));

if (number <= 2){

for(int y = 1 ;y<= number ;y++){
System.out.println("Player :"+y);

int random = 1 +(int)(Math.random()*11);
int random2 = 1 +(int)(Math.random()*11);

System.out.println("1 :"+random);
System.out.println("2 :"+random2);

total[y] = random + random2;

if ( total[y] != 22){
System.out.println(total[y]);

do{
int x = GetInput();
if ( x == JOptionPane.YES_OPTION){
valid = false ;
int random3 = 1 +(int)(Math.random()*11);
total[y] = total[y] + random3;
System.out.println(total[y]);
}

else {

valid =true ;
}
}while (valid == false);

}
else if(total[y] == 22){
total[y] = total[y] - 1;
System.out.println(total[y]+"BLACKJACK");
}
}
}

for(int d = 1;d<=number;d++){
if( total[d] > 21){

total[d]=total[d] - total[d];
}
}

if ((total[0] > total[1] ) && (total[0] > total[2] )){

System.out.println("Computer Has Won!! : " + total[0] );
}
else if (( total[1] > total[0] ) && (total[1] > total[2]) ){

System.out.println("Player 1 Has Won!! : " + total[1] );
}
else if (( total[2] > total[1] ) && (total[2] > total[0] )){

System.out.println("Player 2 Has Won!! : " + total[2] );
}
else if ( (total[2] == total[1] ) && (total[1] == total[0] ) && (total[2] == total[0] )) {

System.out.println("Draw!!! " );
}
else if ( total[2] == total[1] ){

System.out.println("Draw!!! " );
}
else if ( total[2] == total[0] ){

System.out.println("Draw!!! " );
}
else if ( total[1] == total[0] ){

System.out.println("Draw!!! " );
}

System.out.println("pc1 :"+Player1);
System.out.println("pc2 :"+Player2);
System.out.println(total[0]);

}

public static int GetInput(){

return JOptionPane.showConfirmDialog(null,"would u like to deal again?","Choose",JOptionPane.YES_NO_OPTION);
}
}
• 11-18-2013, 12:06 PM
dd09wrx
Re: Need help with my code.
Create a game class with a constructor and have all the games logic within that so you can call Game game = new Game() when you want to start a new game
• 11-18-2013, 12:47 PM
PhHein
Re: Need help with my code.
You're six(6!!!!) years late.
• 11-18-2013, 12:48 PM
gimbal2
Re: Need help with my code.
So let me get this straight: you are suggesting to put actual application logic in the constructor of the class? (disregarding the fact that this is a zombie)
• 11-18-2013, 03:16 PM
jim829
Re: Need help with my code.
Lol. The OP's one and only post and s/he finally gets a reply (however ill-advised) 6 years later.

Regards,
Jim
• 11-18-2013, 03:21 PM
gimbal2
Re: Need help with my code.
Reminds me of that Facebook episode of South Park where a kid with no friends is just unhappily sitting by his computer waiting for something to happen :p

Even if you don't like / hate South Park, that is the one episode that is worth it to watch.
• 11-18-2013, 03:50 PM
JosAH
Re: Need help with my code.
Quote:

Originally Posted by PhHein
You're six(6!!!!) years late.

So what? Six years are effectively zero compared to eternity.

kind regards,

Jos (<-- in a philosophical mood)
• 11-18-2013, 04:40 PM
dd09wrx
Re: Need help with my code.
logic in the game class.
• 11-18-2013, 04:58 PM
gimbal2
Re: Need help with my code.
Quote:

Originally Posted by dd09wrx
logic in the game class.

Yes in the class, not in the constructor.