Click here to Skip to main content
15,921,905 members
Home / Discussions / C#
   

C#

 
QuestionC# working with strings Pin
dcof2-Jun-12 6:48
dcof2-Jun-12 6:48 
AnswerRe: C# working with strings Pin
Paul Conrad2-Jun-12 7:02
professionalPaul Conrad2-Jun-12 7:02 
AnswerRe: C# working with strings Pin
OriginalGriff2-Jun-12 9:01
mveOriginalGriff2-Jun-12 9:01 
Questionsend SMS in C# Pin
sina rahimzadeh1-Jun-12 21:48
sina rahimzadeh1-Jun-12 21:48 
AnswerRe: send SMS in C# Pin
Richard MacCutchan1-Jun-12 22:32
mveRichard MacCutchan1-Jun-12 22:32 
AnswerRe: send SMS in C# Pin
Sander Rossel2-Jun-12 0:43
professionalSander Rossel2-Jun-12 0:43 
AnswerRe: send SMS in C# Pin
taha bahraminezhad Jooneghani3-Jun-12 1:56
taha bahraminezhad Jooneghani3-Jun-12 1:56 
QuestionHow to hold dice in yahtzee Pin
Member 83368481-Jun-12 16:05
Member 83368481-Jun-12 16:05 
Hi I am trying to build a Yahtzee game and am having trouble with the code for the hold dice button, thus to stop the dice from rolling again when button is clicked on.

Also am needing some help and code to make the dice only roll 3 times in a turn. Thanks

Code so far is as follows.
C#
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;
using System.Media;

namespace yahtzeetrial
{
    public partial class Form2 : Form
    {
        //Creates the variables
        int m_nRoll = 0;
        int numRolls = 0;
        private Random RandomPick;
        int[] DiceButtons = new int[] { 0, 0, 0, 0, 0 };
        int[] DiceHold = new int[] { 0, 0, 0, 0, 0 };
        int AllDice = 0;

        public Form2()
        {
            InitializeComponent();
            InitialiseDiceButtons();  //Set the dice face to blank at start of game
        }

        SoundPlayer My_JukeBox = new SoundPlayer(@"C:\Users\Bronwyn\Documents\Yahtzee Game\yahtzeetrial\diceroll.wav");

        public void InitialiseDiceButtons()
        {
            this.btn1.ImageList = imageList1;
            this.btn1.ImageIndex = 0;
            this.btn2.ImageList = imageList1;
            this.btn2.ImageIndex = 0;
            this.btn3.ImageList = imageList1;
            this.btn3.ImageIndex = 0;
            this.btn4.ImageList = imageList1;
            this.btn4.ImageIndex = 0;
            this.btn5.ImageList = imageList1;
            this.btn5.ImageIndex = 0;
        }

        private void btnRoll_Click(object sender, EventArgs e)
        {
            My_JukeBox.Play();

            InitialiseRandomRoll();  //Set a random number
            do
            {
                m_nRoll = 0;  //Reset so always starts as zero
                Roll();       //Roll the dice

                if ((m_nRoll >= 1) || (m_nRoll <= 6))
                {
                    DiceButtons[AllDice] = m_nRoll;
                    AllDice++;
                }
            } while (AllDice < 5);

            //Show the roll
            this.btn1.ImageIndex = DiceButtons[0];
            this.btn2.ImageIndex = DiceButtons[1];
            this.btn3.ImageIndex = DiceButtons[2];
            this.btn4.ImageIndex = DiceButtons[3];
            this.btn5.ImageIndex = DiceButtons[4];
            AllDice = 0;
        }

        public void Roll()
        {
            m_nRoll = (int)(RandomPick.NextDouble() * 6) + 1;
        }

        public void InitialiseRandomRoll()
        {
            DateTime aTime = new DateTime(1000);
            aTime = DateTime.Now;
            int nSeed = (int)(aTime.Millisecond);
            RandomPick = new Random(nSeed);
        }

        private void btnHold1_Click(object sender, EventArgs e)
        {
            if (DiceHold[0] == 0)
            {
                DiceHold[0] = 1;
                this.btnHold1.ForeColor = Color.Red;
            }

            else
            {
                DiceHold[0] = 0;
                this.btnHold1.ForeColor = Color.Black;
            }
        }

        private void btnHold2_Click(object sender, EventArgs e)
        {
            if (DiceHold[1] == 0)
            {
                DiceHold[1] = 1;
                this.btnHold2.ForeColor = Color.Red;
            }
            else
            {
                DiceHold[1] = 0;
                this.btnHold2.ForeColor = Color.Black;
            }
        }

        private void btnHold3_Click(object sender, EventArgs e)
        {
            if (DiceHold[2] == 0)
            {
                DiceHold[2] = 1;
                this.btnHold3.ForeColor = Color.Red;
            }
            else
            {
                DiceHold[2] = 0;
                this.btnHold3.ForeColor = Color.Black;
            }
        }

        private void btnHold4_Click(object sender, EventArgs e)
        {
            if (DiceHold[3] == 0)
            {
                DiceHold[3] = 1;
                this.btnHold4.ForeColor = Color.Red;
            }
            else
            {
                DiceHold[3] = 0;
                this.btnHold4.ForeColor = Color.Black;
            }
        }

        private void btnHold5_Click(object sender, EventArgs e)
        {
            if (DiceHold[4] == 0)
            {
                DiceHold[4] = 1;
                this.btnHold5.ForeColor = Color.Red;
            }
            else
            {
                DiceHold[4] = 0;
                this.btnHold5.ForeColor = Color.Black;
            }
        }
    }
}


modified 1-Jun-12 22:22pm.

AnswerRe: How to hold dice in yahtzee Pin
sina rahimzadeh1-Jun-12 21:46
sina rahimzadeh1-Jun-12 21:46 
GeneralRe: How to hold dice in yahtzee Pin
Member 83368481-Jun-12 23:18
Member 83368481-Jun-12 23:18 
QuestionEntity Framework Problem Pin
Kevin Marois1-Jun-12 14:31
professionalKevin Marois1-Jun-12 14:31 
AnswerRe: Entity Framework Problem Pin
Unnikrishnan_S_N9-Jul-12 5:26
Unnikrishnan_S_N9-Jul-12 5:26 
RantVariable Names In Various Projects (Not My Own) Pin
Matt U.1-Jun-12 5:12
Matt U.1-Jun-12 5:12 
GeneralRe: Variable Names In Various Projects (Not My Own) Pin
Luc Pattyn1-Jun-12 5:57
sitebuilderLuc Pattyn1-Jun-12 5:57 
GeneralRe: Variable Names In Various Projects (Not My Own) Pin
PIEBALDconsult1-Jun-12 6:03
mvePIEBALDconsult1-Jun-12 6:03 
GeneralRe: Variable Names In Various Projects (Not My Own) Pin
Matt U.1-Jun-12 7:36
Matt U.1-Jun-12 7:36 
GeneralRe: Variable Names In Various Projects (Not My Own) Pin
GenJerDan1-Jun-12 10:30
GenJerDan1-Jun-12 10:30 
GeneralRe: Variable Names In Various Projects (Not My Own) Pin
Paul Conrad1-Jun-12 13:55
professionalPaul Conrad1-Jun-12 13:55 
GeneralRe: Variable Names In Various Projects (Not My Own) Pin
Matt U.2-Jun-12 7:38
Matt U.2-Jun-12 7:38 
GeneralRe: Variable Names In Various Projects (Not My Own) Pin
Ravi Bhavnani1-Jun-12 10:12
professionalRavi Bhavnani1-Jun-12 10:12 
GeneralRe: Variable Names In Various Projects (Not My Own) Pin
Matt U.2-Jun-12 7:40
Matt U.2-Jun-12 7:40 
GeneralRe: Variable Names In Various Projects (Not My Own) Pin
jschell4-Jun-12 8:15
jschell4-Jun-12 8:15 
GeneralRe: Variable Names In Various Projects (Not My Own) Pin
Matt U.14-Jun-12 2:37
Matt U.14-Jun-12 2:37 
QuestionWCF service as Windows Service Pin
TwilightEva1-Jun-12 2:26
TwilightEva1-Jun-12 2:26 
AnswerRe: WCF service as Windows Service Pin
sina rahimzadeh1-Jun-12 3:15
sina rahimzadeh1-Jun-12 3:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.