image.png
    image.png

    image.png
    Class:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Threading.Tasks;
    5. using System.Windows.Forms;
    6. namespace _116_石头剪刀布
    7. {
    8. static class Program
    9. {
    10. public static double WinRate=0;
    11. public static int Win=0;
    12. public static int Failure=0;
    13. public static int Draw=0;
    14. /// <summary>
    15. /// 应用程序的主入口点。
    16. /// </summary>
    17. [STAThread]
    18. static void Main()
    19. {
    20. Application.EnableVisualStyles();
    21. Application.SetCompatibleTextRenderingDefault(false);
    22. Application.Run(new Form1());
    23. }
    24. }
    25. }
    26. using System;
    27. using System.Collections.Generic;
    28. using System.Linq;
    29. using System.Text;
    30. using System.Threading.Tasks;
    31. namespace _116_石头剪刀布
    32. {
    33. public enum enumResult
    34. {
    35. 玩家赢,
    36. 电脑赢,
    37. 平局
    38. }
    39. public class Result
    40. {
    41. private int _win;
    42. public int Win
    43. {
    44. get { return _win; }
    45. set { _win = value; }
    46. }
    47. private int _faileure;
    48. public int Failure
    49. {
    50. get { return _faileure; }
    51. set { _faileure = value; }
    52. }
    53. private double _winRate;
    54. public double WinRate
    55. {
    56. get { return _winRate; }
    57. set { _winRate = value; }
    58. }
    59. public int Draw { get => _draw; set => _draw = value; }
    60. private int _draw;
    61. public enumResult Judge(int playerNumber, int computerNumber)
    62. {
    63. int sum;
    64. if (playerNumber - computerNumber == -1 || playerNumber - computerNumber == 2)
    65. {
    66. Program.Win++;
    67. sum = Program.Win + Program.Failure + Program.Draw;
    68. WinRate = (double)Program.Win / sum;
    69. return enumResult.玩家赢;
    70. }
    71. else if (playerNumber == computerNumber)
    72. {
    73. Program.Draw++;
    74. sum = Program.Win + Program.Failure + Program.Draw;
    75. WinRate = (double)Program.Win / sum;
    76. return enumResult.平局;
    77. }
    78. else
    79. {
    80. Program.Draw++;
    81. sum = Program.Win + Program.Failure + Program.Draw;
    82. WinRate =(double) Program.Win / sum;
    83. return enumResult.电脑赢;
    84. }
    85. }
    86. }
    87. }
    88. using System;
    89. using System.Collections.Generic;
    90. using System.Linq;
    91. using System.Text;
    92. using System.Threading.Tasks;
    93. namespace _116_石头剪刀布
    94. {
    95. class Computer
    96. {
    97. /// <summary>
    98. /// 存储电脑出的手势
    99. /// </summary>
    100. public string Fist { get; set; }
    101. public int ShowFist()
    102. {
    103. Random r = new Random();
    104. int rNumber = r.Next(1, 4);
    105. switch (rNumber)
    106. {
    107. case 1: this.Fist = "石头"; break;
    108. case 2: this.Fist = "剪刀"; break;
    109. case 3: this.Fist = "布"; break;
    110. default:
    111. break;
    112. }
    113. return rNumber;
    114. }
    115. }
    116. }
    117. using System;
    118. using System.Collections.Generic;
    119. using System.Linq;
    120. using System.Text;
    121. using System.Threading.Tasks;
    122. namespace _116_石头剪刀布
    123. {
    124. class Player
    125. {
    126. public int ShowFist(string fist)
    127. {
    128. int num = 0;
    129. switch (fist)
    130. {
    131. case "石头": num = 1; break;
    132. case "剪刀": num = 2; break;
    133. case "布": num = 3; break;
    134. }
    135. return num;
    136. }
    137. }
    138. }

    Form:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. namespace _116_石头剪刀布
    11. {
    12. public partial class Form1 : Form
    13. {
    14. public Form1()
    15. {
    16. InitializeComponent();
    17. }
    18. private void btnStone_Click(object sender, EventArgs e)
    19. {
    20. PlayGame(btnStone.Text);
    21. }
    22. private void BtnCut_Click(object sender, EventArgs e)
    23. {
    24. PlayGame(BtnCut.Text);
    25. }
    26. private void btnCloth_Click(object sender, EventArgs e)
    27. {
    28. PlayGame(btnCloth.Text);
    29. }
    30. /// <summary>
    31. /// 点击石头剪刀布判断输赢
    32. /// </summary>
    33. /// <param name="text"></param>
    34. private void PlayGame(string text)
    35. {
    36. //将btnStone的值给lblPlayer
    37. lblPlayer.Text = text;
    38. //调用Player类的ShowFist方法,给玩家出的手势赋int类型的值
    39. Player player = new Player();
    40. int playerNumber = player.ShowFist(lblPlayer.Text);
    41. //调用Computer类的ShowFist方法,给电脑出的手势赋int类型的值
    42. Computer computer = new Computer();
    43. int computerNumber = computer.ShowFist();
    44. //给lblComputer赋值computer.Fist属性
    45. lblComputer.Text = computer.Fist;
    46. //调用Result类的Judge方法,判断输赢
    47. Result result = new Result();
    48. enumResult res = result.Judge(playerNumber, computerNumber);
    49. lblResult.Text = res.ToString();
    50. //胜率
    51. lblWinRate.Text = result.WinRate.ToString();
    52. }
    53. private void label5_Click(object sender, EventArgs e)
    54. {
    55. }
    56. }
    57. }