I'm once more stuck on my Yahtzee game. I can't find out how to calculate wether or not the player got a small, or large straight with the dice in ANY order. If anyone could help I would appreciate it BIG time... I've been sitting here for hours wondering how the heck to do this.
Need help with game
Blitz3D Forums/Blitz3D Beginners Area/Need help with game Could you explain more clearly what your trying to achieve please :o)
I would make something like that:
function count1() : returns the number of dice with a "1" value
function count2() : returns the number of dice with a "2" value
...
and so on
then:
if count1() > 0 and count2() > 0 and count3() > 0 and count4() > 0 then ...
Hope this helps.
function count1() : returns the number of dice with a "1" value
function count2() : returns the number of dice with a "2" value
...
and so on
then:
if count1() > 0 and count2() > 0 and count3() > 0 and count4() > 0 then ...
Hope this helps.
Sort the array or type holding your dice values.
Check if each is +1 the previous.
If it is increment a count.
If the count is 4 or 5 then there's a straight available.
I think.
<edit> Hmmm. not quite right.... or it might be.
Try this...
Check if each is +1 the previous.
If it is increment a count.
If the count is 4 or 5 then there's a straight available.
I think.
For x = 0 To 4 ; or however many die there are in yahtzee. If dice(x)+1 = dice(x+1) count=count+1 Next
<edit> Hmmm. not quite right.... or it might be.
Try this...
Graphics 640,480 SetBuffer BackBuffer() Dim dice(4) SeedRnd MilliSecs() While Not KeyHit(1) count=0 Cls For x = 0 To 4 dice(x)=Rand(1,6) ; Text 0,x*12,dice(x) Next For i%=3 To 1 Step -1 For j%=0 To i%-1 If dice(j%)>dice(j%+1) Temp%=dice(j%) dice(j%)=dice(j%+1) dice(j%+1)=Temp% EndIf Next Next For x = 0 To 4 Text 0,x*12,dice(x) Next For x = 0 To 3 If dice(x)+1 = dice(x+1) count=count+1 Next If count = 3 Text 50,0,"small straight" If count = 4 Text 50,0,"Large straight" Flip If count>2 WaitKey() Wend
A simple way to tackle this might be to sort the dice into ascending order (fits with previous suggestion to sort when looking for 3/4/5 of a kind) and then create a string from them.
Case 1: string = "12345" or string = "23456" --> long straight (exit checking routine)
Case 2: substring = "1234" or substring = "2345" or substring = "3456" --> short straight
Case 1: string = "12345" or string = "23456" --> long straight (exit checking routine)
Case 2: substring = "1234" or substring = "2345" or substring = "3456" --> short straight
Nah, you don't have to sort the dice - simply calculate how many N-of-a-kinds you have, which you'll have to do for most of the rest of your tests anyways (e.g. full house, 3 of a kind, etc). Check out the score_dice$() function to see how simple the logic gets once you've aggregated this data.
The long straight is simple: it's 6 one-of-a-kinds.
The most difficult problem is the short straight, but consider that a 5-length straight will yield 4 one-of-a-kinds and there will either be 6's or (xor!) 1's present.
EDIT: Whoops! That worked for 6 dice but didn't scale for 5.
The long straight is simple: it's 6 one-of-a-kinds.
The most difficult problem is the short straight, but consider that a 5-length straight will yield 4 one-of-a-kinds and there will either be 6's or (xor!) 1's present.
EDIT: Whoops! That worked for 6 dice but didn't scale for 5.
Ok, here's a correct solution for 5 dice. I ended up needing to create a straight_check() function for the short straight after all. Can't do this with N-of-a-kind data very easily for 5 dice; however, the value_count data comes in very handy.
Essentially the short straight test works like this:
are there 1 or more 1's? 2's? 3's? 4's? if all yes, return true
are there 1 or more 2's? 3's? 4's? 5's? if all yes, return true
are there 1 or more 3's? 4's? 5's? 6's? if all yes, return true
And the long straight test is:
are there 1 or more 1's? 2's? 3's? 4's? 5's? if all yes, return true
are there 1 or more 2's? 3's? 4's? 5's? 6's? if all yes, return true
Works for 6 dice too if you change the const.
Essentially the short straight test works like this:
are there 1 or more 1's? 2's? 3's? 4's? if all yes, return true
are there 1 or more 2's? 3's? 4's? 5's? if all yes, return true
are there 1 or more 3's? 4's? 5's? 6's? if all yes, return true
And the long straight test is:
are there 1 or more 1's? 2's? 3's? 4's? 5's? if all yes, return true
are there 1 or more 2's? 3's? 4's? 5's? 6's? if all yes, return true
Const DICE = 5 Global die_value[DICE-1] Global value_count[6] Global of_a_kinds[DICE] Function roll_all_dice() For i = 0 To DICE-1 die_value[i] = Rand(1, 6) Next update_aggregates() End Function Function update_aggregates() For i = 1 To 6 value_count[i] = 0 Next For i = 0 To DICE-1 value_count[die_value[i]] = value_count[die_value[i]] + 1 Next For i = 0 To DICE-1 of_a_kinds[i] = 0 Next For i = 1 To 6 of_a_kinds[value_count[i]] = of_a_kinds[value_count[i]] + 1 Next End Function Function straight_check(length = DICE) For i = 1 To 6-length+1 match = True For n = i To i+length-1 If value_count[n] = 0 Then match = False : Exit ; Exit is an unnecessary optimization - sorry, force of habit Next If match = True Then Return True Next Return False End Function Function score_dice$() If straight_check(DICE) Then Return "long straight" If straight_check(DICE-1) Then Return "short straight" If of_a_kinds[5] = 1 Then Return "5 of a kind" If of_a_kinds[4] = 1 Then Return "4 of a kind" If of_a_kinds[3] = 1 And of_a_kinds[2] = 1 Then Return "full house" If of_a_kinds[3] = 1 Then Return "3 of a kind" If of_a_kinds[2] = 2 Then Return "2 pair" If of_a_kinds[2] = 1 Then Return "pair" Return "nada" End Function SeedRnd MilliSecs() roll_all_dice() Print "roll:" For i = 0 To DICE-1 : Print " " + i + ": " + die_value[i] : Next Print "value_count:" For i = 1 To 6 : Print " " + i + "'s: " + value_count[i] : Next Print "of_a_kinds:" For i = 1 To DICE : Print " " + i + "-of-a-kinds: " + of_a_kinds[i] : Next Print score_dice$()
Works for 6 dice too if you change the const.