public bool DestroyShip(int[] hitPosition, int[,] board, Models.Ship ship) { if (ship.IsHorizontal && hitPosition[0].Equals(ship.StartRow) && hitPosition[1] >= ship.StartColumn && hitPosition[1] <= (ship.Length - 1) + ship.StartColumn) { //check for already hit. if (board[hitPosition[0], hitPosition[1]] != 1) { board[hitPosition[0], hitPosition[1]] = 1; ship.HitCount++; return(true); } } else if (!ship.IsHorizontal && hitPosition[1].Equals(ship.StartColumn) && hitPosition[0] >= ship.StartRow && hitPosition[0] <= (ship.Length - 1) + ship.StartRow) { //check for already hit. if (board[hitPosition[0], hitPosition[1]] != 1) { board[hitPosition[0], hitPosition[1]] = 1; ship.HitCount++; return(true); } } return(false); }
public bool AddShips(int startRow, int startColumn, int length, int alignment, ref List <Models.Ship> shipList, Models.Board board) { var ship = new Models.Ship { StartRow = startRow, StartColumn = startColumn, Length = length, IsHorizontal = alignment == 0 }; if (!Board.CheckOverlap(shipList, ship, board.BoardDimension)) { return(false); } shipList.Add(ship); return(true); }
public bool CheckOverlap(List <Models.Ship> ships, Models.Ship ship, int[,] board) { var horizontalCondition = ship.IsHorizontal && ship.StartColumn + ship.Length - 1 <= board.GetLength(1); var verticalCondition = !ship.IsHorizontal && ship.StartRow + ship.Length - 1 <= board.GetLength(0); if (ships.Count > 0) { foreach (var s in ships) { if (horizontalCondition) { if (s.StartRow == ship.StartRow && s.StartColumn >= ship.StartColumn && s.StartColumn + s.Length - 1 <= ship.StartColumn) { return(false); } } else if (verticalCondition) { if (s.StartColumn == ship.StartColumn && s.StartRow >= ship.StartRow && s.StartRow + s.Length - 1 <= ship.StartRow) { return(false); } } else { return(true); } } } else { return(horizontalCondition || verticalCondition); } return(false); }