C#之winform 猜拳小游戲


 

C#之winform 猜拳小游戲

1、建立項目文件

2、進行界面布局

2、1 玩家顯示(控件:label)

2、2  顯示玩家進行選擇的控件(控件:label)

 2、3 電腦顯示(控件:label)

 2、4   顯示電腦進行選擇的控件(控件:label)

 2、5 結果顯示(控件:label)

 2、6 玩家與電腦的游戲結果(控件:textBox)

 2、7  玩家的選擇按鈕(控件:Button)

2、8 玩家的選擇按鈕(控件:Button)

 2、9  玩家的選擇按鈕(控件:Button)

 2、10 運行

3 、代碼實現

 3、1 創建玩家的類​

3、2 創建電腦的類

3、3 創建裁判類(決策是誰贏了)

3、4 功能實現

3、4、1 打開Form1對應的代碼

 3、4、2 窗口的控制代碼


 

1、建立項目文件

 

2、進行界面布局

 

 

 

 

在這個界面布局中,我們要修改一些屬性(下面的序號與上面的截圖一一對應):

2、1 玩家顯示(控件:label)

 其中,Name :是我們在程序中對這個變量進行控制的名稱

text:控件label在顯示的時候的名稱。

 

 

2、2  顯示玩家進行選擇的控件(控件:label)

Name :是我們在程序中對這個變量進行控制的名稱

這里是指的是選擇的是:石頭、剪刀、布

 

 

 2、3 電腦顯示(控件:label)

 Name :是我們在程序中對這個變量進行控制的名稱

text:控件label在顯示的時候的名稱。

 

 

 

 2、4   顯示電腦進行選擇的控件(控件:label)

Name :是我們在程序中對這個變量進行控制的名稱

這里是指的是選擇的是:石頭、剪刀、布

 

 

  

 2、5 結果顯示(控件:label)

Name :是我們在程序中對這個變量進行控制的名稱
text:控件label在顯示的時候的名稱。

 

 

 2、6 玩家與電腦的游戲結果(控件:textBox)

Name :是我們在程序中對這個變量進行控制的名稱

這里是指的是選擇的是:贏、輸、平

 

 

 

 

 2、7  玩家的選擇按鈕(控件:Button)

Name :是我們在程序中對這個變量進行控制的名稱
text:控件label在顯示的時候的名稱。

 

 

2、8 玩家的選擇按鈕(控件:Button)

Name :是我們在程序中對這個變量進行控制的名稱
text:控件label在顯示的時候的名稱。

 

 2、9  玩家的選擇按鈕(控件:Button)

Name :是我們在程序中對這個變量進行控制的名稱
text:控件label在顯示的時候的名稱。

 

 2、10 運行

  通過上面的布局后,我們可以進行運行一下,會得到一個界面

 

 

 

3 、代碼實現

在這里,我們需要變現相應的代碼,來實現上面的控件所要實現的功能;

 

 3、1 創建玩家的類

 

 創建類

 

 

 

 Player.cs的內容如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaiQuanMyself
{
    class Player
    {
        // 儲存玩家的名稱
        public string playerName
        {
            set;
            get;
        }
        /// <summary>
        /// funtion: 實現玩家猜拳的名稱與數字之間的對應關系
        /// 1石頭     2剪刀     3布
        /// return type: int
        /// </summary>
        /// <param name="str">玩家猜拳的名稱</param>
        /// <returns>猜拳的名稱對應的數字;如玩家選擇"布",對應輸出數字"3"</returns>
        public int PlayerInformation(string str)
        {
            
            int num = 0;
            switch(str)
            {
                case "石頭": num = 1; this.playerName = "石頭"; break;
                case "剪刀": num = 2; this.playerName = "剪刀"; break;
                case "":   num = 3; this.playerName = ""; break;
            }
            return num;
        }
    }
}
View Code

 

 

3、2 創建電腦的類

(創建方式同3、1)

實現的類如下:

Computer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaiQuanMyself
{
    class Computer
    {
        // 電腦猜拳的名稱
        public string computerName
        {
            set;
            get;
        }
        /// <summary>
        /// function: 返回電腦出的拳對應的數字,同時將對應的名稱存儲
        /// return type: int
        /// </summary>
        /// <returns>返回猜拳對的數字</returns>
        public int ComputerInformation()
        {
            //1石頭     2剪刀     3布
            Random rd = new Random();
            int num = rd.Next(1, 4); // 1,2,3之間的隨機數
            switch (num)
            {
                case 1: this.computerName = "石頭"; break;
                case 2: this.computerName = "剪刀"; break;
                case 3: this.computerName = "";   break;
            }

            return num;
        }
    }
}
View Code

 

3、3 創建裁判類(決策是誰贏了)

(創建方式同3、1)

實現的類如下:

Referee.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaiQuanMyself
{
    class Referee
    {
//裁判情況表格
//1石頭  2剪刀  3布
//玩家  對應名稱 電腦  對應名稱 結果  玩家結果
//1        石頭      1        石頭      0            平
//1        石頭      2        剪刀      -1        贏
//1        石頭      3        布        -2        輸
//
//2        剪刀      1        石頭      1            輸
//2        剪刀      2        剪刀      0            平
//2        剪刀      3        布        -1        贏
//
//3        布           1        石頭      2            贏
//3        布        2        剪刀      1            輸
//3        布        3        布        0            平

        /// <summary>
        /// 裁判判決的情況
        /// </summary>
        public enum eResult
        {
            win =0,
            draw = 1,
            loss =2
        }
        /// <summary>
        /// 裁判判決的情況
        /// </summary>
        public string[] sResult = new string[] { "", "", ""};

        /// <summary>
        /// 裁決玩家與電腦之間的猜拳結果
        /// return type : int
        /// </summary>
        /// <param name="playerChoice">玩家猜拳</param>
        /// <param name="computerChocie">電腦猜拳</param>
        /// <returns>猜拳結果</returns>
        public int Judgement(int playerChoice, int computerChocie)
        {
            //result = -1,2贏  0平  其他則輸  (指的是玩家輸贏的情況)
            int result = playerChoice - computerChocie;
            if (result == -1 || result==2 )
            {
                return (int)eResult.win;
            }
            else if (result == 0)
            {
                return (int)eResult.draw;
            }
            else
            {
                return (int)eResult.loss;
            }
        }
    }
}
View Code

 

3、4 功能實現

3、4、1 打開Form1對應的代碼

 

 3、4、2 窗口的控制代碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CaiQuQuanGame
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //禁止最大化窗口
            this.MaximizeBox = false;
            // 禁止對窗口進行拖拉
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        // 點擊事件觸發
        private void butStone_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            RunGame(btn.Text);
        }
        public void RunGame(string playerChoice)
        {
            // 獲取輸入
            Player pChoice = new Player();
            int pResult = pChoice.PlayerInformation(playerChoice);
            labPlayerChoice.Text = pChoice.playerName;

            Computer cChoice = new Computer();
            int cResult = cChoice.ComputerInformation();
            labComputerChoice.Text = cChoice.computerName;

            // 結果判斷
            Referee rChoice = new Referee();
            int rResult = rChoice.Judgement(pResult, cResult);

            // 輸出
            textBoxResult.Text = rChoice.sResult[rResult];

        }
    }
}
View Code

 

 到這里我們就完成了整個猜拳游戲的編寫。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM