CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2013
    Posts
    1

    Simple (to some) dice game

    Please someone help me, I've been staring at this for hours cause I am new to this and coding in general.

    Rules of the Game:

    Two players each roll a pair of dice until one player wins. Each time they roll is called a 'battle.' First to win 5 battles wins the game. The winner of a battle is determined by these rules:

    1. Doubles always wins over non-doubles. E.g., 1-1 wins over 6-5.

    2. Higher doubles wins over lower doubles. E.g., 4-4 wins over 3-3.

    3. If both players roll non-doubles, then the higher total roll wins. E.g., 5-4 (9) wins over 5-3 (8).

    4. If the players roll the same, no one wins that battle.

    here's my dice class which I'm 98% sure if perfect

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace dice1
    {
        class PairOfDice
        {
            private int die1, die2;
            private Random random1;
    
            public PairOfDice()
            {
                random1 = new Random();
            }
    
            public int Die1
            {
                get
                { return die1; }
                set
                { die1 = value; }
            }
    
            public int Die2
            {
                get
                { return die2; }
                set
                { die2 = value; }
            }
    
            public int SumOfFaces()
            {
                return die1 + die2;
            }
    
            public void Roll()
            {
                die1 = random1.Next(1, 7);
                die2 = random1.Next(1, 7);
            }
    
            public bool isDoubles()
            {
                return(die1==die2);
        }
    }
    and here's where i am having so many troubles in my form 1

    Code:
    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 dice1;
    
    namespace DiceBattleKevin
    {
        public partial class Form1 : Form
        {
            private PairOfDice pair1 = new PairOfDice();
            private PairOfDice pair2 = new PairOfDice();
            bool P1_doubles;
            bool P2_doubles;
            int P1_sum; 
            int P2_sum;
            
            public Form1()
            {
                InitializeComponent();
                System.Threading.Thread.Sleep(400);  
                P1_doubles = pair1.isDoubles();
                P2_doubles = pair2.isDoubles();
                P1_sum = pair1.SumOfFaces();
                P2_sum = pair2.SumOfFaces();
            }
    
            private void RollButton_Click(object sender, EventArgs e)
            {
                //Roll the dice
    
                
    
                //When button is clicked, number shows up in for die1 and die2
    
    
                //Show the value of dice
                //disable the roll button
    
                if(P1_doubles)
                { if(P2_doubles)
                    //both doubles
    
    
                }
                else if (P2_doubles)
                { //p2 wins
    
                }
                else
                { //neither are double
                    //check sum
    
                }
    
            }
    
            private bool BattleOver()
            {
    
            }
    
            private void DeclareWinner()
            {
                //Declare winner
            }
            
            private void ShowScores()
            {
                //Set the label of P1's score to his score
                //Set the label of p2's score to his score
            }
    
            private void NewGameButton_Click(object sender, EventArgs e)
            {
    
            }
    
     
    
      
    
        }
    }
    Where i am stuck right now is at RollButton_Click. I just don't know how to get the dice to roll (from the dice class) and how to get that roll to show up. Any help would be great. Thank you!

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Simple (to some) dice game

    Your PairOfDice objects have the exposed methods int SumOfFaces(), void Roll() and bool isDoubles(). You can use these to carry out all the needed functions. For example, to "roll the dice" you can call their roll method:
    Code:
    pair1.Roll(); //Role the first pair of dice
    You can check if both pairs have rolled doubles by:
    Code:
    if( pair1.isDoubles() && pair2.isDoubles() )
    {
        //Do something
    }
    You can check to see if the sum of pair1 exceeds the sum of pair2 like:
    Code:
    if( pair1.SumOfFaces() > pair2.SumOfFaces() )
    {
        //Do something
    }
    And, to display them, you can usually set a label (or textbox) by doing something like:
    Code:
    dice1Label.Text = (pair1.SumOfFaces()).ToString();
    This isn't a complete solution, but I think these examples should let you carry out the other tasks you need to accomplish.

    Minor point: In your PairOfDice class you are missing the closing brackets of isDoubles.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured