C
C#•4mo ago
Bak Lava

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
TheBoxyBear
TheBoxyBear•4mo ago
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) 🙂
Bak Lava
Bak Lava•4mo ago
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.
Bak Lava
Bak Lava•4mo ago
No description
TheBoxyBear
TheBoxyBear•4mo ago
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 false
Bak Lava
Bak Lava•4mo ago
thanks, I will see what I can do with that
TheBoxyBear
TheBoxyBear•4mo ago
Just removing the line basically. The array is stored as a member of the form so the values are kept after exiting a method
Bak Lava
Bak Lava•4mo ago
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
TheBoxyBear
TheBoxyBear•4mo ago
However you would need to initialize the array when creating the form like with DiceValues
Bak Lava
Bak Lava•4mo ago
Sometimes wish there weren't "a million ways to skin a cat"
TheBoxyBear
TheBoxyBear•4mo ago
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
Bak Lava
Bak Lava•4mo ago
As in, you can code something 10 different ways and still get the same result.
TheBoxyBear
TheBoxyBear•4mo ago
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
Bak Lava
Bak Lava•4mo ago
I appreciate your help. I am going to try your recommendations and see if I can make this work better and more efficient
TheBoxyBear
TheBoxyBear•4mo ago
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.
int counter_1 = 0;
int counter_2 = 0;
int counter_3 = 0;
int counter_4 = 0;
int counter_5 = 0;
int counter_6 = 0;

foreach
if else if else if......
int counter_1 = 0;
int counter_2 = 0;
int counter_3 = 0;
int counter_4 = 0;
int counter_5 = 0;
int counter_6 = 0;

foreach
if else if else if......
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% 🙂
Bak Lava
Bak Lava•4mo ago
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.