Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Source Code] BlackJack made in Java

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
ravicus
Master Cheater
Reputation: 0

Joined: 16 Dec 2006
Posts: 464

PostPosted: Mon Mar 19, 2007 8:09 pm    Post subject: [Source Code] BlackJack made in Java Reply with quote

Hey guys, I make a simple BlackJack game in Java, feel free to comment, but don't treat it too harshly, I'm mainly using this thread to transfer the changed source from my laptop to my desktop.
Code:
//Importing Needed Functions
import java.util.Random;
import java.util.Scanner;
import static java.lang.System.out;
 
public class blackjack {
   
    public static void main(String[] args) {
    //Declaring Variables
       Random randomizer = new Random();
       Scanner s = new Scanner (System.in);
       int roundcard = 0;
       int playertotal = 0;
       int aitotal = 0;
       int tf = 1;
       boolean won = false;
       //The welcome message
       out.println ("Welcome to BlackJack!");
       out.println ("Dealing Cards...");
       aitotal = (aitotal + randomizer.nextInt(7) + 15);
       //The main stand/hit loop
       while ((tf) == 1 && (playertotal) <= 21) {
          roundcard = (randomizer.nextInt(9) + 2);
       playertotal = (roundcard + playertotal);
       out.print ("You picked up a card worth ");
       out.print (roundcard);
       out.print (" points, your total score is ");
       out.println (playertotal);
       out.print ("Press 0 to stand or 1 to hit: ");
       tf = (s.nextInt());
       }
       //If statements to determine the outcome of the game
       if ((playertotal) > 21) {
          out.println ("BUST!");
       }else {
          out.print ("Your final score was ");
          out.println (playertotal);
          out.print ("Your opponent's final score was ");
             out.println (aitotal);
             if ((playertotal) > (aitotal) && (playertotal) < 22) {
                out.println ("You Win!");
             }
             else if ((playertotal) < (aitotal)) {
                out.println ("You lose!");
             } else if ((playertotal) == (aitotal)) {
                out.println ("It was a tie!");
             }
       }
    }
}

_________________
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Mon Mar 19, 2007 8:20 pm    Post subject: Reply with quote

looks like poker to me.
Back to top
View user's profile Send private message MSN Messenger
ravicus
Master Cheater
Reputation: 0

Joined: 16 Dec 2006
Posts: 464

PostPosted: Mon Mar 19, 2007 8:47 pm    Post subject: Reply with quote

Poker? Lol.
_________________
Back to top
View user's profile Send private message
DeltaFlyer
Grandmaster Cheater
Reputation: 0

Joined: 22 Jul 2006
Posts: 666

PostPosted: Mon Mar 19, 2007 11:31 pm    Post subject: Reply with quote

The indentation is much better. Code is much easier to read now.

A tip: You can concatenate strings and other primitive types with "+".

eg:

Code:

out.print("Hi ");
out.print(name);
out.println(", how are you?");


could be typed as

Code:

out.println("Hi "+ name + ", how are you?");


Just so you won't have to type that annoying out.print each time. You could also break the code into multiple lines if you think that making them in one line is too messy. It should look fine with colour coding though.

Code:

out.println (
        "Hi " +
        name +
        ", how are you?");


Concatenating the string also increases the speed slightly as you won't have to call the same method multiple times, allocating memory and pushing stuff into the stack.

Also, instead of using multiple println's you can use a single one and split the lines with "\r" , "\n", or "\r\n". Last one is what a return actually is, but they do the same thing by themselves. Some people say this makes your strings unreadable, so I guess it's your choice on that.

_________________

Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for?
Back to top
View user's profile Send private message
ravicus
Master Cheater
Reputation: 0

Joined: 16 Dec 2006
Posts: 464

PostPosted: Tue Mar 20, 2007 6:51 am    Post subject: Reply with quote

Oh, Thanks for the tip about printing, I'm used to visual basic so i couldn't figure out how to put multiple things on 1 line, I used & in vb.
_________________
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites