Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been working on a dice game which allows for two players to play.

Here are my game rules:

- Player rolls 5 dice and the values are added to their totalScore;
- Rolling a 1 or 4 will result in those dice being removed and nothing is added to their totalScore.
- Player continues to roll their remaining dice until they have no dice remaining.
- Players turn ends when they have no dice remaining and they receive their totalScore

My question is, how can I obtain a Player's totalScore to output on a label once their turn is completed?

Please note, I have just started to program, so I am very new. I appreciate the feedback.

Here is my code so far,
C#
public partial class Form1 : Form
 {
    Image[] diceImages;
    Label[] labels;
    int[] dice;
    int diceTotal;
    bool rolledOneOrFour;
    Random r;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        diceImages = new Image[]
    {
        Properties.Resources.blank,
        Properties.Resources.one,
        Properties.Resources.two,
        Properties.Resources.three,
        Properties.Resources.four,
        Properties.Resources.five,
        Properties.Resources.six
    };

        dice = new int[5];
        r = new Random();
        diceTotal = 0;

        labels = new Label[]
    {
        lbl_dice1,
        lbl_dice2,
        lbl_dice3,
        lbl_dice4,
        lbl_dice5
    };
    }

    public void btn_roll_Click(object sender, EventArgs e)
    {
        rollDice();
    }

    private void rollDice()
    {
        bool OneOrFourRolled = false;
        int rollTotal = 0;
        int sum = 0;
        int playerScore = 0;


        for (int i = 0; i < dice.Length; i++)
        {
            if (rolledOneOrFour)
            {
                if (dice[i] == 1 || dice[i] == 4)
                {
                    dice[i] = 0;
                }

                if (dice[i] == 0)
                {
                    continue;
                }
            }

            int rollDice = r.Next(1, 7);
            dice[i] = rollDice;
            sum += rollDice;

            if (dice[roll] == 1 || dice[roll] == 4)
            {
                TwoOrFiveRolled = true;
            }
            rollTotal += dice[roll];
        }

        if (!TwoOrFiveRolled)
        {
            diceTotal += rollTotal;

            //listview to show live results
            lb_liveResults.Items.Add("You scored " + sum + " points for this roll");
        }

        penalty = true;

        for (int i = 0; i < labels.Length; i++)
        {
            labels[i].Image = diceImages[dice[i]];
        }
    }
Posted
Updated 21-Oct-15 23:22pm
v5
Comments
George Jonsson 22-Oct-15 3:24am    
Looks like your code has been truncated.
Member 12078075 22-Oct-15 4:03am    
Apologies, thank you for pointing that out. I'll edit
Richard MacCutchan 22-Oct-15 4:02am    
Just keep a running total of the scores for each roll.
Member 12078075 22-Oct-15 4:04am    
Thanks Richard, this is what I am trying to achieve, however I am unsure how.
Richard MacCutchan 22-Oct-15 4:17am    

int totalScore = 0;
for (int i = 0; i < dice.Length; i++)
{
// do the rolling
totalScore += scoreForThisRoll;
}

Changed the while to for to avoid confusion.

1 solution

Thank you for your help guys this is what I had changed:
C#
if (!TwoOrFiveRolled)
{
    diceTotal += rollTotal;

    //listview to show live results
    lb_liveResults.Items.Add("You scored " + sum + " points for this roll and " + diceTotal + " in total.");
}

It seems to me that I need to work on naming my variables better so I am not getting lost in in the code.
 
Share this answer
 
Comments
Richard MacCutchan 22-Oct-15 11:01am    
Yes, remember a twenty character variable name is just as efficient as a single character. But so much easier to work with when you are looking through your source code listing.

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