using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using QwirkleClassLibrary.Tiles;
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 : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
[DataMember]
private ObservableCollection cells = new ObservableCollection();
public ObservableCollection ReadCells
{
get { return cells; }
set
{
cells = value;
OnPropertyChanged(nameof(ReadCells));
}
}
public int Rows { get; }
public int Columns { get; }
///
/// This is the constructor for the board. The parameters 'rows' and 'cols' are used to calculate the size of the board.
///
/// The numbers of rows in the board.
/// The number of columns in the board.
public Board(int cols, int rows)
{
Rows = rows;
Columns = cols;
for (int a = 0; a < Rows; a++)
{
for (int b = 0; b < Columns; b++)
{
Cell localcell = new(a, b);
cells.Add(localcell);
}
}
}
///
/// This method is used to check if a cell in the board whether it already contains a tile or not.
///
/// Returns a boolean : true if the cell doesn't contain any tile, false if it already contains a tile.
public bool HasOccupiedCase()
{
foreach (var cell in cells)
{
if (!cell.IsFree)
{
OnPropertyChanged(nameof(ReadCells));
return true;
}
}
return false;
}
///
/// This method is used to add a tile specified by the player, to the coordinates x, y of the board, also provided by the player.
///
/// The position of the tile in the x-axis of the board.
/// The position of the tile in the y-axis of the board.
/// The tile the player want to place in the board.
///
public bool AddTileInCell(int x, int y, Tile tile)
{
foreach (var t in cells)
{
if (t.GetX != x || t.GetY != y) continue;
if (t.IsFree)
{
OnPropertyChanged(nameof(ReadCells));
return t.SetTile(tile);
}
}
OnPropertyChanged(nameof(ReadCells));
return false;
}
///
/// A getter for the cells attribute of the Board Class.
///
/// The cells of the board in a List format.
public List GetCells()
{
return cells.ToList();
}
///
/// A getter for the cell in the coordinates x, y of the board.
///
/// The position of the cell in the x-axis of the board.
/// The position of the cell in the y-axis of the board.
///
public Cell? GetCell(int x, int y)
{
foreach (var t in cells)
{
if (t.GetX == x && t.GetY == y)
{
return t;
}
}
return null;
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
| | | |