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
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 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>();
here is the results method: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(); }
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).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"; } }
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!!"
Very nice work! +rep
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:
+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:if (computerchoice == playerchoice) { MessageBox.Show("Tie game, this wont count, do again : P"); winnerresults = "Tie"; picplayer.Image = null; piccomputer.Image = null; }
Something like the above code would fix it.Code:text.enabled = false;![]()
Nicely done. +rep
Your game is very enjoyable, Siten. +rep for real.
I just can't wait for your next game, hi hi...![]()
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
Thanks for the source code. I was wondering how one can program rock, paper, scissors in C#
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks