Added a beggining of persistance (not working with anything for now)
continuous-integration/drone/push Build is passing Details

test_old_branch
Jules LASCRET 11 months ago
parent dc33ff6656
commit 0787add85f

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Runtime.Serialization;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -12,10 +13,12 @@ namespace QwirkleClassLibrary.Boards
/// <summary> /// <summary>
/// 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. /// 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.
/// </summary> /// </summary>
[DataContract]
public class Board public class Board
{ {
public ReadOnlyCollection<Cell> ReadCells => cells.AsReadOnly(); public ReadOnlyCollection<Cell> ReadCells => cells.AsReadOnly();
[DataMember]
private readonly List<Cell> cells = new(); private readonly List<Cell> cells = new();
public int Rows { get; } public int Rows { get; }

@ -1,13 +1,20 @@
// ReSharper disable All // ReSharper disable All
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using QwirkleClassLibrary.Tiles; using QwirkleClassLibrary.Tiles;
namespace QwirkleClassLibrary.Boards; namespace QwirkleClassLibrary.Boards;
[DataContract]
public class Cell public class Cell
{ {
[DataMember]
private readonly int x; private readonly int x;
[DataMember]
private readonly int y; private readonly int y;
[DataMember]
private Tile? tile = null; private Tile? tile = null;
/// <summary> /// <summary>

@ -2,6 +2,6 @@ namespace QwirkleClassLibrary.Events
{ {
public class SwapTilesNotifiedEventArgs(string reason) public class SwapTilesNotifiedEventArgs(string reason)
{ {
public string? Reason { get; private set; } public string Reason { get; private set; } = reason;
} }
} }

@ -8,6 +8,7 @@ using System.Xml.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Collections; using System.Collections;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Runtime.Serialization;
using QwirkleClassLibrary.Tiles; using QwirkleClassLibrary.Tiles;
using QwirkleClassLibrary.Boards; using QwirkleClassLibrary.Boards;
using QwirkleClassLibrary.Events; using QwirkleClassLibrary.Events;
@ -16,22 +17,21 @@ using QwirkleClassLibrary.Players;
namespace QwirkleClassLibrary.Games namespace QwirkleClassLibrary.Games
{ {
[DataContract]
public class Game : IPlayer, IRules public class Game : IPlayer, IRules
{ {
public ReadOnlyDictionary<Player, int> ScoreBoard => scoreBoard.AsReadOnly(); public ReadOnlyDictionary<Player, int> ScoreBoard => scoreBoard.AsReadOnly();
private readonly Dictionary<Player, int> scoreBoard = new(); private readonly Dictionary<Player, int> scoreBoard = new();
private TileBag? bag = null; private TileBag? bag = null;
[DataMember]
public bool GameRunning { get; private set; } public bool GameRunning { get; private set; }
private Board _board = new Board(15, 12); [DataMember]
public Board Board private Board board = new Board(15, 12);
{
get { return _board; }
private set { _board = value; }
}
public ObservableCollection<Cell> GetCellsInBoard => new ObservableCollection<Cell>(Board!.GetCells()); public ObservableCollection<Cell> GetCellsInBoard => new ObservableCollection<Cell>(board!.GetCells());
public ReadOnlyCollection<Player> PlayerList => players.AsReadOnly(); public ReadOnlyCollection<Player> PlayerList => players.AsReadOnly();
private readonly List<Player> players = new(); private readonly List<Player> players = new();
@ -154,7 +154,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() { return Board; } public Board? GetBoard() { return board; }
/// <summary> /// <summary>
/// Returns the tile bag of the game /// Returns the tile bag of the game
@ -168,8 +168,8 @@ namespace QwirkleClassLibrary.Games
/// <returns>Board</returns> /// <returns>Board</returns>
public Board CreateBoard() public Board CreateBoard()
{ {
Board = new Board(15, 12); board = new Board(15, 12);
return Board; return board;
} }
/// <summary> /// <summary>
@ -189,7 +189,7 @@ namespace QwirkleClassLibrary.Games
public void StartGame() public void StartGame()
{ {
if (players.Count < 2 || players.Count >= 5) return; if (players.Count < 2 || players.Count >= 5) return;
Board = CreateBoard(); board = CreateBoard();
bag = CreateTileBag(3); bag = CreateTileBag(3);
GameRunning = true; GameRunning = true;
} }
@ -318,11 +318,11 @@ namespace QwirkleClassLibrary.Games
/// <returns>bool</returns> /// <returns>bool</returns>
public bool PlaceTile(Player player, Tile tile, int x, int y) public bool PlaceTile(Player player, Tile tile, int x, int y)
{ {
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))
{ {
OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "was correctly placed !")); OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "was correctly placed !"));
AddCellUsed(Board.GetCell(x, y)); AddCellUsed(board.GetCell(x, y));
return player.RemoveTileToPlayer(tile); return player.RemoveTileToPlayer(tile);
} }
@ -730,12 +730,12 @@ 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;
if (IsMoveCorrect(t, x, y, Board)) if (IsMoveCorrect(t, x, y, board))
{ {
return true; return true;
} }
@ -773,7 +773,7 @@ namespace QwirkleClassLibrary.Games
scoreBoard.Clear(); scoreBoard.Clear();
cellUsed.Clear(); cellUsed.Clear();
bag = null; bag = null;
Board = CreateBoard(); board = CreateBoard();
GameRunning = false; GameRunning = false;
} }
} }

@ -2,14 +2,19 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Runtime.Serialization;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace QwirkleClassLibrary.Tiles namespace QwirkleClassLibrary.Tiles
{ {
[DataContract]
public class Tile public class Tile
{ {
[DataMember]
private readonly Shape shape; private readonly Shape shape;
[DataMember]
private readonly Color color; private readonly Color color;
/// <summary> /// <summary>

@ -8,6 +8,8 @@ using System.Collections.Immutable;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Diagnostics.Metrics; using System.Diagnostics.Metrics;
using System.Net.Quic; using System.Net.Quic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text; using System.Text;
using System.Transactions; using System.Transactions;
using static System.Console; using static System.Console;
@ -209,7 +211,7 @@ static void MenuSwitch(Game game)
enter = 3; enter = 3;
break; break;
case 3: 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.EmptyCellUsed();
game.DrawTiles(game.GetPlayingPlayer()); game.DrawTiles(game.GetPlayingPlayer());
game.CheckGameOver(game.GetPlayingPlayer()); game.CheckGameOver(game.GetPlayingPlayer());
@ -220,7 +222,7 @@ static void MenuSwitch(Game game)
static void ShowBoard(Game game) static void ShowBoard(Game game)
{ {
Board board = game.Board; Board board = game.GetBoard()!;
for (int i = 0; i < board.Rows; i++) for (int i = 0; i < board.Rows; i++)
{ {
@ -318,7 +320,7 @@ static void MainGame()
while (enter != 3) while (enter != 3)
{ {
Console.ForegroundColor = ConsoleColor.DarkCyan; ForegroundColor = ConsoleColor.DarkCyan;
WriteLine("[1] Create game"); WriteLine("[1] Create game");
WriteLine("[2] Show leaderboard"); WriteLine("[2] Show leaderboard");
WriteLine("[3] Exit"); WriteLine("[3] Exit");
@ -343,8 +345,7 @@ static void MainGame()
{ {
case 1: case 1:
ForegroundColor = ConsoleColor.DarkYellow; ForegroundColor = ConsoleColor.DarkYellow;
WriteLine("Enter minimun 2 player and max 4 player !"); WriteLine("Enter minimun 2 player and max 4 player !\n");
WriteLine();
ResetColor(); ResetColor();
Game game = new Game(); Game game = new Game();
AddPlayers(game); AddPlayers(game);
@ -352,9 +353,11 @@ static void MainGame()
MainMenu(game); MainMenu(game);
leaderboard.AddScoreInLead(game.ScoreBoard); leaderboard.AddScoreInLead(game.ScoreBoard);
break; break;
case 2: case 2:
ShowLeaderboard(leaderboard); ShowLeaderboard(leaderboard);
break; break;
case 3: case 3:
return; 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);

@ -5,7 +5,7 @@ using QwirkleClassLibrary.Games;
namespace Qwirkle namespace Qwirkle
{ {
public partial class App : Application public partial class App
{ {
public App() public App()
{ {

@ -12,10 +12,10 @@ namespace Qwirkle.Pages;
public partial class Gameboard : ContentPage public partial class Gameboard : ContentPage
{ {
public ICommand OnDrop => new Command<Cell>(onDrop); public ICommand OnDrop => new Command<Cell>(OnDropTile);
private Game game = ((App)App.Current!).Game; private Game game = ((App)Application.Current!).Game;
private Tile? tiledrag; private Tile? tiledrag;
public Gameboard() public Gameboard()
@ -23,13 +23,13 @@ public partial class Gameboard : ContentPage
InitializeComponent(); InitializeComponent();
BindingContext = game; 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<Tile> PlayerCells1 { get; set; } = ((App)App.Current!).Game.PlayerList[0].Tiles.ToList(); public List<Tile> 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) 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; game.PlaceTileNotified += Game_PlaceTileNotified;
int x = cell.GetX; int x = cell.GetX;
int y = cell.GetY; int y = cell.GetY;
game.PlaceTile(game.GetPlayingPlayer(), tiledrag, x, y); game.PlaceTile(game.GetPlayingPlayer(), tiledrag!, x, y);
game.PlaceTileNotified -= Game_PlaceTileNotified; game.PlaceTileNotified -= Game_PlaceTileNotified;

Loading…
Cancel
Save