push
continuous-integration/drone/push Build is passing Details

test_old_branch
Jérémy Mouyon 11 months ago
parent 566f6c2e09
commit bb50f74158

@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
@ -14,12 +16,21 @@ 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. /// 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] [DataContract]
public class Board public class Board : INotifyPropertyChanged
{ {
public ReadOnlyCollection<Cell> ReadCells => cells.AsReadOnly(); public event PropertyChangedEventHandler? PropertyChanged;
[DataMember] [DataMember]
private readonly List<Cell> cells = new(); private ObservableCollection<Cell> cells = new ObservableCollection<Cell>();
public ObservableCollection<Cell> ReadCells
{
get { return cells; }
set
{
cells = value;
OnPropertyChanged(nameof(ReadCells));
}
}
public int Rows { get; } public int Rows { get; }
public int Columns { get; } public int Columns { get; }
@ -54,6 +65,7 @@ namespace QwirkleClassLibrary.Boards
{ {
if (!cell.IsFree) if (!cell.IsFree)
{ {
OnPropertyChanged(nameof(ReadCells));
return true; return true;
} }
} }
@ -74,9 +86,11 @@ namespace QwirkleClassLibrary.Boards
if (t.GetX != x || t.GetY != y) continue; if (t.GetX != x || t.GetY != y) continue;
if (t.IsFree) if (t.IsFree)
{ {
OnPropertyChanged(nameof(ReadCells));
return t.SetTile(tile); return t.SetTile(tile);
} }
} }
OnPropertyChanged(nameof(ReadCells));
return false; return false;
} }
@ -87,16 +101,7 @@ namespace QwirkleClassLibrary.Boards
/// <returns>The cells of the board in a List format.</returns> /// <returns>The cells of the board in a List format.</returns>
public List<Cell> GetCells() public List<Cell> GetCells()
{ {
return cells; return cells.ToList();
}
/// <summary>
/// A getter for the ReadCells attribute of the Board Class.
/// </summary>
/// <returns>The cells of the board, but in a IReadOnlyCollection format.</returns>
public IReadOnlyCollection<Cell> GetReadCells()
{
return ReadCells;
} }
/// <summary> /// <summary>
@ -117,5 +122,9 @@ namespace QwirkleClassLibrary.Boards
return null; return null;
} }
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
} }
} }

@ -1,4 +1,5 @@
// ReSharper disable All // ReSharper disable All
using System.ComponentModel;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using QwirkleClassLibrary.Tiles; using QwirkleClassLibrary.Tiles;
@ -6,8 +7,10 @@ using QwirkleClassLibrary.Tiles;
namespace QwirkleClassLibrary.Boards; namespace QwirkleClassLibrary.Boards;
[DataContract] [DataContract]
public class Cell public class Cell : INotifyPropertyChanged
{ {
public event PropertyChangedEventHandler? PropertyChanged;
[DataMember] [DataMember]
private readonly int x; private readonly int x;
@ -15,7 +18,7 @@ public class Cell
private readonly int y; private readonly int y;
[DataMember] [DataMember]
private Tile? tile = null; public Tile? Tile { get; private set;}
/// <summary> /// <summary>
/// This is the constructor for a Cell. /// This is the constructor for a Cell.
@ -58,17 +61,9 @@ public class Cell
/// <returns>True if the cell is empty, false if the cell contains a tile.</returns> /// <returns>True if the cell is empty, false if the cell contains a tile.</returns>
public bool IsFree public bool IsFree
{ {
get { return tile == null; } get { return Tile == null; }
} }
/// <summary>
/// A getter for the tile in the cell.
/// </summary>
/// <returns>The tile if the cell isn't empty, null otherwise.</returns>
public Tile? GetTile
{
get { return tile; }
}
/// <summary> /// <summary>
/// A setter for the tile in the cell. /// A setter for the tile in the cell.
@ -77,9 +72,11 @@ public class Cell
/// <returns>True if added succefully (if the cell didn't already contain a tile), false if there already was a tile in this cell.</returns> /// <returns>True if added succefully (if the cell didn't already contain a tile), false if there already was a tile in this cell.</returns>
public bool SetTile(Tile addedTile) public bool SetTile(Tile addedTile)
{ {
if (tile == null) if (Tile == null)
{ {
tile = addedTile;
Tile = addedTile;
OnPropertyChanged(nameof(Tile));
return true; return true;
} }
else else
@ -87,4 +84,9 @@ public class Cell
return false; return false;
} }
} }
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
} }

@ -32,8 +32,6 @@ namespace QwirkleClassLibrary.Games
private Board board = new Board(15, 12); private Board board = new Board(15, 12);
public Board Board => board; public Board Board => board;
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();
@ -411,18 +409,18 @@ namespace QwirkleClassLibrary.Games
{ {
var extendedCell = b.GetCell(x + i * dx, y + i * dy); var extendedCell = b.GetCell(x + i * dx, y + i * dy);
if (extendedCell?.GetTile == null) if (extendedCell?.Tile == null)
{ {
break; break;
} }
if (extendedCell.GetTile.GetColor != tile.GetColor && extendedCell.GetTile.GetShape != tile.GetShape) if (extendedCell.Tile.GetColor != tile.GetColor && extendedCell.Tile.GetShape != tile.GetShape)
{ {
OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, " : Color / Shape does not match with the surrounding tiles !")); OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, " : Color / Shape does not match with the surrounding tiles !"));
return false; return false;
} }
if (extendedCell.GetTile.GetColor == tile.GetColor && extendedCell.GetTile.GetShape == tile.GetShape) if (extendedCell.Tile.GetColor == tile.GetColor && extendedCell.Tile.GetShape == tile.GetShape)
{ {
OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, " : Tile already placed on the same line / column !")); OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, " : Tile already placed on the same line / column !"));
return false; return false;
@ -496,7 +494,7 @@ namespace QwirkleClassLibrary.Games
return true; return true;
} }
if (b.GetCell(x, y)!.GetTile == null) if (b.GetCell(x, y)!.Tile == null)
{ {
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, ": Cell already used !")); OnPlaceTile(new PlaceTileNotifiedEventArgs(t, ": Cell already used !"));
} }
@ -511,18 +509,18 @@ namespace QwirkleClassLibrary.Games
foreach (var cell in surroundingCells) foreach (var cell in surroundingCells)
{ {
if (cell?.GetTile == null) if (cell?.Tile == null)
{ {
continue; continue;
} }
if (cell.GetTile.GetColor != t.GetColor && cell.GetTile.GetShape != t.GetShape) if (cell.Tile.GetColor != t.GetColor && cell.Tile.GetShape != t.GetShape)
{ {
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : Colors / Shapes do not match with the surrounding tiles !")); OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : Colors / Shapes do not match with the surrounding tiles !"));
return false; return false;
} }
if (cell.GetTile.GetColor == t.GetColor && cell.GetTile.GetShape == t.GetShape) if (cell.Tile.GetColor == t.GetColor && cell.Tile.GetShape == t.GetShape)
{ {
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " is already placed on the same line / column !")); OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " is already placed on the same line / column !"));
@ -545,7 +543,7 @@ namespace QwirkleClassLibrary.Games
return false; return false;
} }
if (surroundingCells.All(cell => cell?.GetTile == null)) if (surroundingCells.All(cell => cell?.Tile == null))
{ {
OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : You can't place a tile that isn't adjacent to another one !")); OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : You can't place a tile that isn't adjacent to another one !"));
return false; return false;
@ -641,7 +639,7 @@ namespace QwirkleClassLibrary.Games
foreach (var adjacentCell in surroundingCells) foreach (var adjacentCell in surroundingCells)
{ {
if (adjacentCell?.GetTile == null || cellsPlayed.Contains(adjacentCell) || checkedSurroundingCells.Contains(adjacentCell)) if (adjacentCell?.Tile == null || cellsPlayed.Contains(adjacentCell) || checkedSurroundingCells.Contains(adjacentCell))
{ {
continue; continue;
} }
@ -675,7 +673,7 @@ namespace QwirkleClassLibrary.Games
{ {
var extendedCell = b.GetCell(cell.GetX + i * direction.Item1, cell.GetY + i * direction.Item2); var extendedCell = b.GetCell(cell.GetX + i * direction.Item1, cell.GetY + i * direction.Item2);
if (extendedCell?.GetTile == null || cellsPlayed.Contains(extendedCell)) if (extendedCell?.Tile == null || cellsPlayed.Contains(extendedCell))
{ {
break; break;
} }

@ -7,9 +7,10 @@ using System.Collections.ObjectModel;
using System.Windows.Input; using System.Windows.Input;
using System.ComponentModel; using System.ComponentModel;
using Cell = QwirkleClassLibrary.Boards.Cell; using Cell = QwirkleClassLibrary.Boards.Cell;
namespace Qwirkle.Pages; namespace Qwirkle.Pages;
public partial class Gameboard : ContentPage, INotifyPropertyChanged public partial class Gameboard : ContentPage
{ {
public ICommand OnDrop => new Command<Cell>(OnDropTile); public ICommand OnDrop => new Command<Cell>(OnDropTile);
@ -17,31 +18,15 @@ public partial class Gameboard : ContentPage, INotifyPropertyChanged
private Tile? tiledrag; private Tile? tiledrag;
private ObservableCollection<Tile> playerCells1;
public ObservableCollection<Tile> PlayerCells1
{
get => playerCells1;
set
{
playerCells1 = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PlayerCells1"));
}
}
public Gameboard() public Gameboard()
{ {
InitializeComponent(); InitializeComponent();
BindingContext = game; BindingContext = game;
PlayerCells1 = new ObservableCollection<Tile>(game.PlayerList[0].Tiles.ToList());
DisplayAlert("List", playerCells1[0].ToString(), "chut");
} }
private void OnDragOver(object sender, DragEventArgs e) private void OnDragOver(object sender, DragEventArgs e)
{ {
foreach (var t in PlayerCells1) foreach (var t in game.PlayerList[game.GetPlayingPlayerPosition()].Tiles)
{ {
if (e.Data.Text == t.ToString()) if (e.Data.Text == t.ToString())
{ {
@ -62,14 +47,10 @@ public partial class Gameboard : ContentPage, INotifyPropertyChanged
game.PlaceTile(game.GetPlayingPlayer(), tiledrag!, x, y); game.PlaceTile(game.GetPlayingPlayer(), tiledrag!, x, y);
game.PlaceTileNotified -= Game_PlaceTileNotified; game.PlaceTileNotified -= Game_PlaceTileNotified;
PlayerCells1 = new ObservableCollection<Tile>(game.PlayerList[0].Tiles.ToList());
} }
private void Game_PlaceTileNotified(object? sender, PlaceTileNotifiedEventArgs args) private void Game_PlaceTileNotified(object? sender, PlaceTileNotifiedEventArgs args)
{ {
DisplayAlert("Tile place notified", args.Reason, "<3"); DisplayAlert("Tile place notified", args.Reason, "<3");
} }
public event PropertyChangedEventHandler? PropertyChanged;
} }

@ -9,7 +9,7 @@
<StackLayout> <StackLayout>
<CollectionView ItemsSource="{Binding GetCellsInBoard}" <CollectionView ItemsSource="{Binding Board.ReadCells}"
HorizontalOptions="Center" HorizontalOptions="Center"
VerticalOptions="Center"> VerticalOptions="Center">
<CollectionView.ItemsLayout> <CollectionView.ItemsLayout>
@ -26,13 +26,13 @@
DropCommand="{Binding OnDrop, Source={x:Reference root}}" DropCommand="{Binding OnDrop, Source={x:Reference root}}"
DropCommandParameter="{Binding .}" /> DropCommandParameter="{Binding .}" />
</Border.GestureRecognizers> </Border.GestureRecognizers>
<Label Text="{Binding GetTile.Name}" /> <Label Text="{Binding tile}" />
</Border> </Border>
</DataTemplate> </DataTemplate>
</CollectionView.ItemTemplate> </CollectionView.ItemTemplate>
</CollectionView> </CollectionView>
<CollectionView ItemsSource="{Binding PlayerCells1, Source={x:Reference root}}" <CollectionView ItemsSource="{Binding PlayerList[0].Tiles}"
HorizontalOptions="Center" HorizontalOptions="Center"
VerticalOptions="Start" > VerticalOptions="Start" >
<CollectionView.ItemsLayout> <CollectionView.ItemsLayout>
@ -51,8 +51,34 @@
</Border> </Border>
</DataTemplate> </DataTemplate>
</CollectionView.ItemTemplate> </CollectionView.ItemTemplate>
</CollectionView>
<CollectionView ItemsSource="{Binding PlayerList[1].Tiles}"
HorizontalOptions="End"
VerticalOptions="End" >
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Border WidthRequest="70" HeightRequest="70"
BackgroundColor="WhiteSmoke"
Margin="0">
<Label Text="{Binding}">
<Label.GestureRecognizers>
<DragGestureRecognizer />
</Label.GestureRecognizers>
</Label>
</Border>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView> </CollectionView>
</StackLayout> </StackLayout>
</ScrollView> </ScrollView>

@ -66,25 +66,6 @@ public class TestBoard
Assert.Equal((12 * 12), board.GetCells().Count); Assert.Equal((12 * 12), board.GetCells().Count);
} }
[Fact]
public void Test_GetReadCells()
{
Board board = new Board(12, 12);
List<Cell> cells = new List<Cell>();
for (int a = 0; a < 12; a++)
{
for (int b = 0; b < 12; b++)
{
Cell localcell = new(a, b);
cells.Add(localcell);
}
}
ReadOnlyCollection<Cell> readCells = cells.AsReadOnly();
Assert.Equal(readCells.Count, board.GetReadCells().Count);
}
[Theory] [Theory]
[InlineData(true)] [InlineData(true)]

Loading…
Cancel
Save