How to realize the game of rock scissors and paper with C #
Today, I would like to share with you the relevant knowledge of how C# implements the stone scissors paper game. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
You need to implement the man-machine fist guessing Mini Game shown in the following figure:
We need to create a player class Player, a computer class Computer, and a referee class Judge to simulate their respective operations:
[Player.cs]
/ * * author: JeronZhou * time: 2021-11-01 * function: stone scissors paper game * / using System;namespace Test2_2 {public class Player {public string FistName {get; set;} public int Play (string name) {FistName = name Switch (FistName) {case "rock": return 1; case "scissors": return 2; case "paper": return 3; default: return 0 }}
[Computer.cs]
/ * * author: JeronZhou * time: 2021-11-01 * function: stone scissors paper game * / using System;namespace Test2_2 {public class Computer {public string FistName {get; set;} public int RandomPlay () {Random random = new Random (Guid.NewGuid (). GetHashCode ()); int num = random.Next (1,4) Switch (num) {case 1: FistName = "rock"; break; case 2: FistName = "scissors"; break Case 3: FistName = "cloth"; break;} return num;}}
[Judge.cs]
/ * * author: JeronZhou * time: 2021-11-01 * function: stone scissors paper game * / using System;namespace Test2_2 {public class Judge {public string Win (int play, int computer) {int result = play-computer Switch (result) {case-1: return "you win"; case 2: return "you win"; case-2: return "you lose" Case 1: return "you lost"; default: return "tie";}}
[form Design]
There are 5 tags (3 empty tags) and three buttons.
[MainForm.cs]
/ * * author: JeronZhou * time: 2021-11-01 * function: stone scissors paper game * / using System;using System.Windows.Forms;namespace Test2_2 {public partial class MainForm: Form {public MainForm () {InitializeComponent () } void Button1Click (object sender, EventArgs e) {Player p = new Player (); int playerName = p.Play (button1.Text); label3.Text = p.FistName; Computer c = new Computer (); int computerName = c.RandomPlay (); label4.Text = c.FistName Judge judge = new Judge (); label5.Text = judge.Win (playerName, computerName);} void Button2Click (object sender, EventArgs e) {Player p = new Player (); int playerName = p.Play (button2.Text); label3.Text = p.FistName; Computer c = new Computer () Int computerName = c.RandomPlay (); label4.Text = c.FistName; Judge judge = new Judge (); label5.Text = judge.Win (playerName, computerName);} void Button3Click (object sender, EventArgs e) {Player p = new Player (); int playerName = p.Play (button3.Text) Label3.Text = p.FistName; Computer c = new Computer (); int computerName = c.RandomPlay (); label4.Text = c.FistName; Judge judge = new Judge (); label5.Text = judge.Win (playerName, computerName);}
[Program.cs]
/ * * author: JeronZhou * time: 2021-11-01 * function: stone scissors paper game * / using System;using System.Windows.Forms;namespace Test2_2 {internal sealed class Program {[STAThread] private static void Main (string [] args) {Application.EnableVisualStyles () Application.SetCompatibleTextRenderingDefault (false); Application.Run (new MainForm ());}}
[test results]
These are all the contents of this article "how to realize the game of rock scissors and paper in C #". Thank you for your reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.