Need help in getting my dice to hold and release in a yahtzee type game.
I am having issues with being able to hold and release dice in a yahtzee game. This was working, but when I went away from forcing the dice to a specific player, it stopped working. I'm not sure if I am on the right track and need some help. Can someone take a look at the events for checking the checkboxes and holding and releasing the dice?
15 Replies
Related to line 64?
That seems off as you're evaluating values in a newly created array without putting values in said array so it will always have the default values
Also if it helps avoid repetition with the UI, you can create arrays of controls (labels, textboxes, etc) 🙂
Not sure I completely understand what the issue is with line 64.
When the roll button is clicked, it should roll the dice. Before the first roll, no dice will be held. The user can then click the checkbox underneath the die to save it. When the roll button is clicked a second time, it should check to see if any dice are held and then continue to roll the remaining dice. The user can click the roll button a 3rd time, it should check to see if any dice are held. If at any time the dice are released, it will reroll those dice as well. As I said this was working before, but I've made so many changes to the logic I can't seem to get it back working.
In the RollDice method, you indeed check for the hold status of each dice, however the line right above it
HoldStatus = new bool[5];
resets the array to be all falsethanks, I will see what I can do with that
Just removing the line basically. The array is stored as a member of the form so the values are kept after exiting a method
And I found a way to dynamically create buttons, trying to figure out how that will work with the player scores, but will definitely come in handy with the dice instead of using checkboxes below them
However you would need to initialize the array when creating the form like with DiceValues
Sometimes wish there weren't "a million ways to skin a cat"
As in different syntaxes? Sometimes I feel the same, it's mostly a result of the language evolving while trying to remaining compatible with legacy code and practices
As in, you can code something 10 different ways and still get the same result.
And sometimes literally the same result once compiled (keyword: syntax sugar)
But it can help with the clarity of your code even if the resulting instructions don't need to change
I appreciate your help. I am going to try your recommendations and see if I can make this work better and more efficient
And like I mentionned, I see you're already using loops and arrays and that's good, but for your case, you can use them a lot more often to avoid repetition. can be turned into an array of counters which you access via the dice value. For the labels, you can also have a
Label[]
storing the fields lblOnesP1
, lblTwosP1
, etc and iterate through that to assign the Text property.
With that and reusing event handlers, you could probably reduce your code by 50-60% 🙂Thank you. My knowledge is limited, but I am definitely taking these into consideration. Currently, trying to reproduce the looks of the UI using dynamically created controls.