如何显示石头剪刀布游戏的结果
本文关键字:石头剪刀布 游戏 结果 显示 何显示 | 更新日期: 2023-09-27 18:02:42
我正在制作一个石头剪刀布控制台应用程序。我可以让游戏正确运行,并显示个人游戏的获胜者。我无法得到所有的结果显示在最后。在游戏结束时,它应该显示所有游戏的结果,当有人赢得足够多的游戏时,它应该停止。应用程序目前关闭后,最后一场比赛,我不知道为什么。这是我的代码,有3个不同的类。
类1namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Game rps = new Game();
rps.printHeader();
rps.userSettings();
rps.gameStart();
}
}
}
二班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;
}
}
}
嘿,我的朋友,我会帮你的。但是如果你接受了@ bloorgbeard的评论,你就不需要这个了(真的是一个感谢的评论,接受它)。
首先在类1
中有一个错误 namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Game rps = new Game();
rps.printHeader();
rps.userSettings();
rps.gameStart();
}
}
删除"rpps . 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();
}