public void PlayGameRequest_Correct_ObjectCreated()
        {
            // Arrange
            var playGameRequest = new PlayGameRequest
            {
                PlayerName = this.playerName,
                NextMove   = this.nextMove
            };

            // Act
            // Assert
            Assert.Equal(this.playerName, playGameRequest.PlayerName);
            Assert.Equal(this.nextMove, playGameRequest.NextMove);
        }
Exemplo n.º 2
0
        public IActionResult Post(string gameName, string playerName, Move nextMove)
        {
            var playGameRequest = new PlayGameRequest
            {
                GameName   = gameName,
                PlayerName = playerName,
                NextMove   = nextMove
            };
            var playGameResponse = this.gameService.PlayGame(playGameRequest);

            if (playGameResponse?.IsSuccessful == false)
            {
                return(this.BadRequest(playGameResponse));
            }

            return(this.Created("api/games/guid/playerId/nextMove", playGameResponse));
        }
Exemplo n.º 3
0
        public PlayGameResponse PlayGame(PlayGameRequest request)
        {
            // Validate request, you will never know again and again
            if (string.IsNullOrEmpty(request?.GameName) || string.IsNullOrEmpty(request?.PlayerName) ||
                request?.NextMove == Move.Empty)
            {
                return(new PlayGameResponse
                {
                    IsSuccessful = false,
                    Error = new ResponseError
                    {
                        ErrorCode = HttpStatusCode.BadRequest,
                        Description = HttpStatusCode.BadRequest.ToString()
                    }
                });
            }

            // Validate Game exists
            var game = this.games.FirstOrDefault(item => item.Name == request.GameName);

            if (game == null)
            {
                return(new PlayGameResponse
                {
                    IsSuccessful = false,
                    Error = new ResponseError
                    {
                        ErrorCode = HttpStatusCode.BadRequest,
                        Description = HttpStatusCode.BadRequest.ToString()
                    }
                });
            }

            // Validate Game isn't finished
            if (game.IsFinished)
            {
                return(new PlayGameResponse
                {
                    IsSuccessful = false,
                    Error = new ResponseError
                    {
                        ErrorCode = HttpStatusCode.BadRequest,
                        Description = HttpStatusCode.BadRequest.ToString()
                    }
                });
            }

            // Validate that player is allowed to play in this game
            if (game.FirstPlayer.Name != request.PlayerName && game.SecondPlayer.Name != request.PlayerName)
            {
                return(new PlayGameResponse
                {
                    IsSuccessful = false,
                    Error = new ResponseError
                    {
                        ErrorCode = HttpStatusCode.BadRequest,
                        Description = HttpStatusCode.BadRequest.ToString()
                    }
                });
            }

            // Time to play
            // If player one move is pending
            if (game.FirstPlayer.Name == request.PlayerName)
            {
                switch (game.Status)
                {
                case GameStatus.PlayerOneMovePending:
                    game.FirstPlayer.Move = request.NextMove;
                    game.IsFinished       = true;
                    break;

                case GameStatus.Created:
                    game.FirstPlayer.Move = request.NextMove;
                    game.Status           = GameStatus.PlayerTwoMovePending;
                    break;

                default:
                    return(new PlayGameResponse
                    {
                        IsSuccessful = false,
                        Error = new ResponseError
                        {
                            ErrorCode = HttpStatusCode.BadRequest,
                            Description = HttpStatusCode.BadRequest.ToString()
                        }
                    });
                }
            }

            // If player two move is pending
            else if (game.SecondPlayer.Name == request.PlayerName)
            {
                switch (game.Status)
                {
                case GameStatus.PlayerTwoMovePending:
                    game.SecondPlayer.Move = request.NextMove;
                    game.IsFinished        = true;
                    break;

                case GameStatus.Created:
                    game.SecondPlayer.Move = request.NextMove;
                    game.Status            = GameStatus.PlayerOneMovePending;
                    break;

                default:
                    return(new PlayGameResponse
                    {
                        IsSuccessful = false,
                        Error = new ResponseError
                        {
                            ErrorCode = HttpStatusCode.BadRequest,
                            Description = HttpStatusCode.BadRequest.ToString()
                        }
                    });
                }
            }

            return(new PlayGameResponse
            {
                IsSuccessful = true
            });
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Rock, Paper, Scissors Game!");
            Console.WriteLine("You are playing this game with Computer.");
            Console.WriteLine("PLayer , what is your name? : ");
            var player1Name = Console.ReadLine();
            var player2Name = "Computer";

            GameService gameService = new GameService();
            var         gameProfile = gameService.CreateGameProfile(); //Create Game Profile

            // Registration of 1nd player in Team
            JoinTeam player1 = new JoinTeam
            {
                Player = new Model.Player
                {
                    Id   = new Guid(),
                    Name = player1Name
                }
            };

            gameService.JoinTeam(player1);

            // Registration of 2nd player in Team
            JoinTeam player2 = new JoinTeam
            {
                Player = new Model.Player
                {
                    Id   = new Guid(),
                    Name = player2Name
                }
            };

            gameService.JoinTeam(player2);


            Console.WriteLine("Welcome {0} and {1}", player1Name, player2Name);
            Console.WriteLine("{0}, Please pick Rock, Paper, or Scissors. Press 1 for Rock, 2 for Paper, 3 for Scissors", player1Name);
            int input = Convert.ToInt32(Console.ReadLine());

            PlayGameRequest playGameRequest = new PlayGameRequest
            {
                PlayerName = player1Name,
                NextMove   = (Enums.MoveType)input
            };
            var gameResponse = gameService.StartMove(playGameRequest);

            Random          random            = new Random();
            int             secondInput       = random.Next(1, 3);
            PlayGameRequest secondGameRequest = new PlayGameRequest
            {
                PlayerName = player2Name,
                NextMove   = (Enums.MoveType)secondInput
            };
            var secondGameResponse = gameService.StartMove(secondGameRequest);

            //Moves done and calculate Result
            if (gameResponse.IsSuccessful && secondGameResponse.IsSuccessful)
            {
                GameStatusRequest gameStatusRequest = new GameStatusRequest
                {
                    GameId = gameProfile.GameId
                };
                var result = gameService.CheckGameStatus(gameStatusRequest);
                if (result.IsSuccessful)
                {
                    Console.WriteLine("==================================");
                    if (result.Status == Enums.GameStatus.PlayerOneWon)
                    {
                        Console.WriteLine("Player : " + player1Name + " is the winner");
                    }
                    else if (result.Status == Enums.GameStatus.PlayerTwoWon)
                    {
                        Console.WriteLine("Player : " + player2Name + " is the winner");
                    }
                    else
                    {
                        Console.WriteLine("Player : " + result.Status);
                    }
                }
            }
            Console.WriteLine("Press any key to exit.");
        }