Jump to content

C# Dice game

JanY
Go to solution Solved by cluelessgenius,
else 

            {

                if (som == 7)

                {

                     MessageBox.Show("Je hebt verloren", "Spijtig");   

                }

                else if (som == vorig)

                {

                    MessageBox.Show("Je hebt gewonnen!", "Gefeliciteerd");

                }

                else

                {

                    vorig = som;

                    MessageBox.Show("Probeer het nog een keer", "Opnieuw");

                }

                

            }

 

//so as i get it this might work if 7 is a no-go

Guys I need this answered by tonight so please help me :3,

 

I have to make a program which shows I can use graphical objects, if else, switch, for, while, do while, and other objects for an exam.

Since this is the second time I'm taking the test I am trying to make the program that was the test last time (sry for bad english).

 

It's a game about dice:

 

if the sum of two random dice throws is 7 or 11, you've won.

 

if the sum is 2, 3 or 12 you've lost.

 

if it's anything else you have to throw again (click button again) but from here on you have to throw the same number as your previous outcome to win.

 

for example: so the first try if you have thrown a sum of 5, then you must throw 5 again to win. if you then throw 8, you must throw 8 the third time to win.

 

Also if you don't throw 2, 3, 12, 7 or 11 the first time then when the second time starts the number 7 means you've lost.

 

It's difficult to explain so I hope the piece of code I have makes it a bit more understandable.

 

I also have to add a timer, a rectangle which show how many times I've won and the computer won and a flickering message at the end but I don't think I'll need help with those.

 

Btw: I speak dutch so the variables will be in dutch but that shouldn't be a problem, quick translation of the words used: som=sum, getal1/2 = number1/2, eenmaal= 1st try, vorig = previous, the messages just say you've won of lost or try again.

 

Also that part in the else with vorig = som is probably completely wrong I was just trying out stuff.

 

 

TL;DR: Look at my code pls :'(

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

That's weird, it didn't add my file? I'll just add it in text to be safe:

 

 

amespace examen_semester_1
{
    public partial class Form1 : Form
    {
        Random rand = new Random();
        int som, getal1, getal2, eenmaal, vorig;
 
        public Form1()
        {
            InitializeComponent();
            eenmaal = 0;
 
        }
 
        private void btnGooi_Click(object sender, EventArgs e)
        {
            getal1 = rand.Next(1, 7);
            getal2 = rand.Next(1, 7);
 
            som = getal1 + getal2;
 
 
            lblGetal1.Text = Convert.ToString(getal1);
            lblGetal2.Text = Convert.ToString(getal2);
            lblSom.Text = Convert.ToString(som);
                       
 
            if (eenmaal == 0)
            {
                
                switch (som)
                {
                    case 2:
                        MessageBox.Show("Je hebt verloren", "Spijtig");
                        break;
                    case 3:
                        MessageBox.Show("Je hebt verloren", "Spijtig");
                        break;
                    case 7:
                        MessageBox.Show("Je hebt gewonnen!", "Gefeliciteerd");
                        break;
                    case 11:
                        MessageBox.Show("Je hebt gewonnen!", "Gefeliciteerd");
                        break;
                    case 12:
                        MessageBox.Show("Je hebt verloren", "Spijtig");
                        break;
                    default:
                        MessageBox.Show("Probeer het nog een keer", "Opnieuw");
                        vorig = som;
                        eenmaal++;
                        break;
 
                 }
            }
            else 
            {
                if (som == vorig)
                {
                    MessageBox.Show("Je hebt gewonnen!", "Gefeliciteerd");         
                }
                if (som == 7)
                {
                    MessageBox.Show("Je hebt verloren", "Spijtig");
                }
                else
                {
                    MessageBox.Show("Probeer het nog een keer", "Opnieuw");
                }
                
            }
            
        }
                
    }
Link to comment
Share on other sites

Link to post
Share on other sites

 

That's weird, it didn't add my file? I'll just add it in text to be safe:

 

 

amespace examen_semester_1
{
    public partial class Form1 : Form
    {
        Random rand = new Random();
        int som, getal1, getal2, eenmaal, vorig;
 
        public Form1()
        {
            InitializeComponent();
            eenmaal = 0;
 
        }
 
        private void btnGooi_Click(object sender, EventArgs e)
        {
            getal1 = rand.Next(1, 7);
            getal2 = rand.Next(1, 7);
 
            som = getal1 + getal2;
 
 
            lblGetal1.Text = Convert.ToString(getal1);
            lblGetal2.Text = Convert.ToString(getal2);
            lblSom.Text = Convert.ToString(som);
                       
 
            if (eenmaal == 0)
            {
                
                switch (som)
                {
                    case 2:
                        MessageBox.Show("Je hebt verloren", "Spijtig");
                        break;
                    case 3:
                        MessageBox.Show("Je hebt verloren", "Spijtig");
                        break;
                    case 7:
                        MessageBox.Show("Je hebt gewonnen!", "Gefeliciteerd");
                        break;
                    case 11:
                        MessageBox.Show("Je hebt gewonnen!", "Gefeliciteerd");
                        break;
                    case 12:
                        MessageBox.Show("Je hebt verloren", "Spijtig");
                        break;
                    default:
                        MessageBox.Show("Probeer het nog een keer", "Opnieuw");
                        vorig = som;
                        eenmaal++;
                        break;
 
                 }
            }
            else 
            {
                if (som == vorig)
                {
                    MessageBox.Show("Je hebt gewonnen!", "Gefeliciteerd");         
                }
                if (som == 7)
                {
                    MessageBox.Show("Je hebt verloren", "Spijtig");
                }
                else
                {
                    MessageBox.Show("Probeer het nog een keer", "Opnieuw");
                }
                
            }
            
        }
                
    }

 

ok so what seems to be the problem?

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

 else 
            {
                if (som == vorig)
                {
                    MessageBox.Show("Je hebt gewonnen!", "Gefeliciteerd");         
                }
                if (som == 7)
                {
                    MessageBox.Show("Je hebt verloren", "Spijtig");
                }
                else
                {
                    vorig = som;
                    MessageBox.Show("Probeer het nog een keer", "Opnieuw");
                }
                
            }

im not sure but did you remember to set the vorig after each throw? maybe this helps.

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

Well the part which follows the "try again" part, basically everything under else is wrong. I need something to replace the vorig = som part. BTW it's prolly something with a loop cuz I haven't used that yet.

Link to comment
Share on other sites

Link to post
Share on other sites

im not sure but did you remember to set the vorig after each throw? maybe this helps.

 

yeah that was definitely missing, it kinda seeems to work but everytime I win by throwing the same number twice It gives me the "you've won" and the "try again" message back to back.

Link to comment
Share on other sites

Link to post
Share on other sites

try this :

 

 else 
            {
                if (som == vorig)
                {
                    MessageBox.Show("Je hebt gewonnen!", "Gefeliciteerd");         
                }
                else if (som == 7)
                {
                    MessageBox.Show("Je hebt verloren", "Spijtig");
                }
                else
                {
                    vorig = som;
                    MessageBox.Show("Probeer het nog een keer", "Opnieuw");
                }
                
            }

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

do i loose if i throw a seven and then another seven ?

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

anyway the 2 messages is because you got 2 ifs in the last else. it basicly checks if som == vorig and then however that outcome is it also checks if its 7 you need to have some kind of dependency in there so it stop checking if one message is thrown. maybe try a break; after each messagebox

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

do i loose if i throw a seven and then another seven ?

the game "resets" after throwing the first 7, you only lose after throwing a 1, 4, 5, 6, 8, 9, 10 first and then a 7 or however many tries it takes to reach 7 before reaching the same number again.

Link to comment
Share on other sites

Link to post
Share on other sites

but be sure it still fits the rules as i didnt really check for that 

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

first try = 4;

second try: 7;

third try: 7;

 

do i loose because the second try was 7 or do i win because i hit it again?

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

else 

            {

                if (som == 7)

                {

                     MessageBox.Show("Je hebt verloren", "Spijtig");   

                }

                else if (som == vorig)

                {

                    MessageBox.Show("Je hebt gewonnen!", "Gefeliciteerd");

                }

                else

                {

                    vorig = som;

                    MessageBox.Show("Probeer het nog een keer", "Opnieuw");

                }

                

            }

 

//so as i get it this might work if 7 is a no-go

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

first try = 4;

second try: 7;

third try: 7;

 

do i loose because the second try was 7 or do i win because i hit it again?

 

you lost because second try was 7

Link to comment
Share on other sites

Link to post
Share on other sites

you lost because second try was 7

so then my last posted code should work

 

just check for the 7 first because its clearlyy higher prioritized and then if its not 7 check with a else if if its the same sum as before. my code should work like that

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

else 
            {
                if (som == 7)
                {
                     MessageBox.Show("Je hebt verloren", "Spijtig");   
                }
                else if (som == vorig)
                {
                    MessageBox.Show("Je hebt gewonnen!", "Gefeliciteerd");
                }
                else
                {
                    vorig = som;
                    MessageBox.Show("Probeer het nog een keer", "Opnieuw");
                }
                
            }
 
//so as i get it this might work if 7 is a no-go

 

 

Wow I just tested it and the result seems to work, Idk if there are still some small faults in there but with this I would definitely pass the exam. Thanks for the help!

Link to comment
Share on other sites

Link to post
Share on other sites

no problem as i get it you are a beginner so this is really no hard work for me. if have any further questions in the future fell free to contact me im glad to help as i know what its like as an IT-Student.

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

no problem as i get it you are a beginner so this is really no hard work for me. if have any further questions in the future fell free to contact me im glad to help as i know what its like as an IT-Student.

 

Thanks a bunch! I will now try to complete the rest of the program but I might end up revisting this thread to ask about something 

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks a bunch! I will now try to complete the rest of the program but I might end up revisting this thread to ask about something 

sure whatever u need mate

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

so this is what i threw together in 10 minutes during my break from work. its really simple but maybe it helps.

 

 

namespace dice

{
    public partial class Form1 : Form
    {
        Random random = new Random();
        int dice1, dice2, throwcount, lastthrow, sum;
 
        public Form1()
        {
            InitializeComponent();
            throwcount = 0;
        }
 
        private void Btn_throwdice_Click(object sender, EventArgs e)
        {
            //contains the random dice number for dice 1
            dice1 = random.Next(1, 7);
            //contains the random dice number for dice 2
            dice2 = random.Next(1, 7);
            //contains the sum of dice 1 and 2
            sum = dice1 + dice2;
 
            //Output
            Lbl_dice1.Text = dice1.ToString();
            Lbl_dice2.Text = dice2.ToString();
            Lbl_sum.Text = sum.ToString();
 
            //catches wehter or not its the first throw
            if (throwcount==0)
            {
                //catches all cases where you loose
                if (sum==2||sum==3||sum==12)
                {
                    MessageBox.Show("Je hebt verloren", "Spijtig");
                }
                //catches all cases where you win
                else if (sum==7||sum==11)
                {
                    MessageBox.Show("Je hebt gewonnen!", "Gefeliciteerd");
                }
                //lets you throw a second time
                else
                {
                    MessageBox.Show("Probeer het nog een keer", "Opnieuw");
                    //counts number of throws (indexes are zero-based)
                    throwcount++;
                    //updates the variable wich contains the sum of the last throw
                    lastthrow = sum;
                }
            }
            //triggers if its not the first throw
            else
            {
                //catches all cases where you loose
                if (sum == 7)
                {
                    MessageBox.Show("Je hebt verloren", "Spijtig");
                }
                //catches all cases where you win
                else if (sum == lastthrow)
                {
                    MessageBox.Show("Je hebt gewonnen!", "Gefeliciteerd");
                }
                //lets you throw one more time
                else
                {
                    MessageBox.Show("Probeer het nog een keer", "Opnieuw");
                    //counts number of throws (indexes are zero-based)
                    throwcount++;
                    //updates the variable wich contains the sum of the last throw
                    lastthrow = sum;
                }
            }
        }
    }
}

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

so this is what i threw together in 10 minutes during my break from work. its really simple but maybe it helps.

 

even though it is shorter and easier I have to use a switch in that part because it's a chapter we learned so I'm pretty sure I would get less points because it would seem that I don't know how to use the switch but ty anyway.

Link to comment
Share on other sites

Link to post
Share on other sites

sure then use a switch i only avoid it at times because when they get larger they tend to have more of an performance impact than ifs but since this thing isnt even 100 lines it mkes no difference but honestly switch here for me comes across as unneccesary

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

dont forget comments at least in my college they were a must

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×