Kevin Borkowski Kevin Borkowski - 3 months ago 6
C# Question

Rock Paper Scissor Array application C#

Let me start off with what i have so far. Here are my 3 classes:

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





class GameDetails
{
public string Name;
public int game;

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




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



public int NumGames
{
get
{
return numPlays;
}
set
{
numPlays = 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()
{

for (game = 1; game <= numPlays; game++)

Console.WriteLine("Please choose Rock, Paper, or Scissors");
string userSelection = Console.ReadLine();

Random r = new Random();
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");

}

Console.ReadLine();
// int arrayIndex = game - 1;
// gameArray[arrayIndex].Result = game;
// }


// }

//public override string ToString()
//{


// string outputString = game + "\n";

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

// return outputString;
//}


}
}
}


I need this program to only run for the specified number of games, determine a winner of a best of (number of games chosen) series, and display the results of each game. I am not receiving any errors, but when i run the code, it asks for the name and how many games. After i choose how many games to play, it just displays "Please choose Rock, Paper, or Scissors" the same number of times that i have chosen for the amount of games. How can i make this correctly count the number of games, determine a winner, and display the correct output.

Answer

Your for loop for (game = 1; game <= numPlays; game++) is missing the braces around the game method. From how it appears

for (game = 1; game <= numPlays; game++)
     Console.WriteLine("Please choose Rock, Paper, or Scissors");

The part Console.WriteLine(..) will run as many times as the numPlays is set.

What you will want to do is wrap the entire method body in the for loop and that should run the game repeatadly for 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");

            }

            Console.ReadLine();
            //        int arrayIndex = game - 1;
            //        gameArray[arrayIndex].Result = game;
            //    }


            //    }

            //public override string ToString()
            //{


            //    string outputString = game + "\n";

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

            //    return outputString;
            //}


        }
    }
}