Tough assignment today. I've been working in circles for about five hours. I just can't wrap my head around this and see through the fog on this one. Here is the assignment our teacher assigned tonight:
Create a new Visual Studio Console Application named RockPaperScissors.
The game Rock/Paper/Scissors has the following rules:
- Scissor always wins against Paper
- Rock always wins against Scissors
- Paper always wins against Rock
Create the following three classes that follow these rules:
PlayerRock – Always performs the Act() of Rock.
PlayerScissors – Always performs the Act() of Scissors.
PlayerPaper – Always performs the Act() of Paper.
Create a Game class that has a method named Fight() that satisifies these requirements:
The Fight() method accepts two parameters: Player1 and Player2.
The Fight() method calls both player's Act() methods.
The Fight() method returns the winning player using the Rock/Paper/Scissors rules above.
After 100 rounds, which player wins?
I can easily do the game logic and script this game out, no problem. How to make it fire the Act methods for each weapon? And pass a player1 and player2 as well? Do I need classes to create new player objects? I've got classes for Rock, Paper, Scissors, but they basically only have a method inside them returning rock, paper and scissors. I'm not asking for anyone to make this game for me, but can anybody get me pointed in the right direction? Thanks all!
It sounds like you need an abstract
base class called Player
with an abstract Act()
method that returns an GameAction
(could be simply an enum
, unless you want to encode the game logic in the GameAction
class, which is fine).
You could derive the three classes PlayerRock
etc., from Player
, and override the Act()
method in each derived class.
Call Game.Fight(Player player1, Player player2)
, passing in two instances of your derived player classes. Inside Fight(...)
you should call the Act()
method on both players, and decide who wins (if anyone) based on the result.
The same player will win each time though, don't need 100 rounds to show that. Perhaps you are supposed to generate some players at random, or have some players with more subtle strategies later on?
Example of the abstract and concrete classes:
namespace RockPaperScissors
{
enum GameAction
{
Rock,
Paper,
Scissors
}
abstract class Player
{
public abstract GameAction Act();
}
class PlayerRock : Player
{
public override GameAction Act()
{
return GameAction.Rock;
}
}