Sie sind auf Seite 1von 4

Now, let's start this tutorial!

1. Let's start with creating a Windows Form Application in C# for this tutorial by following the
following steps in Microsoft Visual Studio: Go to File, click New Project, and chooseWindows
Application.
2. Next, add one Combobox named ComboBox1, three labels named Label1 labeled as Current
Choice:, Label2 labeled as PC Choice: and Label3 as . Add also one button
named Button1 for us to know who won the game. You must design your interface like this:

3. Add this code as your Form_Load. This will add the choices of Rock, Paper, and Scissor in our
combobox as the input of the player.

1. public void Form1_Load(System.Object sender, System.EventArgs e)


2. {
3. // add items to combobox
4. ComboBox1.Items.Add("Rock");
5. ComboBox1.Items.Add("Paper");
6. ComboBox1.Items.Add("Scissors");
7.
8. }

4. Now put this code for your code module. This code is for Button1_Click. This will trigger to
decide who won the game.
1. public void Button1_Click(System.Object sender, System.EventArgs e)
2. {
3. Random r = new Random(); //create a random variable
4. int rr = r.Next(3); //int variable that has 3 values
5. int cChoice = rr; //holds the rr variable
6. int uChoice = ComboBox1.SelectedIndex; // choice as selected in combobox
7.
8. string pcChoice = "Rock";
9. if (cChoice == 1)
10. {
11. pcChoice = "Paper";
12. }
13. if (cChoice == 2)
14. {
15. pcChoice = "Scissors";
16. }
17. Label3.Text = pcChoice;
18.
19. if (cChoice == uChoice)
20. {
21. MessageBox.Show("Draw!");
22. }
23. else
24. {
25. if (cChoice == 0)
26. {
27. if (uChoice == 1)
28. {
29. MessageBox.Show("PC Choice: Rock" +
Constants.vbNewLine + "You Win!");
30. }
31. else
32. {
33. MessageBox.Show("PC Choice: Rock" +
Constants.vbNewLine + "You Lose!");
34. }
35. }
36. else if (cChoice == 1)
37. {
38. if (uChoice == 0)
39. {
40. MessageBox.Show("PC Choice: Paper" +
Constants.vbNewLine + "You Lose!");
41. }
42. else
43. {
44. MessageBox.Show("PC Choice: Paper" +
Constants.vbNewLine + "You Win!");
45. }
46. }
47. else
48. {
49. if (uChoice == 0)
50. {
51. MessageBox.Show("PC Choice: Scissors" +
Constants.vbNewLine + "You Win!");
52. }
53. else
54. {
55. MessageBox.Show("PC Choice: Scissors" +
Constants.vbNewLine + "You Lose!");
56. }
57. }
58. }
59. rr = default(int);
60. uChoice = default(int);
61. cChoice = default(int);
62. }
63. }

We have initialized r variable as our random number, rr variable that will hold an integer variable of
3 as the ccChoice variable will hold this random integer value and will display the random choice in
Label3. We also initialized variable uChoice as an Integer that will locate the inputted index of the
Combobox. If the random input of cChoice is the same with variable uChoice, then it will display a
draw. Otherwise if the random integer value of uChoice is 0 which means Rock and the inputted
index value of the user in the combobox is 1 which means Paper then it will display You Won
otherwise You Lose will display. If the random integer value of uChoice is 1 which means Paper
and the inputted index value of the user in the combobox is 0 which means Rock then it will display
You Won otherwise You Lose will display. And otherwise it will trigger to the result value if one
them inputted a scissor.

Output:

Das könnte Ihnen auch gefallen