Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey There all! this is my first post so thanks for letting me post my question to the community about my little dice game called Greedy I was able to piece together for fun in my spare time by watching and reading other posts (including those found here before I signed up myself). What I have so far is working well by itself but now I want to add some additional functionality to make the game a little more challenging.

It is a six dice game I learned from a friend years ago and various combos of random dice results would equal specific point values. Currently what I have in place accurately detects any of the point outcomes in each 6 dice roll and displays it into a text field allowing a player to write down what was earned and keep track of how high of a score they go... but that is a bit redundant in itself and interest in playing can be lost quickly because there is no excitement built-in to the application like there was when playing with friends a few years ago as mentioned.

To change that I want to incorporate Checkboxes under (or make the graphic I use to display the various Dice rolled a button to hold them) for each of the dice to decide to hold or not -similarly to Yahtzee- before the next roll. Each time a dice is locked it will remain that way until all dice have been used or score before they can be used again to continue earning points.

Question 1: How do I get the checkboxes to keep up to all 6 dice from being re rolled while allowing the others to continue until a non scoring roll is made?

When the Button is pressed I have this:

C#
private void btn_RollDice_Click(object sender, EventArgs e)
        {
            RollDice();

            GetResults();

            ResetResults();
}


With each Button press the labels I have showing dice get reset but the image stays until the Roll button is pressed again.

Question 2: Do I need to take out he Reset for this to work or how can it be modified to accommodate not resetting the checked dice before the Roll button is pressed again.

Here is the Reset section:
C#
private void ResetResults()
        {
            for (int i = 0; i < diceResults.Length; i++)
                diceResults[i] = 0;
        }


Question 3: Do I need to modify the following sections for each checkBox (1-6) to allow the checkbox to determine weather holding the label associated with the checkbox can happen or not?

C#
private void checkBox1_CheckChanged(object sender, EventArgs e)


Anyway. I'll stop there, Any assistance would be greatly appreciated. I look forward to your replies.

I see what you have suggested and it makes sense but I was not sure how to apply it to my specific game project using the variables I already have in place. As you can tell my skills need some polishing so here is the whole code:

C#
//Game structure code and Scoring Logic by RCEditor Sept, 2017. Thanks all for the assist in advance of any contribution to this project.
//Please add name or handle here to be credited with any additions. These comments are to remain with this source code indefinately.

#region Using Statements

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
#endregion

namespace DiceGame
{         
    public partial class Form1 : Form
    {
        #region Declaration

        Image[] diceImages;
        int[] dice;
        int[] diceResults;
        Random rand;
                
        #endregion

        #region Initialization

        public Form1()
        {
            InitializeComponent();

        }              

        private void Form1_Load(object sender, EventArgs e)
        {            
            diceImages = new Image[7];
            diceImages[0] = Properties.Resources.BlankDice;
            diceImages[1] = Properties.Resources.Dice1;
            diceImages[2] = Properties.Resources.Dice2;
            diceImages[3] = Properties.Resources.Dice3;
            diceImages[4] = Properties.Resources.Dice4;
            diceImages[5] = Properties.Resources.Dice5;
            diceImages[6] = Properties.Resources.Dice6;

            dice = new int[6] { 0, 0, 0, 0, 0, 0 };

            diceResults = new int[6] { 0, 0, 0, 0, 0, 0 };

            rand = new Random();
            
        }

        public class Die
        {
            public bool SetState { get; set; }
            public int Value { get; set; }
        }

        #endregion

        #region Private Methods
                
        private void RollDice()
        {
            for (int i = 0; i < dice.Length; i++)
            {
                dice[i] = rand.Next(1, 6 + 1);

                switch (dice[i])
                {
                    case 1:
                        diceResults[0]++;
                        break;
                    case 2:
                        diceResults[1]++;
                        break;
                    case 3:
                        diceResults[2]++;
                        break;
                    case 4:
                        diceResults[3]++;
                        break;
                    case 5:
                        diceResults[4]++;
                        break;
                    case 6:
                        diceResults[5]++;
                        break;
                }
            }

            label1.Image = diceImages[dice[0]];
            label2.Image = diceImages[dice[1]];
            label3.Image = diceImages[dice[2]];
            label4.Image = diceImages[dice[3]];
            label5.Image = diceImages[dice[4]];
            label6.Image = diceImages[dice[5]];
            
        }
                

        private void btn_RollDice_Click(object sender, EventArgs e)
        {
            RollDice();
            
            GetResults();

            ResetResults();
        }

        private void GetResults()
        {
            bool oneThroughSix = false, allDoubles = false, threeOnes = false, threeOnesPlus = false,
                threeTwos = false, threeTwosPlus = false, threeThrees = false, threeThreesPlus = false, threeFours = false, threeFoursPlus = false,
                threeFives = false, threeFivesPlus = false, threeSixes = false, threeSixesPlus = false, aOne = false, aFive = false,
                haveOneDouble = false, haveTwoDoubles = false, aBust = false, haveSix = false, haveFive = false, haveFour = false, haveThree = false,
                haveTwo = false, haveOne = false;



            #region Scoring Logic
            // Look for 1-6 Bonus
            if (diceResults[0] == 1 &&
                        diceResults[1] == 1 &&
                        diceResults[2] == 1 &&
                        diceResults[3] == 1 &&
                        diceResults[4] == 1 &&
                        diceResults[5] == 1)
                oneThroughSix = true;

            for (int i = 0; i < diceResults.Length; i++)
            {
                // Look for All doubles Bonus
                if (diceResults[0] == 2 &&
                     diceResults[1] == 2 &&
                     diceResults[2] == 2)
                    allDoubles = true;
                else if (diceResults[1] == 2 &&
                     diceResults[2] == 2 &&
                     diceResults[3] == 2)
                    allDoubles = true;
                else if (diceResults[2] == 2 &&
                     diceResults[3] == 2 &&
                     diceResults[4] == 2)
                    allDoubles = true;
                else if (diceResults[3] == 2 &&
                     diceResults[4] == 2 &&
                     diceResults[5] == 2)
                    allDoubles = true;
                else if (diceResults[4] == 2 &&
                     diceResults[3] == 2 &&
                     diceResults[1] == 2)
                    allDoubles = true;

                else if (diceResults[2] == 2 &&
                    diceResults[3] == 2 &&
                    diceResults[5] == 2)
                    allDoubles = true;
                else if (diceResults[3] == 2 &&
                    diceResults[1] == 2 &&
                    diceResults[0] == 2)
                    allDoubles = true;
                else if (diceResults[3] == 2 &&
                    diceResults[4] == 2 &&
                    diceResults[5] == 2)
                    allDoubles = true;
                else if (diceResults[4] == 2 &&
                    diceResults[5] == 2 &&
                    diceResults[1] == 2)
                    allDoubles = true;
                else if (diceResults[4] == 2 &&
                    diceResults[2] == 2 &&
                    diceResults[5] == 2)
                    allDoubles = true;
                else if (diceResults[0] == 2 &&
                    diceResults[2] == 2 &&
                    diceResults[3] == 2)
                    allDoubles = true;
                else if (diceResults[0] == 2 &&
                    diceResults[2] == 2 &&
                    diceResults[4] == 2)
                    allDoubles = true;
                else if (diceResults[0] == 2 &&
                    diceResults[2] == 2 &&
                    diceResults[5] == 2)
                    allDoubles = true;
                else if (diceResults[0] == 2 &&
                    diceResults[3] == 2 &&
                    diceResults[4] == 2)
                    allDoubles = true;
                else if (diceResults[0] == 2 &&
                    diceResults[3] == 2 &&
                    diceResults[5] == 2)
                    allDoubles = true;
                else if (diceResults[0] == 2 &&
                    diceResults[4] == 2 &&
                    diceResults[5] == 2)
                    allDoubles = true;
                else if (diceResults[2] == 2 &&
                    diceResults[4] == 2 &&
                    diceResults[1] == 2)
                    allDoubles = true;

                else if (diceResults[5] == 2 &&
                    diceResults[0] == 2 &&
                    diceResults[1] == 2)
                    allDoubles = true;
                else if (diceResults[5] == 2 &&
                    diceResults[1] == 2 &&
                    diceResults[2] == 2)
                    allDoubles = true;
                else if (diceResults[3] == 2 &&
                    diceResults[5] == 2 &&
                    diceResults[1] == 2)
                    allDoubles = true;

                else if (diceResults[0] == 3)
                    threeOnes = true;
                else if (diceResults[0] == 4)
                    threeOnesPlus = true;
                else if (diceResults[0] == 5)
                    threeOnesPlus = true;

                else if (diceResults[5] == 3)
                    threeSixes = true;
                else if (diceResults[5] == 4)
                    threeSixesPlus = true;
                else if (diceResults[5] == 5)
                    threeSixesPlus = true;

                else if (diceResults[4] == 3)
                    threeFives = true;
                else if (diceResults[4] == 4)
                    threeFivesPlus = true;
                else if (diceResults[4] == 5)
                    threeFivesPlus = true;

                else if (diceResults[3] == 3)
                    threeFours = true;
                else if (diceResults[3] == 4)
                    threeFoursPlus = true;
                else if (diceResults[3] == 5)
                    threeFoursPlus = true;

                else if (diceResults[2] == 3)
                    threeThrees = true;
                else if (diceResults[2] == 4)
                    threeThreesPlus = true;
                else if (diceResults[2] == 5)
                    threeThreesPlus = true;

                else if (diceResults[1] == 3)
                    threeTwos = true;
                else if (diceResults[1] == 4)
                    threeTwosPlus = true;
                else if (diceResults[1] == 5)
                    threeTwosPlus = true;
                //score a 1
                else if (diceResults[0] == 1)
                    aOne = true;
                else if (diceResults[0] == 2)
                    aOne = true;
                //score a 5
                else if (diceResults[4] == 1)
                    aFive = true;

                //look for two doubles
                for (int j = 0; j < diceResults.Length; j++)
                {
                    if (diceResults[1] == 4)
                        haveTwoDoubles = true;
                    else if (diceResults[0] == 2 &&
                            diceResults[1] == 2)
                        haveTwoDoubles = true;
                    else if (diceResults[0] == 2 &&
                            diceResults[2] == 2)
                        haveTwoDoubles = true;
                    else if (diceResults[0] == 2 &&
                            diceResults[3] == 2)
                        haveTwoDoubles = true;
                    else if (diceResults[0] == 2 &&
                            diceResults[4] == 2)
                        haveTwoDoubles = true;
                    else if (diceResults[0] == 2 &&
                            diceResults[5] == 2)
                        haveTwoDoubles = true;                    
                    else if (diceResults[4] == 2 &&
                            diceResults[1] == 2)
                        haveTwoDoubles = true;
                    else if (diceResults[4] == 2 &&
                            diceResults[2] == 2)
                        haveTwoDoubles = true;
                    else if (diceResults[4] == 2 &&
                            diceResults[3] == 2)
                        haveTwoDoubles = true;
                    else if (diceResults[4] == 4)
                        haveTwoDoubles = true;
                    else if (diceResults[4] == 2 &&
                            diceResults[5] == 2)
                        haveTwoDoubles = true;
                }

                // look for doubles
                if (diceResults[0] == 2)
                    haveOneDouble = true;
                else if (diceResults[4] == 2)
                    haveOneDouble = true;

                                        // look for Bust
                else if (diceResults[1] == 1)
                    aBust = true;
                else if (diceResults[2] == 1)
                    aBust = true;
                else if (diceResults[3] == 1)
                    aBust = true;
                else if (diceResults[5] == 1)
                    aBust = true;

            }

            for (int i = 0; i < dice.Length; i++)
            {
                switch (dice[i])
                {
                    case 6:
                        haveSix = true;
                        break;
                    case 5:
                        haveFive = true;
                        break;
                    case 4:
                        haveFour = true;
                        break;
                    case 3:
                        haveThree = true;
                        break;
                    case 2:
                        haveTwo = true;
                        break;
                    case 1:
                        haveOne = true;
                        break;
                }

 #endregion

 #region Message Output
            }

            if (oneThroughSix)
                lbl_displayResults.Text = "One through Six is worth 2000 points!";
            else if (allDoubles)
                lbl_displayResults.Text = "All Doubles are worth 1500 points!";
            else if (threeOnes)
                lbl_displayResults.Text = "Three Ones are worth 1000 points!";
            else if (threeOnesPlus)
                lbl_displayResults.Text = "Three Ones are worth 1000 points and remain consecutive at that value until next roll.";
            else if (threeSixes)
                lbl_displayResults.Text = "Three Sixes are worth 600 points!";
            else if (threeSixesPlus)
                lbl_displayResults.Text = "Three Sixes are worth 600 points and remain consecutive at that value until next roll.";
            else if (threeFives)
                lbl_displayResults.Text = "Three Fives are worth 500 points!";
            else if (threeFivesPlus)
                lbl_displayResults.Text = "Three Fives are worth 500 points and remain consecutive at that value until next roll.";
            else if (threeFours)
                lbl_displayResults.Text = "Three Fours are worth 400 points!";
            else if (threeFoursPlus)
                lbl_displayResults.Text = "Three Fours are worth 400 points and remain consecutive at that value until next roll.";
            else if (threeThrees)
                lbl_displayResults.Text = "Three Threes are worth 300 points!";
            else if (threeThreesPlus)
                lbl_displayResults.Text = "Three Threes are worth 300 points and remain consecutive at that value until next roll.";
            else if (threeTwos)
                lbl_displayResults.Text = "Three Twos are worth 200 points!";
            else if (threeTwosPlus)
                lbl_displayResults.Text = "Three Twos are worth 200 points and remain consecutive at that value until next roll.";
            else if (aOne)
                lbl_displayResults.Text = "Ones are always worth 100 points!";
            else if (aFive)
                lbl_displayResults.Text = "Fives are always worth 50 points!";
            else if (haveTwoDoubles)
                lbl_displayResults.Text = "Two Doubles only score if 1's or 5's\r\n(1's = 100 and 5's = 50)";
            else if (haveOneDouble)
                lbl_displayResults.Text = "Doubles only score if 1's or 5's\r\n(1's = 100 and 5's = 50)";
            else if (aBust)
                lbl_displayResults.Text = "You were too GREEDY!!!\r\nNothing scored!\r\n-GAME OVER-";
        }


        private void ResetResults()
        {            
            for (int i = 0; i < diceResults.Length; i++)
                diceResults[i] = 0;
        }

        #endregion
        

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void lbl_displayResults_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
 }
#endregion


What I have tried:

I have tried assigning each checkbox a boolean value of false and having it become true when checked but could not fill in the gap to associate this action with holding the checked dice before next re-roll. Seems like it should work but I am unsure where to have the result of the checkbox being selected or not in the code to make sure it does not roll those selected when the
C#
btn_RollDice_Clicked
AKA The Roll Button variable is next clicked.

I have a couple more snags I have run into so far but I will ask those in a later post if still unclear after the community here puts its heads together to provide a working solution!

Anyway.... Chat with you soon!

And Thanks in advance for taking the time to look over what I have posted here.

-Regards
Posted
Updated 2-Sep-17 20:17pm
v4

1 solution

There are many ways of doing this. The simplest is a Die (single Dice) class that holds the value and the set state. Something like:
C#
public class Die
{
    public bool SetState { get; set; }
    public int Value {get; set; }
}

And hold the Die in a Dice collection:
C#
public List<Die> Dice { get; } = new List<Die>
{
   new Die(),
   new Die(),
   new Die(),
   new Die(),
   new Die(),
   new Die()
}

And when it is time to roll:
C#
foreach(var die in Dice.Where(x => !x.SetState))
{
   // we can roll the die that is not set...
}

As you the check image, set the PictureBox (check image) Visible property dependant on the SetState property of the Die class.
 
Share this answer
 
Comments
Graeme_Grant 3-Sep-17 1:12am    
That is because your code belongs above in the question and not in the replies. Click on the "Improve question" widget and place your code there. Once done, delete all the replies with code.
RCEditor 3-Sep-17 2:05am    
Thanks! Done ;)
RCEditor 3-Sep-17 2:23am    
Did I put the:

public class Die
{
public bool SetState { get; set; }
public int Value { get; set; }
}
section you suggested in the correct place?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900