Zeksy
Zeksy
CC#
Created by Zeksy on 6/7/2024 in #help
My First Ever C-Sharp Mini Project!
Hey there! I've invested 6 hours in learning C# so far and cleared the basics overall/got a rough idea of the syntax. I'm a python programmer working with that language for over a year. After finishing the basics, I just made my first C# minigame, RPS nothing too crazy. The goal was to achieve a program with the properties: -> The program takes input from the user -> Until the input is rock, paper or scissor, the program asks the user to re-try because invalid input -> There are aliases for rock, paper and scissor such as r, p and s. So if a user types r the program should automatically understand that they are picking rock and continue from there. -> Making computer pick a random choice from RPS -> Declaring the win/loss with a custom message. I succeeded in achieving it without external help yippies and here's what the code looks like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
internal class RPS
{
static void Main(string[] args)
{

List<string> PossibleChoices = new List<string> {"rock", "paper", "scissor"};

// A dictionary that stores alternative names for RPS entires
Dictionary<string, List<string> > ChoicesDictionary = new Dictionary<string, List<string>>
{
{ "rock" , new List<string> {"rock", "rocks", "r"} },
{ "paper" , new List<string> {"papers", "p", "paper"} },
{ "scissor" , new List<string> {"s", "scissors", "scissor"} },
};


// A dictionary that tells who wins against who
Dictionary <string, string> WinsAgainst = new Dictionary<string, string>
{
{ "rock" , "scissor"},
{ "paper" , "rock"},
{ "scissor" , "paper"},
};

string MyChoice = "";
do
{
Console.WriteLine("Welcome to the game of Rock Paper and Scissor!\nTo play, make a choice!\n");
MyChoice = Console.ReadLine() ?? "";
if (! PossibleChoices.Contains(MyChoice.ToLower()))
{
foreach (string key in ChoicesDictionary.Keys)
{
if (ChoicesDictionary[key].Contains(MyChoice))
{
MyChoice = key;
break;
}
}
if (! PossibleChoices.Contains(MyChoice.ToLower()))
{
Console.WriteLine($"{MyChoice} is not a valid choice try again\n");
}

};

} while (! PossibleChoices.Contains(MyChoice.ToLower()));

Random random = new Random();
string computerChoice = PossibleChoices[random.Next(PossibleChoices.Count)];


string status = "";
if (computerChoice == MyChoice)
{
status = "The game resulted in a draw";
}
else if (WinsAgainst[MyChoice] == computerChoice) {
status = "Congrats! You won the game!";
}
else status = "You lost, sadly. Better luck next time!";
Console.WriteLine($"You chose {MyChoice} & Computer chose {computerChoice} " + status);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
internal class RPS
{
static void Main(string[] args)
{

List<string> PossibleChoices = new List<string> {"rock", "paper", "scissor"};

// A dictionary that stores alternative names for RPS entires
Dictionary<string, List<string> > ChoicesDictionary = new Dictionary<string, List<string>>
{
{ "rock" , new List<string> {"rock", "rocks", "r"} },
{ "paper" , new List<string> {"papers", "p", "paper"} },
{ "scissor" , new List<string> {"s", "scissors", "scissor"} },
};


// A dictionary that tells who wins against who
Dictionary <string, string> WinsAgainst = new Dictionary<string, string>
{
{ "rock" , "scissor"},
{ "paper" , "rock"},
{ "scissor" , "paper"},
};

string MyChoice = "";
do
{
Console.WriteLine("Welcome to the game of Rock Paper and Scissor!\nTo play, make a choice!\n");
MyChoice = Console.ReadLine() ?? "";
if (! PossibleChoices.Contains(MyChoice.ToLower()))
{
foreach (string key in ChoicesDictionary.Keys)
{
if (ChoicesDictionary[key].Contains(MyChoice))
{
MyChoice = key;
break;
}
}
if (! PossibleChoices.Contains(MyChoice.ToLower()))
{
Console.WriteLine($"{MyChoice} is not a valid choice try again\n");
}

};

} while (! PossibleChoices.Contains(MyChoice.ToLower()));

Random random = new Random();
string computerChoice = PossibleChoices[random.Next(PossibleChoices.Count)];


string status = "";
if (computerChoice == MyChoice)
{
status = "The game resulted in a draw";
}
else if (WinsAgainst[MyChoice] == computerChoice) {
status = "Congrats! You won the game!";
}
else status = "You lost, sadly. Better luck next time!";
Console.WriteLine($"You chose {MyChoice} & Computer chose {computerChoice} " + status);
}
}
}
It's working as intended but no doubt that I could make improvements in the code. Anyone whos more experienced with the lang please tell me how would you approach it/what changes would you make to achieve the same thing in much easier/better way or simply put, how can I improve the program
10 replies
CC#
Created by Zeksy on 6/6/2024 in #help
Where do I code?!
Hello a little query! I just started learning C#, it's my second language let's say. Other than that I've strong knowledge of python which I always wrote in vsc For C#, I downloaded VS for the first time yesterday however I couldn't make myself fit into it lol it all seemed a lot more complicated and uneasy compared to VSC so I downloaded some extensions into vsc to make c# work there. Frankly I've only worked with vsc so far for all my projects Did some research and found out the core differences between VS and VSC, however would still like someone to tell me if I'm all good sticking to vsc for now/for long term or if I should root out and start writing in vs :muidead:
38 replies