我正在岩石剪刀控制檯應用程序上工作。我可以讓遊戲正常運行,並顯示單個遊戲的贏家。我無法獲得所有結果顯示在最後。在遊戲結束時,它應該顯示所有遊戲的結果,並且當有人贏得足夠的遊戲時它應該停止。應用程序目前在上一場比賽結束後關閉,我無法弄清楚原因。這是我有的代碼,有3個不同的類。如何顯示岩石紙剪刀遊戲的結果
1個
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;
}
}
}
您應該瞭解調試器 - 嘗試設置斷點並逐步執行代碼。 – Blorgbeard
從程序的外觀來看,你已經付出了努力。但是現在你已經遇到了一堵牆,或者你的挫折門檻,並且只是把整個東西扔在這裏,並要求我們調試幾百行代碼。您必須設置一些調試點,或者可以將變量的值打印到控制檯,並觀察程序執行時的情況。 –
問題/提示:您如何/在哪裏打印結果?你的主要有gamestart ....然後呢? – kurakura88