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.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
/// <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.
/// </summary>
[DataContract]
public class Board
{
public ReadOnlyCollection<Cell> ReadCells => cells.AsReadOnly();
[DataMember]
private readonly List<Cell> cells = new();
public int Rows { get; }

@ -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;
/// <summary>

@ -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;
}
}

@ -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<Player, int> ScoreBoard => scoreBoard.AsReadOnly();
private readonly Dictionary<Player, int> 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<Cell> GetCellsInBoard => new ObservableCollection<Cell>(Board!.GetCells());
public ObservableCollection<Cell> GetCellsInBoard => new ObservableCollection<Cell>(board!.GetCells());
public ReadOnlyCollection<Player> PlayerList => players.AsReadOnly();
private readonly List<Player> players = new();
@ -154,7 +154,7 @@ namespace QwirkleClassLibrary.Games
/// Returns the Board of the game
/// </summary>
/// <returns>Board</returns>
public Board? GetBoard() { return Board; }
public Board? GetBoard() { return board; }
/// <summary>
/// Returns the tile bag of the game
@ -168,8 +168,8 @@ namespace QwirkleClassLibrary.Games
/// <returns>Board</returns>
public Board CreateBoard()
{
Board = new Board(15, 12);
return Board;
board = new Board(15, 12);
return board;
}
/// <summary>
@ -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
/// <returns>bool</returns>
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;
}
}

@ -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;
/// <summary>

@ -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);

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

@ -12,10 +12,10 @@ namespace Qwirkle.Pages;
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;
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<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)
{
@ -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;

Loading…
Cancel
Save