| View previous topic :: View next topic |
| Author |
Message |
MoolT Cheater
Reputation: 0
Joined: 16 Mar 2007 Posts: 38
|
Posted: Tue Jun 19, 2007 3:05 pm Post subject: [release]BlackJack {JAVA} + Question |
|
|
I've been studying java for a year by now, high school.
Apparently the last thing we've learned was do-while loop so I thought of making a blackjack game. btw, I used the IO library. anyhow it works pretty good however when you get a blackjack the program enters a loop and crashes. Can anyone help me solve the problem?
[didn't really make if(or case-switch) for the ACE card cause of laziness.
| Code: |
/**
* cards game: BLACK JACK
* Sunday, 17.6.07
* created by Ohad aka MOOLT.
*/
class Black_Jack
{
public static void main (String []args)
{
String player, dealer;
int count1, count2, count3, count4, count5, count6, count7, count8, count9, count10;
int Dcard, Pcard, Pnum, Dnum, Psum, Dsum;
char check, check1;
count1=0;
count2=0;
count3=0;
count4=0;
count5=0;
count6=0;
count7=0;
count8=0;
count9=0;
count10=0;
Dsum = 0;
Psum = 0;
Dnum = 0;
Pnum = 0;
player=IO.readString ("Enter the player's name ");
dealer=IO.readString ("Enter the dealer's name ");
check1 = 'y';
while (check1 == 'y')
{
IO.writeln ("*****A NEW GAME HAS BEGAN*****");
Dnum=(int)(Math.random()*11)+1;
if (Dnum == 1)
count1++;
if (Dnum == 2)
count2++;
if (Dnum == 3)
count3++;
if (Dnum == 4)
count4++;
if (Dnum == 5)
count5++;
if (Dnum == 6)
count6++;
if (Dnum == 7)
count7++;
if (Dnum == 8)
count8++;
if (Dnum == 9)
count9++;
if (Dnum == 10)
count10++;
while ((count1>4) || (count2>4) || (count3>4) || (count4>4) || (count5>4) || (count6>4) || (count7>4) || (count8>4) || (count9>4) || (count10>16))
{
Dnum=(int)(Math.random()*11)+1;
}
IO.writeln (dealer + " the dealer got " + Dnum);
Dsum=Dnum;
Pnum=(int)(Math.random()*11)+1;
if (Pnum == 1)
count1++;
if (Pnum == 2)
count2++;
if (Pnum == 3)
count3++;
if (Pnum == 4)
count4++;
if (Pnum == 5)
count5++;
if (Pnum == 6)
count6++;
if (Pnum == 7)
count7++;
if (Pnum == 8)
count8++;
if (Pnum == 9)
count9++;
if (Pnum == 10)
count10++;
while ((count1>4) || (count2>4) || (count3>4) || (count4>4) || (count5>4) || (count6>4) || (count7>4) || (count8>4) || (count9>4) || (count10>16))
{
Pnum=(int)(Math.random()*11)+1;
}
IO.writeln (player + " got " + Pnum);
Psum=Pnum;
Pnum=(int)(Math.random()*11)+1;
if (Pnum == 1)
count1++;
if (Pnum == 2)
count2++;
if (Pnum == 3)
count3++;
if (Pnum == 4)
count4++;
if (Pnum == 5)
count5++;
if (Pnum == 6)
count6++;
if (Pnum == 7)
count7++;
if (Pnum == 8)
count8++;
if (Pnum == 9)
count9++;
if (Pnum == 10)
count10++;
while ((count1>4) || (count2>4) || (count3>4) || (count4>4) || (count5>4) || (count6>4) || (count7>4) || (count8>4) || (count9>4) || (count10>16))
{
Pnum=(int)(Math.random()*11)+1;
}
IO.writeln (player + " got " + Pnum);
Psum=Pnum+Psum;
IO.writeln (player + " has got " + Psum + " points");
if (Psum == 21)
{
IO.writeln ("BLACKJACK!");
check1 = IO.readChar ("Would you like to have one more game? -> y - yes, n - no");
}
else
{
check=IO.readChar ("Would you like to have one more card? -> y - yes, n - no");
if ((check == 'y') && (Psum <= 21))
{
Pnum=(int)(Math.random()*11)+1;
if (Pnum == 1)
count1++;
if (Pnum == 2)
count2++;
if (Pnum == 3)
count3++;
if (Pnum == 4)
count4++;
if (Pnum == 5)
count5++;
if (Pnum == 6)
count6++;
if (Pnum == 7)
count7++;
if (Pnum == 8)
count8++;
if (Pnum == 9)
count9++;
if (Pnum == 10)
count10++;
while ((count1>4) || (count2>4) || (count3>4) || (count4>4) || (count5>4) || (count6>4) || (count7>4) || (count8>4) || (count9>4) || (count10>16))
{
Pnum=(int)(Math.random()*11)+1;
}
IO.writeln (player + " got " + Pnum);
Psum=Pnum+Psum;
IO.writeln (player + " has got " + Psum + " points");
if (Psum == 21)
{
IO.writeln ("BLACKJACK!");
check1 = IO.readChar ("Would you like to have one more game? -> y - yes, n - no");
}
else
{
if (Psum < 21)
{
check=IO.readChar ("Would you like to have one more card? -> y - yes, n - no");
}
else
{
check = 'y';
IO.writeln (player + " has lost");
check1 = IO.readChar ("Would you like to have one more game? -> y - yes, n - no");
Psum = 0;
Pnum = 0;
Dsum = 0;
Dnum = 0;
}
}
while ((check == 'y') && (Psum <= 21))
{
if (Psum == 21)
{
IO.writeln (player + " has got 21 points");
IO.writeln ("BLACKJACK!");
}
if (Psum < 21)
{
Pnum=(int)(Math.random()*11)+1;
if (Pnum == 1)
count1++;
if (Pnum == 2)
count2++;
if (Pnum == 3)
count3++;
if (Pnum == 4)
count4++;
if (Pnum == 5)
count5++;
if (Pnum == 6)
count6++;
if (Pnum == 7)
count7++;
if (Pnum == 8)
count8++;
if (Pnum == 9)
count9++;
if (Pnum == 10)
count10++;
while ((count1>4) || (count2>4) || (count3>4) || (count4>4) || (count5>4) || (count6>4) || (count7>4) || (count8>4) || (count9>4) || (count10>16))
{
Pnum=(int)(Math.random()*11)+1;
}
IO.writeln (player + " got " + Pnum);;
Psum=Pnum+Psum;
IO.writeln (player + " has got" + " " + Psum + " " + "points");
if (Psum == 21)
{
IO.writeln (player + " has got 21 points");
IO.writeln ("BLACKJACK!");
check1 = IO.readChar ("Would you like to have one more game? -> y - yes, n - no");
}
else
{
if (Psum > 21)
{
check = 'y';
IO.writeln (player + " has lost");
check1 = IO.readChar ("Would you like to have one more game? -> y - yes, n - no");
Psum = 0;
Pnum = 0;
Dsum = 0;
Dnum = 0;
}
else
check=IO.readChar ("Would you like to have one more card? -> y - yes, n - no");
if (Psum == 21)
{
IO.writeln (player + " has got 21 points");
IO.writeln ("BLACKJACK!");
check1 = IO.readChar ("Would you like to have one more game? -> y - yes, n - no");
}
}
}
}
while ((Dsum < Psum) && (Dsum <=16) && (Psum <= 21))
{
Dnum=(int)(Math.random()*11)+1;
if (Dnum == 1)
count1++;
if (Dnum == 2)
count2++;
if (Dnum == 3)
count3++;
if (Dnum == 4)
count4++;
if (Dnum == 5)
count5++;
if (Dnum == 6)
count6++;
if (Dnum == 7)
count7++;
if (Dnum == 8)
count8++;
if (Dnum == 9)
count9++;
if (Dnum == 10)
count10++;
while ((count1>4) || (count2>4) || (count3>4) || (count4>4) || (count5>4) || (count6>4) || (count7>4) || (count8>4) || (count9>4) || (count10>16))
{
Dnum=(int)(Math.random()*11)+1;
}
IO.writeln ("Dealer got " + Dnum);
Dsum=Dnum+Dsum;
IO.writeln (dealer + " has got" + " " + Dsum + " " + "points");
}
if ((Dsum > Psum) && (Dsum < 22))
{
IO.writeln (dealer + " the dealer won");
Psum = 0;
Pnum = 0;
Dsum = 0;
Dnum = 0;
}
else
{
if (Dsum == Psum)
{
IO.writeln ("It's a tie");
}
else
{
IO.writeln (player + " won");
Psum = 0;
Pnum = 0;
Dsum = 0;
Dnum = 0;
}
}
check1 = IO.readChar ("Would you like to have one more game? -> y - yes, n - no");
}
}
}
}
}
|
Dnum = Dealer Card Number
Pnum = Player --"-- --"--
I am aware of the problem with y 'n' n a.k.a yes and no answers due to the fact that they can answer 't' and it will be accepted.. but oh well.
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Tue Jun 19, 2007 3:44 pm Post subject: |
|
|
Bwah i think i found it.
after you do the blackjacks and the one more card/game
put a break?
if its entering a loop a break function will stop it
OR you could make a new counter
ill rewrite how it might work in like 1 minute
| Code: |
if (Psum == 21 && BlackjackCount == 0)
{
IO.writeln ("BLACKJACK!");
check1 = IO.readChar ("Would you like to have one more game? -> y - yes, n - no");
BlackjackCount++
}
|
Edit:
Just remember that you have to reset the BlackjackCount to 0 for the next black jack . or a BlackJack wont happen
_________________
Last edited by HomerSexual on Tue Jun 19, 2007 3:45 pm; edited 1 time in total |
|
| Back to top |
|
 |
DeltaFlyer Grandmaster Cheater
Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Tue Jun 19, 2007 3:44 pm Post subject: |
|
|
Wow, a year in high school Java course and they STILL didn't teach you guys arrays?
I didn't read into your program much, but it's evident that your code could be shortened with arrays, especially the counts.
If it's done with arrays, your huge checking selection could be shortened to one line:
_________________
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 |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Tue Jun 19, 2007 3:51 pm Post subject: |
|
|
btw they teach arrays
if thats the AP java course that my school -had- then yeah
they cancelled it because it had 5 people not 8
and i had did everything in the book this year so i was ready next year
dam schools
_________________
|
|
| Back to top |
|
 |
MoolT Cheater
Reputation: 0
Joined: 16 Mar 2007 Posts: 38
|
Posted: Tue Jun 19, 2007 4:08 pm Post subject: |
|
|
| blankrider wrote: | Bwah i think i found it.
after you do the blackjacks and the one more card/game
put a break?
if its entering a loop a break function will stop it
OR you could make a new counter
ill rewrite how it might work in like 1 minute
| Code: |
if (Psum == 21 && BlackjackCount == 0)
yuppi! thanks man :)
{
IO.writeln ("BLACKJACK!");
check1 = IO.readChar ("Would you like to have one more game? -> y - yes, n - no");
BlackjackCount++
}
|
Edit:
Just remember that you have to reset the BlackjackCount to 0 for the next black jack . or a BlackJack wont happen |
edit:
| DeltaFlyer wrote: | Wow, a year in high school Java course and they STILL didn't teach you guys arrays?
I didn't read into your program much, but it's evident that your code could be shortened with arrays, especially the counts.
If it's done with arrays, your huge checking selection could be shortened to one line:
|
I've taught myself how to make libraries like IO but for some reason I'm getting an error with "shell" blah blah w/e I'll learn it next year.
Bottom line is I could've shortened it alot.. well I'll try learning arrays.
Last edited by MoolT on Tue Jun 19, 2007 4:11 pm; edited 1 time in total |
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Tue Jun 19, 2007 4:09 pm Post subject: |
|
|
that code made me cry, I don't even know java
_________________
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Tue Jun 19, 2007 4:24 pm Post subject: |
|
|
yea it is horrible
but it works
if he used a dif style of if and while, the code would be neater
edit:
i have a challenge for you
turn this into a GUI program with images
_________________
|
|
| Back to top |
|
 |
MoolT Cheater
Reputation: 0
Joined: 16 Mar 2007 Posts: 38
|
Posted: Wed Jun 20, 2007 1:11 am Post subject: |
|
|
| blankrider wrote: | yea it is horrible
but it works
if he used a dif style of if and while, the code would be neater
edit:
i have a challenge for you
turn this into a GUI program with images  |
lil problem, I don't have a clue how. What you see in the code is simply everything which I've learned this year. Sadly next year I'm gonna study pascal (retarded school, taking a step backwards) and in 2 years I'll be back in java.
|
|
| Back to top |
|
 |
Madman I post too much
Reputation: 1
Joined: 04 May 2006 Posts: 3978
|
Posted: Wed Jun 20, 2007 1:21 am Post subject: |
|
|
im so fucking pissed.
my school used to have a computer programming class, and right before i got into highschool the bastards took it out.
fucking douches.
one third of the way through my freshmen year i found the engineering club was providing tutorials on... none other than stupid shitty vb.
i did that for awhile but it sucked ass. i could have more fun taking a shit.
_________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Wed Jun 20, 2007 6:11 am Post subject: |
|
|
| please god at least use switch statements if you're going to use massive if chains, they're much neater, not to mention usually faster.
|
|
| Back to top |
|
 |
ravicus Master Cheater
Reputation: 0
Joined: 16 Dec 2006 Posts: 464
|
Posted: Wed Jun 20, 2007 9:13 am Post subject: |
|
|
If thats an AP course... I'm 13, and I already programmed a blackjack.
_________________
|
|
| Back to top |
|
 |
RS Newbie cheater
Reputation: 0
Joined: 12 Jun 2007 Posts: 20
|
Posted: Wed Jun 20, 2007 3:15 pm Post subject: |
|
|
I tried to debug your code -- it made me cry a little (not really, but I'll only debug that version if you REALLY want). I rewrote the code using the Java 1.5.0 API (http://java.sun.com/j2se/1.5.0/docs/api/) and tried to eliminate redundancies while maintaining the logic and structure. Hope it helps.
| Code: | /*
* NOTE:
* Using int instead of short to simplify understanding
* No handling for Ace (11)
*/
import java.io.*;
class Blackjack
{
public static void main(String[] arguments) throws IOException
{
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String dn, pn; // dealer name, player name
System.out.print("Enter the deader's name: ");
dn = keyboard.readLine();
System.out.print("Enter the player's name: ");
pn = keyboard.readLine();
Deck d = new Deck();
int dh, ph; //dealer hand, player hand
String input = "";
int draw;
while(!input.equals("n")) // game loop
{
System.out.println("*****A NEW GAME HAS BEGUN*****");
ph = 0; dh = 0;
// DEALER PHASE
draw = d.draw(); dh += draw;
System.out.println(dn + " the dealer got " +draw);
input = "y";
while(input.equals("y")) // dealer draw
{
draw = d.draw();
dh += draw;
System.out.println(dn +" the dealer got " +draw);
System.out.println(dn +" has got " +dh +" points");
if(dh > 21){System.out.println("BUST!"); break;}
System.out.println("Would you like to have one more card? -> y - yes, n - no");
input = keyboard.readLine();
}
System.out.println("\n\n"); // format spacer
// PLAYER PHASE
if(dh != 21) // check for dealer auto-win
{
draw = d.draw(); ph += draw;
System.out.println(pn + " the player got " +draw);
input = "y";
while(input.equals("y")) // player draw
{
draw = d.draw();
ph += draw;
System.out.println(pn + " the player got " +draw);
System.out.println(pn + " has got " + ph + " points");
if(dh > 21){System.out.println("BUST!"); break;}
System.out.println("Would you like to have one more card? -> y - yes, n - no");
input = keyboard.readLine();
}
} // end if
System.out.println("\n\n"); // format spacer
if(dh > ph && dh <= 21) System.out.println(dn + " the dealer won");
else if(ph > dh && ph <= 21) System.out.println(pn + " the player won");
else if(ph > 21 && dh > 21) System.out.println("You both suck");
else /* ph == dh */ System.out.println("It's a tie");
System.out.println("Would you like to have one more game? -> y - yes, n - no");
input = keyboard.readLine();
} // end game loop
} // end main
private static class Deck
{
private int[] deck = new int[10]; // Note: Deck != deck
public Deck() { reset(); }
public void reset()
{
for(int x=0; x<9; x++)
deck[x] = 4;
deck[9] = 12;
}
public int draw()
{
while(true)
{
int card = (int)(Math.random()*100)%9;
if(deck[card] > 0)
{
deck[card]--;
return card+1; // offset for index
}
}
}
} //end class deck
} //end class BJ |
|
|
| Back to top |
|
 |
|