+ Reply to Thread
Results 1 to 6 of 6

Thread: Rock Paper Scissors!!

  1. #1
    Siten0308's Avatar
    Siten0308 is offline Programming Professional
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    302
    Rep Power
    16

    Talking Rock Paper Scissors!!

    Hello Everyone,

    Got a request from my mentor to post this, so I am, and i think everyone could benefit from this since it has a lot of cool tricks to it such as using the list, soundclip class, picture boxes, and a lot of if statements:

    here I just getting my fields ready, I need string and int for each to display some numbers and show a message to put into a list, but this will come together later on

    Code:
            public string playerchoice { get; set; }
            public string computerchoice { get; set; }
            public string winnerresults { get; set; }
            public string collectdata { get; set; }
            public int playerscore = 0;
            public string playerScore;
            public int computerscore = 0;
            public string computerScore;
            System.Drawing.Bitmap paper = rockpaperscissors.Properties.Resources.paper;
            System.Drawing.Bitmap rock = rockpaperscissors.Properties.Resources.rock;
            System.Drawing.Bitmap scissors = rockpaperscissors.Properties.Resources.scissors;
            List<string> ListResults = new List<string>();
    first method is for the computer choice, i had the computer choose a random number between 0-3 using Randomnumber.Next(), and if computer chooses 0, then do this from the result method, sorry if its a little confusing having a method inside a method or not good practice, just let me know and I can fix it up.

    Code:
    public void computerchoicestart()
            {
                Random RandomNumber = new Random();
                int x = RandomNumber.Next(0, 3);
                if (x == 0)
                {
                    piccomputer.Image = new Bitmap(rock);
                    computerchoice = "Rock";
                }
                if (x == 1)
                {
                    piccomputer.Image = new Bitmap(paper);
                    computerchoice = "Paper";
                }
                if (x == 2)
                {
                    piccomputer.Image = new Bitmap(scissors);
                    computerchoice = "Scissors";
                }
                results();            
            }
    here is the results method:
    Code:
    public void results()
            {
                if (computerchoice == "Rock" && playerchoice == "Rock" || computerchoice == "Scissors" && playerchoice == "Scissors" ||
                    computerchoice == "Paper" && playerchoice == "Paper")
                {
                    MessageBox.Show("Tie game, this wont count, do again : P");
                    winnerresults = "Tie";
                    picplayer.Image = null;
                    piccomputer.Image = null;
                }
                if (computerchoice == "Paper" && playerchoice == "Rock" || computerchoice == "Scissors" && playerchoice == "Paper" ||
                    computerchoice == "Rock" && playerchoice == "Scissors")
                {
                    MessageBox.Show("Computer Wins");
                    winnerresults = "Computer";
                    picplayer.Image = null;
                    piccomputer.Image = null;
                    computerscore++;
                    computerScore = Convert.ToString(computerscore);
                    txt_computer.Text = computerScore;
                }
                if (playerchoice == "Paper" && computerchoice == "Rock" || playerchoice == "Scissors" && computerchoice == "Paper" ||
                    playerchoice == "Rock" && computerchoice == "Scissors")
                {
                    MessageBox.Show("You Win");
                    winnerresults = "Player";
                    picplayer.Image = null;
                    piccomputer.Image = null;
                    playerscore++;
                    playerScore = Convert.ToString(playerscore);
                    txt_player.Text = playerScore;
                }
                int i = 3;
                ListViewItem LVI = new ListViewItem();
                LVI.Text = playerchoice;
                LVI.SubItems.Add(computerchoice);
                LVI.SubItems.Add(winnerresults);
                lstscore.Items.Add(LVI);
                if (LVI.Index > i)
                {
                    if (playerscore > computerscore)
                    {
                        SoundPlayer cheer = new SoundPlayer(rockpaperscissors.Resources.Resource1.crowdapplause);
                        cheer.Play();
                        MessageBox.Show("Player Wins: )\nPlayer Score: " + playerscore + "\nComputer Score: " + computerscore);
                    }
                    if (playerscore < computerscore)
                    {
    
                        SoundPlayer boo = new SoundPlayer(rockpaperscissors.Resources.Resource1.boo3);
                        boo.Play();
                        MessageBox.Show("Computer Wins, YOU LOSER!! \nPlayer Score: " + playerscore + "\nComputer Score: " + computerscore);
                    }
                    if (playerscore == computerscore)
                    {
                        MessageBox.Show("Tie Game : P do it again");
                    }
                    lstscore.Items.Clear();
                    playerscore = 0;
                    txt_player.Text = "0";
                    computerscore = 0;
                    txt_computer.Text = "0";
                }
            }
    Last thing in the results method is you can see that the last 3 if statements decide who wins and who lost, including if its a tie (in this game you can tie).

    I hope this is a great learning experience to anyone, the only thing that I didnt go over in detail was the sound, you wouold just use the Soudplayer class which you can see above and how to embed the code you can easily google it to find the answer however if you wish to get the info please let me know and I can forward you screen shots on how to embed a picture/audio file. Also for everyones information because i didnt know this either, when you embed or use any audio file, only WAV files only, no mp3 or avi, it wont work. Thank you for reading and enjoy the game, or as i like to say "GAME ON!!"
    Attached Files Attached Files

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Rock Paper Scissors!!

    Very nice work! +rep

  4. #3
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Rock Paper Scissors!!

    Very interesting! I'm going to try it out in a bit.

    For your code where you determine if there is a tie, this would also work:

    Code:
    if (computerchoice == playerchoice)
                {
                    MessageBox.Show("Tie game, this wont count, do again : P");
                    winnerresults = "Tie";
                    picplayer.Image = null;
                    piccomputer.Image = null;
                }
    +rep for you! The text boxes that show the users score. The user can edit those. You might not want to let them do that.

    Code:
    text.enabled = false;
    Something like the above code would fix it.

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Rock Paper Scissors!!

    Nicely done. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    Re: Rock Paper Scissors!!

    Your game is very enjoyable, Siten. +rep for real.

    I just can't wait for your next game, hi hi...

  7. #6
    doomsaber is offline Newbie
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Re: Rock Paper Scissors!!

    Thanks for the source code. I was wondering how one can program rock, paper, scissors in C#

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Choosing a topic for a research paper
    By gammaman in forum General Programming
    Replies: 3
    Last Post: 01-25-2009, 04:59 AM
  2. Game - stone,paper and scissor
    By Turk4n in forum Java Tutorials
    Replies: 14
    Last Post: 01-12-2009, 12:38 AM
  3. Why vista won't suck! Will it rock?
    By TcM in forum The Lounge
    Replies: 23
    Last Post: 07-29-2008, 10:46 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts