I am a beginner programmer trying to go past the simple tutorials like Codecademy. I decided to make a rock paper scissors game to get some practice in making projects. Any feedback is welcome!
using System;
using System.Reflection.Metadata.Ecma335;
using System.Runtime.CompilerServices;
namespace ConsoleApp1
{
class Program
{
static public int game()
{
int winner = 0;
int input;
Random random = new Random();
//show options
Console.WriteLine("1.Rock");
Console.WriteLine("2.Paper");
Console.WriteLine("3.Scissors");
//get user input
try
{
input = Convert.ToInt32(Console.ReadLine());
}
catch
{
input = 4;
}
//pick random input for opponent
int opponent = random.Next(3);
Console.Clear();
//logic and stuff
if(input == 1)
{
if(opponent == 0)
{
Console.WriteLine("Tie! The opponent picked rock.");
winner = 0;
}
else if(opponent == 1)
{
Console.WriteLine("you lost! The opponent picked paper.");
winner = 1;
}
else if(opponent == 2)
{
Console.WriteLine("you won! The opponent picked scissors.");
winner = 2;
}
}
else if(input == 2)
{
if (opponent == 0)
{
Console.WriteLine("you won! The opponent picked scissors.");
winner = 2;
}
else if (opponent == 1)
{
Console.WriteLine("Tie! The opponent picked paper.");
winner = 0;
}
else if (opponent == 2)
{
Console.WriteLine("you lost! The opponent picked scissors.");
winner = 1;
}
}
else if(input == 3)
{
if (opponent == 0)
{
Console.WriteLine("you lost! The opponent picked rock.");
winner = 1;
}
else if(opponent == 1)
{
Console.WriteLine("you won! The opponent picked scissors.");
winner = 2;
}
else if(opponent == 2)
{
Console.WriteLine("Tie! The opponent picked scissors.");
winner = 0;
}
}
else
{
Console.WriteLine("Pick a Number between 1 and 3!");
winner = 0;
}
//return score
return winner;
}
static void Main(string[] args)
{
int playerScore = 0;
int opponentScore = 0;
bool keepPlaying = true;
char i = 'y';
Console.WriteLine("Welcome to Rock paper scissors!");
while (keepPlaying)
{
int gameResult = game();
if (gameResult == 1)
{
opponentScore++;
}
else if (gameResult == 2)
{
playerScore++;
}
else if(gameResult != 0)
{
Console.WriteLine("Something went wrong!");
}
string s = String.Format("Your score:{0} Opponents score:{1}", playerScore, opponentScore);
Console.WriteLine(s);
Console.WriteLine("Do you want to keep playing? y/n");
try
{
i = Convert.ToChar(Console.ReadLine());
}
catch
{
i = 'n';
}
if (i != 'y')
{
keepPlaying = false;
}
}
}
}
}
```