Home > Archive > Cobol > February 2005 > COBOL Black Jack Game
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
COBOL Black Jack Game
|
|
| Dragfameson 2005-02-06, 3:55 am |
| Hello everyone
I'm a new student to COBOL trying to have some fun with this language!
I'm trying to write a basic code for a Black Jack and I have ran into a
problem.
I need the program somehow to generate a random number.
This is what I would like to have happen...
Is there a way it can randomly choose a number from a list?
Like if I have a list as follows:
2
3
4
5
6
7
8
9
10
10 (jack)
10 (queen)
10 (king)
10 (ace)
I would want it to randomly select two numbers from that 'master' list of
numbers, and then add those two randomly selected numbers together... hope
ya'll can understand what I'm trying to do here!
Thank you for taking the time!
Jesse
| |
| William M. Klein 2005-02-06, 3:55 am |
| Which compiler are you using? Chances are that it has the RANDOM intrinsic
function. There are (relatively easy) ways to convert the returned 0 to 1 values
to 1 to 13.
--
Bill Klein
wmklein <at> ix.netcom.com
"Dragfameson" <dragfameson@hotmail.com> wrote in message
news:8608b3a30b79554dff16e53aff7a74b8@lo
calhost.talkaboutprogramming.com...
> Hello everyone
>
> I'm a new student to COBOL trying to have some fun with this language!
>
> I'm trying to write a basic code for a Black Jack and I have ran into a
> problem.
>
> I need the program somehow to generate a random number.
>
> This is what I would like to have happen...
>
> Is there a way it can randomly choose a number from a list?
>
> Like if I have a list as follows:
>
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
> 10 (jack)
> 10 (queen)
> 10 (king)
> 10 (ace)
>
> I would want it to randomly select two numbers from that 'master' list of
> numbers, and then add those two randomly selected numbers together... hope
> ya'll can understand what I'm trying to do here!
>
> Thank you for taking the time!
> Jesse
>
| |
|
| Dragfameson wrote:
> Hello everyone
>
> I'm a new student to COBOL trying to have some fun with this language!
That must be why this smells like homework... :)
> I'm trying to write a basic code for a Black Jack and I have ran into a
> problem.
>
> I need the program somehow to generate a random number.
There's an "Intrinsic Function" that will do that - check out Function
Random.
> This is what I would like to have happen...
>
> Is there a way it can randomly choose a number from a list?
But of course!
> Like if I have a list as follows:
>
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
> 10 (jack)
> 10 (queen)
> 10 (king)
> 10 (ace)
>
> I would want it to randomly select two numbers from that 'master' list of
> numbers, and then add those two randomly selected numbers together... hope
> ya'll can understand what I'm trying to do here!
Sure do. I'll show you how I would do it in W/S... (You've also got to
account for the four suits (and jokers, if you want 'em), and making
sure you don't pick the same cards twice...)
01 playing-cards.
12 spades pic X(13) value "234567890JQKA".
12 diamonds pic X(13) value "234567890JQKA".
12 clubs pic X(13) value "234567890JQKA".
12 hearts pic X(13) value "234567890JQKA".
12 jokers pic X(02) value "!!".
01 deck-of-cards redefines cards.
12 playing-card occurs 54 times pic X(01).
Use Function Random to find your index into the table, and store them
off. You can use the "remainder" functionality of the divide statement
to determine the card's value (keeping in mind that an Ace can be 1 or
11). For display the suit/card to the user, you'll need to determine
the range of the index. Check out the "Evaluate" statement, and its
Thru capabilities for contiguous values.
Then, once you've determined that you don't have the same card, and
you've calculated a value for each card, you're good to go.
> Thank you for taking the time!
You're welcome. :) Hope it helps get you on your way.
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
~ / \ / ~ Live from Montgomery, AL! ~
~ / \/ o ~ ~
~ / /\ - | ~ daniel@thebelowdomain ~
~ _____ / \ | ~ http://www.djs-consulting.com ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ GEEKCODE 3.12 GCS/IT d s-:+ a C++ L++ E--- W++ N++ o? K- w$ ~
~ !O M-- V PS+ PE++ Y? !PGP t+ 5? X+ R* tv b+ DI++ D+ G- e ~
~ h---- r+++ z++++ ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~
| |
| Dragfameson 2005-02-06, 9:07 am |
| Wow, thanks for all the help William and LX-i
This actually isnt homework, but I wish it was... because at least it would be fun! Like I said, I am a new student. To be more specific - I just started COBOL 1 this semester and only had 2 class's and 2 labs so far. The labs only consisted of copying a sample program from the book and then editing a few things. It's just an easy sample which reads employee data from input.dat, multiplies hours by rate and then writes totals to output.dat.
I'm actually doing this on my own because I like teaching myself better, and it can only help me in the class room.
As far as which compiler I'm using: Its Microfocus at the school, but at home I'm using Fujitsu Cobol V3.
I'll have to read into that Function Random and Evaluate statement before I can continue, since I know nothing about them yet. | |
| epc8@juno.com 2005-02-06, 8:55 pm |
|
LX-i wrote:
> Dragfameson wrote:
language![color=darkred]
>
> That must be why this smells like homework... :)
>
into a[color=darkred]
>
> There's an "Intrinsic Function" that will do that - check out
Function
> Random.
[snip]
> Sure do. I'll show you how I would do it in W/S... (You've also got
to
> account for the four suits (and jokers, if you want 'em), and making
> sure you don't pick the same cards twice...)
>
> 01 playing-cards.
> 12 spades pic X(13) value "234567890JQKA".
> 12 diamonds pic X(13) value "234567890JQKA".
> 12 clubs pic X(13) value "234567890JQKA".
> 12 hearts pic X(13) value "234567890JQKA".
> 12 jokers pic X(02) value "!!".
> 01 deck-of-cards redefines cards.
> 12 playing-card occurs 54 times pic X(01).
>
> Use Function Random to find your index into the table, and store them
> off. You can use the "remainder" functionality of the divide
statement
> to determine the card's value (keeping in mind that an Ace can be 1
or
> 11). For display the suit/card to the user, you'll need to determine
> the range of the index. Check out the "Evaluate" statement, and its
> Thru capabilities for contiguous values.
>
> Then, once you've determined that you don't have the same card, and
> you've calculated a value for each card, you're good to go.
To avoid keeping track of which cards were already drawn, first fill a
table with the numbers 1 to 52 in order, then shuffle them. See Knuth,
vol 2, page 139.
At each step there are i cards left to shuffle. We generate a random
number j from 1 to i, then swap the ith and jth cards. Decrease i until
1 card remains.
(fujitsu cobol v3.0)
identification division.
program-id. shuffle.
data division.
working-storage section.
01 d.
02 c pic 99 occurs 52 times.
01 i pic 99.
01 j pic 99.
01 t pic 99.
procedure division.
main.
perform varying i from 1 by 1 until i is greater than 52
move i to c(i)
end-perform.
perform varying i from 52 by -1 until i is less than 2
compute j = 1 + i * function random
move c(i) to t
move c(j) to c(i)
move t to c(j)
end-perform.
display d.
stop run.
| |
| Dragfameson 2005-02-07, 6:29 pm |
| epc8@juno.com
Thanks for the response. Can you (or someone) explain the logic to me please?
I dont understand it.
I did compile and run this code and the output was one long string of numbers.
Thanks,
Jesse |
|
|
|
|