2015年11月14日 星期六

C9_4 Client.cs 使用者端程式碼

using System;
namespace C9_4
{
    ///
    ///使用者端
    ///
    public class Client
    {
        public static void Main()
        {
            //初始分數
            int score = 500;
            //每一個贏取的分數,隨機獲得的圖形號,每一局下的柱
            int win, choice, bet;
            //宣告對象
            Shape sp = new Shape();
            Random ran = new Random();
            Message msg = new Message();
            //調用Message 類別的遊戲開始方法
            msg.Begin();
            while (true)
            {
                if (!msg.Ask())
                {
                    break;
                }
                Console.WriteLine("你現在的分數:{0}", score);
                Console.WriteLine("請押住:");
                Console.ReadLine();
                //如果押注的輸入不正確進行異常處理並預設下注為100分
                try
                {
                    bet = Convert.ToInt32(Console.ReadLine());

                }
                catch
                {
                    bet = 100;
                }
                if (bet < score)
                {
                    score -= bet;
                }
                else
                {
                    bet = score;
                    score = 0;
                }
                Console.WriteLine("剩餘分數:{0}",score);
                win = 0;
                for (int i = 0; i < 3; i++)
                {
                    choice = ran.Next() % 4;
                    switch (choice)
                    {
                        case 0:
                            sp = new RectTriangle(5, 4);
                            goto end;
                        case 1:
                            sp = new RectEqualTriangle(5);
                            goto end;
                        case 2:
                            sp = new Rectangle(5, 4);
                            goto end;
                        case 3:
                            sp = new Square(5);
                            goto end;

                    }
                end:
                    //利用多型性計算得分
                sp.Draw();
                win += sp.GetArea() * (i + 1) * bet / 100;
                Console.WriteLine("本次贏得分數:{0}", win);  
                }
                score += win;
                Console.WriteLine("餘下總分數:{0}", score);
                if (score < 100)
                {
                    Console.WriteLine("對不起,你的分數不噢,請退出遊戲!!!");
                    break;
                }
            }
        }
    }
}

沒有留言: