如何计算胜利次数,频率和胜率

时间:2015-10-16 13:50:28

标签: c# console console-application

我之前为用户创建了一个程序来玩NumBaseBall游戏。我想在其中添加三个功能:

(1)显示获胜次数,
(2)显示播放的频率
(3)显示玩家的获胜%

我有一个名为GameResults的外部类,其中包含一个计算确定游戏结果的罢工或球的方法,另一个用于检查玩家是否想再玩一次的方法。 如果用户决定再次播放,则每次创建GameResults的新实例时,如何在不删除每个累积数据的情况下实现这些额外的三个功能? 这是我的工作代码:

class Program
{
    static void Main(string[] args)
    {
        do
        {
           PlayBall();
        }
        while (GameResults.PlayAgain());
    }

    public static void PlayBall()
    {       
        Console.WriteLine("\t\t\t*************************************");
        Console.WriteLine("\t\t\t*          Let's Have Fun           *");
        Console.WriteLine("\t\t\t*           Welcome To The          *");
        Console.WriteLine("\t\t\t*       Number Baseball Game        *");
        Console.WriteLine("\t\t\t*************************************\n");

        GameResults gameresults =  new GameResults(); 

            for (int trysCounter = 1; trysCounter <= 5; trysCounter++)
            {
                gameresults.Strikes = 0;

                Random r = new Random();
                var myRange = Enumerable.Range(1, 9);
                var computerNumbers = myRange.OrderBy(i => r.Next()).Take(3).ToList();
                Console.WriteLine("The Game's Three Random Integers Are: (Hidden from user)");
                foreach (int integer in computerNumbers)
                {
                    Console.WriteLine("{0}", integer);
                }

                List<int> playerNumbers = new List<int>();
                Console.WriteLine("Please Enter Three Unique Single Digit Integers and Press ENTER after each:");
                for (int i = 0; i < 3; i++)
                {
                    Console.Write("");
                    int number = Convert.ToInt32(Console.ReadLine());
                    playerNumbers.Add(number);
                }

                gameresults.StrikesOrBalls(computerNumbers, playerNumbers);
                Console.WriteLine("-----------------------> Computer's Numbers = {0}{1}{2}", computerNumbers[0], computerNumbers[1], computerNumbers[2]);
                Console.WriteLine("-----------------------> Player's Numbers = {0}{1}{2}", playerNumbers[0], playerNumbers[1], playerNumbers[2]);
                Console.WriteLine("-----------------------> Game Results = {0} STRIKES & {1} BALLS\n", gameresults.Strikes, gameresults.Balls);
                Console.WriteLine("You have tried {0} times\n", trysCounter);

                gameresults.TotalStrikes = gameresults.TotalStrikes + gameresults.Strikes;

                Console.WriteLine("STRIKES = {0} ", gameresults.TotalStrikes);

                if (gameresults.TotalStrikes >= 3)
                {
                    Console.WriteLine("-----------------------> YOU ARE A WINNER!!!");
                    break;
                }
            }

            if (gameresults.TotalStrikes < 3)
            {
                Console.WriteLine("YOU LOSE :(");
            } 
    }

}

以下是我的外部方法:

public void StrikesOrBalls(List<int> computer, List<int> player)
    {
        for (int c = 0; c < 3; c++)
        {
            for (int p = 0; p < 3; p++)
            {
                if (computer[c] == player[p] && computer[c] != player[c])
                    Balls++;
            }
        }

        if(computer[0]== player[0])
            Strikes++;
        if(computer[1] == player[1])
            Strikes++;
        if(computer[2] == player[2])
            Strikes++;
    }

    public static bool PlayAgain()
    {
        while (true)
        {
            Console.Write("Do you want to play again [YES/NO]?\n");
            string answer = Console.ReadLine().ToUpper();
            if (answer == "YES")
                return true;
            if (answer == "NO")
                Console.WriteLine("Thanks for playing NumBaseBall :) Goodbye!");
            return false;
        }
    }

0 个答案:

没有答案