C#------猜拳遊戲

來源:互聯網
上載者:User

標籤:

namespace 出拳遊戲05{    //玩家類    class Player    {        //儲存出拳的內容,顯示到表單中        public string FistName { get; set; }        //出拳的方法        public int Play(string name)        {             this.FistName = name;            switch (name)            {                case "石頭": return 1;                case"剪刀":return 2;                case"布":return 3;            }            return 0;        }    }}

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 出拳遊戲05{    //電腦類    class Computer    {        //儲存出拳的內容        public string FistName { get; set; }        public int Play()        {            Random r = new Random();            int num = r.Next(1, 4);            switch (num)            {                case 1: this.FistName = "石頭"; break;                case 2: this.FistName = "剪刀"; break;                case 3: this.FistName = "布"; break;            }            return num;        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 出拳遊戲05{    //裁判類    class CaiPan    {        public string WhoWin(int pNum, int cNum)        {             //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 平手            if (pNum - cNum == -1 || pNum - cNum == 2)            {                return "玩家贏了";            }            else if (pNum - cNum == -2 || pNum - cNum == 1)            {                return "電腦贏了";            }            else            {                return "平手了";            }        }    }}
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 出拳遊戲05{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void btnshitou_Click(object sender, EventArgs e)        {            PlayGame(btnshitou.Text);        }        private void PlayGame(string name)        {            Player p = new Player();            int pNum = p.Play(name);            //把出拳的內容顯示到表單中            labPlay.Text = p.FistName;            //電腦            Computer c = new Computer();            int cNum = c.Play();            labComputer.Text = c.FistName;            //裁判            CaiPan cp = new CaiPan();            labres.Text = cp.WhoWin(pNum, cNum);        }        private void btnjiandao_Click(object sender, EventArgs e)        {            PlayGame(btnjiandao.Text);        }        private void btnbu_Click(object sender, EventArgs e)        {            PlayGame(btnbu.Text);        }    }}

 

C#------猜拳遊戲

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.