ok im working on my second project(and probably "in engineering overkill")
but have you ever played the dice game "10,000" or "farkle" (as ive seen it called in the midwest)its alot like yahtzee. if not then heres the skinny of it: a player gets 5 (some ppl play with 6) dice and at minimum has to roll atleast a one or a 5, (thats the very minimum if you dont roll any point value dice then turns over) but then theres 3 of a kind, a straight, and that sorts of scoring. a player can keep rolling as long as they got a point value, minus the dice that scored; ie: if a player got three 6's (in one roll, you cant keep 2 from 1 roll and one from another to make three of a kind) then they keep those three and roll the last two. 1s and 5s are the only single digit dice that can score, so if the player gets two 1s or 5s (or combo of two) then the player gets to roll all five again and their score keeps adding up until they decide to stop rolling (if you only have 1 die and you decide to roll it and it doesnt score then your turns over and you lose all the points accumulated for that roll) ok now that weve got the idea of the game i was wondering if any1(hartnell most likely) had any pointers on 1) any loops to make less works 2) the scoring and 3) anything else i may want to consider thanx in advance
To keep track of the values of the dice, I suggest using an array. An array is like a variable, but it has multiple slots. It's like having multiple variables in one. For example, you have 5 dice, you declare the array like this :
dim dice(5) as integer
Then you can assign each die's value like so :
dice(1) = 2
dice(2) = 3
dice(3) = 6
' etc
because this array is going to be used in subroutines and functions, I suggest dimming it as shared. That way your subs and functions can see it :
dim shared dice(5) as integer
Here's a subroutine to randomly roll all the dice.
sub rollDice
for i = 1 to 5
dice(i) = int(rnd(1)*6)+1
next i
end sub
Anytime you want to roll all the dice, just call the subroutine by it's name.
rollDice
Here's an example of all this together :
randomize timer ' see the random number generator
dim shared dice(5) as integer
sub rollDice
for i = 1 to 5
dice(i) = int(rnd(1)*6)+1
next i
end sub
rollDice ' call the sub
' print out each die
for i = 1 to 5
print dice(i)
next i
sleep
end
That should get you started. As always, I'll do my best to answer further questions.
—hartnell
ok first question 1) how do i set each die as a variable so the user can decide which dice to keep with a for…next loop? that would have to be reset for every player/turn…..right? 2)how do i return to the previous results if the player inputs a letter/word thats not y/n on the 'would you like to keep die 1?' input part of the code? heres what i have so far:
Print "Welcome to 10,000"
Dim As Integer nop
Dim As String p1n, p2n, p3n, p4n, p5n, p6n, ans
Sleep
playerselection:
Cls
Input "How many players "; nop
If nop = 1 Then
Print "You cant Play by yourself!!!"
Print "(press any key)"
Sleep
Goto playerselection
Elseif nop = 2 Then
Goto p2
Elseif nop = 3 Then
Goto p3
Elseif nop = 4 Then
Goto p4
Elseif nop = 5 Then
Goto p5
Elseif nop = 6 Then
Goto p6
End If
p2:
Cls
Input "Player one enter your name:", p1n
Cls
Input "Player two enter your name:", p2n
Cls
Goto game
p3:
Cls
Input "Player one enter your name:", p1n
Cls
Input "Player two enter your name:", p2n
Cls
Input "Player three enter your name:", p3n
Cls
Goto game
p4:
Cls
Input "Player one enter your name:", p1n
Cls
Input "Player two enter your name:", p2n
Cls
Input "Player three enter your name:", p3n
Cls
Input "Player four enter your name:", p4n
Cls
Goto game
p5:
Cls
Input "Player one enter your name:", p1n
Cls
Input "Player two enter your name:", p2n
Cls
Input "Player three enter your name:", p3n
Cls
Input "Player four enter your name:", p4n
Cls
Input "Player five enter your name:", p5n
Cls
Goto game
p6:
Cls
Input "Player one enter your name:", p1n
Cls
Input "Player two enter your name:", p2n
Cls
Input "Player three enter your name:", p3n
Cls
Input "Player four enter your name:", p4n
Cls
Input "Player five enter your name:", p5n
Cls
Input "Player six enter your name:", p6n
Cls
Goto game
game:
Print p1n; " rolls first"
Print
Print
Print "Press any key to roll"
Sleep
roll1:
Cls
Randomize Timer
dim shared dice(5) as integer
dim shared d as integer
sub rollDice
for i = 1 to 5
dice(i) = int(rnd(1)*6)+1
next i
end sub
rollDice
for i = 1 to 5
results1:
print "die "; i;": "; dice(i)
next i
Print
Print
Print
keep1a:
input "Would you like to keep Die 1? ", ans
if ans = "y" then
print "Die kept"
goto roll_a
elseif ans = "n" then
print "Die not kept"
goto roll_b
else
print "Please type [Y/N] on your keyboard, followed by [Enter]"
print "(press any key to continue)"
sleep
goto results1
end if
roll_a:
input "Would you like to keep die 2? ", ans
if ans = "y" then
print "Die kept"
goto roll_a1
elseif ans = "n" then
print "Die not kept"
goto roll_bb1
else
print "Please type [Y/N] on your keyboard, followed by [Enter]"
print "(press any key to continue)"
sleep
goto results1
end if
roll_b:
roll_a1:
roll_bb1:
Sleep
dont worry about the labels at the bottom, while i was writing it i was thinking that i was going to have to goto each section depending on the users decision on each dice and had to put the labels so i could test! thats not my problem….. my problem is when the user inputs anything other than [y/n] it just goes straight to 'die 6: 0 would you like to keep die 1?' (compile and run you'll see) the only other way i can figure out is:
Randomize Timer
Print
Die1 = Int(Rnd * 6 + 1)
Die2 = Int(Rnd * 6 + 1)
Die3 = Int(Rnd * 6 + 1)
Die4 = Int(Rnd * 6 + 1)
Die5 = Int(Rnd * 6 + 1)
Die6 = Int(Rnd * 6 + 1)
results1:
Print "Die 1: "; Die1
Print "Die 2: "; Die2
Print "Die 3: "; Die3
Print "Die 4: "; Die4
Print "Die 5: "; Die5
Print "Die 6: "; Die6
Print
Print
Print
keep1a:
input "Would you like to keep Die 1? ", ans
if ans = "y" then
print "Die kept"
goto roll_a
elseif ans = "n" then
print "Die not kept"
goto roll_b
else
print "Please type [Y/N] on your keyboard, followed by [Enter]"
print "(press any key to continue)"
sleep
goto results1
end if
again the only goto/label in question is the "results1" (disregard all other goto/labels, these are from one of multiple saves/versions/experiments!!!)whew!!! now THATS a question!!!!!!!!
Here's what I came up with. It's alot of code you don't know so I'll explain it bit by bit after the listing :
randomize timer
dim choice as string
dim keepLooping as integer
dim shared dice(6) as integer
dim shared keptdice(6) as integer
sub resetDice
for i = 1 to 6
keptDice(i) = 0
next i
end sub
sub rollDice
for i = 1 to 6
if keptDice(i) = 0 then dice(i) = int(rnd(1)*6)+1
next i
end sub
sub printDice
print
for i = 1 to 6
print "Die"; i; ": "; dice(i);
if keptDice(i) = 1 then
print " Kept"
else
print " Not Kept"
end if
next i
end sub
resetDice
rollDice
keepLooping = 1
do
cls
printDice
print "Which die would you like to keep? (e) to end keeping."
input "> ", choice
if choice = "e" then
print "Die keeping phase over"
keepLooping = 0
elseif val(choice) > 0 and val(choice) < 7 then
print "Die "; choice; " kept."
keptDice(val(choice)) = 1
else
print "Enter the number of the die to keep or else Enter d"
end if
sleep 1000, 1
loop until not keepLooping
print "Press any key"
sleep
Start the explanation here :
dim keepLooping as integer
Used to control the do loop. Detailed later.
dim choice as string
Used to hold the player's choice. Detailed later.
dim shared dice(6) as integer
dim shared keptDice(6) as integer
These are two arrays used to keep track of the dice. dice() holds the actual number value of the die and keptDice() holds whether or not it has been kept. The value each element of keptdice() is 1 or 0. If the die is kept, it is 1. If the die hasn't been kept, it is 0.
sub resetDice
for i = 1 to 6
keptDice(i) = 0
next i
end sub
resetDice is a subroutine you can call anytime you want to reset the "kept status" of any die. All it does is go through the array and set all the elements to zero.
sub rollDice
for i = 1 to 6
if keptDice(i) = 0 then dice(i) = int(rnd(1)*6)+1
next i
end sub
rollDice is a subroutine you can use to, er, roll the dice. It goes through each element in the dice() array. During each iteration, it first checks to see if the die hasn't been kept if keptdice(i) = 0 and if it hasn't, then generates a random value. This subroutine is used in combination with resetDice :
If you want to start a fresh roll, first reset the "kept status" with resetDice and then generate a random value with rollDice.
resetDice ' clear kept status of all dice
rollDice ' roll the dice
If you want to roll again, just use rollDice as it figures in the "kept status" of each die :
rollDice
… more to come
sub printDice
print
for i = 1 to 6
print "Die"; i; ": "; dice(i);
if keptDice(i) = 1 then
print " Kept"
else
print " Not Kept"
end if
next i
end sub
printDice prints the dice. It prints the die number, the die's value, and whether or not it has been kept. You can use it anywhere to print the dice.
printDice
The do loop :
A do loop like this will loop infinately :
do
loop
Luckily we can make check a condition. In this case, the variable keepLooping is checked.
keepLooping = 1
do
loop until not keepLooping
This do loop will loop forever, or until keepLooping = 0. So, when you want to break out of this loop, just set keepLooping to 0. This is done when the player enters "e" to exit the die keeping process.
val(). Val converts a string to a number. Normally you cannot compare strings and numbers.
' this doesn't work
if "5" = 5 then print "Holy Type Mismatch Batman!"
So, we use val() to convert the string to a number before doing the comparison :
if val("5") = 5 then print "This does work"
Doing this allows the player to enter both text and numbers in the same input.
About if val(choice) > 0 and val(choice) < 7 then :
The player has 7 choices that he can make during the keeping phase. 1 through 6 and "e". "e" has already been checked. The above line checks to see if choice is a number between greater than 0 and less than 7 - in other words : a number between 1 and 6. If so, it sets the kept dice status of that die.
Finally, if the player didn't input "e" or a number between 1 or 6, then the game tells him the kind of input it expects.
That's it. Questions always welcome. —hartnell
One more thing : I wrote a dice game in FreeBASIC a couple of weeks ago. It's called Three Dice and the source code is available.
-hartnell
ok thanx! that was a very clear and descriptive explanation(thats what im talking about). so im gonna study this a little more and compare against the FB manual and your three dice game and hopefully i should have an alpha release for the works in progress section pretty soon (if not you'll be the first to know!!!!!!hehe ;) )consider the development te(I)am hard at work!
ok im having problem with "Three Dice" it just keeps looping to "what number do you want to bet on?" i'll type a no. 1-6 and press [enter] but it just asks "what number do you want to bet on?" again and again. if i press anything else then it will print "there are only 6………" and back to "what number do you want to bet on?" im sure i can figure it out (with a good ole goto statement im getting good at!!) but i was letting you know about this "bug"