public bool MoveFocusFigure(IBoardCell moveToCell) { if (!HasFocusFigure || moveToCell.Status != BoardCellStatus.AvailableForMove) { return(false); } ResetCellStatusToEmpty(_focusFigure.PlacedCell); ResetAvailableCellsToEmpty(); RemoveFigure(moveToCell); FigureController.MoveFigure(_focusFigure, moveToCell); if (!GameModel.GameDef.Validator.ValidateResult()) { Debug.LogWarning("Game complete"); OnCompleteGame?.Invoke(this, EventArgs.Empty); return(true); } var figure = _focusFigure; Reset(); FigurePostTurnLogicManager.ActivateFigurePostTurnLogic(figure); return(true); }
public int CountNeighboursOfCell(IBoard <ConwayCellState> board, IBoardCell <ConwayCellState> currentCell) { var count = 0; // Iterate the 8 cells around the current cell // ╔═══════════╦════════════╦══════════╗ // ║ 1 (-1,-1) ║ 2 (0,-1) ║ 3 (1,-1) ║ // ╠═══════════╬════════════╬══════════╣ // ║ 4 (-1,0) ║ Cell (0,0) ║ 5 (1,0) ║ // ╠═══════════╬════════════╬══════════╣ // ║ 6 (-1,1) ║ 7 (0,1) ║ 8 (1,1) ║ // ╚═══════════╩════════════╩══════════╝ for (var x = -1; x <= 1; x++) { for (var y = -1; y <= 1; y++) { if (x == 0 && y == 0) { continue; // Ignore our self... } var newX = currentCell.X + x; var newY = currentCell.Y + y; if (board.GetCell(newX, newY).IsAlive()) { count += 1; } } } return(count); }
private IEnumerable <IGameMoveTurnData> GetMoveTurnData(IBoardCell cell, IList <int> deltaIndexes) { var newIndexX = cell.IndexX; var newIndexY = cell.IndexY; IGameMoveTurnData lastMoveTurnData; do { newIndexX += deltaIndexes[0]; newIndexY += deltaIndexes[1]; lastMoveTurnData = CreateMoveTurnData(newIndexX, newIndexY); if (lastMoveTurnData.IsEmpty) { continue; } yield return(lastMoveTurnData); if (lastMoveTurnData.HasRemovedFigure) { break; } } while (!lastMoveTurnData.IsEmpty); }
public void MoveFigure(IFigureEntity figure, IBoardCell cell) { figure.PlacedCell?.Clear(); figure.PlacedCell = cell; cell.Figure = figure; OnMovedFigure?.Invoke(this, new MovedFigureEventArgs(figure, cell)); }
private IEnumerable <IGameMoveTurnData> CreateMoveTurnData(IBoardCell cell, IList <int> deltaIndexes) { var deltaIndexX = deltaIndexes[0]; var deltaIndexY = deltaIndexes[1]; var curIndexX = cell.IndexX; var curIndexY = cell.IndexY; void IncrementCurIndexes() { curIndexX += deltaIndexX; curIndexY += deltaIndexY; } var rangeTurnCount = 0; do { IncrementCurIndexes(); var removedFigure = default(IFigureEntity); var moveToCell = BoardModel.GetCell(curIndexX, curIndexY); if (moveToCell == null || moveToCell.Status == BoardCellStatus.AvailableForMove) { yield break; } if (moveToCell.IsBusy) { var figure = moveToCell.Figure; if (figure.Team == GameModel.CurTeamTurn) { yield break; } //если клетка занята вражеской фигурой var nextCell = BoardModel.GetCell(curIndexX + deltaIndexX, curIndexY + deltaIndexY); if (nextCell != null && !nextCell.IsBusy) { moveToCell = nextCell; removedFigure = figure; rangeTurnCount += _rangeTurn; IncrementCurIndexes(); } else { yield break; } } moveToCell.Status = BoardCellStatus.AvailableForMove; yield return(new GameMoveTurnData(moveToCell, removedFigure)); } while (++rangeTurnCount < _rangeTurn); }
private void RemoveFigure(IBoardCell moveToCell) { var removedFigure = GameMoveTurnManager.GetRemovedFigure(moveToCell); if (removedFigure != null) { FigureController.RemoveFigure(removedFigure); } }
public void _board_cell_null_exception_thrown() { //Arrange ICreateBoard createBoard = new CreateNewBoard(); var board = createBoard.CreateBoard(); IBoardCell cellToAttack = null; //Act and Assert Assert.Throws <BoardCellNullException>(() => _attack.AttackShip(board, cellToAttack)); }
private void OnClickCellView(object sender, IBoardCell cell) { if (cell.Status == BoardCellStatus.AvailableForMove) { GameMaster.MoveFocusFigure(cell); } else if (!GameMaster.IsBlockFocused && cell.IsBusy) { GameMaster.SetAndActivateFocusFigure(cell.Figure); } }
/// Perform a specified attack on the board public AttackResult Attack(IBoard board, IBoardCell cellToAttack) { if (board == null) { throw new BoardNullException("The board is null and the attack cannot be completed."); } if (cellToAttack == null || cellToAttack.RowCoordinate <= 0 || cellToAttack.ColumnCoordinate <= 0) { throw new BoardCellNullException("The board is null and the attack cannot be completed."); } return(_attack.AttackShip(board, cellToAttack)); }
/// <summary> /// Perform a specified attack on the board. /// </summary> /// <param name="board">Current Board.</param> /// <param name="cellToAttack">Cell information that needs to be attacked.</param> /// <returns>Returns the output of an attack.</returns> public AttackResult Attack(IBoard board, IBoardCell cellToAttack) { if (board == null) { _logger.LogError("The board is null."); throw new ArgumentNullException("The board is null and the attack cannot be completed."); } if (cellToAttack == null || cellToAttack.XCoordinate <= 0 || cellToAttack.YCoordinate <= 0) { _logger.LogError("The attack cell is not provided."); throw new ArgumentNullException("The board is null and the attack cannot be completed."); } return(_attacker.AttackShip(board, cellToAttack)); }
public void SetUp() { var cells = new IBoardCell[, ] { { new BoardCell(BoardCellCol.ColA, BoardCellRow.Row1, 0, 0), new BoardCell(BoardCellCol.ColB, BoardCellRow.Row1, 1, 0) }, { new BoardCell(BoardCellCol.ColC, BoardCellRow.Row2, 0, 1), new BoardCell(BoardCellCol.ColD, BoardCellRow.Row2, 1, 1) } }; BoardModel = new BoardModel(); BoardModel.SetCells(cells); }
public void CreateCells(IBoardModel boardModel) { var cells = new IBoardCell[MaxColCells, MaxRowCells]; for (var i = 0; i < MaxRowCells; i++) { for (var j = 0; j < MaxColCells; j++) { var col = Cols[j]; var row = Rows[i]; var cell = new BoardCell(col, row, i, j); cells[i, j] = cell; } } boardModel.SetCells(cells); }
public void SetUp() { BoardModel = new BoardModel(); var boardCreator = new BoardCreator(); boardCreator.CreateCells(BoardModel); GameModel = Substitute.For <IGameModel>(); GameModel.CurTeamTurn.Returns(Team); //create figure and cell _mainFigure = Substitute.For <IFigureEntity>(); _mainFigure.Team.Returns(Team); _mainCell = BoardModel.GetCell(CellCol, CellRow); _mainFigure.PlacedCell.Returns(_mainCell); }
private IList <IGameMoveTurnData> CreateMoveTurnData(IBoardCell cell) { var moveTurnData = new List <IGameMoveTurnData>(); var team = cell.Figure.Team; if (_canMoveAllDirection || team == GameTeam.White) { moveTurnData.AddRange(CreateMoveTurnData(cell, DeltaIndexesRightTop)); moveTurnData.AddRange(CreateMoveTurnData(cell, DeltaIndexesLeftTop)); } if (_canMoveAllDirection || team == GameTeam.Black) { moveTurnData.AddRange(CreateMoveTurnData(cell, DeltaIndexesRightDown)); moveTurnData.AddRange(CreateMoveTurnData(cell, DeltaIndexesLeftDown)); } return(moveTurnData); }
public IEnumerable <IBoardCell> ListOfCellsAffected(IShip ship, IBoardCell startingCell, IBoard board) { switch (ship.Orientation) { case Enumerations.OrientationType.Vertical: int numberOfCells = startingCell.XCoordinate + ship.Width; return(board.BoardCells.Where(x => x.XCoordinate >= startingCell.XCoordinate && x.YCoordinate >= startingCell.YCoordinate && x.XCoordinate < numberOfCells && x.YCoordinate <= startingCell.YCoordinate)); case Enumerations.OrientationType.Horizontal: numberOfCells = startingCell.YCoordinate + ship.Width; return(board.BoardCells.Where(x => x.XCoordinate >= startingCell.XCoordinate && x.YCoordinate >= startingCell.YCoordinate && x.XCoordinate <= startingCell.XCoordinate && x.YCoordinate < numberOfCells)); } return(null); }
public IList <IBoardCell> ListOfCellsAffected(IBattleship ship, IBoardCell startingCell, IBoard board) { switch (ship.Orientation) { case OrientationType.Vertical: int numberOfCells = startingCell.RowCoordinate + ship.Width; return(board.BoardCells.Where(x => x.RowCoordinate >= startingCell.RowCoordinate && x.ColumnCoordinate >= startingCell.ColumnCoordinate && x.RowCoordinate < numberOfCells && x.ColumnCoordinate <= startingCell.ColumnCoordinate).ToList()); case OrientationType.Horizontal: numberOfCells = startingCell.ColumnCoordinate + ship.Width; return(board.BoardCells.Where(x => x.RowCoordinate >= startingCell.RowCoordinate && x.ColumnCoordinate >= startingCell.ColumnCoordinate && x.RowCoordinate <= startingCell.RowCoordinate && x.ColumnCoordinate < numberOfCells).ToList()); } return(null); }
protected IEnumerable <IGameMoveTurnData> CreateMoveTurnData(IBoardCell cell, ICollection <int> deltaIndexes1, ICollection <int> deltaIndexes2) { var curIndexX = cell.IndexX; var curIndexY = cell.IndexY; foreach (var deltaIndexX in deltaIndexes1) { foreach (var deltaIndexY in deltaIndexes2) { var newIndexX = curIndexX + deltaIndexX; var newIndexY = curIndexY + deltaIndexY; var data = CreateMoveTurnData(newIndexX, newIndexY); if (!data.IsEmpty) { yield return(data); } } } }
/// <summary> /// Perform an attack on the supplied board and return the outcome. /// </summary> /// <param name="board">The current board on which the attack will be performed.</param> /// <param name="cellToAttack">The board position on which the attack will be performed.</param> /// <returns>Outcome of the attack. Whether it is a Hit or a Miss.</returns> public AttackResult AttackShip(IBoard board, IBoardCell cellToAttack) { if (board == null) { throw new BoardNullException("The board is null and the attack cannot be completed."); } if (cellToAttack == null || cellToAttack.RowCoordinate <= 0 || cellToAttack.ColumnCoordinate <= 0) { throw new BoardCellNullException("The board cell is null and the attack cannot be completed."); } var attackedCell = board.BoardCells.Where(x => x.RowCoordinate == cellToAttack.RowCoordinate && x.ColumnCoordinate == cellToAttack.ColumnCoordinate).FirstOrDefault(); if (attackedCell != null && attackedCell.IsOccupied) { attackedCell.IsHit = true; return(AttackResult.Hit); } return(AttackResult.Miss); }
private bool CheckCanMove(IBoardCell moveToCell) { if (moveToCell == null) { return(false); } if (moveToCell.IsBusy) { if (moveToCell.Figure.Team == GameModel.CurTeamTurn) { return(false); } } else if (moveToCell.Status == BoardCellStatus.AvailableForMove) { return(false); } return(true); }
/// <summary> /// Perform an attack on the supplied board and return the outcome. /// </summary> /// <param name="board">The current board on which the attack will be performed.</param> /// <param name="cellToAttack">The board position on which the attack will be performed.</param> /// <returns>Outcome of the attack. Whether it is a Hit or a Miss.</returns> public AttackResult AttackShip(IBoard board, IBoardCell cellToAttack) { if (board == null) { _logger.LogError("The board is null."); throw new ArgumentNullException("The board is null and the attack cannot be completed."); } if (cellToAttack == null || cellToAttack.XCoordinate <= 0 || cellToAttack.YCoordinate <= 0) { _logger.LogError("The attack cell is not provided."); throw new ArgumentNullException("The board is null and the attack cannot be completed."); } var attackedCell = board.BoardCells.Where(x => x.XCoordinate == cellToAttack.XCoordinate && x.YCoordinate == cellToAttack.YCoordinate).FirstOrDefault(); if (attackedCell != null && attackedCell.Occupied) { attackedCell.IsHit = true; return(AttackResult.Hit); } return(AttackResult.Miss); }
/// Add a battleship to the board at a specified position. public bool AddBattleship(IBoard board, IBoardCell startCell, IBattleship ship) { if (board == null || startCell == null || ship == null) { return(false); } var startingCell = board.BoardCells.Where(x => x.RowCoordinate == startCell.RowCoordinate && x.ColumnCoordinate == startCell.ColumnCoordinate).FirstOrDefault(); if (startingCell == null) { return(false); } if (_placeBattleship.CanShipBePlaced(ship, startCell, board)) { _placeBattleship.ListOfCellsAffected(ship, startCell, board).ToList().ForEach(x => x.IsOccupied = true); return(true); } return(false); }
public bool CanShipBePlaced(IBattleship ship, IBoardCell startingCell, IBoard board) { //If the ship width is less than one then an exception is thrown if (ship.Width < 1) { throw new ShipWidthCannotBeLessThanOneException("The ship width must be greater than 0"); } if (ship.Orientation == OrientationType.Vertical && (ship.Width + startingCell.RowCoordinate) > board.BoardCells.Max(x => x.RowCoordinate)) { return(false); } if (ship.Orientation == OrientationType.Horizontal && (ship.Width + startingCell.ColumnCoordinate) > board.BoardCells.Max(x => x.ColumnCoordinate)) { return(false); } var listOfCellsAffected = ListOfCellsAffected(ship, startingCell, board); return(!listOfCellsAffected.Any(x => x.IsOccupied)); }
public bool ValidateShipCanBePlaced(IShip ship, IBoardCell startingCell, IBoard board) { if (ship.Width <= 0) { _logger.LogError("The ship's width cannot be less than 1."); return(false); } if (ship.Orientation == Enumerations.OrientationType.Vertical && (ship.Width + startingCell.XCoordinate) > board.BoardCells.Max(x => x.XCoordinate)) { return(false); } if (ship.Orientation == Enumerations.OrientationType.Horizontal && (ship.Width + startingCell.YCoordinate) > board.BoardCells.Max(x => x.YCoordinate)) { return(false); } var listOfCellsAffected = ListOfCellsAffected(ship, startingCell, board); return(!listOfCellsAffected.Any(x => x.Occupied)); }
public void SetUp() { var boardModel = Substitute.For <IBoardModel>(); GameModel = Substitute.For <IGameModel>(); var figureModel = Substitute.For <IFigureModel>(); var figurePostTurnLogicManager = Substitute.For <IFigurePostTurnLogicManager>(); var gameMoveTurnManager = Substitute.For <IGameMoveTurnManager>(); var figureController = new FigureController(figureModel); GameMaster = new GameMaster(figureController, boardModel, GameModel, figurePostTurnLogicManager, gameMoveTurnManager); _figure = new FigureEntity(Substitute.For <IFigureDef>(), GameTeam.White); _cell = new BoardCell(BoardCellCol.ColA, BoardCellRow.Row1, 0, 0) { Figure = _figure }; _moveToCell = new BoardCell(BoardCellCol.ColA, BoardCellRow.Row2, 0, 0) { Status = BoardCellStatus.AvailableForMove }; }
/// <summary> /// Add a battleship to the board at a specified position. /// </summary> /// <param name="board">Current board.</param> /// <param name="startCell">Starting position where the battleship has to be placed.</param> /// <param name="ship">The battleship that needs to be placed.</param> /// <returns></returns> public bool AddBattleship(IBoard board, IBoardCell startCell, IShip ship) { if (board == null || startCell == null || ship == null) { _logger.LogError("The required values to place the battleship are null."); return(false); } var startingCell = board.BoardCells.Where(x => x.XCoordinate == startCell.XCoordinate && x.YCoordinate == startCell.YCoordinate).FirstOrDefault(); if (startingCell == null) { _logger.LogError("The starting coordinates doesn't seem to be on the board."); return(false); } if (_validator.ValidateShipCanBePlaced(ship, startCell, board)) { _validator.ListOfCellsAffected(ship, startCell, board).ToList().ForEach(x => x.Occupied = true); return(true); } return(false); }
private void SetPosition(IFigureView figureView, IBoardCell cell) { var cellView = BoardViewModel.GetView(cell); figureView.Position = cellView.Position; }
public PageControlViewModel(IBoardCell boardCell) { _boardCell = boardCell; RegisterBoardCellEvent(); }
public MovedFigureEventArgs(IFigureEntity figureEntity, IBoardCell boardCell) { Figure = figureEntity.CheckNull(); Cell = boardCell.CheckNull(); }
public IFigureEntity GetRemovedFigure(IBoardCell moveToCell) { MoveTurnData.TryGetValue(moveToCell, out var data); return(data?.RemovedFigure); }
public GameMoveTurnData(IBoardCell moveToCell, IFigureEntity removedFigure) { MoveToCell = moveToCell; RemovedFigure = removedFigure; }