Skip to content

Commit

Permalink
Some more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kalina559 committed Jul 19, 2024
1 parent 25aa20e commit 4404372
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 32 deletions.
15 changes: 9 additions & 6 deletions Battleships.Common/GameClasses/GameState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ namespace Battleships.Common.GameClasses
{
public class GameState
{
public List<Ship> UserShips { get; set; } = [];
public List<Ship> OpponentShips { get; set; } = [];
public List<Shot> PlayerShots { get; set; } = [];
public List<Shot> OpponentShots { get; set; } = [];
public AiType? PlayerAiType { get; set; } = null;
public AiType OpponentAiType { get; set; }
public PlayerInfo Human { get; set; } = new();
public PlayerInfo Bot { get; set; } = new();
public bool ShipsCanTouch { get; set; }
}

public class PlayerInfo
{
public List<Ship> Ships { get; set; } = [];
public List<Shot> Shots { get; set; } = [];
public AiType? AiType { get; set; } = null;
}
}
2 changes: 1 addition & 1 deletion Battleships.Core/Services/AiTypeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public List<AiType> GetAllTypes()
public void SelectAiType(AiType type)
{
var gameState = gameStateService.GetGameState();
gameState.OpponentAiType = type;
gameState.Human.AiType = type;
gameStateService.SaveGameState(gameState);

logger.LogInformation("AiType updated to {type}.", type);
Expand Down
26 changes: 10 additions & 16 deletions Battleships.Core/Services/GameStateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public void SaveGameState(GameState gameState)
public ShotResult ProcessShot(int x, int y, bool isPlayer)
{
var gameState = GetGameState();
var targetShips = isPlayer ? gameState.OpponentShips : gameState.UserShips;
var shotList = isPlayer ? gameState.PlayerShots : gameState.OpponentShots;
var targetShips = isPlayer ? gameState.Human.Ships : gameState.Bot.Ships;
var shotList = isPlayer ? gameState.Bot.Shots : gameState.Human.Shots;

var hit = targetShips.Any(ship => ship.Coordinates.Any(coord => coord.X == x && coord.Y == y));
shotList.Add(new Shot { X = x, Y = y, IsHit = hit });
Expand All @@ -73,8 +73,8 @@ public bool CheckWinCondition()
{
var gameState = GetGameState();

var allPlayerShipsSunk = gameState.UserShips.All(ship => ship.IsSunk);
var allOpponentShipsSunk = gameState.OpponentShips.All(ship => ship.IsSunk);
var allPlayerShipsSunk = gameState.Bot.Ships.All(ship => ship.IsSunk);
var allOpponentShipsSunk = gameState.Human.Ships.All(ship => ship.IsSunk);

if (allPlayerShipsSunk || allOpponentShipsSunk)
{
Expand All @@ -95,12 +95,12 @@ private void SaveGameSessionToDb(GameState gameState, bool playerWon)
GameStateJson = JsonConvert.SerializeObject(gameState),
SessionId = httpContextAccessor.HttpContext.Request.Headers["X-Session-Id"].ToString(),
DateCreated = DateTime.UtcNow,
AiType = (int)gameState.OpponentAiType,
AiType = (int)gameState.Human.AiType,

Check warning on line 98 in Battleships.Core/Services/GameStateService.cs

View workflow job for this annotation

GitHub Actions / build

Nullable value type may be null.
ShipsCanTouch = gameState.ShipsCanTouch,
OpponentShipsSunk = gameState.OpponentShips.Where(x => x.IsSunk).Count(),
PlayersShipsSunk = gameState.UserShips.Where(x => x.IsSunk).Count(),
OpponentMovesCount = gameState.OpponentShots.Count(),
PlayerMovesCount = gameState.PlayerShots.Count(),
OpponentShipsSunk = gameState.Human.Ships.Where(x => x.IsSunk).Count(),
PlayersShipsSunk = gameState.Bot.Ships.Where(x => x.IsSunk).Count(),
OpponentMovesCount = gameState.Human.Shots.Count(),
PlayerMovesCount = gameState.Bot.Shots.Count(),
PlayerWon = playerWon
};

Expand All @@ -110,13 +110,7 @@ private void SaveGameSessionToDb(GameState gameState, bool playerWon)

public void ClearGameState()
{
var gameState = new GameState
{
UserShips = [],
OpponentShips = [],
PlayerShots = [],
OpponentShots = []
};
var gameState = new GameState();
SaveGameState(gameState);
}

Expand Down
6 changes: 3 additions & 3 deletions Battleships.WebApi/Controllers/ShipLocationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ShipLocationsController(IShipLocationService shipLocationService, I
public IActionResult SetUserShips([FromBody] List<Ship> ships)
{
var gameState = gameStateService.GetGameState();
gameState.UserShips = ships;
gameState.Bot.Ships = ships;
gameStateService.SaveGameState(gameState);
return Ok();
}
Expand All @@ -27,10 +27,10 @@ public ActionResult<List<Ship>> GetOpponentShips()

var gameState = gameStateService.GetGameState();

gameState.OpponentShips = shipLocationService.GenerateOpponentShips();
gameState.Human.Ships = shipLocationService.GenerateOpponentShips();
gameStateService.SaveGameState(gameState);

return Ok(gameState.OpponentShips);
return Ok(gameState.Human.Ships);
}
}
}
2 changes: 1 addition & 1 deletion Battleships.WebApi/Controllers/ShotController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public IActionResult UserShot([FromBody] Shot shot)
public IActionResult OpponentShot()
{
var gameState = gameStateService.GetGameState();
var (X, Y) = opponentMoveService.GenerateMove(gameState.OpponentShots, gameState.UserShips, gameState.ShipsCanTouch, gameState.OpponentAiType);
var (X, Y) = opponentMoveService.GenerateMove(gameState.Human.Shots, gameState.Bot.Ships, gameState.ShipsCanTouch, gameState.Human.AiType.Value);

Check warning on line 23 in Battleships.WebApi/Controllers/ShotController.cs

View workflow job for this annotation

GitHub Actions / build

Nullable value type may be null.

var result = gameStateService.ProcessShot(X, Y, isPlayer: false);
var win = gameStateService.CheckWinCondition();
Expand Down
16 changes: 11 additions & 5 deletions Tests/OpponentMoveServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@ public void HeuristicStrategy_IgnoreCellsAdjacentToSunkShips()
{
var gameState = new GameState
{
OpponentAiType = AiType.LocationAndHitHeuristic,
OpponentShots = [new() { X = 5, Y = 4, IsHit = true }, new Shot { X = 5, Y = 5, IsHit = true }, new Shot { X = 5, Y = 6, IsHit = true }],
ShipsCanTouch = false,
UserShips =
Human = new()
{
AiType = AiType.LocationAndHitHeuristic,
Shots = [new() { X = 5, Y = 4, IsHit = true }, new Shot { X = 5, Y = 5, IsHit = true }, new Shot { X = 5, Y = 6, IsHit = true }],
},
Bot = new()
{
Ships =
[
new() { Size = 1, Coordinates = [new() { X = 0, Y = 0, IsHit = false }], IsSunk = false },
new() { Size = 2, Coordinates = [new() { X = 2, Y = 0, IsHit = false }, new() { X = 2, Y = 1, IsHit = false }], IsSunk = false },
new() { Size = 3, Coordinates = [new() { X = 5, Y = 4, IsHit = true }, new() { X = 5, Y = 5, IsHit = true }, new() { X = 5, Y = 6, IsHit = true }], IsSunk = true },
new() { Size = 4, Coordinates = [new() { X = 7, Y = 0, IsHit = false }, new() { X = 7, Y = 1, IsHit = false }, new() { X = 7, Y = 2, IsHit = false }, new() { X = 7, Y = 3, IsHit = false }], IsSunk = false },
new() { Size = 5, Coordinates = [new() { X = 9, Y = 0, IsHit = false }, new() { X = 9, Y = 1, IsHit = false }, new() { X = 9, Y = 2, IsHit = false }, new() { X = 9, Y = 3, IsHit = false }, new() { X = 9, Y = 4, IsHit = false }], IsSunk = false },
]
},
ShipsCanTouch = false,
};

IEnumerable<(int, int)> forbiddenCells = GridHelper.GetAllAdjacentCells(5, 4).Concat(GridHelper.GetAllAdjacentCells(5, 5)).Concat(GridHelper.GetAllAdjacentCells(5, 6));
Expand All @@ -39,7 +45,7 @@ public void HeuristicStrategy_IgnoreCellsAdjacentToSunkShips()

for (int i = 0; i < 100; i++)
{
var move = _opponentMoveService.GenerateMove(gameState.OpponentShots, gameState.UserShips, gameState.ShipsCanTouch, gameState.OpponentAiType);
var move = _opponentMoveService.GenerateMove(gameState.Human.Shots, gameState.Bot.Ships, gameState.ShipsCanTouch, (AiType)gameState.Human.AiType);

if (forbiddenCells.Contains(move))
{
Expand Down

0 comments on commit 4404372

Please sign in to comment.