Results 1 to 9 of 9
  1. #1
    stormviper is offline Member
    Join Date
    Jul 2007
    Posts
    1
    Rep Power
    0

    Default 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:
    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);
    }
    }
    Last edited by stormviper; 07-12-2007 at 03:59 PM.

  2. #2
    dd09wrx is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default 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

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Need help with my code.

    You're six(6!!!!) years late.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,096
    Rep Power
    4

    Default 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)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,949
    Rep Power
    4

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,096
    Rep Power
    4

    Default 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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need help with my code.

    Quote Originally Posted by PhHein View Post
    You're six(6!!!!) years late.
    So what? Six years are effectively zero compared to eternity.

    kind regards,

    Jos (<-- in a philosophical mood)
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    dd09wrx is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Re: Need help with my code.

    logic in the game class.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,096
    Rep Power
    4

    Default Re: Need help with my code.

    Quote Originally Posted by dd09wrx View Post
    logic in the game class.
    Yes in the class, not in the constructor.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 10:12 PM
  2. Pls some one to help mi wit this code
    By _nik_ in forum New To Java
    Replies: 3
    Last Post: 02-10-2008, 02:02 AM
  3. I need help on my code
    By jason27131 in forum New To Java
    Replies: 4
    Last Post: 07-28-2007, 04:23 AM
  4. tic tac toe code
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 04:36 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •