1.需求

现在要制作一个游戏,玩家与计算机进行猜拳游戏,玩家出拳,计算机出拳,计算机自动判断输赢。

2.需求分析

根据需求,来分析一下对象,可分析出:玩家对象(Player)、计算机对象(Computer)、裁判对象(Judge)。 玩家出拳由用户控制,使用数字代表:1石头、2剪子、3布 计算机出拳由计算机随机产生 裁判根据玩家与计算机的出拳情况进行判断输赢

3.类对象的实现

玩家类示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class Player
{
 
    string name;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
 
    public int ShowFist()
    {
        Console.WriteLine("请问,你要出什么拳?  1.剪刀     2.石头    3.布");
        int result = ReadInt(1, 3);
        string fist = IntToFist(result);
        Console.WriteLine("玩家:{0}出了1个{1}", name, fist);
        return result;
    }
 
    /// <summary>
    /// 将用户输入的数字转换成相应的拳头
    /// </summary>
    /// <param name="input">
    /// <returns></returns>
    private string IntToFist(int input)
    {
        string result = string.Empty;
 
        switch (input)
        {
            case 1:
                result = "剪刀";
                break;
            case 2:
                result = "石头";
                break;
            case 3:
                result = "布";
                break;
        }
        return result;
    }
 
    /// <summary>
    /// 从控制台接收数据并验证有效性
    /// </summary>
    /// <param name="min">
    /// <param name="max">
    /// <returns></returns>
    private int ReadInt(int min,int max)
    {
        while (true)
        {
            //从控制台获取用户输入的数据
            string str = Console.ReadLine();
 
            //将用户输入的字符串转换成Int类型
            int result;
            if (int.TryParse(str, out result))
            {
                //判断输入的范围
                if (result >= min && result <= max)
                {
                    return result;
                }
                else
                {
                    Console.WriteLine("请输入1个{0}-{1}范围的数", min, max);
                    continue;
                }
            }
            else
            {
                Console.WriteLine("请输入整数");
            }
        }
    }
}

计算机类示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Computer
{
    //生成一个随机数,让计算机随机出拳
    Random ran = new Random();
    public int ShowFist()
    {
        int result = ran.Next(1, 4);
        Console.WriteLine("计算机出了:{0}", IntToFist(result));
        return result;
    }
 
    private string IntToFist(int input)
    {
        string result = string.Empty;
 
        switch (input)
        {
            case 1:
                result = "剪刀";
                break;
            case 2:
                result = "石头";
                break;
            case 3:
                result = "布";
                break;
        }
        return result;
    }
}

裁判类示例代码 这个类通过一个特殊的方式来判定结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Judge
{
    public void Determine(int p1, int p2)
    {
        //1剪刀   2石头 3布
        //1 3   1-3=-2 在玩家出1剪刀的情况下,计算机出3布,玩家赢
        //2 1   2-1=1   在玩家出2石头的情况下,计算机出1剪刀,玩家赢
        //3 2   3-2=1   在玩家出3布的情况下,计算机出2石头,玩家赢
        if (p1 - p2 == -2 || p1 - p2 == 1)
        {
            Console.WriteLine("玩家胜利!");
        }
        else if (p1 == p2)
        {
            Console.WriteLine("平局");
        }
        else
        {
            Console.WriteLine("玩家失败!");
        }
    }
}

4.对象的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
static void Main(string[] args)
{
    Player p1 = new Player() { Name="Tony"};
    Computer c1 = new Computer();
    Judge j1 = new Judge();
    while (true)
    {
        int res1 = p1.ShowFist();
        int res2 = c1.ShowFist();
        j1.Determine(res1, res2);
        Console.ReadKey();
    }
}

最新文章

  1. Android 手机卫士--获取联系人信息并显示与回显
  2. 请不要重复犯我在学习Python和Linux系统上的错误
  3. AnagularJs之directive
  4. 工行ATM转账——事务操作
  5. PackageManager源码分析
  6. sql 几点记录
  7. mysql将int 时间类型格式化
  8. 关于Asp.Net Forms身份认证
  9. node创建第一个应用
  10. 菜鸟随谈 Bootstrap 框架
  11. Scikit-learn使用总结
  12. Linux中ansible批量管理软件部署及剧本编写
  13. 类加载过程&amp;对象的创建过程
  14. What is a working set and how do I use it?
  15. 2.11 alert\confirm\prompt
  16. LeetCode-3.无重复字符的最长字串
  17. Linux基础命令---sudo
  18. python之多并发socket
  19. 微服务深入浅出(9)-- Nginx
  20. HDUOJ---(4708)Rotation Lock Puzzle

热门文章

  1. SpringInAction读书笔记--第4章面向切面
  2. Android基础之Activity
  3. 代码静态分析工具PCLint, Splint
  4. WIX: Hide installed program from the Add/Remove Programs window.
  5. Web前端新人笔记之jquery选择符
  6. DataGridView实现分页
  7. Nginx禁止通过IP,未绑定域名访问服务器
  8. django1.6之mysql配置
  9. python偏函数(functool.partail)
  10. shell写的计算器