I just started using Godot, and I am attempting to build a Yahtzee-type game on my own. I created a function to evaluate the dice/roll and have it working correctly, I believe. I was hoping to get some more experienced eyes to look over my solution and provide some helpful feedback/criticism.
Instead of storing each die as an individual value, my first instinct was to group values, kind of tallying the total of each side rolled. I'm mainly curious if this is efficient enough and if someone can spot an edge case. But I'm also interested in improving it, so anything helpful is appreciated.
Edit:
I noticed this as well: When I duplicate the array in the chunk, I evaluating for "Pairs" and remove at that index. Using .Find(2)
instead of .Any()
in the following if statement will return true in cases where it shouldn't (only 1 pair). I looked at both functions in the class definition, and I am still confused about why they act like they do. Maybe someone can explain?
var ones: int = 0
var twos: int = 0
var threes: int = 0
var fours: int = 0
var fives: int = 0
var sixes: int = 0
var straightIndex: = 0
func evalute_roll():
var roll: Array[int] = [ones,twos,threes,fours,fives,sixes]
if roll.any(func(number): return number == 5): # Yatzhee
rollText.text = "Yatzee!"
elif roll.any(func(number): return number == 4): # 4 of a Kind
rollText.text = "4 of a Kind!"
elif roll.any(func(number): return number == 3): # Trips or Full House
if roll.any(func(number): return number == 2):
rollText.text = "Full House!"
else:
rollText.text = "3 of a Kind!"
elif roll.any(func(number): return number == 2): # Pair or 2Pair, Identify Pair Type.
var side: String = ""
var tempArray = roll.duplicate()
var index: int = tempArray.find(2)
tempArray.remove_at(index)
if tempArray.any(func(number): return number == 2):
rollText.text = "2 Pair!"
else:
match index:
0: side = "Ones"
1: side = "Twos"
2: side = "Threes"
3: side = "Fours"
4: side = "Fives"
5: side = "Sixes"
rollText.text = "Pair of " + side
elif roll.any(func(number): return number == 1): # Small and Large Straight
for n in roll.size():
if roll[n] == 1:
straightIndex += 1
if straightIndex == 4:
rollText.text = "Small Straight!"
elif straightIndex == 5:
rollText.text = "Large Straight!"
elif roll[n] == 0 and straightIndex != 0:
straightIndex -= 1
else:
rollText.text = "Scratch"
ones
..sixes
declarations are pretty odd, given that you don't even use them. It would be more natural to just fill theroll[]
array, and document the meaning of its entries. // As far as readability goes, I confess it took me a long moment to figure out that "roll" here is a noun, not a verb. It's a good identifier; keep it as-is. \$\endgroup\$