C
C#2y ago
Ronnie

✅ rock paper scissors game

// beginner project - rock, paper scissors game

using System;
using System.Collections.Generic;

namespace RockPaperScissors
{
class Program
{
static void Main(string[] args )
{
Console.WriteLine("Welcome to my rock paper scissors game!");

string[] choice = { "Rock", "Paper", "Scissors" };


int round = 0;

// counters here help to keep track of the amount of points the player and the computer have
int PointsUser = 0;
int PointsComp = 0;


while (round < 3) // best out of three
{
Random rand = new Random();
int index = rand.Next(choice.Length);

var CompChoice = choice[index];

Console.WriteLine("Choose your element: ");
string UserInput = Console.ReadLine();


if (UserInput == "Rock") && (CompChoice == "Paper")
{
Console.WriteLine("Round 1 goes to the user");
round++;
PointsUser++;
}
}

}
}
}
// beginner project - rock, paper scissors game

using System;
using System.Collections.Generic;

namespace RockPaperScissors
{
class Program
{
static void Main(string[] args )
{
Console.WriteLine("Welcome to my rock paper scissors game!");

string[] choice = { "Rock", "Paper", "Scissors" };


int round = 0;

// counters here help to keep track of the amount of points the player and the computer have
int PointsUser = 0;
int PointsComp = 0;


while (round < 3) // best out of three
{
Random rand = new Random();
int index = rand.Next(choice.Length);

var CompChoice = choice[index];

Console.WriteLine("Choose your element: ");
string UserInput = Console.ReadLine();


if (UserInput == "Rock") && (CompChoice == "Paper")
{
Console.WriteLine("Round 1 goes to the user");
round++;
PointsUser++;
}
}

}
}
}
I'm getting an error with the if statement, says that && is invalid
12 Replies
sibber
sibber2y ago
if (UserInput == "Rock" && CompChoice == "Paper")
if (UserInput == "Rock" && CompChoice == "Paper")
Ronnie
RonnieOP2y ago
oof thx il try this now
sibber
sibber2y ago
the whole expression needs to be wrapper in parens
Ronnie
RonnieOP2y ago
oh ok when writing the if else statements to decide who has won the round, do you know if there is a faster way than writing a bunch of if else statements?
if (UserInput == "Rock") && (CompChoice == "Paper")
{
Console.WriteLine("Round 1 goes to the user");
round++;
PointsUser++;
}
if (UserInput == "Rock") && (CompChoice == "Paper")
{
Console.WriteLine("Round 1 goes to the user");
round++;
PointsUser++;
}
so instead of repeating this over and over again for paper and scissors @Cyberrex
sibber
sibber2y ago
are you familiar with methods?
Ronnie
RonnieOP2y ago
yes oh yeah
sibber
sibber2y ago
you could write a method that returns who won instead of having that in Main
Ronnie
RonnieOP2y ago
ahh ok
sibber
sibber2y ago
what you can also do instead of a million else ifs is for each choice to check all other possible choices and return who won
Ronnie
RonnieOP2y ago
ok wait so would i have all the else ifs in the method or separate else if for each choice?
sibber
sibber2y ago
inside the method that returns the winner
Ronnie
RonnieOP2y ago
il use switch case instead
Want results from more Discord servers?
Add your server