diff --git a/Qwirkle/Files/Game.xml b/Qwirkle/Files/Game.xml
index 3909ecb..b191d2f 100644
--- a/Qwirkle/Files/Game.xml
+++ b/Qwirkle/Files/Game.xml
@@ -1,1550 +1 @@
-
- true
-
-
-
- Red
- Square
-
-
- Blue
- Square
-
-
- Green
- Square
-
-
- Yellow
- Square
-
-
- Orange
- Square
-
-
- Purple
- Square
-
-
- Red
- Round
-
-
- Blue
- Round
-
-
- Green
- Round
-
-
- Orange
- Round
-
-
- Purple
- Round
-
-
- Green
- Rhombus
-
-
- Yellow
- Rhombus
-
-
- Purple
- Rhombus
-
-
- Red
- Club
-
-
- Blue
- Club
-
-
- Green
- Club
-
-
- Yellow
- Club
-
-
- Purple
- Club
-
-
- Red
- Shuriken
-
-
- Blue
- Shuriken
-
-
- Green
- Shuriken
-
-
- Purple
- Shuriken
-
-
- Red
- Star
-
-
- Blue
- Star
-
-
- Green
- Star
-
-
- Yellow
- Star
-
-
- Orange
- Star
-
-
- Purple
- Star
-
-
- Red
- Square
-
-
- Blue
- Square
-
-
- Green
- Square
-
-
- Yellow
- Square
-
-
- Blue
- Round
-
-
- Purple
- Round
-
-
- Red
- Rhombus
-
-
- Green
- Rhombus
-
-
- Yellow
- Rhombus
-
-
- Purple
- Rhombus
-
-
- Blue
- Club
-
-
- Green
- Club
-
-
- Yellow
- Club
-
-
- Purple
- Club
-
-
- Red
- Shuriken
-
-
- Green
- Shuriken
-
-
- Yellow
- Shuriken
-
-
- Orange
- Shuriken
-
-
- Purple
- Shuriken
-
-
- Red
- Star
-
-
- Blue
- Star
-
-
- Orange
- Star
-
-
- Purple
- Star
-
-
- Red
- Square
-
-
- Blue
- Square
-
-
- Yellow
- Square
-
-
- Orange
- Square
-
-
- Purple
- Square
-
-
- Blue
- Round
-
-
- Green
- Round
-
-
- Yellow
- Round
-
-
- Purple
- Round
-
-
- Red
- Rhombus
-
-
- Blue
- Rhombus
-
-
- Green
- Rhombus
-
-
- Yellow
- Rhombus
-
-
- Orange
- Rhombus
-
-
- Purple
- Rhombus
-
-
- Red
- Club
-
-
- Blue
- Club
-
-
- Green
- Club
-
-
- Yellow
- Club
-
-
- Orange
- Club
-
-
- Purple
- Club
-
-
- Red
- Shuriken
-
-
- Blue
- Shuriken
-
-
- Green
- Shuriken
-
-
- Yellow
- Shuriken
-
-
- Orange
- Shuriken
-
-
- Purple
- Shuriken
-
-
- Red
- Star
-
-
- Green
- Star
-
-
- Yellow
- Star
-
-
- Orange
- Star
-
-
- Purple
- Star
-
-
- Orange
- Round
-
-
- Orange
- Club
-
-
- Red
- Round
-
-
-
-
- 15
- 12
-
-
-
- Blue
- Rhombus
-
- 0
- 0
-
-
-
- 0
- 1
-
-
-
- Orange
- Round
-
- 0
- 2
-
-
-
- Yellow
- Round
-
- 0
- 3
-
-
-
- Red
- Round
-
- 0
- 4
-
-
-
- 0
- 5
-
-
-
- 0
- 6
-
-
-
- 0
- 7
-
-
-
- 0
- 8
-
-
-
- 0
- 9
-
-
-
- 0
- 10
-
-
-
- 0
- 11
-
-
-
- 0
- 12
-
-
-
- 0
- 13
-
-
-
- 0
- 14
-
-
-
- Red
- Rhombus
-
- 1
- 0
-
-
-
- Blue
- Rhombus
-
- 1
- 1
-
-
-
- Orange
- Rhombus
-
- 1
- 2
-
-
-
- 1
- 3
-
-
-
- 1
- 4
-
-
-
- 1
- 5
-
-
-
- 1
- 6
-
-
-
- 1
- 7
-
-
-
- 1
- 8
-
-
-
- 1
- 9
-
-
-
- 1
- 10
-
-
-
- 1
- 11
-
-
-
- 1
- 12
-
-
-
- 1
- 13
-
-
-
- 1
- 14
-
-
-
- 2
- 0
-
-
-
- Blue
- Star
-
- 2
- 1
-
-
-
- 2
- 2
-
-
-
- 2
- 3
-
-
-
- 2
- 4
-
-
-
- 2
- 5
-
-
-
- 2
- 6
-
-
-
- 2
- 7
-
-
-
- 2
- 8
-
-
-
- 2
- 9
-
-
-
- 2
- 10
-
-
-
- 2
- 11
-
-
-
- 2
- 12
-
-
-
- 2
- 13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
-
-
- 7
- 11
-
-
-
- 7
- 12
-
-
-
- 7
- 13
-
-
-
- 7
- 14
-
-
-
- 8
- 0
-
-
-
- 8
- 1
-
-
-
- 8
- 2
-
-
-
- 8
- 3
-
-
-
- 8
- 4
-
-
-
- 8
- 5
-
-
-
- 8
- 6
-
-
-
- 8
- 7
-
-
-
- 8
- 8
-
-
-
- 8
- 9
-
-
-
- 8
- 10
-
-
-
- 8
- 11
-
-
-
- 8
- 12
-
-
-
- 8
- 13
-
-
-
- 8
- 14
-
-
-
- 9
- 0
-
-
-
- 9
- 1
-
-
-
- 9
- 2
-
-
-
- 9
- 3
-
-
-
- 9
- 4
-
-
-
- 9
- 5
-
-
-
- 9
- 6
-
-
-
- 9
- 7
-
-
-
- 9
- 8
-
-
-
- 9
- 9
-
-
-
- 9
- 10
-
-
-
- 9
- 11
-
-
-
- 9
- 12
-
-
-
- 9
- 13
-
-
-
- 9
- 14
-
-
-
- 10
- 0
-
-
-
- 10
- 1
-
-
-
- 10
- 2
-
-
-
- 10
- 3
-
-
-
- 10
- 4
-
-
-
- 10
- 5
-
-
-
- 10
- 6
-
-
-
- 10
- 7
-
-
-
- 10
- 8
-
-
-
- 10
- 9
-
-
-
- 10
- 10
-
-
-
- 10
- 11
-
-
-
- 10
- 12
-
-
-
- 10
- 13
-
-
-
- 10
- 14
-
-
-
- 11
- 0
-
-
-
- 11
- 1
-
-
-
- 11
- 2
-
-
-
- 11
- 3
-
-
-
- 11
- 4
-
-
-
- 11
- 5
-
-
-
- 11
- 6
-
-
-
- 11
- 7
-
-
-
- 11
- 8
-
-
-
- 11
- 9
-
-
-
- 11
- 10
-
-
-
- 11
- 11
-
-
-
- 11
- 12
-
-
-
- 11
- 13
-
-
-
- 11
- 14
-
-
-
-
-
-
- true
- Jules
-
-
- Yellow
- Star
-
-
- Orange
- Club
-
-
- Orange
- Square
-
-
- Yellow
- Round
-
-
- Orange
- Rhombus
-
-
- Green
- Star
-
-
-
-
- false
- Jérémy
-
-
- Green
- Square
-
-
- Purple
- Square
-
-
- Red
- Club
-
-
- Green
- Round
-
-
- Yellow
- Shuriken
-
-
- Orange
- Shuriken
-
-
-
-
-
-
-
- 8
-
-
-
- 9
-
-
-
\ No newline at end of file
+false15120001020304050607080901001101201301410RedRound11RedSquare121314151617181911011111211311420OrangeRound21OrangeSquare222324252627282921021121221321430PurpleRound31PurpleSquare323334353637383931031131231331440BlueRound41BlueSquare424344454647484941041141241341450YellowRound51525354555657585951051151251351460GreenRound6162636465666768696106116126136147071727374757677787971071171271371480818283848586878889810811812813814909192939495969798999109119129139141001011021031041051061071081091010101110121013101411011111211311411511611711811911101111111211131114falseJulesGreenSquareYellowSquaretrueJérémy430
\ No newline at end of file
diff --git a/Qwirkle/Files/LeaderBoard.json b/Qwirkle/Files/LeaderBoard.json
index fbec232..d109a1c 100644
--- a/Qwirkle/Files/LeaderBoard.json
+++ b/Qwirkle/Files/LeaderBoard.json
@@ -1 +1,16 @@
-{"leaderboard":[]}
\ No newline at end of file
+{
+ "leaderboard": [
+ {
+ "Date": "\/Date(1717432093871+0200)\/",
+ "PlayerName": "Jérémy",
+ "Points": 30,
+ "Victories": 2
+ },
+ {
+ "Date": "\/Date(1717432093871+0200)\/",
+ "PlayerName": "Jules",
+ "Points": 4,
+ "Victories": 0
+ }
+ ]
+}
\ No newline at end of file
diff --git a/Qwirkle/QwirkleClassLibrary/Games/Game.cs b/Qwirkle/QwirkleClassLibrary/Games/Game.cs
index 464bee2..c1b9d3a 100644
--- a/Qwirkle/QwirkleClassLibrary/Games/Game.cs
+++ b/Qwirkle/QwirkleClassLibrary/Games/Game.cs
@@ -1,13 +1,5 @@
-using System;
-using System.Collections.Generic;
-using System.Collections.ObjectModel;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using System.Xml.Linq;
+using System.Collections.ObjectModel;
using System.Security.Cryptography;
-using System.Collections;
-using System.Collections.Immutable;
using System.Runtime.Serialization;
using QwirkleClassLibrary.Tiles;
using QwirkleClassLibrary.Boards;
@@ -21,13 +13,13 @@ namespace QwirkleClassLibrary.Games
public class Game : IPlayer, IRules
{
[DataMember]
- public TileBag? bag = null;
+ private TileBag? bag = null;
[DataMember]
public bool GameRunning { get; set; }
[DataMember]
- private Board board = new Board(15, 12);
+ private Board board = new(15, 12);
public bool PlayerSwapping { get; set; }
public Board Board => board;
@@ -35,15 +27,15 @@ namespace QwirkleClassLibrary.Games
public ReadOnlyCollection PlayerList => players.AsReadOnly();
[DataMember]
- private readonly List players = new();
+ private readonly List players = [];
[DataMember]
- private readonly Dictionary scoreBoard = new();
+ private readonly Dictionary scoreBoard = [];
public ReadOnlyDictionary ScoreBoard => scoreBoard.AsReadOnly();
[DataMember]
- private readonly List cellUsed = new();
+ private readonly List cellUsed = [];
public ReadOnlyCollection CellsUsed => cellUsed.AsReadOnly();
@@ -270,7 +262,7 @@ namespace QwirkleClassLibrary.Games
{
if (bag != null && p.Tiles.Count < 6)
{
- int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count);
+ int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag!.Count);
p.AddTileToPlayer(bag.TilesBag[val]);
bag.RemoveTileInBag(bag.TilesBag[val]);
@@ -362,7 +354,7 @@ namespace QwirkleClassLibrary.Games
{
while (player.Tiles.Count < 6)
{
- if (bag!.TilesBag.Count == 0)
+ if (bag!.TilesBag!.Count == 0)
{
return false;
}
@@ -384,13 +376,19 @@ namespace QwirkleClassLibrary.Games
/// bool
public bool SwapTiles(Player player, List tilesToSwap)
{
+ if (cellUsed.Count != 0)
+ {
+ OnSwapTiles(new SwapTilesNotifiedEventArgs("You can't swap tiles after placing some !"));
+ return false;
+ }
+
if (tilesToSwap.Count == 0)
{
OnSwapTiles(new SwapTilesNotifiedEventArgs("You must select at least one tile to swap !"));
return false;
}
- if (bag!.TilesBag.Count < tilesToSwap.Count)
+ if (bag!.TilesBag!.Count < tilesToSwap.Count)
{
OnSwapTiles(new SwapTilesNotifiedEventArgs("Not enough tiles in the bag to swap !"));
return false;
@@ -719,7 +717,7 @@ namespace QwirkleClassLibrary.Games
{
List playerTilesBagPos = [];
- if (bag!.TilesBag.Count == 0)
+ if (bag!.TilesBag!.Count == 0)
{
for (int i = 0; i < players.Count; i++)
{
@@ -771,7 +769,7 @@ namespace QwirkleClassLibrary.Games
{
List playerTilesBagPos = CheckTilesBag();
- if (playerTilesBagPos.Count != 0 && !CheckPlacementPossibilities(playerTilesBagPos) || bag!.TilesBag.Count == 0 && players[GetPlayingPlayerPosition()].Tiles.Count==0)
+ if (playerTilesBagPos.Count != 0 && !CheckPlacementPossibilities(playerTilesBagPos) || bag!.TilesBag!.Count == 0 && players[GetPlayingPlayerPosition()].Tiles.Count==0)
{
OnEndOfGame(new EndOfGameNotifiedEventArgs(player));
GameRunning = false;
diff --git a/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistenceXml.cs b/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistenceXml.cs
index 8325cc2..564134a 100644
--- a/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistenceXml.cs
+++ b/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistenceXml.cs
@@ -20,9 +20,17 @@ public class GamePersistenceXml : IGamePersistence
{
var serializer = new DataContractSerializer(typeof(Game));
- using (Stream reader = File.OpenRead("Game.xml"))
+ try
{
- return serializer.ReadObject(reader) as Game ?? throw new InvalidOperationException();
+ using (Stream reader = File.OpenRead("Game.xml"))
+ {
+ var newGame = serializer.ReadObject(reader) as Game;
+ return newGame!;
+ }
+ }
+ catch
+ {
+ return new Game();
}
}
}
\ No newline at end of file
diff --git a/Qwirkle/QwirkleClassLibrary/Players/Leaderboard.cs b/Qwirkle/QwirkleClassLibrary/Players/Leaderboard.cs
index 3a06518..db9a0b6 100644
--- a/Qwirkle/QwirkleClassLibrary/Players/Leaderboard.cs
+++ b/Qwirkle/QwirkleClassLibrary/Players/Leaderboard.cs
@@ -41,7 +41,7 @@ namespace QwirkleClassLibrary.Players
///
public void AddScoreInLead(ReadOnlyDictionary scoreBoard)
{
- DateTime now = DateTime.Today;
+ DateTime now = DateTime.Now;
bool first = true;
var sb = scoreBoard.OrderByDescending(x => x.Value).ThenBy(x => x.Key.NameTag);
@@ -59,7 +59,10 @@ namespace QwirkleClassLibrary.Players
leaderboard[i].Victories++;
}
- leaderboard[i].Points = pair.Value;
+ if (pair.Value > leaderboard[i].Points)
+ {
+ leaderboard[i].Points = pair.Value;
+ }
}
else
diff --git a/Qwirkle/QwirkleClassLibrary/Players/Player.cs b/Qwirkle/QwirkleClassLibrary/Players/Player.cs
index 0274620..5d8cb7a 100644
--- a/Qwirkle/QwirkleClassLibrary/Players/Player.cs
+++ b/Qwirkle/QwirkleClassLibrary/Players/Player.cs
@@ -37,7 +37,7 @@ namespace QwirkleClassLibrary.Players
}
NameTag = name;
- Tiles = new ObservableCollection();
+ Tiles = [];
}
[DataMember]
diff --git a/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs b/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs
index fc6381a..291a4d0 100644
--- a/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs
+++ b/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs
@@ -12,9 +12,9 @@ namespace QwirkleClassLibrary.Tiles
public class TileBag
{
[DataMember]
- private readonly List tiles = new List();
+ private readonly List tiles = [];
- public ReadOnlyCollection TilesBag { get; private set; }
+ public ReadOnlyCollection? TilesBag { get; private set; }
///
@@ -35,7 +35,7 @@ namespace QwirkleClassLibrary.Tiles
{
foreach (Color c in Enum.GetValues(typeof(Color)))
{
- Tile t = new Tile(s, c);
+ Tile t = new(s, c);
tiles.Add(t);
}
}
diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs
index a55e702..a02170a 100644
--- a/Qwirkle/QwirkleConsoleApp/Program.cs
+++ b/Qwirkle/QwirkleConsoleApp/Program.cs
@@ -1,17 +1,9 @@
using QwirkleClassLibrary.Boards;
-using QwirkleClassLibrary.Events;
using QwirkleClassLibrary.Games;
using QwirkleClassLibrary.Players;
using QwirkleClassLibrary.Tiles;
using QwirkleConsoleApp;
-using System.Collections.Immutable;
-using System.Collections.ObjectModel;
-using System.Diagnostics.Metrics;
-using System.Net.Quic;
-using System.Runtime.Serialization;
-using System.Runtime.Serialization.Json;
using System.Text;
-using System.Transactions;
using QwirkleClassLibrary.Persistences;
using static System.Console;
@@ -407,12 +399,10 @@ static void MainGame()
game.StartGame();
MainMenu(game);
- foreach (var player in game.PlayerList)
+ if (!game.GameRunning)
{
- if (game.CheckGameOver(player))
- {
- leaderboard.AddScoreInLead(game.ScoreBoard);
- }
+ leaderboard.AddScoreInLead(game.ScoreBoard);
+ break;
}
ILeaderboardPersistence leaderboardSave = new LeaderboardPersistenceJson();
@@ -424,17 +414,50 @@ static void MainGame()
Game loadedGame;
IGamePersistence gameLoad = new GamePersistenceXml();
- loadedGame = gameLoad.LoadGame();
+
+ try
+ {
+ loadedGame = gameLoad.LoadGame();
+ }
+ catch
+ {
+ ForegroundColor = ConsoleColor.Yellow;
+ WriteLine("WARNING : No game saved ! Creating a new game !");
+ ResetColor();
+ loadedGame = new Game();
+ }
ILeaderboardPersistence leaderboardLoad = new LeaderboardPersistenceJson();
leaderboard = leaderboardLoad.LoadLeaderboard();
-
- MainMenuContinue(loadedGame);
- leaderboard.AddScoreInLead(loadedGame.ScoreBoard);
+
+ if (loadedGame.GameRunning)
+ {
+ MainMenuContinue(loadedGame);
+ }
+ else
+ {
+ loadedGame = new Game();
+ ForegroundColor = ConsoleColor.Yellow;
+ WriteLine("WARNING : The previous saved game was over ! Creating a new game !");
+ ResetColor();
+ AddPlayers(loadedGame);
+ loadedGame.StartGame();
+ MainMenu(loadedGame);
+ }
+
+ if (!loadedGame.GameRunning)
+ {
+ leaderboard.AddScoreInLead(loadedGame.ScoreBoard);
+ }
+
+ ILeaderboardPersistence leaderboardSave2 = new LeaderboardPersistenceJson();
+ leaderboardSave2.SaveLeaderboard(leaderboard);
break;
case 3:
+ ILeaderboardPersistence leaderboardLoad2 = new LeaderboardPersistenceJson();
+ leaderboard = leaderboardLoad2.LoadLeaderboard();
ShowLeaderboard(leaderboard);
break;
diff --git a/Qwirkle/TestBase/TestPlayer.cs b/Qwirkle/TestBase/TestPlayer.cs
index 5c2b92a..1727423 100644
--- a/Qwirkle/TestBase/TestPlayer.cs
+++ b/Qwirkle/TestBase/TestPlayer.cs
@@ -7,14 +7,14 @@ public class TestPlayers
[Theory]
[InlineData(true, "Mathis")]
[InlineData(false, null)]
- public void Test_CreatePlayer(bool isValid, string playertag)
+ public void Test_CreatePlayer(bool isValid, string? playertag)
{
if (!isValid)
{
- Assert.Throws(() => new Player(playertag));
+ Assert.Throws(() => new Player(playertag!));
return;
}
- Player player = new Player(playertag);
+ Player player = new Player(playertag!);
Assert.Equal(playertag, player.NameTag);
}
diff --git a/Qwirkle/TestBase/TestTileBag.cs b/Qwirkle/TestBase/TestTileBag.cs
index cbfc07b..e202809 100644
--- a/Qwirkle/TestBase/TestTileBag.cs
+++ b/Qwirkle/TestBase/TestTileBag.cs
@@ -16,7 +16,7 @@ public class TestTileBag
return;
}
TileBag bag = new TileBag(nbset);
- Assert.Equal(bag.TilesBag.Count, nbset * 36);
+ Assert.Equal(bag.TilesBag!.Count, nbset * 36);
}
@@ -30,11 +30,11 @@ public class TestTileBag
if (except)
{
- Assert.True(bag.RemoveTileInBag(bag.TilesBag[1]));
+ Assert.True(bag.RemoveTileInBag(bag.TilesBag![1]));
return;
}
- Tile tile = bag.TilesBag[0];
+ Tile tile = bag.TilesBag![0];
bag.RemoveTileInBag(tile);
Assert.False(bag.RemoveTileInBag(tile));
| | |