using Gameboard.ShogiUI.Rules; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using ServiceTypes = Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; namespace Gameboard.ShogiUI.Sockets.Models { public class BoardState { // TODO: Create a custom 2D array implementation which removes the (x,y) or (y,x) ambiguity. public Piece?[,] Board { get; } public IReadOnlyCollection Player1Hand { get; } public IReadOnlyCollection Player2Hand { get; } /// /// Move is null in the first BoardState of a Session, before any moves have been made. /// public Move? Move { get; } public BoardState() : this(new ShogiBoard()) { } public BoardState(Piece?[,] board, IList player1Hand, ICollection player2Hand, Move move) { Board = board; Player1Hand = new ReadOnlyCollection(player1Hand); } public BoardState(ShogiBoard shogi) { Board = new Piece[9, 9]; for (var x = 0; x < 9; x++) for (var y = 0; y < 9; y++) { var piece = shogi.Board[x, y]; if (piece != null) { Board[x, y] = new Piece(piece); } } Player1Hand = shogi.Hands[WhichPlayer.Player1].Select(_ => new Piece(_)).ToList(); Player2Hand = shogi.Hands[WhichPlayer.Player2].Select(_ => new Piece(_)).ToList(); Move = new Move(shogi.MoveHistory[^1]); } public ServiceTypes.BoardState ToServiceModel() { var board = new ServiceTypes.Piece[9, 9]; for (var x = 0; x < 9; x++) for (var y = 0; y < 9; y++) { var piece = Board[x, y]; if (piece != null) { board[x, y] = piece.ToServiceModel(); } } return new ServiceTypes.BoardState { Board = board, Player1Hand = Player1Hand.Select(_ => _.ToServiceModel()).ToList(), Player2Hand = Player2Hand.Select(_ => _.ToServiceModel()).ToList() }; } } }