【问题标题】:How to display results of rock paper scissors game [closed]如何显示剪刀石头布游戏的结果[关闭]
【发布时间】:2016-08-16 11:54:45
【问题描述】:

我正在开发一个石头剪刀布控制台应用程序。我可以让游戏正确运行,并显示个人游戏的获胜者。我无法在最后显示所有结果。在游戏结束时,它应该显示所有游戏的结果,并且当有人赢得足够的游戏时它应该停止。该应用程序当前在最后一场比赛结束后关闭,我不知道为什么。这是我的代码,有 3 个不同的类。

第一类

namespace ConsoleApplication1
    {
        class Program
    {
         static void Main(string[] args)
        {
            Game rps = new Game();
            rps.printHeader();
            rps.userSettings();
            rps.gameStart();
        }
    }
}

2 级

namespace ConsoleApplication1
    {
        class GameDetails
        {
            public string Name;
            public int game;
            public string Result;

        public GameDetails()
        {
            Name = "unknown";
            game = 0;
        }

        public GameDetails(string winner)
        {
            Result = winner;
        }
    }
}

最后是 3 级

namespace ConsoleApplication1
{
    class Game
    {
        string name;
        string winner;
        int numPlays;
        int game;
        GameDetails[] gameArray;



        public int NumGames
        {
            get
            {
                return numPlays;
            }
            set
            {
                numPlays = value;
            }

        }

        public string Winner
        {
            get
            {
                return winner;
            }
            set
            {
                winner = value;
            }
        }

        public void printHeader()
        {
            Console.WriteLine("Welcome to rock, paper, scissors");
            this.userSettings();
        }

        private void InitializeArrays()
        {
            gameArray = new GameDetails[game];
            for (int game = 0; game < numPlays; game++)
            {
                gameArray[game] = new GameDetails();
            }

        }

        public void userSettings()
        {
            Console.WriteLine("What is your name: ");
            name = Console.ReadLine();

            Console.WriteLine("How many games would you like to play?: ");
            Int32.TryParse(Console.ReadLine(), out numPlays);
            while (numPlays < 10 && numPlays % 2 == 0)
            {
                Console.WriteLine("\nNumber is not odd try again.");
                Console.WriteLine("How many games would you like to play?: ");
                Int32.TryParse(Console.ReadLine(), out numPlays);
            }

        }


        public void gameStart()
        {
            Random r = new Random();
            for (game = 1; game <= numPlays; game++)
            {
                Console.WriteLine("Please choose Rock, Paper, or Scissors");
                string userSelection = Console.ReadLine();


                int computerSelection = r.Next(4);


                if (computerSelection == 1)
                {
                    if (userSelection == "rock")
                    {
                        Console.WriteLine("Computer Choice: Rock\n");
                        Console.WriteLine("Game [{0}] is a tie", game);
                    }
                    else if (userSelection == "paper")
                    {
                        Console.WriteLine("Computer Choice: Paper\n");
                        Console.WriteLine("Game[{0}] is a tie", game);
                    }
                    else if (userSelection == "scissors")
                    {
                        Console.WriteLine("Computer Choice: Scissors\n");
                        Console.WriteLine("Game [{0}] is a tie", game);
                    }
                    else
                    {
                        Console.WriteLine("You must choose either rock, paper or scissors");
                    }

                }

                else if (computerSelection == 2)
                {
                    if (userSelection == "rock")
                    {
                        Console.WriteLine("Computer Choice: Paper\n");
                        Console.WriteLine("You lose game [{0}], papaer beats rock", game);

                    }
                    else if (userSelection == "paper")
                    {
                        Console.WriteLine("Computer Choice: Scissors\n");
                        Console.WriteLine("You lose game [{0}], scissors beats paper", game);

                    }
                    else if (userSelection == "scissors")
                    {
                        Console.WriteLine("Computer Choice: Rock\n");
                        Console.WriteLine("You lose game [{0}], Rock beats scissors", game);
                    }
                    else
                    {
                        Console.WriteLine("You must choose either rock, paper or scissors");
                    }

                }


                else if (computerSelection == 3)
                {
                    if (userSelection == "rock")
                    {
                        Console.WriteLine("The computer chose scissors");
                        Console.WriteLine("You win game [{0}], rock beats scissors", game);

                    }
                    else if (userSelection == "paper")
                    {
                        Console.WriteLine("The computer chose rock");
                        Console.WriteLine("You win game [{0}],paper beats rock", game);

                    }
                    else if (userSelection == "scissors")
                    {
                        Console.WriteLine("The computer chose paper");
                        Console.WriteLine("You win game [{0}], scissors beats paper!", game);

                    }
                    else
                    {
                        Console.WriteLine("You must choose either rock, paper or scissors");

                    }

                    winner = Console.ReadLine();

                }


            }
        }

        public override string ToString()
        {
            int arrayIndex = game - 1;
            gameArray[arrayIndex].Result = winner;

            string outputString = game + "\n";

            for (int game = 1; game < numPlays; game++)
            {
                int index = game - 1;
                outputString += "Game " + game + ":" + gameArray[index].Result + "\n";
                }

                return outputString;
        }


    }
}

【问题讨论】:

  • 您应该了解调试器 - 尝试设置断点并单步执行您的代码。
  • 一个问题/提示:您如何/在哪里打印结果?你的主要有游戏开始....然后呢?
  • @kurakura88 你说得很好。我需要调整我的主要

标签: c#


【解决方案1】:

嘿,我的朋友,我会帮忙的。但是,如果您允许 @Blorgbeard 的评论,您将不需要这个(真的是一个感谢评论,请放慢它)。

首先你在类 1 中有一个错误

 namespace ConsoleApplication1
{
    class Program
{
     static void Main(string[] args)
    {
        Game rps = new Game();
        rps.printHeader();
        rps.userSettings();
        rps.gameStart();
    }
}

删除这一行“rps.userSettings();”因为你已经在 printHeader() 过程中有这个了。

我使用“调试器 - 尝试设置断点并单步执行代码”捕获的下一个错误 :) 在 Class 3 的这一行中

    int computerSelection = r.Next(4);

改成:

    int computerSelection = r.Next(1,3);

如果您阅读此https://msdn.microsoft.com/es-es/library/2dx6wyd4(v=vs.110).aspx,您就会知道为什么错了:)

现在我发现的最后一个错误是因为您关闭了控制台,只需将此行添加到所有这些句子中(Console.ReadLine()):

    if (userSelection == "rock")
                {
                    Console.WriteLine("Computer Choice: Rock\n");
                    Console.WriteLine("Game [{0}] is a tie", game);
                    Console.ReadLine();
                }

希望对你有帮助。

【讨论】:

  • "int computerSelection = r.Next(1,3)" 在这句话中使用 (1,4) 你会得到更好的结果。
  • 帮助很大。非常感谢
猜你喜欢
  • 1970-01-01
  • 2016-01-16
  • 2022-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多