Fixed a lot of code smelt
continuous-integration/drone/push Build is passing Details

test_old_branch
Jules LASCRET 11 months ago
parent 6514f11296
commit 69f9b3d404

@ -48,7 +48,7 @@ namespace QwirkleClassLibrary
for (int i = 0; i < cells.Count; i++) for (int i = 0; i < cells.Count; i++)
{ {
if (this.cells[i].GetX != x || this.cells[i].GetY != y) continue; if (this.cells[i].GetX != x || this.cells[i].GetY != y) continue;
if (cells[i].IsFree == true) if (cells[i].IsFree)
{ {
return cells[i].SetTile(tile); return cells[i].SetTile(tile);
} }

@ -13,8 +13,8 @@ namespace QwirkleClassLibrary
public class Game : IPlayer, IRules public class Game : IPlayer, IRules
{ {
private Dictionary<Player, int> scoreBoard = new();
public Dictionary<Player, int> ScoreBoard = new();
private TileBag bag; private TileBag bag;
public bool GameRunning { get; private set; } public bool GameRunning { get; private set; }
private Board board; private Board board;
@ -49,13 +49,13 @@ namespace QwirkleClassLibrary
public bool AddPlayerInGame(string? playerTag) public bool AddPlayerInGame(string? playerTag)
{ {
if (string.IsNullOrWhiteSpace(playerTag) == true) if (string.IsNullOrWhiteSpace(playerTag))
{ {
OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The name is null or white space.")); OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The name is null or white space."));
return false; return false;
} }
if(this.GameRunning == true) if(this.GameRunning)
{ {
OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The game is running.")); OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The game is running."));
return false; return false;
@ -134,7 +134,7 @@ namespace QwirkleClassLibrary
{ {
for (int i = 0; i < players.Count; i++) for (int i = 0; i < players.Count; i++)
{ {
if (players[i].IsPlaying == true) if (players[i].IsPlaying)
{ {
return i; return i;
} }
@ -163,7 +163,7 @@ namespace QwirkleClassLibrary
public string SetFirstPlayer() public string SetFirstPlayer()
{ {
if (GameRunning == true) if (GameRunning)
{ {
players[0].IsPlaying = true; players[0].IsPlaying = true;
//OnNextPlayer(new NextPlayerNotifiedEventArgs(players[0])); //OnNextPlayer(new NextPlayerNotifiedEventArgs(players[0]));
@ -194,9 +194,9 @@ namespace QwirkleClassLibrary
public bool PlaceTile(Player player, Tile tile, int x, int y) public bool PlaceTile(Player player, Tile tile, int x, int y)
{ {
if (board.AddTileInCell(x, y, tile) == true) if (board.AddTileInCell(x, y, tile))
{ {
return player.RemoveTileToPlayer(tile) == true; return player.RemoveTileToPlayer(tile);
} }
else else
{ {
@ -269,13 +269,13 @@ namespace QwirkleClassLibrary
if (extendedCell.GetTile.GetColor != tile.GetColor && extendedCell.GetTile.GetShape != tile.GetShape) if (extendedCell.GetTile.GetColor != tile.GetColor && extendedCell.GetTile.GetShape != tile.GetShape)
{ {
OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "Les couleurs/formes adjacentes ne correspondes pas")); OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "Color / Shape does not match with the surrounding tiles !"));
return false; return false;
} }
if (i == 6) if (i == 6)
{ {
OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "La ligne ou colonne fait deja 6 tuiles !")); OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "Row/Column already 6 tiles long !"));
return false; return false;
} }
} }
@ -311,10 +311,10 @@ namespace QwirkleClassLibrary
{ {
if (!b.HasOccupiedCase()) if (!b.HasOccupiedCase())
{ {
if (this.PlaceTile(this.GetPlayingPlayer(), t, x, y) == true) if (this.PlaceTile(this.GetPlayingPlayer(), t, x, y))
{ {
this.AddCellUsed(this.GetBoard().GetCell(x, y)); this.AddCellUsed(this.GetBoard().GetCell(x, y));
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "Tile correctement placé")); OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "Tile correctly placed"));
return true; return true;
} }
} }
@ -343,15 +343,12 @@ namespace QwirkleClassLibrary
var dx = cell.GetX - x; var dx = cell.GetX - x;
var dy = cell.GetY - y; var dy = cell.GetY - y;
if (CheckExtendedSurroundingCells(t, x, y, dx, dy, b) == false) return CheckExtendedSurroundingCells(t, x, y, dx, dy, b);
{
return false;
}
} }
if (CheckTilesInLine(this.cellUsed, b, x, y) && surroundingCells.Any(cell => cell?.GetTile != null)) if (CheckTilesInLine(this.cellUsed, b, x, y) && surroundingCells.Any(cell => cell?.GetTile != null))
{ {
if(this.PlaceTile(this.GetPlayingPlayer(), t, x, y) == true) if(this.PlaceTile(this.GetPlayingPlayer(), t, x, y))
{ {
this.AddCellUsed(this.GetBoard().GetCell(x, y)); this.AddCellUsed(this.GetBoard().GetCell(x, y));
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "Tile correctement placé")); OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "Tile correctement placé"));
@ -363,57 +360,6 @@ namespace QwirkleClassLibrary
return false; return false;
} }
// public int GetPlayerScore(Player player, List<Cell> cellsPlayed, Board b)
// {
// // Compte le nombre de tuiles et ajoute ce nombre au score
// int score = cellsPlayed.Count;
//
// // Check les lignes / colonnes adjacentes aux tuiles placées
// for(int i= 0; i < cellsPlayed.Count; i++)
// {
// // A chaque tour, crée la liste des 4 cellules adjacentes à la cellule sélectionnée
// var surroundingCells = new[]
// {
// b.GetCell(cellsPlayed[i].GetX + 1, cellsPlayed[i].GetY),
// b.GetCell(cellsPlayed[i].GetX - 1, cellsPlayed[i].GetY),
// b.GetCell(cellsPlayed[i].GetX, cellsPlayed[i].GetY + 1),
// b.GetCell(cellsPlayed[i].GetX, cellsPlayed[i].GetY - 1)
// };
//
// // Parcourt le tableau dans la direction par rapport à ces cellules adjacentes
// foreach (var cell in surroundingCells)
// {
// // Si la cellule adjacente ne contient pas de tuile, on passe à la cellule adjacente suivante
// if (cell?.GetTile == null) break;
//
// if (cellsPlayed.Contains(cell) != true)
// {
//
// var dx = cell.GetX - cellsPlayed[i].GetX;
// var dy = cell.GetY - cellsPlayed[i].GetY;
//
// for (int j = 1; j < b.Rows; j++)
// {
// var extendedCell = b.GetCell(cellsPlayed[i].GetX + j * dx, cellsPlayed[i].GetY + j * dy);
//
// if (extendedCell?.GetTile == null)
// {
// break;
// }
//
// score += 1;
//
// }
// }
// }
//
// }
//
// ScoreBoard.Add(player, score);
//
// return score;
// }
public int GetPlayerScore(Player player, ReadOnlyCollection<Cell> cellsPlayed, Board b) public int GetPlayerScore(Player player, ReadOnlyCollection<Cell> cellsPlayed, Board b)
{ {
int score = cellsPlayed.Count; int score = cellsPlayed.Count;
@ -424,9 +370,9 @@ namespace QwirkleClassLibrary
} }
if(ScoreBoard.TryAdd(player, score) == false) if(scoreBoard.TryAdd(player, score) == false)
{ {
ScoreBoard[player] += score; scoreBoard[player] += score;
} }
return score; return score;

@ -8,19 +8,18 @@ namespace QwirkleClassLibrary
{ {
public struct Score public struct Score
{ {
private int score { get; set; } private int PlayerScore { get; set; }
public string playerTag { get; } public string PlayerTag { get; }
public Score(Player p) public Score(Player? p)
{ {
if (p == null) if (p == null)
{ {
throw new ArgumentException(); throw new NullReferenceException();
} }
else else
{ { PlayerScore = 0;
score = 0; PlayerTag = p.NameTag;
playerTag = p.NameTag;
} }
} }
} }

@ -12,11 +12,11 @@ namespace QwirkleClassLibrary
{ {
private readonly Shape shape; private readonly Shape shape;
private readonly Color color; private readonly Color color;
public Tile(Shape sh, Color co) public Tile(Shape sh, Color co)
{ {
shape = sh; shape = sh;
color = co; color = co;
//Console.WriteLine("A tile of shape " + shape + " and color " + color + " has been created.");
} }
public string NameColorTile() public string NameColorTile()

@ -42,7 +42,7 @@ static void ShowTiles(Game game)
var stringBuilder = new StringBuilder(); var stringBuilder = new StringBuilder();
for (int i = 0; i < currentPlayer.Tiles.Count(); i++) for (int i = 0; i < currentPlayer.Tiles.Count; i++)
{ {
stringBuilder.Append("[" + (i + 1) + "] "); stringBuilder.Append("[" + (i + 1) + "] ");
stringBuilder.AppendLine(currentPlayer.Tiles[i].ToString()); stringBuilder.AppendLine(currentPlayer.Tiles[i].ToString());
@ -94,7 +94,7 @@ static void SwapTile(Game game)
ShowTiles(game); ShowTiles(game);
while (continueSwap == true) while (continueSwap)
{ {
Write("Enter the number of the tile you want to swap : "); Write("Enter the number of the tile you want to swap : ");
int no = Convert.ToInt32(ReadLine()); int no = Convert.ToInt32(ReadLine());
@ -165,7 +165,6 @@ static void MenuSwitch(Game game)
static void ShowBoard(Game game) static void ShowBoard(Game game)
{ {
Board board = game.GetBoard(); Board board = game.GetBoard();
List<Cell> cells = board.GetCells();
for(int i = 0; i<board.Rows; i++) for(int i = 0; i<board.Rows; i++)
{ {
@ -174,7 +173,10 @@ static void ShowBoard(Game game)
if(board.GetCell(y, i)!.IsFree == false) if(board.GetCell(y, i)!.IsFree == false)
{ {
Tile? tile = board.GetCell(y, i)?.GetTile; Tile? tile = board.GetCell(y, i)?.GetTile;
Console.Write("| " + tile.GetShape.ToString()[0] + tile.GetShape.ToString()[1] + tile.GetColor.ToString()[0] + " |"); if (tile != null)
{
Console.Write("| " + tile.GetShape.ToString()[0] + tile.GetShape.ToString()[1] + tile.GetColor.ToString()[0] + " |");
}
} }
else else
{ {
@ -205,7 +207,7 @@ static void MainMenu(Game game)
game.DrawTiles(game.GetPlayingPlayer()); game.DrawTiles(game.GetPlayingPlayer());
MenuSwitch(game); MenuSwitch(game);
} while (game.IsGameOver()); //while (game.GetPlayingPlayerPosition() != game.PlayerList.Count - 1); } while (game.IsGameOver());
} }
static void MainGame() static void MainGame()

@ -13,7 +13,7 @@ public class TestScore
Assert.Throws<ArgumentNullException>(() => new Score(null)); Assert.Throws<ArgumentNullException>(() => new Score(null));
return; return;
}*/ }*/
Player p = new Player("test"); Player? p = new Player("test");
Score score = new Score(p); Score score = new Score(p);
Assert.True(true); Assert.True(true);
} }

@ -22,11 +22,11 @@ public class TestTileBag
[Fact] [Fact]
public void Test_AddTileInBag() public void Test_AddTileInBag()
{ {
Tile t = null; Tile? t = null;
Tile tok = new(Shape.Club, Color.Green); Tile tok = new(Shape.Club, Color.Green);
TileBag bag = new TileBag(2); TileBag bag = new TileBag(2);
if (bag.AddTileInBag(t) == false) if (t != null && bag.AddTileInBag(t) == false)
{ {
Assert.True(bag.AddTileInBag(tok)); Assert.True(bag.AddTileInBag(tok));
@ -37,12 +37,12 @@ public class TestTileBag
[Fact] [Fact]
public void Test_RemoveTileInBag() public void Test_RemoveTileInBag()
{ {
Tile t = null; Tile? t = null;
Tile tok = new(Shape.Club, Color.Green); Tile tok = new(Shape.Club, Color.Green);
TileBag bag = new TileBag(2); TileBag bag = new TileBag(2);
bag.AddTileInBag(tok); bag.AddTileInBag(tok);
if (bag.RemoveTileInBag(t) == false) if (t != null && bag.RemoveTileInBag(t) == false)
{ {
Assert.True(bag.RemoveTileInBag(tok)); Assert.True(bag.RemoveTileInBag(tok));

Loading…
Cancel
Save