• Post Reply Bookmark Topic Watch Topic
  • New Topic

random card generator  RSS feed

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been trying to get a card generator working, but hells bells nothing is easy in java (unless you already the correct answer) trying to figure out the correct way to apply a method you don't understand is like being asked for the German word for banana in Spanish class.

If someone can finish off this segment of code, I can adapt it to fit my project and be done with this nightmare.

If you solve this problem for me, I will give you a years supply of rep points !!! (unless trading rep points is against the forum rules, or I forget)

Seriously, thanks.



As it stands, I can't make i and ncard interact, to form two different numbers,

I tried:
int ncard = i; -- symbol ncard already used
int i = ncard; -- symbol already used

for int (i=0; i>11; ++i) -- illegal statement
for (int i=0; i>11; ++i) -- illegal statement

my next step might be using object (hammer >=computer)=watch telly instead.

Thanks for the help.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first, your for-loop is wrong. it contains no body, so it basically does nothing.

next. your print statement prints the same variable twice, so it will always print the same number. If you want two different values, you need to generate two different values and save each of them.
 
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did this once. I used an arraylist containing all 52 cards and when I dealt one and removed it from the arraylist the arraylist length automatically changes, which gives you the new current upper bound for your random number. This is one case where Java makes it very easy. It was an arraylist of card objects, and the card objects contained suit, numeric value, and a bitmap of the card face.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also...

Vincent Tyson wrote:
I tried:
int ncard = i; -- symbol ncard already used
int i = ncard; -- symbol already used

for int (i=0; i>11; ++i) -- illegal statement
for (int i=0; i>11; ++i) -- illegal statement

my next step might be using object (hammer >=computer)=watch telly instead.


Programming isn't about throwing different statements together and see what works or what is correct. You get to the correct solution, by understanding the language, and by understanding the issue/program. Without the later, you don't know what to fix, and without the first, you can't get to the solution.

Henry
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you solve the above code for me as is?

I have the rest of my project almost complete, but it won't work without the card generator. I just need a simple 1-11 random number to generate.

I can save the int in a placeholder like

int ptotal = ncard +ncard
int pcurrent =ptotal +ncard

and the dealer
int dtotal =ncard +ncard
int dcurrent =dtotal +ncard

and use if statements to decided if either player is bust or has blackjack.

it's just the damn generator won't do what i want it too, If I need x2 sys.out lines I can live with that.
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know what your saying Henry, I almost agree with you, but until I see the method working I can't decipher the logic, once I understand the logic, I can do anything with it, until then, I'm lost.

I can't find a page that explains what it is I'm trying to do, there is no text on the subject nor any published work I can draw from, so all I'm left with is guess work, and yes, I don't understand it.. yet, but I will if someone shows me the correct method, instead of telling me the 1000's of things I've done wrong..... if that makes sense.

anyways, without your help I can go no further, so help or not. your choice.

Vincent
 
Greenhorn
Posts: 16
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vincent, Henry is giving sound advice. You don't understand what is wrong at the moment, why would a method that works allow you to understand why it works? You need to understand all of the code, including why your for-loop is wrong:



At the moment, you are printing out the same value twice (ncard). If you need two different values based on the first variable (ncard), you need to create a new variable to store the new value after you have calculated whatever it is you need to calculate (I have no idea how to even play BlackJack so can't help here)


Hope it helps...
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing you'll have to understand, Vincent, is that we're NotACodeMill (←click that link) and we expect you to DoYourOwnHomework (←click). That means if you make 1000 mistakes, we won't give 1000 solutions but instead 1000 hints on how to correct your mistakes.

Why don't you describe, in plain English, what exactly you want the card generator to do. If you can't explain it simply to other people in plain English, then you won't have much luck trying to explain it to a computer in Java.
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ash,

That helps, I wasn't having a go at Henry, I know better than that.
I just responded to his post, if I offended I apologize.

I came to the forum looking for help, but all I got so far was figure it out for yourself and stop bothering me.

I've asked for the code to be finished for me, because I don't // can't get to grips with it, and once I see what it should look like, I can analyze and compare to my own failed attempts
and perhaps say to myself "AH FFS Vincent!!! there is your problem right there" -- or -- "Oh so that's how it works, NICE!" and learn how not to make that mistake again.

I have been trying very hard on my own, I can show you 4-5 saved fails from my computer, but that will not help understand what is wrong, until I know what right looks like.

I hope I have been clear, I'm not trying to be argumentative in the least, I am spun out, dizzy from fails, and need to see where I'm going wrong so I can fix it.

Vincent.
 
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vincent Tyson wrote:
int dtotal =ncard +ncard

This would be the same as

I don't see the purpose of this at all. Why *2 ?
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Junilu

I would like my generator to produce a random number between 1-11, and save that number somewhere.
then another number, show it to me, and add it to the first number.
x=4
y=11
total = 15

without having to make 20+ int place holders (variables) or an array.

I would like ncard to = 1+ r.nextInt(11); every time I call it. So when a player hits,
system.out.println("your next card is"+ncard);

look... this is my project so far, and it will work perfectly if I can figure the card generator part, which I probably won't because no one will show me what it's supposed to look like <- not judging "notacodemill!"




the while loop will not work correctly until I can eliminate the 100's of int variables, and have only two, or three.
I still need to write the dealers part, but that will be simple enough.

So if the generator could simply make a random number for me to save and +ncard to, I could start gluing back in all the hair I've pulled out over the last week.

Thanks for replies.
Vincent.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vincent Tyson wrote:I have been trying very hard on my own, I can show you 4-5 saved fails from my computer, but that will not help understand what is wrong, until I know what right looks like.
I hope I have been clear, I'm not trying to be argumentative in the least, I am spun out, dizzy from fails, and need to see where I'm going wrong so I can fix it.
Vincent.

This is known as "thrashing". Forget Java for a moment, can you write down the steps in English?
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Carey

the int = ncard +ncard;

would look like:

int = random3+ random6

but I've not gotten to that part yet, should be easy to create a temp int variable to store the first card drawn, and then add the second to it, for a player total per hit,
it's really just there to remind me to fix it once the card generator is done.

eg 3+6+8=17
17+5=bust!
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Carey

Sure.

Welcome to BlackJack!
What is your name?
Hello Vincent
Lets play some Blackjack

Dealer draws a 8

Vincent draws a 8
and a 8
Your total is: 16
Would you like to Hit or Stick?
h
Vincent draws a: 4
Your total is:20

Dealers turn
Dealer must draw until 17

Dealer draws a:9
Dealer total: 17
Dealer draws a:10
Dealer: BUST

Congratulations.
You Win.

Would you like to play again? Yes or No.

That is the entirety of what I am trying to accomplish.
 
Sheriff
Posts: 4931
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vincent,

Quite good start, but it is an actual game flow (you'll have it at the end, once you have working program), try to start from the very abstract steps then move on to more specific.

For instance:
1. Show greeting message
2. Ask player name
3. Greet player
Now your turn

After you finish 1st round, write down more specific steps of each abstract step (1 abstract step can/probably will extend to more specific steps), for instance:
1. Print message "BlackJack!"
2.1 Ask player name by printing message "What is your name?"
2.2 Store player name
3. Print greeting message and previously stored player name
...

Then 3rd round (you can add some technical details).. and so on.
 
Bartender
Posts: 1838
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I think you would be better off generating a "deck" of 52 cards into an array and then "shuffling" them.
You can then "deal" from the deck starting at index 0, and going through all the cards.

Getting a random number from 0 to 11 isn't actually modelling the game properly. There are 13 cards in a suit, and four suits.
Because the picture cards all have a value of "10" in Blackjack, 10 is meant to be more likely to obtain than any other value - something not currently being modelled in your solution
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Stefan. You may have bitten off more than you can chew right now, Vincent. Trying to do this all in main() is not the best way to learn Java programming, especially for this problem. The ideal solution would involve different classes like Deck and Hand to represent a deck of cards and the hand each player is dealt. It would also involve using the List interface and one of its implementations, usually ArrayList. And then there are object-oriented concepts you need to know like encapsulation.

If you want to keep going the way you're going though, perhaps you can get some hints from searching for Java Blackjack implementation
 
Stefan Evans
Bartender
Posts: 1838
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the risk of being a code mill:
Here is a method to help you get rid of all of those ints.



Right now it uses a (slightly modified) version of your random number generator.
But it should help you get rid of all of those variables you have.
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stefan,

I was attempting that for 9 hours, before I ran out of hair to pull out ;)

Since then I found a way to skirt the generator completely, and generate the new random card inside a loop, which is working very well at the moment, but it looks like I've got a broken while loop now, and all I need to do is fix it, and add a start again loop.

I'll copy that section into my method library for future reference, it will be very useful in up coming projects, I see how you made the J,Q,K come out as 10's and I didn't realize I could make conditions for the int value, which would of made life a lot easier in the first place



you'll see, I added a new int value inside the loop, so each time it runs, a new random occurs.

But now, my problem is the dealers loop, I can't single out just one instance of the end game, either two or more run at the same time, or I need to adjust the if((dscore==(pscore))) line so the loop calls it correctly.

I realize this is yet another question, but I'm not sure if I should use one thread, or start a new one.

Thanks again.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vincent,

I'm not sure how you got to doing it the way you are but your use and placement of braces, while syntactically correct, is very unconventional and potentially very confusing and misleading. Your code indentation is also all over the place. For example:

There is nothing syntactically wrong with this, which may surprise a few who read this. It is valid code and will compile, as you obviously know. However, per normal conventions, the indentation of the code implies that the intent was for lines 85 and 86 to be executed on the condition that dscore == pscore. Also, you have used more parentheses than are really necessary on line 84. If written using standard conventions, your code would be written as:

However, your code is actually executed as if it were written in this more standard way:

Braces are used to group together statements to be executed as one "block" - in fact, that's what zero or more statements surrounded by balanced, matching braces is called: a statement block. Statement blocks are needed when you want to execute them as part of any control flow statement such as an if-else, while-loop, for-loop, etc. Braces around a block of statements are optional if they are not associated with a control-flow structure, that's why your code still compiles. If you want to visually see some separation between logical groups of statements, use a blank line or two. Better yet, put related statements in a method that represents a chunk of work to be done.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you have put in a lot of effort already, let me try to give you something that might help your organize your program better. The idea here is trying to tell the "story" at a high-level first, then progressively drill down to more detailed levels. Note that the main method consists only of two statements: create an instance of the Blackjack game and then invoke its play() method, which is about as high-level as you can get. Then the play() method outlines the general flow and calls other methods to get smaller tasks done.

Methods like declareWinner() and playAgain() are at the lowest level of abstraction and detail - that's where the nitty-gritty works gets done. Methods like giveTurn() and dealHand() are at a level where the code is still abstract and you're trying to break down a chunk of the story into smaller chunks. So you continue to outline and refine.

By breaking the problem down into progressively smaller chunks, you can manage the complexity and get a better handle on how things are supposed to flow in your program. By starting at a high level of abstraction, you get the lay of the land first and an overall picture of what's going to happen. As you drill down into the details, you just follow the same strategy of slowly dividing and conquering the work to be done. As I mentioned previously, you'll also want to create separate classes to represent a Hand and a Deck of cards, respectively.
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I've been told off about my braces before, but the college instructor wants it done his way to avoid confusion on his part. eg

public stacic void main(String[] args) { //body

}

he prefers
Public stactic void main(String[] args)
{//body
}

and I guess, it's confusing even me now at this point, It feels like I'm trying to follow two sets of rules, and now I'm paying for it in spades (pardon the pun)

Thanks for the revision, I won't be copy pasting, but I will try to indent and use braces correctly as you've laid out here, it's really useful to see how it should look.
We haven't touched on the public void play () { yet, and although I do sort of understand what it's doing, I fear that is well outside of my wheelhouse. I should be able to modify my loops as you suggested and make each loop end correctly, instead of seeing all three when the game ends.

I honestly can't express how helpful you all have been, and I wish there was a way I could repay your kindness, I will probably go find a private tutor for java and see if I can learn the more popular, conventional way of structuring code, because everyone has told me it looks like a dogs dinner or something similar.

I've got an exam in networking (MUCH easier than java ;) ) tomorrow, but I should still brush up. I'll be back in a couple of days with a progress report if you'd like one.

Vincent.
 
Stefan Evans
Bartender
Posts: 1838
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Style on placement of braces is a religious war that will never end.
Whichever way you do them however, they do at least need to match your intentions

rather than


You need



The braces group together the set of statements to execute for the 'if' statement. If you don't have braces after the if statement, only the very next line is tied to the if statement, and the rest are not.

 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've made the final revisions, to my code and now after almost 6 days of serious confusion, I have a finished product. - almost.

I have a slight problem in one of the loops, where the program freezes if certain conditions are met,and the players can still draw 11 for both cards in the beginning, forcing a bust before the game starts. I've moved if statements outside the {} braces for flow control.
Overall I have a 99% working blackjack game, with Strings, Scanners, int variables, while loops, if-else statements and correctly indented and structured code. I reckon it's worth at least a B+. the grade belongs to the forum members without them I would have nothing at all. I'll keep on with bugs and see if I can do something with them, I just wanted to say thanks for all the help, and I hope someday I'll be able to offer advise and information like the people in this community do.

I know some people will be curious as to how it looks and reads now, I haven't copy pasted any code offered here as a guideline, I learned the method after I was shown what the working model looks like, and thanks again for being so patient with me, I knew this wouldn't be easy, but I never thought it would be this hard.

Thanks again for everything, -- marked as resolved --

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vincent Tyson wrote:and the players can still draw 11 for both cards in the beginning, forcing a bust before the game starts.


An Ace is either a one or an eleven. So two aces is either a two or a twelve. You will need to adjust for this case.

On the same note, if a player draws a card, that busts the hand, you will also need to check to readjusts any aces that were previously treated as an eleven, as it should be now treated as a one.

Henry
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vincent,

This is what happens when I hard-coded the initial cards dealt to the player and dealer to be all 11, i.e. they both bust at the start.

I had to input my name (Line 6) and on line 21, I was prompted if I wanted to Hit or Stick and I entered "H" (Line 22). So there's still some logic that's off. You made a valiant run in getting to this point though and I have to commend you for your perseverance.
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry,
After some discussion about this double 11 situation with my instructor, he said it was ok if the rules weren't identical to the original blackjack game because we haven't studied Case() as you have mentioned, and we probably won't get to it by terms end in may.

So as an alternative, I call 10+11=21 blackjack, which is still possible in the game, and I can make two more if statements to declare if (card1+card2==22) declare a bad deal, end the program.

My real problem is I haven't learned the methods needed yet, and although there is plenty of text out there on the subject, I have to make the game work within the confines of what we have covered in class, but I guess I've already come this far. Maybe my local store has a good book on Java that covers all the good stuff like cases and arrays. If they have examples and exercises I can learn just about anything, so fingers crossed.

@junilu,

Yep that's exactly what I get too, and I just found another bug which causes an infinite loop when some unknown exact criteria are met, but on a positive side, my instructor said the work so far was, far beyond what he had expected of me, I suppose technically it wasn't my code entirely, but he said as long as I understand it, it doesn't matter where it comes from. I'll go see if I can find that book, maybe there is a way to salvage this and submit a bug free program.

Vincent.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kudos for the contributers of this topic. Once again showing the strength of this forum.

Having the guru's getting the student to really go through a learning proces. And having the "newbie" overcome his frustation and open up to really learn to code.

Thumbs up.

PS. and making it a nice read for other newbies in the mean time
 
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can't generate perfect random number for deck of cards using shuffling. For perfect randomness possible combinations are 52! which is
not possible to do on 64 bit machine. It requires 226 bit instead. Here is the Wikipedia link, http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

I am sorry if its is irrelevant to the post.
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You raised an interesting point there, the idea of pure randomness is not exactly possible, and the algorithm ( a little above my pay grade ) seems to state that.
So I suppose the correct thing would be to say an unpredictable card, rather than a random one.

That said, for the purpose of this school exercise, I don't think it would make a tremendous difference to the outcome of any of the games played within.

Thanks for the interesting aside.
Vincent
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing mechanical can produce true randomness. You can get true randomness from some systems, e.g. radioactive decay, thermal noise, Brownian motion, etc. Now you aren't going to have a lump of radium or a Brownian motion producer (not even a really hot cup of tea) in your computer, so the nearest you will get is pseudo‑randomness. The number of possible deals of cards, allowing for there being 52! orders for the cards, is so many that the java.util.Random class can never produce them all. But, as you say, you are not going to notice that in your exercise. It is worth mentioning in the accompanying report, however.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote: . . . you aren't going to have a . . . ( . . . really hot cup of tea) in your computer . . .
Not unless you are very careless, that is
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!