C # WinForm Learn---Realize the game of stone scissors cloth

Source: Internet
Author: User

This article uses WinForm to realize the simple stone scissors cloth game, the main realization, the computer randomly punches, the player manually clicks the fist, realizes the simple background picture 3 seconds to switch, the simple statistic information.

1.

2. Implementing the Code

Create a new Windows Forms program, with the number 1 for the stone, the number 2 for the scissors, the number 3 for the cloth, the result of the player and the computer to punch the difference, there are three results

    • Player win: -1,2
    • Tie: 0
    • Player loses: Other values

Create a new 3 class:

1) Computer.cs Computer random Punch

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceStone Scissors Cloth {classComputer { Public  stringFist {Get; Set; }         Public intshowfist () {Random rnd=NewRandom (); intfist = rnd. Next (1,4); Switch(fist) { Case 1: Fist ="Stone"; Break;  Case 2: Fist ="Scissors"; Break;  Case 3: Fist ="cloth"; Break; }            returnFist; }    }}

2), Judge.cs Referee class judgment win or lose

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceStone Scissors Cloth {classJudge { Public enumRESULT {player wins, computer wins, tie} Public StaticRESULT Whowin (intPlayernum,intcomputernum) {            intresult = Playernum-Computernum; if(Result = =-1|| result = =2)            {                returnRESULT. Player wins; }            Else if(Result = =0)            {                returnRESULT. a tie; }            Else            {                returnRESULT. Computer wins; }        }    }}

3), Player.cs players, punch

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceStone Scissors Cloth {classPlayer { Public Static intShowfist (stringFist) {            Switch(fist) { Case "Stone":return 1;  Case "Scissors":return 2;  Case "cloth":return 3; default:return 0; }        }    }}

Interface Background Implementation code:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceStone Scissors Cloth { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        /// <summary>        ///Click the stone button/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidBtnstone_click (Objectsender, EventArgs e) {String Fist="Stone";        Game (fist); }        /// <summary>        ///Click the Scissors button/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidBtnscissors_click (Objectsender, EventArgs e) {String Fist="Scissors";        Game (fist); }        /// <summary>        ///Click the cloth button/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidBtncloth_click (Objectsender, EventArgs e) {String Fist="cloth";        Game (fist); }        //background picture Carouselstring[] paths = Directory.GetFiles (@"C:\work\stone");//there must be a picture in this directory, otherwise it will be error        Private voidTimer1_Tick (Objectsender, EventArgs e) {             This. BackgroundImage = Image.FromFile (paths[NewRandom (). Next (0, Paths.        Length)]); }        Static intPlayerwintimes =0;//the number of times the player wins        Static intGametimes =0;//total number of times        Static intTietimes =0;//Tie Times        /// <summary>        ///General Methods/// </summary>        /// <param name= "fist" ></param>        Private voidGame (String fist) {gametimes++; Lbplayer.text=Fist; intPlayernum =player.showfist (fist); Computer CPU=Newcomputer (); intCpunum =CPU.            Showfist (); Lbcomputer.text=CPU.            Fist; Judge.result RESULT=Judge.whowin (Playernum, cpunum); Lbjudge.text=result.            ToString (); Lbstatistics.text="statistics: \n\n1. You won."+ Playerwintimes +"Game!\n\n"+"2. It's a tie ."+ Tietimes +"times; \ n"+"3. Lost"+ (Gametimes-playerwintimes-tietimes) +"N. a game;"+"4. A total of"+ Gametimes +"Game!\n\n"; if(Result = =Judge.result. Player wins) {Playerwintimes++; MessageBox.Show ("Congratulations, you've won."+ Playerwintimes +"game! "+"a total of"+ Gametimes +"game! "); }            Else if(Result = =Judge.result.) {Tietimes++; }        }    }}

The difficulty of implementing the game is to think of the stone scissors cloth with numbers to replace, if the logic of the implementation is not difficult.

This article source: Https://github.com/amosli/CSharp/tree/stone

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.