欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#面向?qū)ο缶幊讨氯螒驅(qū)崿F(xiàn)方法

 更新時間:2014年11月22日 15:56:01   投稿:shichen2014  
這篇文章主要介紹了C#面向?qū)ο缶幊讨氯螒驅(qū)崿F(xiàn)方法,以一個完整的猜拳游戲為例講述了C#面向?qū)ο蟪绦蛟O計的具體實現(xiàn)步驟,具有一定的學習與借鑒價值,需要的朋友可以參考下

本文實例講述了C#面向?qū)ο缶幊讨氯螒驅(qū)崿F(xiàn)方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

1.需求

現(xiàn)在要制作一個游戲,玩家與計算機進行猜拳游戲,玩家出拳,計算機出拳,計算機自動判斷輸贏。

2.需求分析

根據(jù)需求,來分析一下對象,可分析出:玩家對象(Player)、計算機對象(Computer)、裁判對象(Judge)。 玩家出拳由用戶控制,使用數(shù)字代表:1石頭、2剪子、3布 計算機出拳由計算機隨機產(chǎn)生 裁判根據(jù)玩家與計算機的出拳情況進行判斷輸贏。

3.類對象的實現(xiàn)

①.玩家類示例代碼:

復制代碼 代碼如下:
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>
    /// 將用戶輸入的數(shù)字轉(zhuǎn)換成相應的拳頭
    /// </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>
    /// 從控制臺接收數(shù)據(jù)并驗證有效性
    /// </summary>
    /// <param name="min">
    /// <param name="max">
    /// <returns></returns>
    private int ReadInt(int min,int max)
    {
        while (true)
        {
            //從控制臺獲取用戶輸入的數(shù)據(jù)
            string str = Console.ReadLine();
 
            //將用戶輸入的字符串轉(zhuǎn)換成Int類型
            int result;
            if (int.TryParse(str, out result))
            {
                //判斷輸入的范圍
                if (result >= min && result <= max)
                {
                    return result;
                }
                else
                {
                    Console.WriteLine("請輸入1個{0}-{1}范圍的數(shù)", min, max);
                    continue;
                }
            }
            else
            {
                Console.WriteLine("請輸入整數(shù)");
            }
        }
    }
}

②.計算機類示例代碼:
復制代碼 代碼如下:
class Computer
{
    //生成一個隨機數(shù),讓計算機隨機出拳
    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;
    }
}

③.裁判類示例代碼 這個類通過一個特殊的方式來判定結(jié)果:
復制代碼 代碼如下:
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("玩家失敗!");
        }
    }
}

④.對象的實現(xiàn):
復制代碼 代碼如下:
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();
    }
}

希望本文所述對大家的C#程序設計有所幫助。

相關文章

最新評論