C # Winform learning --- implement the game of stone scissors cloth
. Implement code to create a windows form program, use number 1 to represent the stone, use number 2 to represent the scissors, and use number 3 to represent the cloth. The result is the gap between the player and the computer, there are three results for players to win:-1, 2 flat hands: 0 players to lose: other values create 3 categories: 1) Computer. cs computer random punch copy code using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace stone scissors cloth {class Computer {public string Fist {get; set;} public int ShowFist () {Random rnd = new Random (); int fist = rnd. next (1, 4); switch (fist) {case 1: Fist = "Stone"; break; case 2: Fist = "Scissors"; break; case 3: Fist = "cloth"; break ;}return fist ;}}} copy Code 2), Judge. cs referee judgment win-win copy code using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace stone scissors cloth {class Judge {public enum RESULT {player win, computer win, flat} public static RESULT WhoWin (int playerNum, int computerNum) {int result = playerNum-computer Num; if (result =-1 | result = 2) {return RESULT. player win;} else if (result = 0) {return RESULT. flat;} else {return RESULT. computer win; }}} copy code 3), Player. cs player, copy the code using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace stone scissors cloth {class Player {public static int ShowFist (string fist) {switch (fist) {case "Stone": return 1; case "Scissors": r Eturn 2; case "cloth": return 3; default: return 0 ;}}} copy the code interface Background implementation code: copy the code using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. IO; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace stone scissors cloth {public partial class Form1: Form {public Form1 () {InitializeComponent () ;}/// <summa Ry> /// click the stone button /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void btnStone_Click (object sender, eventArgs e) {String fist = ""; Game (fist );} /// <summary> /// click the scissors button /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void btnScissors_Click (object sender, eventArgs e) {String fist = "Scissors"; Game (fist) ;}/// <summary> /// Click the cloth button /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void btnCloth_Click (object sender, eventArgs e) {String fist = "cloth"; Game (fist) ;}// background image carousel String [] paths = Directory. getFiles (@ "C: \ work \ stone"); // this directory must contain images; otherwise, the private void timereffectick (object sender, EventArgs e) {this. backgroundImage = Image. fromFile (paths [new Random (). next (0, paths. length)]);} sta Tic int playerWinTimes = 0; // Number of player wins static int gameTimes = 0; // total number of static int tieTimes = 0; // flat times /// <summary> /// general method // </summary> /// <param name = "fist"> </param> private void Game (String fist) {gameTimes ++; lbPlayer. text = fist; int playerNum = Player. showFist (fist); Computer cpu = new Computer (); int cpuNum = cpu. showFist (); lbComputer. text = cpu. fist; Judge. RESULT result = Judge. whoWin (p LayerNum, cpuNum); lbJudge. Text = result. ToString (); lbStatistics. Text = "Statistics: \ n \ n1. you win" + playerWinTimes +! \ N "+" 2. it has been flat for "+ tieTimes +" times; \ n "+" 3. lost "+ (gameTimes-playerWinTimes-tieTimes) +" game; \ n "+" 4. A total of "+ gameTimes +" games were held! \ N "; if (result = Judge. RESULT. player win) {playerWinTimes ++; MessageBox. show ("Congratulations, you have won" + playerWinTimes +! "+" A total of "+ gameTimes +" matches! ");} Else if (result = Judge. RESULT. Flat) {tieTimes ++ ;}}}}