-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRockPaperScissors.cs
99 lines (80 loc) · 3.25 KB
/
RockPaperScissors.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
namespace RockPaperScissors
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("First to three!");
Console.WriteLine("Rock, paper, or scissors?");
Console.ResetColor();
bool won = false;
int playerWins = 0;
int computerWins = 0;
string[] choices = { "rock", "paper", "scissors" };
Random random = new Random();
while (!won)
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write(": ");
Console.ResetColor();
string? players_choice = Console.ReadLine();
players_choice = players_choice?.ToLower();
if (string.IsNullOrEmpty(players_choice) || !Array.Exists(choices, choice => choice == players_choice))
{
Console.WriteLine("Choose rock, paper, or scissors.");
continue;
}
string computers_choice = choices[random.Next(choices.Length)];
if (players_choice == computers_choice)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("It's a tie!");
Console.ResetColor();
}
else if ((players_choice == "rock" && computers_choice == "scissors") ||
(players_choice == "paper" && computers_choice == "rock") ||
(players_choice == "scissors" && computers_choice == "paper"))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("You won!");
Console.ResetColor();
playerWins++;
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You lost!");
Console.ResetColor();
computerWins++;
}
DisplayChoices(players_choice, computers_choice);
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine($"Player: {playerWins}, Computer: {computerWins}.");
Console.ResetColor();
if (playerWins == 3 || computerWins == 3)
{
won = true;
}
}
if (playerWins == 3)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Good job! You won!");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Good try! You lost!");
Console.ResetColor();
}
}
static void DisplayChoices(string p_choice, string c_choice)
{
Console.WriteLine($"Players choice: {p_choice}.");
Console.WriteLine($"Computers choice: {c_choice}.");
}
}
}