Rock paper scissors in C#

Posted by Samath
  10505
December 24, 2012

 
Hello world! today I am going to show you how to create a rock paper sessiors game in the C# programming language. Rock paper scissors is a hand game played by two people.The players usually count aloud to four, or speak the name of the game (e.g. "Rock Paper Scissors!" ), each time either raising one hand in a fist and swinging it down on the count or holding it behind. On the third count (saying, "Shoot!" or "Go!" "Scissors!" or "Bo!"), the players change their hands into one of three gestures, which they then "throw" by extending it towards their opponent.

I am going to show you how to create this game step by step by providing code and screen shot. if you have any question about the code, leave your question in a comment below.

Screen Shots








Code

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

InitializeComponent();

         }

        private void Button1_Click(object sender, EventArgs e)

        {

            this.Close();

        }

        private void btnClear_Click(object sender, EventArgs e)

        {

computerpicturebox.BackgroundImage = PictureBox1.BackgroundImage;

PlayerPictureBox.BackgroundImage = PictureBox1.BackgroundImage;

        }

        private void rockPicturebox_Click(object sender, EventArgs e)

        {

            Random randomGenerator = new Random();

            int computerChoice;

 

PlayerPictureBox.BackgroundImage = rockPicturebox.BackgroundImage;

computerChoice = randomGenerator.Next(1, 4);

 

            switch (computerChoice)

            {

case 1:

computerpicturebox.BackgroundImage = rockPicturebox.BackgroundImage;

winnerLabel.Text = " TIE ";

break;

case 2:

computerpicturebox.BackgroundImage = paperPicturebox.BackgroundImage;

winnerLabel.Text = " Computer wins because paper covers rock ";

break;

case 3:

computerpicturebox.BackgroundImage = scissorsPictureBox.BackgroundImage;

winnerLabel.Text = " Player wins because rock breaks scissors ";

break;

            }

        }

        private void paperPicturebox_Click(object sender, EventArgs e)

        {

            Random randomGenerator = new Random();

            int computerChoice;

 

PlayerPictureBox.BackgroundImage = paperPicturebox.BackgroundImage;

computerChoice = randomGenerator.Next(1, 4);

            switch (computerChoice)

            {

case 1:

      computerpicturebox.BackgroundImage = rockPicturebox.BackgroundImage;

         winnerLabel.Text = " Payer wins because paper covers rock ";

break;

case 2:

       computerpicturebox.BackgroundImage = paperPicturebox.BackgroundImage;

winnerLabel.Text = " TIE ";

break;

case 3:

      computerpicturebox.BackgroundImage = scissorsPictureBox.BackgroundImage;

          winnerLabel.Text = " Computer wins because scissors cut paper ";

break;

            }

        }

        private void scissorsPictureBox_Click(object sender, EventArgs e)

        {

            Random randomGenerator = new Random();

            int computerChoice;

 

   PlayerPictureBox.BackgroundImage = scissorsPictureBox.BackgroundImage;

computerChoice = randomGenerator.Next(1, 4);

            switch (computerChoice)

            {

case 1:

         computerpicturebox.BackgroundImage = rockPicturebox.BackgroundImage;

              winnerLabel.Text = " Computer wins because rock breaks scissors ";

                    break;

case 2:

computerpicturebox.BackgroundImage = paperPicturebox.BackgroundImage;

winnerLabel.Text = " Player wins because scissors cut paper ";

break;

                case 3:

computerpicturebox.BackgroundImage = scissorsPictureBox.BackgroundImage;

winnerLabel.Text = " TIE ";

break;

            }

        }

    }

}




   
  
 
 
   

 
mark123
December 29, 2012
 
after finishing c++ and doing java i realized that c# was relatively easy and user friendly, i cannot see myself doing prolog for too long with c# around although  the two languages are from different paradigms.
Samath
January 02, 2013
 
true!
matt027
June 26, 2013
 
Please help me how to do the players box image appear ? i'm a beginner , i do this program and when i click on 1 of the 3 image above it didn't appear on PLAYER and COMPUTER image box  just the text i win,lose, or tie