Refactor and clean up codebase:
- Introduced `IGameRepository` interface and its implementation. - Transitioned namespaces to `file-scoped` for consistency. - Simplified class definitions, e.g., `Game` and `SixDigitInt`. - Restructured `GameField` logic for improved readability and functionality. - Fixed formatting issues in `API.csproj` and added a `using` directive in `Program.cs`.
This commit is contained in:
+15
-22
@@ -1,26 +1,19 @@
|
||||
using API.Models.DataClasses;
|
||||
|
||||
namespace API.Models.Game
|
||||
namespace API.Models.Game;
|
||||
|
||||
public enum GameState
|
||||
{
|
||||
public enum GameState
|
||||
{
|
||||
Lobby,
|
||||
Running,
|
||||
Ended
|
||||
}
|
||||
|
||||
public class Game
|
||||
{
|
||||
public string Id { get; init; } = Guid.NewGuid().ToString();
|
||||
public SixDigitInt GameCode { get; }
|
||||
public string?[] PlayerConnectionIds { get; set; } = new string?[2];
|
||||
public GameState State { get; private set; } = GameState.Lobby;
|
||||
public GameField Field { get; }
|
||||
|
||||
public Game(Coordinates gFs, SixDigitInt gameCode)
|
||||
{
|
||||
Field = new GameField(gFs);
|
||||
GameCode = gameCode;
|
||||
}
|
||||
}
|
||||
Lobby,
|
||||
Running,
|
||||
Ended
|
||||
}
|
||||
|
||||
public class Game(Coordinates gFs, SixDigitInt gameCode)
|
||||
{
|
||||
public string Id { get; init; } = Guid.NewGuid().ToString();
|
||||
public SixDigitInt GameCode { get; } = gameCode;
|
||||
public string?[] PlayerConnectionIds { get; set; } = new string?[2];
|
||||
public GameState State { get; private set; } = GameState.Lobby;
|
||||
public GameField Field { get; } = new(gFs);
|
||||
}
|
||||
@@ -1,86 +1,79 @@
|
||||
namespace API.Models.Game
|
||||
namespace API.Models.Game;
|
||||
|
||||
public class Coordinates
|
||||
{
|
||||
public class Coordinates
|
||||
public int X;
|
||||
public int Y;
|
||||
}
|
||||
|
||||
public enum PlaceResult
|
||||
{
|
||||
OutOfGameField,
|
||||
NotAllowedPlayer,
|
||||
OccupiedRed,
|
||||
OccupiedYellow,
|
||||
InvalidFieldValue,
|
||||
Placed
|
||||
}
|
||||
|
||||
public enum FieldState
|
||||
{
|
||||
OutOfGameField,
|
||||
Empty,
|
||||
OccupiedRed,
|
||||
OccupiedYellow,
|
||||
InvalidFieldValue
|
||||
}
|
||||
|
||||
public class GameField(Coordinates gFs)
|
||||
{
|
||||
public int[,] CurrentField { get; } = new int[gFs.Y, gFs.X];
|
||||
public int[,] BackupField { get; } = new int[gFs.Y, gFs.X];
|
||||
|
||||
public PlaceResult Place(Coordinates coordinates, int player)
|
||||
{
|
||||
public int X;
|
||||
public int Y;
|
||||
}
|
||||
if (coordinates.X < 0 || coordinates.X >= CurrentField.GetLength(1) ||
|
||||
coordinates.Y < 0 || coordinates.Y >= CurrentField.GetLength(0))
|
||||
return PlaceResult.OutOfGameField;
|
||||
|
||||
public enum PlaceResult
|
||||
{
|
||||
OutOfGameField,
|
||||
NotAllowedPlayer,
|
||||
OccupiedRed,
|
||||
OccupiedYellow,
|
||||
InvalidFieldValue,
|
||||
Placed
|
||||
}
|
||||
if (player != 1 && player != 2)
|
||||
return PlaceResult.NotAllowedPlayer;
|
||||
|
||||
public enum FieldState
|
||||
{
|
||||
OutOfGameField,
|
||||
Empty,
|
||||
OccupiedRed,
|
||||
OccupiedYellow,
|
||||
InvalidFieldValue
|
||||
}
|
||||
|
||||
public class GameField(Coordinates gFs)
|
||||
{
|
||||
public int[,] CurrentField { get; } = new int[gFs.Y, gFs.X];
|
||||
public int[,] BackupField { get; } = new int[gFs.Y, gFs.X];
|
||||
|
||||
public PlaceResult Place(Coordinates coordinates, int player)
|
||||
{
|
||||
if (coordinates.X < 0 || coordinates.X >= CurrentField.GetLength(1) ||
|
||||
coordinates.Y < 0 || coordinates.Y >= CurrentField.GetLength(0))
|
||||
{
|
||||
return PlaceResult.OutOfGameField;
|
||||
}
|
||||
|
||||
if (player != 1 && player != 2)
|
||||
return PlaceResult.NotAllowedPlayer;
|
||||
|
||||
int currentValue = CurrentField[coordinates.Y, coordinates.X];
|
||||
|
||||
if (currentValue != 0)
|
||||
{
|
||||
return currentValue switch
|
||||
{
|
||||
1 => PlaceResult.OccupiedRed,
|
||||
2 => PlaceResult.OccupiedYellow,
|
||||
_ => PlaceResult.InvalidFieldValue
|
||||
};
|
||||
}
|
||||
|
||||
CreateSave();
|
||||
CurrentField[coordinates.Y, coordinates.X] = player;
|
||||
|
||||
return PlaceResult.Placed;
|
||||
}
|
||||
|
||||
public FieldState CheckField(Coordinates coordinates)
|
||||
{
|
||||
if (coordinates.X < 0 || coordinates.X >= CurrentField.GetLength(1) ||
|
||||
coordinates.Y < 0 || coordinates.Y >= CurrentField.GetLength(0))
|
||||
{
|
||||
return FieldState.OutOfGameField;
|
||||
}
|
||||
|
||||
int currentValue = CurrentField[coordinates.Y, coordinates.X];
|
||||
var currentValue = CurrentField[coordinates.Y, coordinates.X];
|
||||
|
||||
if (currentValue != 0)
|
||||
return currentValue switch
|
||||
{
|
||||
0 => FieldState.Empty,
|
||||
1 => FieldState.OccupiedRed,
|
||||
2 => FieldState.OccupiedYellow,
|
||||
_ => FieldState.InvalidFieldValue
|
||||
1 => PlaceResult.OccupiedRed,
|
||||
2 => PlaceResult.OccupiedYellow,
|
||||
_ => PlaceResult.InvalidFieldValue
|
||||
};
|
||||
}
|
||||
|
||||
private void CreateSave()
|
||||
CreateSave();
|
||||
CurrentField[coordinates.Y, coordinates.X] = player;
|
||||
|
||||
return PlaceResult.Placed;
|
||||
}
|
||||
|
||||
public FieldState CheckField(Coordinates coordinates)
|
||||
{
|
||||
if (coordinates.X < 0 || coordinates.X >= CurrentField.GetLength(1) ||
|
||||
coordinates.Y < 0 || coordinates.Y >= CurrentField.GetLength(0))
|
||||
return FieldState.OutOfGameField;
|
||||
|
||||
var currentValue = CurrentField[coordinates.Y, coordinates.X];
|
||||
|
||||
return currentValue switch
|
||||
{
|
||||
Array.Copy(CurrentField, BackupField, CurrentField.Length);
|
||||
}
|
||||
0 => FieldState.Empty,
|
||||
1 => FieldState.OccupiedRed,
|
||||
2 => FieldState.OccupiedYellow,
|
||||
_ => FieldState.InvalidFieldValue
|
||||
};
|
||||
}
|
||||
|
||||
private void CreateSave()
|
||||
{
|
||||
Array.Copy(CurrentField, BackupField, CurrentField.Length);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user