View previous topic :: View next topic |
Author |
Message |
ravicus Master Cheater Reputation: 0
Joined: 16 Dec 2006 Posts: 464
|
Posted: Mon Mar 19, 2007 8:09 pm Post subject: [Source Code] BlackJack made in Java |
|
|
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 |
|
|
Noz3001 I'm a spammer Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Mon Mar 19, 2007 8:20 pm Post subject: |
|
|
looks like poker to me.
|
|
Back to top |
|
|
ravicus Master Cheater Reputation: 0
Joined: 16 Dec 2006 Posts: 464
|
Posted: Mon Mar 19, 2007 8:47 pm Post subject: |
|
|
Poker? Lol.
_________________
|
|
Back to top |
|
|
DeltaFlyer Grandmaster Cheater Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Mon Mar 19, 2007 11:31 pm Post subject: |
|
|
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 |
|
|
ravicus Master Cheater Reputation: 0
Joined: 16 Dec 2006 Posts: 464
|
Posted: Tue Mar 20, 2007 6:51 am Post subject: |
|
|
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 |
|
|
|