School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,144 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,737 people online right now. Registration is fast and FREE... Join Now!




Heads or Tails with Windows Forms (C#)

 
Reply to this topicStart new topic

> Heads or Tails with Windows Forms (C#), Simple Heads or Tails game in Visual C#!

s3thst4
Group Icon



post 17 Sep, 2009 - 02:32 PM
Post #1


How to make a simple Heads or Tails game in Visual C#.

I used 2 pictures I found on Google for Heads and Tails, here they are:
Pictures
or you can download the source:
Source


This is just a very simple game of Heads or Tails, with a betting box for you to bet money in.

First things first, we need to put the pictures in our Resources. So click Properties, and it should open a new tab. In this tab, you see a line of things to choose from, but click Resources. In Resources, drag and drop your two images there. Make sure the names are "Heads" and "Tails".
Picture:
Attached Image


So, for the GUI, we're going to need the following:
1 picturebox.
2 buttons.
2 radiobuttons.
1 textbox.
1 label.

Leave the picturebox empty.
Change the text property 1 button "Place Your Bet" and the other "Flip!".
Now, change the text property of 1 radibutton to "Head", and the other "Tails".
For the label, change the text property to "$100".
On the "Flip!" button, change the property "Enabled" to false.

Okay, here's how my GUI looks, but your's can look different if you wish:
Attached Image

First, we need to declare our variables that will be used throughout the program:

CODE

int money = 100, bet = 0;
string headOrTail = "", userGuess = "";


Declare these Variables above the first method, which will probably be button1_Click.

"money" is (obviously) how much money the user currently has. "bet" is how much the user bets on getting the coin flip right. "headOrTail" is what the flip actually is, and "userGuess" is the user's guess on what the coin flip will be.

Now, double click the Place Your Bet button. We're going to write the code for it!
It's very simple; here it is:

CODE

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (!textBox1.Text.Equals(""))
                    bet = int.Parse(textBox1.Text);
                else
                    MessageBox.Show("Please don't leave the textbox empty.", "Empty Textbox.");
            }
            catch
            {
                textBox1.Clear();
                MessageBox.Show("Please only enter integers.", "Error!");
            }
            if (bet <= 0 || bet >= money)
            {
                MessageBox.Show("Your bet must be higher than 0 and less or equal to your current money!", "Invalid bet");
                textBox1.Clear();
            }
            else
            {
                button2.Enabled = true;
            }
        }


Okay. So, we are using the try/catch so that, if the user enters a string, it will tell them that strings are not acceptable, and only to enter integers.
The reason for the if/else in the try/catch is so that if they leave it empty, we tell them not to.
bet = int.Parse(textBox1.Text); is saying that our interger (bet) will be equal to what is in the textBox (where the user bets). The only problem with that is that bet is an integer, and textboxes contain Strings, so we have to Parse what's in the textBox so it can be a integer, and now we have the user's bet.
The second if/else is if the user trys to bet 0 or less than 0, or (|| means or) the user tries to bet more money than they have, we tell them that can't do that. Otherwise, if the bet is valid, then the "Flip!" button is enabled.

Next, we're going to write our method to determine what the flip is.

CODE

        public void HeadsorTails()
        {
            Random headsOrTails = new Random();
            int headsTails = headsOrTails.Next(1, 3);
            if (headsTails.Equals(1))
            {
                pictureBox1.Image = Properties.Resources.Heads;
                headOrTail = "Heads";
            }
            else
            {
                pictureBox1.Image = Properties.Resources.Tails;
                headOrTail = "Tails";
            }
        }


So, here what we're doing is making a public void (public so all methods can use it) to assign a value to "headOrTail". First, here, we're making a Random. This is so we can randomly get a heads or tails. Then we are making an int, and [index]headsOrTails.Next(1, 3);[/index] is just saying create a random number between one and three.
If the integer's number is 1, then we make the pictureBox's picture show the Heads image we put in Resources and make "headOrTail" Heads. Otherwise, we show the Tails image and make "headOrTail" Tails.
Simple, huh? smile.gif

Finally, we need the code for the "Flip!" button! Here it is:
CODE

        private void button2_Click(object sender, EventArgs e)
        {
            if (!radioButton1.Checked && !radioButton2.Checked)
            {
                MessageBox.Show("Please select a choice!", "Error!");
                button2.Enabled = false;
            }
            else
            {
                if (radioButton1.Checked)
                    userGuess = "Heads";
                if (radioButton2.Checked)
                    userGuess = "Tails";
                HeadsorTails();
                if (!userGuess.Equals(headOrTail))
                {
                    MessageBox.Show("You lost $" + bet + "!", "You lost!");
                    money -= bet;
                    label1.Text = "$" + money.ToString();
                    button2.Enabled = false;
                    pictureBox1.Image = null;
                    textBox1.Clear();
                }
                else
                {
                    MessageBox.Show("You won $" + bet + "!", "You won!");
                    money += bet;
                    label1.Text = "$" + money.ToString();
                    button2.Enabled = false;
                    pictureBox1.Image = null;
                    textBox1.Clear();
                }
            }
        }


So, firstly, this code checks if either of the radioButtons are checked, if they aren't, it tells you to check one. Otherwise, it sees which one is checked, if the radioButton entitled "Heads" is checked, the "userGuess" variable is "Heads", and if the radioButton entitled "Tails" is checked, the "userGuess" variable is "Tails".
Then, we call the "HeadsorTails" method to see what the flip is. If the user's guess isn't what the actual flip was, then we tell the user they lost, and we subtract the bet they made, then we make the label say "$(money)", money being the variable "money" converted to a String with .ToString().
Then, we disable the button, make the pictureBox's image null (no image), and we clear the textBox.
If we don't have the same guess, we tell the user they won, add the bet to the money, make label1 tell how much money we now have, disable the button, make the pictureBox's image null (no image), and clear the textBox.

There you go, now you have a simple Heads or Tails game! I hope you enjoyed this tutorial.

Best regards,
~Seth.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 03:37PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month