Код: Выделить всё
IPlayerКод: Выделить всё
HumanPlayerКод: Выделить всё
ComputerPlayerКод: Выделить всё
GameManagerСообщение об ошибке:
Код: Выделить всё
There is no argument given that corresponds to the required parameter 'player1' of 'GameManager.GameManager(IPlayer, IPlayer)'
- Интерфейс IPlayer:
Код: Выделить всё
namespace DependencyInjection { public interface IPlayer { GameManager.Choice GetChoice(); } } - Класс HumanPlayer (в отдельном файле):
Код: Выделить всё
namespace DependencyInjection { public class HumanPlayer : IPlayer { public GameManager.Choice GetChoice() { GameManager.Choice p1; do { Console.Write("Enter Choice: (R)ock, (P)aper, (S)cissors: "); string input = Console.ReadLine().ToUpper(); if (input == "R") { p1 = GameManager.Choice.Rock; break; } else if (input == "S") { p1 = GameManager.Choice.Scissors; break; } else if (input == "P") { p1 = GameManager.Choice.Paper; break; } else { Console.WriteLine("Invalid choice, try again!"); } } while (true); Console.WriteLine($"Player 1 picked {p1.ToString()}"); return p1; } } } - Класс ComputerPlayer (в отдельном файле):
Код: Выделить всё
namespace DependencyInjection { public class ComputerPlayer : IPlayer { private Random _rng = new Random(); public GameManager.Choice GetChoice() { int randomChoice = _rng.Next(0, 3); return (GameManager.Choice)randomChoice; } } } - Класс GameManager:
Код: Выделить всё
namespace DependencyInjection { public class GameManager { private IPlayer _player1; private IPlayer _player2; public GameManager(IPlayer player1, IPlayer player2) { _player1 = player1; _player2 = player2; } public RoundResult PlayRound() { Choice p1 = _player1.GetChoice(); Choice p2 = _player2.GetChoice(); if (p1 == p2) { return RoundResult.Draw; } if ((p1 == Choice.Rock && p2 == Choice.Scissors) || (p1 == Choice.Paper && p2 == Choice.Rock) || (p1 == Choice.Scissors && p2 == Choice.Paper)) { return RoundResult.Player1Win; } return RoundResult.Player2Win; } public enum Choice { Rock, Paper, Scissors } public enum RoundResult { Player1Win, Player2Win, Draw } } } - Program.cs:
Код: Выделить всё
using DependencyInjection; using static DependencyInjection.GameManager; GameManager gm = new GameManager(new HumanPlayer(), new ComputerPlayer()); do { RoundResult result = gm.PlayRound(); if (result == RoundResult.Player1Win) { Console.WriteLine("Player 1 Wins"); } else if (result == RoundResult.Player2Win) { Console.WriteLine("Player 2 Wins"); } else { Console.WriteLine("It's a draw!"); } Console.WriteLine("Do you want to play again? (Y/N):"); } while (Console.ReadLine().ToUpper() == "Y");
Код: Выделить всё
IPlayerКод: Выделить всё
HumanPlayerКод: Выделить всё
ComputerPlayerКод: Выделить всё
GameManagerКод: Выделить всё
IPlayerКод: Выделить всё
Program.csКод: Выделить всё
GameManagerКод: Выделить всё
new HumanPlayer()Код: Выделить всё
new ComputerPlayer()Подробнее здесь: https://stackoverflow.com/questions/791 ... player1-of