fixed all code smells normally now
continuous-integration/drone/push Build is passing Details

master
Jules LASCRET 11 months ago
parent c45e859e83
commit 3455682073

@ -164,7 +164,7 @@ namespace QwirkleClassLibrary.Games
/// Returns the Board of the game /// Returns the Board of the game
/// </summary> /// </summary>
/// <returns>Board</returns> /// <returns>Board</returns>
public Board? GetBoard() public Board GetBoard()
{ {
return board; return board;
} }
@ -362,8 +362,8 @@ namespace QwirkleClassLibrary.Games
return false; return false;
} }
if (!IsMoveCorrect(tile, x, y, board!)) return false; if (!IsMoveCorrect(tile, x, y, board)) return false;
if (board!.AddTileInCell(x, y, tile)) if (board.AddTileInCell(x, y, tile))
{ {
AddCellUsed(board.GetCell(x, y)); AddCellUsed(board.GetCell(x, y));
return player.RemoveTileToPlayer(tile); return player.RemoveTileToPlayer(tile);
@ -479,7 +479,7 @@ namespace QwirkleClassLibrary.Games
previousTilesFound = true; previousTilesFound = true;
} }
if (extendedCell?.Tile == null) if (extendedCell?.Tile! == null!)
{ {
break; break;
} }
@ -552,9 +552,16 @@ namespace QwirkleClassLibrary.Games
return false; return false;
} }
public static bool CheckTileInCompletedLines(Tile? t1, ref int nbTiles, ref List<Tile> checkdoubles) /// <summary>
/// Check that there isn't any same tile on a said line when a tile is forming a line
/// </summary>
/// <param name="t1"></param>
/// <param name="nbTiles"></param>
/// <param name="checkdoubles"></param>
/// <returns></returns>
private static bool CheckTileInCompletedLines(Tile? t1, ref int nbTiles, ref List<Tile> checkdoubles)
{ {
if (t1 != null) if (t1! != null!)
{ {
nbTiles++; nbTiles++;
@ -569,7 +576,18 @@ namespace QwirkleClassLibrary.Games
return true; return true;
} }
public bool CheckWrongCompletedLines(Tile tile, int x, int y, int dx, int dy, Board b, ref List<Tile> checkdoubles) /// <summary>
/// Check if the line is completed with the tile placed
/// </summary>
/// <param name="tile"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="dx"></param>
/// <param name="dy"></param>
/// <param name="b"></param>
/// <param name="checkdoubles"></param>
/// <returns></returns>
private static bool CheckWrongCompletedLines(int x, int y, int dx, int dy, Board b, ref List<Tile> checkdoubles)
{ {
int nbTiles = 1; int nbTiles = 1;
@ -578,7 +596,7 @@ namespace QwirkleClassLibrary.Games
var extendedCell = b.GetCell(x + i * dx, y + i * dy); var extendedCell = b.GetCell(x + i * dx, y + i * dy);
var extendedCell2 = b.GetCell(x - i * dx, y - i * dy); var extendedCell2 = b.GetCell(x - i * dx, y - i * dy);
if (extendedCell?.Tile == null && extendedCell2?.Tile == null) if (extendedCell?.Tile! == null! && extendedCell2?.Tile! == null!)
{ {
break; break;
} }
@ -591,30 +609,17 @@ namespace QwirkleClassLibrary.Games
return nbTiles <= 6; return nbTiles <= 6;
} }
/// <summary>
/// Main method to check if the move the player is trying to make is correct
/// </summary>
/// <param name="t"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="b"></param>
/// <returns>bool</returns>
public bool IsMoveCorrect(Tile t, int x, int y, Board b) public bool IsMoveCorrect(Tile t, int x, int y, Board b)
{ {
bool previousTilesFound = false;
var checkDoubles = new List<Tile>();
if (!b.HasOccupiedCase()) if (!b.HasOccupiedCase())
{ {
return true; return true;
} }
if (b.GetCell(x, y)!.Tile != null) if (b.GetCell(x, y)!.Tile! != null!)
{ {
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : Cell already used !")); OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : Cell already used !"));
return false;
} }
var surroundingCells = new List<Cell?> var surroundingCells = new List<Cell?>
@ -625,9 +630,25 @@ namespace QwirkleClassLibrary.Games
b.GetCell(x, y - 1) b.GetCell(x, y - 1)
}; };
if (surroundingCells.All(cell => cell?.Tile! == null!))
{
OnPlaceTile(new PlaceTileNotifiedEventArgs(t,
" : You can't place a tile that isn't adjacent to another one !"));
return false;
}
return IsTilePlacementCorrect(t, x, y, b, surroundingCells);
}
public bool IsTilePlacementCorrect(Tile t, int x, int y, Board b, List<Cell?> surroundingCells)
{
bool previousTilesFound = false;
var checkDoubles = new List<Tile>();
foreach (var cell in surroundingCells) foreach (var cell in surroundingCells)
{ {
if (cell?.Tile == null) if (cell?.Tile! == null!)
{ {
continue; continue;
} }
@ -642,7 +663,6 @@ namespace QwirkleClassLibrary.Games
if (cell.Tile.GetColor == t.GetColor && cell.Tile.GetShape == t.GetShape) if (cell.Tile.GetColor == t.GetColor && cell.Tile.GetShape == t.GetShape)
{ {
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " is already placed on the same line / column !")); OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " is already placed on the same line / column !"));
return false; return false;
} }
@ -654,12 +674,12 @@ namespace QwirkleClassLibrary.Games
return false; return false;
} }
if (CheckWrongCompletedLines(t, x, y, dx, dy, b, ref checkDoubles)) continue; if (CheckWrongCompletedLines(x, y, dx, dy, b, ref checkDoubles)) continue;
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : You can't complete this line ! (More than 6 tiles / same tiles on the line)")); OnPlaceTile(new PlaceTileNotifiedEventArgs(t,
" : You can't complete this line ! (More than 6 tiles / same tiles on the line)"));
return false; return false;
} }
if (!CheckTilesInLine(cellUsed, b, x, y)) if (!CheckTilesInLine(cellUsed, b, x, y))
{ {
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, OnPlaceTile(new PlaceTileNotifiedEventArgs(t,
@ -667,18 +687,10 @@ namespace QwirkleClassLibrary.Games
return false; return false;
} }
if (surroundingCells.All(cell => cell?.Tile == null))
{
OnPlaceTile(new PlaceTileNotifiedEventArgs(t,
" : You can't place a tile that isn't adjacent to another one !"));
return false;
}
if (previousTilesFound) return true; if (previousTilesFound) return true;
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, OnPlaceTile(new PlaceTileNotifiedEventArgs(t,
" : You must place your tile next / on the same line as the ones previously placed !")); " : You must place your tile next / on the same line as the ones previously placed !"));
return false; return false;
} }
@ -771,7 +783,7 @@ namespace QwirkleClassLibrary.Games
foreach (var adjacentCell in surroundingCells) foreach (var adjacentCell in surroundingCells)
{ {
if (adjacentCell?.Tile == null || cellsPlayed.Contains(adjacentCell) || if (adjacentCell?.Tile! == null! || cellsPlayed.Contains(adjacentCell) ||
checkedSurroundingCells.Contains(adjacentCell)) checkedSurroundingCells.Contains(adjacentCell))
{ {
continue; continue;
@ -808,7 +820,7 @@ namespace QwirkleClassLibrary.Games
{ {
var extendedCell = b.GetCell(cell.GetX + i * direction.Item1, cell.GetY + i * direction.Item2); var extendedCell = b.GetCell(cell.GetX + i * direction.Item1, cell.GetY + i * direction.Item2);
if (extendedCell?.Tile == null || cellsPlayed.Contains(extendedCell)) if (extendedCell?.Tile! == null! || cellsPlayed.Contains(extendedCell))
{ {
break; break;
} }
@ -864,7 +876,7 @@ namespace QwirkleClassLibrary.Games
{ {
foreach (var t in players[t1].Tiles) foreach (var t in players[t1].Tiles)
{ {
for (int b = 0; b < board!.ReadCells.Count; b++) for (int b = 0; b < board.ReadCells.Count; b++)
{ {
int x = board.ReadCells[b].GetX; int x = board.ReadCells[b].GetX;
int y = board.ReadCells[b].GetY; int y = board.ReadCells[b].GetY;

@ -41,19 +41,13 @@ namespace QwirkleClassLibrary.Tiles
/// A getter for the shape of the Tile. /// A getter for the shape of the Tile.
/// </summary> /// </summary>
/// <returns>The shape attribute of the Tile.</returns> /// <returns>The shape attribute of the Tile.</returns>
public Shape GetShape public Shape GetShape => shape;
{
get { return shape; }
}
/// <summary> /// <summary>
/// A getter for the color of the Tile. /// A getter for the color of the Tile.
/// </summary> /// </summary>
/// <returns>The color attribute of the Tile.</returns> /// <returns>The color attribute of the Tile.</returns>
public Color GetColor public Color GetColor => color;
{
get { return color; }
}
/// <summary> /// <summary>
/// This method is used to override the ToString() method. It is simply a tool to facilitate the development. /// This method is used to override the ToString() method. It is simply a tool to facilitate the development.
@ -66,20 +60,66 @@ namespace QwirkleClassLibrary.Tiles
public int CompareTo(object? obj) public int CompareTo(object? obj)
{ {
if (obj == null) return 1; if (obj == null)
{
throw new NullReferenceException();
}
var otherTile = obj as Tile; var otherTile = obj as Tile;
if (otherTile != null)
if (color == otherTile!.color)
{ {
if (color == otherTile.color) return shape.CompareTo(otherTile.shape);
{ }
return shape.CompareTo(otherTile.shape);
} return color.CompareTo(otherTile.color);
return color.CompareTo(otherTile.color); }
public override bool Equals(object? obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
} }
throw new ArgumentException("Object is not a Tile"); var otherTile = obj as Tile;
return color == otherTile!.color && shape == otherTile.shape;
}
public override int GetHashCode()
{
return HashCode.Combine(color, shape);
}
public static bool operator ==(Tile tile1, Tile tile2)
{
return EqualityComparer<Tile>.Default.Equals(tile1, tile2);
}
public static bool operator !=(Tile tile1, Tile tile2)
{
return !(tile1 == tile2);
}
public static bool operator <(Tile tile1, Tile tile2)
{
return tile1.CompareTo(tile2) < 0;
}
public static bool operator >(Tile tile1, Tile tile2)
{
return tile1.CompareTo(tile2) > 0;
}
public static bool operator <=(Tile tile1, Tile tile2)
{
return tile1.CompareTo(tile2) <= 0;
}
public static bool operator >=(Tile tile1, Tile tile2)
{
return tile1.CompareTo(tile2) >= 0;
} }
} }
} }
Loading…
Cancel
Save