diff --git a/Qwirkle/QwirkleClassLibrary/Boards/Board.cs b/Qwirkle/QwirkleClassLibrary/Boards/Board.cs index 00a2506..496f0e3 100644 --- a/Qwirkle/QwirkleClassLibrary/Boards/Board.cs +++ b/Qwirkle/QwirkleClassLibrary/Boards/Board.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using System.Runtime.Serialization; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; @@ -12,10 +13,12 @@ namespace QwirkleClassLibrary.Boards /// /// This class is used to create the board for our Qwirkle Game. It uses others classes, such as Cell and Tile, to take care of the tiles placements during the game. /// + [DataContract] public class Board { public ReadOnlyCollection ReadCells => cells.AsReadOnly(); - + + [DataMember] private readonly List cells = new(); public int Rows { get; } diff --git a/Qwirkle/QwirkleClassLibrary/Boards/Cell.cs b/Qwirkle/QwirkleClassLibrary/Boards/Cell.cs index 3c2581d..5a894f0 100644 --- a/Qwirkle/QwirkleClassLibrary/Boards/Cell.cs +++ b/Qwirkle/QwirkleClassLibrary/Boards/Cell.cs @@ -1,13 +1,20 @@ // ReSharper disable All using System.Runtime.CompilerServices; +using System.Runtime.Serialization; using QwirkleClassLibrary.Tiles; namespace QwirkleClassLibrary.Boards; +[DataContract] public class Cell { + [DataMember] private readonly int x; + + [DataMember] private readonly int y; + + [DataMember] private Tile? tile = null; /// diff --git a/Qwirkle/QwirkleClassLibrary/Events/SwapTilesNotifiedEventArgs.cs b/Qwirkle/QwirkleClassLibrary/Events/SwapTilesNotifiedEventArgs.cs index c75b65f..e59c3c1 100644 --- a/Qwirkle/QwirkleClassLibrary/Events/SwapTilesNotifiedEventArgs.cs +++ b/Qwirkle/QwirkleClassLibrary/Events/SwapTilesNotifiedEventArgs.cs @@ -2,6 +2,6 @@ namespace QwirkleClassLibrary.Events { public class SwapTilesNotifiedEventArgs(string reason) { - public string? Reason { get; private set; } + public string Reason { get; private set; } = reason; } } \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/Games/Game.cs b/Qwirkle/QwirkleClassLibrary/Games/Game.cs index fc176b5..5664acd 100644 --- a/Qwirkle/QwirkleClassLibrary/Games/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Games/Game.cs @@ -8,6 +8,7 @@ using System.Xml.Linq; using System.Security.Cryptography; using System.Collections; using System.Collections.Immutable; +using System.Runtime.Serialization; using QwirkleClassLibrary.Tiles; using QwirkleClassLibrary.Boards; using QwirkleClassLibrary.Events; @@ -16,22 +17,21 @@ using QwirkleClassLibrary.Players; namespace QwirkleClassLibrary.Games { + [DataContract] public class Game : IPlayer, IRules { public ReadOnlyDictionary ScoreBoard => scoreBoard.AsReadOnly(); private readonly Dictionary scoreBoard = new(); private TileBag? bag = null; + + [DataMember] public bool GameRunning { get; private set; } - private Board _board = new Board(15, 12); - public Board Board - { - get { return _board; } - private set { _board = value; } - } + [DataMember] + private Board board = new Board(15, 12); - public ObservableCollection GetCellsInBoard => new ObservableCollection(Board!.GetCells()); + public ObservableCollection GetCellsInBoard => new ObservableCollection(board!.GetCells()); public ReadOnlyCollection PlayerList => players.AsReadOnly(); private readonly List players = new(); @@ -154,7 +154,7 @@ namespace QwirkleClassLibrary.Games /// Returns the Board of the game /// /// Board - public Board? GetBoard() { return Board; } + public Board? GetBoard() { return board; } /// /// Returns the tile bag of the game @@ -168,8 +168,8 @@ namespace QwirkleClassLibrary.Games /// Board public Board CreateBoard() { - Board = new Board(15, 12); - return Board; + board = new Board(15, 12); + return board; } /// @@ -189,7 +189,7 @@ namespace QwirkleClassLibrary.Games public void StartGame() { if (players.Count < 2 || players.Count >= 5) return; - Board = CreateBoard(); + board = CreateBoard(); bag = CreateTileBag(3); GameRunning = true; } @@ -318,11 +318,11 @@ namespace QwirkleClassLibrary.Games /// bool public bool PlaceTile(Player player, Tile tile, int x, int y) { - if (!IsMoveCorrect(tile, x, y, Board!)) return false; - if (Board!.AddTileInCell(x, y, tile)) + if (!IsMoveCorrect(tile, x, y, board!)) return false; + if (board!.AddTileInCell(x, y, tile)) { OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "was correctly placed !")); - AddCellUsed(Board.GetCell(x, y)); + AddCellUsed(board.GetCell(x, y)); return player.RemoveTileToPlayer(tile); } @@ -730,12 +730,12 @@ namespace QwirkleClassLibrary.Games { 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 y = Board.ReadCells[b].GetY; + int x = board.ReadCells[b].GetX; + int y = board.ReadCells[b].GetY; - if (IsMoveCorrect(t, x, y, Board)) + if (IsMoveCorrect(t, x, y, board)) { return true; } @@ -773,7 +773,7 @@ namespace QwirkleClassLibrary.Games scoreBoard.Clear(); cellUsed.Clear(); bag = null; - Board = CreateBoard(); + board = CreateBoard(); GameRunning = false; } } diff --git a/Qwirkle/QwirkleClassLibrary/Tiles/Tile.cs b/Qwirkle/QwirkleClassLibrary/Tiles/Tile.cs index 70d1b2f..2a2efb9 100644 --- a/Qwirkle/QwirkleClassLibrary/Tiles/Tile.cs +++ b/Qwirkle/QwirkleClassLibrary/Tiles/Tile.cs @@ -2,14 +2,19 @@ using System.Collections.Generic; using System.Drawing; using System.Linq; +using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; namespace QwirkleClassLibrary.Tiles { + [DataContract] public class Tile { + [DataMember] private readonly Shape shape; + + [DataMember] private readonly Color color; /// diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index 80e3f59..4754c99 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -8,6 +8,8 @@ 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 static System.Console; @@ -209,7 +211,7 @@ static void MenuSwitch(Game game) enter = 3; break; case 3: - WriteLine("Your score on this turn : " + game.GetPlayerScore(game.GetPlayingPlayer(), game.CellsUsed, game.Board)); + WriteLine("Your score on this turn : " + game.GetPlayerScore(game.GetPlayingPlayer(), game.CellsUsed, game.GetBoard()!)); game.EmptyCellUsed(); game.DrawTiles(game.GetPlayingPlayer()); game.CheckGameOver(game.GetPlayingPlayer()); @@ -220,7 +222,7 @@ static void MenuSwitch(Game game) static void ShowBoard(Game game) { - Board board = game.Board; + Board board = game.GetBoard()!; for (int i = 0; i < board.Rows; i++) { @@ -318,7 +320,7 @@ static void MainGame() while (enter != 3) { - Console.ForegroundColor = ConsoleColor.DarkCyan; + ForegroundColor = ConsoleColor.DarkCyan; WriteLine("[1] Create game"); WriteLine("[2] Show leaderboard"); WriteLine("[3] Exit"); @@ -343,8 +345,7 @@ static void MainGame() { case 1: ForegroundColor = ConsoleColor.DarkYellow; - WriteLine("Enter minimun 2 player and max 4 player !"); - WriteLine(); + WriteLine("Enter minimun 2 player and max 4 player !\n"); ResetColor(); Game game = new Game(); AddPlayers(game); @@ -352,9 +353,11 @@ static void MainGame() MainMenu(game); leaderboard.AddScoreInLead(game.ScoreBoard); break; + case 2: ShowLeaderboard(leaderboard); break; + case 3: return; } @@ -362,5 +365,29 @@ static void MainGame() } } -MainGame(); +// MainGame(); + +var game = new Game(); +game.AddPlayerInGame(["Player1", "Player2", "Player3", "Player4"]); + +game.StartGame(); +game.SetNextPlayer(); + +game.PlaceTile(game.GetPlayingPlayer(), new Tile(Shape.Round, Color.Blue), 0, 0); +game.PlaceTile(game.GetPlayingPlayer(), new Tile(Shape.Square, Color.Blue), 0, 1); + +Directory.SetCurrentDirectory(Path.Combine(Directory.GetCurrentDirectory(), "..\\..\\..\\..\\Files")); + +var serializer = new DataContractJsonSerializer(typeof(Game)); +using (Stream s = File.Create("game.json")) +{ + serializer.WriteObject(s, game); +} + +Game game2; +using (Stream s = File.OpenRead("game.json")) +{ + game2 = (serializer.ReadObject(s) as Game)!; +} +WriteLine(game2.GetBoard()!.GetCell(0, 0)!.GetTile); \ No newline at end of file diff --git a/Qwirkle/QwirkleViews/App.xaml.cs b/Qwirkle/QwirkleViews/App.xaml.cs index 263106d..f5ce5e1 100644 --- a/Qwirkle/QwirkleViews/App.xaml.cs +++ b/Qwirkle/QwirkleViews/App.xaml.cs @@ -5,7 +5,7 @@ using QwirkleClassLibrary.Games; namespace Qwirkle { - public partial class App : Application + public partial class App { public App() { diff --git a/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs b/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs index 9a127a1..f933643 100644 --- a/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs +++ b/Qwirkle/QwirkleViews/Pages/GameBoard.xaml.cs @@ -12,10 +12,10 @@ namespace Qwirkle.Pages; public partial class Gameboard : ContentPage { - public ICommand OnDrop => new Command(onDrop); + public ICommand OnDrop => new Command(OnDropTile); - private Game game = ((App)App.Current!).Game; + private Game game = ((App)Application.Current!).Game; private Tile? tiledrag; public Gameboard() @@ -23,13 +23,13 @@ public partial class Gameboard : ContentPage InitializeComponent(); BindingContext = game; - DisplayAlert("List", ((App)App.Current!).Game.PlayerList[0].Tiles[0].ToString(), "chut"); + DisplayAlert("List", ((App)Application.Current!).Game.PlayerList[0].Tiles[0].ToString(), "chut"); } - public List PlayerCells1 { get; set; } = ((App)App.Current!).Game.PlayerList[0].Tiles.ToList(); + public List PlayerCells1 { get; set; } = ((App)Application.Current!).Game.PlayerList[0].Tiles.ToList(); - void OnDragOver(object sender, DragEventArgs e) + private void OnDragOver(object sender, DragEventArgs e) { foreach (var t in PlayerCells1) { @@ -42,14 +42,14 @@ public partial class Gameboard : ContentPage } - private void onDrop(Cell cell) + private void OnDropTile(Cell cell) { game.PlaceTileNotified += Game_PlaceTileNotified; int x = cell.GetX; int y = cell.GetY; - game.PlaceTile(game.GetPlayingPlayer(), tiledrag, x, y); + game.PlaceTile(game.GetPlayingPlayer(), tiledrag!, x, y); game.PlaceTileNotified -= Game_PlaceTileNotified;