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);
}
}