Skip to content

Instantly share code, notes, and snippets.

@bolenton
Last active April 27, 2017 05:44
Show Gist options
  • Save bolenton/fe145b20076fde0c00eb3bd2e10a4a37 to your computer and use it in GitHub Desktop.
Save bolenton/fe145b20076fde0c00eb3bd2e10a4a37 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RockPaperScissors
{
class Program
{
static void Main(string[] args)
{
Game game = new Game();
game.Run();
}
}
}
using System;
using System.Collections.Generic;
/// <summary>
/// // TO DO
///
/// 1. Add validation - game should force user to only enter corect value, and shouldnt end round until they do
/// 2. The computer hand is not randamized
/// 3. The games needs to handle tie
///
/// </summary>
namespace RockPaperScissors
{
/// ======== >>>A good habit is to relegate each class to their own class, Pro c# devs perfare this ===== >>>>>>>
// ROCK PAPER SCISSORS GAME \\
class Game
{
/// ======== >>>If we are not using this why have it >>>>>>
public List<string> Rpc { get; set; }
/// ======== >>>If we are not using this why have it >>>>>>
public string PlayerHand { get; set; }
/// ======== >>>If we are not using this why have it >>>>>>
public string ComputerHand { get; set; }
public int PlayerScore { get; set; }
public int ComputerScore { get; set; }
// GET RANDOM HAND THAT COMPUTER PLAYS \\
public string GetRandomHand()
{
var choices = new List<string> { "r", "p", "s" };
Random random = new Random();
var pickFrom = random.Next(choices.Count);
string randomHand = choices[pickFrom];
return randomHand;
}
/// ======== >>>I absolutly love that you abstracted this out to a method of the game class. Great move >>>>>>
// COMPARE HANDS \\
public void Compare(string playerHand, string computerHand)
{
switch (playerHand)
{
case "r":
switch (computerHand)
{
case "r":
PlayerScore++;
break;
case "p":
PlayerScore++;
break;
case "s":
ComputerScore++;
break;
}
break;
case "p":
switch (computerHand)
{
case "r":
PlayerScore++;
break;
case "p":
PlayerScore++;
break;
case "s":
ComputerScore++;
break;
}
break;
case "s":
switch (computerHand)
{
case "r":
PlayerScore++;
break;
case "p":
PlayerScore++;
break;
case "s":
ComputerScore++;
break;
}
break;
}
}
public void DisplayScore()
{
Console.WriteLine("========================");
Console.WriteLine($"| Player: {PlayerScore} |");
Console.WriteLine($"| Computer: {ComputerScore} |");
Console.WriteLine("========================");
}
public void Run()
{
var playOn = "";
/// ======== >>>up here you computer hand is always the same >>>>>>
//var computerHand = GetRandomHand();
do
{
/// ======== HERE is a neat trick to keep display on top :) >>>
Console.Clear();
/// ======== we always dis play at the top, to ensure that score is always on top >>>
DisplayScore();
/// ======== >>> Since this is in the class now we can take advantage of the property you used up top >>>>>>
/// ======== >>> placing it in here makes it truly random >>>>>>
ComputerHand = GetRandomHand();
// START GAME \\
Console.WriteLine("Please choose a hand to play by typing in r, p or s");
// TYPE IN YOUR HAND \\
PlayerHand = Console.ReadLine();
// PLAYER HAND AND COMPUTER HAND BEING COMPARED \\
Console.WriteLine("You input " + PlayerHand);
Console.WriteLine("Computer got " + ComputerHand);
// COMPARE HANDS \\
Compare(PlayerHand, ComputerHand);
// WHO GOT THE POINT \\
Console.WriteLine("You got " + PlayerScore);
Console.WriteLine("computer got " + ComputerScore);
Console.WriteLine("If you wish to continue playing please type in 'Y', if not please press enter");
/// ======== >>> I would add a ToUpper() here, so the user doesn't mess up because of 'y' ====== >>>>>>>
playOn = Console.ReadLine().ToUpper();
/// ======== >>> I would get rid of this and just check the reply directly
//if (reply != "Y")
//{
// playON = false;
//}
/// ======== >>> havent to press enter an extra time is annoying
//Console.ReadLine();
/// ======== >>> see how this makes more sense, as long as the user doen't enter n, the games continues, this way they can enter anything else it wont break
} while (playOn != "n"); ///////// I pefer a do while here just cuz it makes more sence to think about.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment