Looping the game

I made this game but when the game ends, I need to restart every game each time. How can I loop it so I can play another game right after
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks.Dataflow;

Console.WriteLine("Choose between Rock Raper or Scissors:");

String [] choices = {"rock", "paper", "scissors"};
String plrChoice = Console.ReadLine()!.ToLower();
Random random = new Random();
String cmprChoice = choices[random.Next(choices.Length)];
Console.WriteLine("Cmp choice: " + cmprChoice );
Console.WriteLine("Your choice: " + plrChoice);

if (plrChoice == cmprChoice)
{
Console.WriteLine("It's a tie!");
}else if(plrChoice == "rock")
{
if(cmprChoice == "paper")
{
Console.WriteLine("Computer wins!");
}else if(cmprChoice == "scissors")
{
Console.WriteLine("Player wins!");
}
}else if(plrChoice == "paper")
{
if(cmprChoice == "scissors")
{
Console.WriteLine("Computer wins!");
}else if(cmprChoice == "rock")
{
Console.WriteLine("Player wins!");
}
}else if(plrChoice == "scissors")
{
if(cmprChoice == "rock")
{
Console.WriteLine("Computer wins!");
}else if(cmprChoice == "paper")
{
Console.WriteLine("Player wins!");
}
}
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks.Dataflow;

Console.WriteLine("Choose between Rock Raper or Scissors:");

String [] choices = {"rock", "paper", "scissors"};
String plrChoice = Console.ReadLine()!.ToLower();
Random random = new Random();
String cmprChoice = choices[random.Next(choices.Length)];
Console.WriteLine("Cmp choice: " + cmprChoice );
Console.WriteLine("Your choice: " + plrChoice);

if (plrChoice == cmprChoice)
{
Console.WriteLine("It's a tie!");
}else if(plrChoice == "rock")
{
if(cmprChoice == "paper")
{
Console.WriteLine("Computer wins!");
}else if(cmprChoice == "scissors")
{
Console.WriteLine("Player wins!");
}
}else if(plrChoice == "paper")
{
if(cmprChoice == "scissors")
{
Console.WriteLine("Computer wins!");
}else if(cmprChoice == "rock")
{
Console.WriteLine("Player wins!");
}
}else if(plrChoice == "scissors")
{
if(cmprChoice == "rock")
{
Console.WriteLine("Computer wins!");
}else if(cmprChoice == "paper")
{
Console.WriteLine("Player wins!");
}
}
15 Replies
Angius
Angius•2w ago
The best way to loop a piece of code, is to... use a loop
Angius
Angius•2w ago
A do...while loop works well for this purpose Or a basic while loop
DidierThePenguin 🇫🇷
okay a while (true) works ?
Angius
Angius•2w ago
Sure As long as you remember to break out of it to end the game Otherwise it will just run forever
DidierThePenguin 🇫🇷
so I can make an option to continue the game or to stop it with this ?
Angius
Angius•2w ago
ye
DidierThePenguin 🇫🇷
I'll try just to know if the player still wants to play what do I do I do a return; ?
Angius
Angius•2w ago
That should work, yeah Or break, seeing how you're in a loop
Angius
Angius•2w ago
Ah On "yes" just don't do anything Just don't have that else if
DidierThePenguin 🇫🇷
ok so if yes then nothing happens and it will continue
Angius
Angius•2w ago
Yep
DidierThePenguin 🇫🇷
it's working thanks!
Angius
Angius•2w ago
:Ok:

Did you find this page helpful?