patterncsharpMinor
Rock-Paper-Scissors game with OOP concepts
Viewed 0 times
conceptsscissorspaperwithgamerockoop
Problem
This is code for a Rock-Paper-Scissors game I made using some of the OOP concepts I learned in C#. I don't know if I am making the right choices but I am fairly confident. It would be great if I could receive guidance here.
I chose to have a
```
using System;
enum RockPaperScissors
{
Rock,
Paper,
Scissors
}
abstract class Participant
{
public int wins { get; set; }
float _winRate;
protected RockPaperScissors selection;
protected float winRate
{
get
{
return _winRate;
}
set
{
if (value 100)
{
throw new Exception("value cannot be less than 0 or greater than 100");
}
_winRate = value;
}
}
public void PrintWinRate()
{
this.winRate = ((float)wins / Game.Info.GamesPlayed) * 100;
string winRate = "win rate: " + this.winRate.ToString() + "%";
Console.WriteLine(winRate.PadLeft(50));
}
public abstract RockPaperScissors Select();
}
class Computer : Participant
{
public override RockPaperScissors Select()
{
Random rand = new Random();
selection = (RockPaperScissors)rand.Next(0, Enum.GetValues(typeof(RockPaperScissors)).Length);
return selection;
}
}
class Player : Participant
{
public override RockPaperScissors Select()
{
bool isValid;
string input;
do
{
Console.Write("Please enter a valid selection: ");
input = Console.ReadLine().Trim();
isValid = Enum.TryParse(input, true, out selection);
} while (!isValid);
return selection;
}
}
class Game
{
public struct GameInfo
{
public int Gam
I chose to have a
Player and Computer class inheriting from an abstract Participant class. Everything else is control flow which is inside the Gameclass which has a struct inside it, GameInfo, which will store all information regarding global game data.```
using System;
enum RockPaperScissors
{
Rock,
Paper,
Scissors
}
abstract class Participant
{
public int wins { get; set; }
float _winRate;
protected RockPaperScissors selection;
protected float winRate
{
get
{
return _winRate;
}
set
{
if (value 100)
{
throw new Exception("value cannot be less than 0 or greater than 100");
}
_winRate = value;
}
}
public void PrintWinRate()
{
this.winRate = ((float)wins / Game.Info.GamesPlayed) * 100;
string winRate = "win rate: " + this.winRate.ToString() + "%";
Console.WriteLine(winRate.PadLeft(50));
}
public abstract RockPaperScissors Select();
}
class Computer : Participant
{
public override RockPaperScissors Select()
{
Random rand = new Random();
selection = (RockPaperScissors)rand.Next(0, Enum.GetValues(typeof(RockPaperScissors)).Length);
return selection;
}
}
class Player : Participant
{
public override RockPaperScissors Select()
{
bool isValid;
string input;
do
{
Console.Write("Please enter a valid selection: ");
input = Console.ReadLine().Trim();
isValid = Enum.TryParse(input, true, out selection);
} while (!isValid);
return selection;
}
}
class Game
{
public struct GameInfo
{
public int Gam
Solution
What is the point of this
It stores one value, and nothing else. Even if you intend to add more fields to this
As mentioned in an answer to your previous Rock Paper Scissors question, this
The reason this is better named
Other than those two suggestions, most of the good points were made in @MatMug's excellent answer to your previous Rock Paper Scissors question.
struct?public struct GameInfo
{
public int GamesPlayed;
}It stores one value, and nothing else. Even if you intend to add more fields to this
struct, they should all just be static fields that belong to the Game class, as things like GamesPlayed make sense when they're stored in the Game class. In addition, with the struct, you're always going to have to add a Info. qualification whenever you need to access info that pertains to the game. This will begin to make your code look somewhat cluttered and ugly.As mentioned in an answer to your previous Rock Paper Scissors question, this
enum would be better named Selection:enum RockPaperScissors
{
Rock,
Paper,
Scissors
}The reason this is better named
Selection is because the enum represents exactly that - a selection. You also refer to variables/parameters/fields of the type RockPaperScissors as selections across your code as well.Other than those two suggestions, most of the good points were made in @MatMug's excellent answer to your previous Rock Paper Scissors question.
Code Snippets
public struct GameInfo
{
public int GamesPlayed;
}enum RockPaperScissors
{
Rock,
Paper,
Scissors
}Context
StackExchange Code Review Q#128971, answer score: 3
Revisions (0)
No revisions yet.