Я работаю над консольным приложением для скалолазания. Я могу заставить игру работать правильно и отображать победителя отдельной игры. Я не могу получить все результаты для отображения в конце. В конце игры он должен отображать результаты всех игр, и он должен остановиться, когда кто-то выиграет достаточно игр. Приложение в настоящее время закрывается после того, как была сыграна последняя игра, и я не могу понять, почему. Это код, который у меня есть, есть 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
Из взгляда программы вы вложили в нее усилие. Но теперь вы столкнулись с стеной или порогом фрустрирования, и сорта просто сбросили все это здесь и попросили отладить пару сотен строк кода. Вам нужно будет установить некоторые отладочные точки или, возможно, распечатать значения переменных на консоли и посмотреть, как все меняется по мере выполнения программы. –
Вопрос/подсказка: как и где вы печатаете результаты? У твоей главной игры есть галстук ... то что? – kurakura88