Need help with game

Blitz3D Forums/Blitz3D Beginners Area/Need help with game

Q(Posted 2005) [#1]
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.


Ross C(Posted 2005) [#2]
Could you explain more clearly what your trying to achieve please :o)


kragoth74(Posted 2005) [#3]
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.


tonyg(Posted 2005) [#4]
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.
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




BlackJumper(Posted 2005) [#5]
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


octothorpe(Posted 2005) [#6]
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.


octothorpe(Posted 2005) [#7]
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.