zoukankan      html  css  js  c++  java
  • 枚举、封装方法的应用--剪刀石头布游戏

    Form

    封装方法:将需要封装成方法的代码选中,右键-重构-提取方法

    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 _5.石头剪刀布游戏
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string str = "剪刀";
                Game(str);
            }
    
            private void Game(string str)
            {
                lblPlayer.Text = str;
    
                Computer computer = new Computer();
                int computerNum = computer.ShowFist(ref lblComputer);
    
                Player player = new Player();
                int playerNum = player.ShowFist(str);
    
                Result res = Judge.showJudge(playerNum, computerNum);
                lblResult.Text = res.ToString();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                string str = "石头";
                Game(str);
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                string str = "布";
                Game(str);
            }
        }
    }
    

      

    Palyer.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _5.石头剪刀布游戏
    {
        class Player
        {
            public int ShowFist(string str)
            {
                switch(str)
                {
                    case "剪刀":
                        return 1;
                        break;
                    case "石头":
                        return 2;
                    case "布":
                        return 3;
                    default :
                        return 0;
                }
            }
        }
    }
    

    Computer.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace _5.石头剪刀布游戏
    {
        class Computer
        {
            public string Fist
            {
                get;
                set;
            }
            public int ShowFist(ref Label lblComputer)
            {
                Random r = new Random();
                int rNum = r.Next(1, 4);
                switch (rNum)
                {
                    case 1:
                        Fist = "剪刀";
                        break;
                    case 2:
                        this.Fist = "石头";
                        break;
                    case 3:
                        this.Fist = "布";
                        break;
                }
    
                lblComputer.Text = this.Fist;
                return rNum;
            }
        }
    }
    

    Judge.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _5.石头剪刀布游戏
    {
        public enum Result
        {
            玩家赢,
            电脑赢,
            平局
        }
        class Judge
        {
            public static Result showJudge(int palyer,int computer)
            {
                int res = palyer - computer;
                if (res == -2 || res == 2 || res == 1)
                    return Result.玩家赢;
                else if (res == 0)
                {
                    return Result.平局;
                }
                else
                    return Result.电脑赢;
            }
    
        }
    }
    

      

  • 相关阅读:
    Oracle数据库的备份及恢复策略研讨
    ast入门 (一)
    DisableThreadLibraryCalls
    写入注册表实现自启动
    QT学习1 hello程序
    打印断言函数
    注册表基本知识
    RAS详解
    const
    QT安装
  • 原文地址:https://www.cnblogs.com/my-cat/p/7418697.html
Copyright © 2011-2022 走看看