Simple IF Statement instead of Array for rolling 3 dice by two players

Nas

Member
Joined
Oct 13, 2018
Messages
5
Programming Experience
Beginner
Hello,
I have this code written using Arrays for two players rolling 3 dice each and then state who got the highest score (the score is not adding the 3 numbers for each player but putting the numbers beside each other and reading it as a 3 digits integer, for example if player 1 rolls 3, 2 and 5 and player 2 rolls 1, 7 and 4 then the results will be:
Player 1 got: 325 and
Player 2 got 174
then displays Player 1 wins.

Is there a way to use simple IF Statement for the same concept instead of Arrays. That's how I want to do it, just simple IF Statements, no loops or Arrays, I know it is much longer but I want to learn it the easy way first.

P.S. the code below has an error at the end ONLY after running the code (incorrect format used)... I know I messed up the Int/String bit but I got all confused and stopped and couldn't figure out how to fix it.

I appreciate your help...
Thanks
---------------------
namespace ThreeDice
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private Random myDie = new Random();


        public int RollDie()
        {
            return myDie.Next(1, 6 + 1);
        }


        private void Play_Button_Click(object sender, EventArgs e)
        {
            int[] dice_Player1 = new int[3];
            for (int i = 0; i < dice_Player1.Length; i++)
            {
                dice_Player1[i] = RollDie();
            }


            Array.Sort(dice_Player1);


            int[] dice_Player2 = new int[3];
            for (int i = 0; i < dice_Player2.Length; i++)
            {
                dice_Player2[i] = RollDie();
            }


            Array.Sort(dice_Player2);


            Dice1_input_Player1.Text = dice_Player1[0].ToString();
            Dice2_input_Player1.Text = dice_Player1[1].ToString();
            Dice3_input_Player1.Text = dice_Player1[2].ToString();


            Dice1_input_Player2.Text = dice_Player2[0].ToString();
            Dice2_input_Player2.Text = dice_Player2[1].ToString();
            Dice3_input_Player2.Text = dice_Player2[2].ToString();


            Player1_Result.Text = string.Format("Player 1: {0}{1}{2}", Dice1_input_Player1.Text, Dice2_input_Player1.Text, Dice3_input_Player1.Text);
            Player2_Result.Text = string.Format("Player 2: {0}{1}{2}", Dice1_input_Player2.Text, Dice2_input_Player2.Text, Dice3_input_Player2.Text);


            int PR1 = Convert.ToInt32(Player1_Result.Text);
            int PR2 = Convert.ToInt32(Player2_Result.Text);


            if (PR1 > PR2)
                Player_Wins.Text = string.Format("Player 1 Wins! {0}", Player1_Result.Text);
            else
                Player_Wins.Text = string.Format("Player 2 Wins! {0}", Player2_Result.Text);


        }
    }
}
 
Instead of thinking about what programming constructs you want to use first, start by working out an algorithm. That's not even programming so anyone can do it. Pretend that this task has to be carried out by hand and you have to write a set of instructions for a person to follow. Those instructions need to be as detailed as possible, i.e. break everything down into the most elementary steps you can. Once that's done, you have your algorithm and that is what you need to write code to implement. You can write code for each step in the algorithm independently and each one should take no more than two or three lines of code. Because you're dealing with such small chunks at a time, everything is very simple.
 
Back
Top Bottom