Page 1 of 1

Rock, Paper, Scissors in Windows Form (C#) A basic Rock, Paper, Scissors game written in Visual C#!

#1 s3thst4  Icon User is offline

  • a * a = (b * b) + (c * c) - 2(b)(c)cos(A)
  • member icon

Reputation: 10
  • View blog
  • Posts: 587
  • Joined: 20-November 08

Posted 11 September 2009 - 05:27 PM

How to make a simple Rock, Paper, Scissors in Windows Form in Visual C#!

For those who don't want to manually do it, the source can be downloaded here.

First, change the form size to 528, 442.
Then, add 3 buttons. Size: 136, 93. Font: Microsoft Sans Serif, size 10, bold.
Make the buttons say "Rock", "Paper", and "Scissors".
Add a textbox, make it multiline, and change the size to 388, 181. Make it readonly. I changed the BackColor to ButtonHighlight so it will still be white as readonly.

Here is what my GUI looks like; please note your's can look different if you wish, and none of the above is required.

First, let's make our Variables. Click the "Rock" button, and above the event, type/paste the following:

string prps = "", airps = ""; // player's rockpaperscissors, AI's rockpaperscissors
Random uir = new Random(); // UI's randomizer



prps is the variable we will use to store our choice of rock paper or scissors.
aiprs is the variable we will use to store the AI's choice of rock paper or scissors.
air is the randomizer we will use to determine what the AI's pick will be.

Now, inside of the "Rock" button's click event, type/paste the following:

			prps = "Rock";
			if (textBox1.Text.Equals(""))
				textBox1.AppendText("Player Chose: " + prps);
			else
				textBox1.AppendText("\r\nPlayer chose: " + prps);
			AI();
			CheckForWinner();



Alright, the first thing that happens here is we are setting prps to Rock, what you selected.
Secondly, we're seeing if the textbox is empty. This if/else is completely due to my OCD about everything have to be neat. Basically, the if/else if saying if the textbox is empty, then don't make a newline (the first press), otherwise, add a newline.
Then it calls the method "AI", which determines what the AI chooses. That method will be found below.
After that, it calls the method "CheckForWinner" which, obviously, just checks who won. This method will also be found below.

Do this for each button, except, for the "Paper" button, change prps = "Rock"; to prps = "Paper";, and for the "Scissors" button, change it to prps = "Scissors";

Now it's time to do the AI. Rock, Paper, Scissors has very basic AI. We call a random number (1-4), and, depending on what the random number was, it assigns a value to airps.
Please type/paste the following AFTER the last button event you do:

		public void AI()
		{
			int which = air.Next(1, 4);
			if (which.Equals(1))
				airps = "Rock";
			if (which.Equals(2))
				airps = "Paper";
			if (which.Equals(3))
				airps = "Scissors";
			textBox1.AppendText("\r\nAI Chose: " + airps);
		}



We do 1-4 instead of 1-3 because it never seems to choose 3 unless it's 1-4.
air.Next(1, 4); is seeding a random number between 1-4, then we assign a Rock, Paper, or Scissors value to airps. Then, we add "AI Chose: (what the AI chose)" to the textbox.

So now that we have our choice and the AI's choice, it's time to see who wins! I will not deeply explain this next code, though I will explain one line (all lines are the same, just with slightly different values).

public void CheckForWinner()
		{
			if (prps.Equals("Rock") && airps.Equals("Rock"))
			{
				textBox1.AppendText("\r\nPlayer tied with AI!");
				if (MessageBox.Show("Tie! Would you like to play again?", "Tie!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
					this.Close();
			}
			else if (prps.Equals("Paper") && airps.Equals("Paper"))
			{
				textBox1.AppendText("\r\nPlayer tied with AI!");
				if (MessageBox.Show("Tie! Would you like to play again?", "Tie!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
					this.Close();
			}
			else if (prps.Equals("Scissors") && airps.Equals("Scissors"))
			{
				textBox1.AppendText("\r\nPlayer tied with AI!");
				if (MessageBox.Show("Tie! Would you like to play again?", "Tie!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
					this.Close();
			}
			else if (prps.Equals("Rock") && airps.Equals("Scissors"))
			{
				textBox1.AppendText("\r\nPlayer beat AI!");
				if (MessageBox.Show("Player Won! Would you like to play again?", "Player Won!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
					this.Close();
			}
			else if (prps.Equals("Paper") && airps.Equals("Rock"))
			{
				textBox1.AppendText("\r\nPlayer beat AI!");
				if (MessageBox.Show("Player won! Would you like to play again?", "Player Won!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
					this.Close();
			}
			else if (prps.Equals("Scissors") && airps.Equals("Paper"))
			{
				textBox1.AppendText("\r\nPlayer beat AI!");
				if (MessageBox.Show("Player won! Would you like to play again?", "Player Won!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
					this.Close();
			}
			else if (prps.Equals("Rock") && airps.Equals("Paper"))
			{
				textBox1.AppendText("\r\nPlayer lost against AI!");
				if (MessageBox.Show("Player lost! Would you like to play again?", "Player Lost!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
					this.Close();
			}
			else if (prps.Equals("Paper") && airps.Equals("Scissors"))
			{
				textBox1.AppendText("\r\nPlayer lost against AI!");
				if (MessageBox.Show("Player lost! Would you like to play again?", "Player Lost!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
					this.Close();
			}
			else if (prps.Equals("Scissors") && airps.Equals("Rock"))
			{
				textBox1.AppendText("\r\nPlayer lost against AI!");
				if (MessageBox.Show("Player lost! Would you like to play again?", "Player Lost!", MessageBoxButtons.YesNo).Equals(DialogResult.No))
					this.Close();
			}



So, basically, it's seeing what our choice was, what the AI's choice was, and comparing. Then, it tells us if we won, the AI won, or it was a tie.
Then, we ask if we want to play again. If you choose yes, then we just continue, otherwise, it closes.

Thank you for reading, for any additional information about the game, please comment or PM me.

Is This A Good Question/Topic? 1
  • +

Replies To: Rock, Paper, Scissors in Windows Form (C#)

#2 Guest_Brandon*


Reputation:

Posted 11 December 2010 - 02:01 PM

hey i'm having a small problem with this code.
where it is "03 int which = air.Next(1, 4);"
my computer is saying that "air" doesn't exist in current context. is there something i can do to fix that?

please email me
wbdudley2010@gmail.com

thanks!
-Brandon
Was This Post Helpful? 0

#3 suprwmn45  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-July 11

Posted 27 July 2011 - 11:28 AM

View PostBrandon, on 11 December 2010 - 02:01 PM, said:

hey i'm having a small problem with this code.
where it is "03 int which = air.Next(1, 4);"
my computer is saying that "air" doesn't exist in current context. is there something i can do to fix that?

please email me
wbdudley2010@gmail.com

thanks!
-Brandon



If you add to variables at top:
Random air = new Random(); //AI's randomizer

that will fix things :gun_bandana:
Was This Post Helpful? 0
  • +
  • -

#4 thebahalo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 28-December 11

Posted 28 December 2011 - 01:58 AM

Just a quick tip, instead of doing
public void CheckForWinner()
        {
                if(prps.Equals("Rock") && airps.Equals("Rock"));


and so on for each rock, paper and scissor.
You can do it like this:
public void CheckForWinner()
        {
                if(prps == airps);


Which give the same effect. :gunsmilie:
Was This Post Helpful? 1
  • +
  • -

#5 kwontum  Icon User is offline

  • New D.I.C Head

Reputation: -20
  • View blog
  • Posts: 19
  • Joined: 24-July 13

Posted 04 August 2013 - 05:57 PM

Four years old?! no wonder VS 2013 is having fits...
Was This Post Helpful? -4
  • +
  • -

Page 1 of 1