public void Movement_On_GameOver_Should_Not_Be_Allowed() { GameService gameService = GameServiceFactory.Create(); Guid gameId = gameService.StartNewGame("Marta", "Carlos"); Game game = gameService.GetGame(gameId); gameService = GameServiceFactory.Create(game.WhitesPlayer.Id); MakeMoveResult makeMoveResult = gameService.MakeMove(gameId, Position.Create("E2"), Position.Create("E4")); Assert.IsTrue(makeMoveResult.Success); gameService = GameServiceFactory.Create(game.BlacksPlayer.Id); makeMoveResult = gameService.MakeMove(gameId, Position.Create("D7"), Position.Create("D5")); Assert.IsTrue(makeMoveResult.Success); gameService = GameServiceFactory.Create(game.WhitesPlayer.Id); makeMoveResult = gameService.MakeMove(gameId, Position.Create("F1"), Position.Create("B5")); Assert.IsTrue(makeMoveResult.Success); gameService = GameServiceFactory.Create(game.BlacksPlayer.Id); makeMoveResult = gameService.MakeMove(gameId, Position.Create("A7"), Position.Create("A6")); Assert.IsTrue(makeMoveResult.Success); gameService = GameServiceFactory.Create(game.WhitesPlayer.Id); makeMoveResult = gameService.MakeMove(gameId, Position.Create("B5"), Position.Create("E8")); Assert.IsTrue(makeMoveResult.Success); Assert.AreEqual(EventType.GameOver, makeMoveResult.TurnLog.TurnEvents.Last().EventType); Assert.IsTrue(gameService.GetGame(gameId).IsOver); }
public void Try_Movement_Before_Game_Can_Start_Should_Not_Be_Allowed() { GameService gameService = GameServiceFactory.Create(); Guid gameId = gameService.PrepareGame(); MakeMoveResult makeMoveResult = gameService.MakeMove(gameId, Position.Create("E2"), Position.Create("E4")); Assert.IsFalse(makeMoveResult.Success); }
public void MakeMove_From_Incorrect_Turn_Should_Return_Fail() { GameService gameService = GameServiceFactory.Create(); Guid gameId = gameService.StartNewGame("Carlos", "Marta"); Game game = gameService.GetGame(gameId); MakeMoveResult makeMoveResult = gameService.MakeMove(gameId, Position.Create("A7"), Position.Create("A6")); Assert.IsFalse(makeMoveResult.Success); }
public void MakeMove_From_Empty_Square_Should_Return_Fail() { GameService gameService = GameServiceFactory.Create(); Guid gameId = gameService.StartNewGame("Carlos", "Marta"); Game game = gameService.GetGame(gameId); MakeMoveResult makeMoveResult = gameService.MakeMove(gameId, Position.Create("A3"), Position.Create("A4")); Assert.IsFalse(makeMoveResult.Success); Assert.IsFalse(game.Board.GetSquare("A2").IsEmpty); Assert.IsTrue(game.Board.GetSquare("A3").IsEmpty); }
public async Task <ActionResult <TurnLog> > Move(Guid gameId, [FromBody] MoveArguments arguments) { MakeMoveResult makeMoveResult = _gameService.MakeMove(gameId, Position.Create(arguments.Origin), Position.Create(arguments.Destination)); if (!makeMoveResult.Success) { return(BadRequest(makeMoveResult.FailReason)); } if (_hub != null) { await _hub.Clients.All.SendAsync("RefreshGame"); } return(makeMoveResult.TurnLog); }
public void MakeMove_Should_Change_Piece_Position() { GameService gameService = GameServiceFactory.Create(); Guid gameId = gameService.StartNewGame("Carlos", "Marta"); Game game = gameService.GetGame(gameId); gameService = GameServiceFactory.Create(game.WhitesPlayer.Id); Assert.IsFalse(game.Board.GetSquare("A2").IsEmpty); Assert.IsTrue(game.Board.GetSquare("A4").IsEmpty); MakeMoveResult makeMoveResult = gameService.MakeMove(gameId, Position.Create("A2"), Position.Create("A4")); Assert.IsTrue(makeMoveResult.Success); Assert.IsTrue(game.Board.GetSquare("A2").IsEmpty); Assert.IsFalse(game.Board.GetSquare("A4").IsEmpty); }
public MakeMoveResult MakeMove(Guid gameId, Position origin, Position destination) { Game game = GetGame(gameId); if (!game.CanStart) { return(MakeMoveResult.CreateFailedResult("The game needs two players to start.")); } if (game.IsOver) { return(MakeMoveResult.CreateFailedResult("Game is over, no more movements allowed.")); } if (game.CurrentTurnPlayer.Id != _playerSession.PlayerId) { return(MakeMoveResult.CreateFailedResult($"Player trying to move is not {game.CurrentTurnPlayer.Color} player.")); } Square originSquare = game.Board.GetSquare(origin.Id); if (originSquare.IsEmpty) { return(MakeMoveResult.CreateFailedResult($"Origin square {origin.Id} is empty.")); } if (originSquare.Piece.Color != game.CurrentTurnPlayer.Color) { return(MakeMoveResult.CreateFailedResult($"Piece to be moved does not belong to the {game.CurrentTurnPlayer.Color} player.")); } Piece piece = originSquare.Piece; Player currentTurnPlayer = game.CurrentTurnPlayer; bool success = game.Board.HandleMove(PieceMovement.Create(piece, origin, destination)); return(success ? MakeMoveResult.CreateSuccessResult(currentTurnPlayer.GetLastTurn()) : MakeMoveResult.CreateFailedResult($"Movement was denied.")); }