Created
June 14, 2017 16:49
-
-
Save tomaszbartoszewski/e25904feb5e06f1f1c8d933dbfc8edbd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var boardGenerator = new BoardGenerator(); | |
var board = boardGenerator.GetBoard(10, new[] {new ShipDetails(4, 1), new ShipDetails(3, 2), new ShipDetails(2, 3), new ShipDetails(1, 4)}); | |
PrintBoard(board); | |
Console.ReadLine(); | |
} | |
private static void PrintBoard(FieldValue[,] board) | |
{ | |
Console.Write(" "); | |
for (int h = 0; h < board.GetLength(0); h++) | |
Console.Write(h); | |
Console.WriteLine(); | |
for (int w = 0; w < board.GetLength(1); w++) | |
{ | |
Console.Write(w); | |
for (int h = 0; h < board.GetLength(0); h++) | |
{ | |
Console.Write(GetFieldValue(board[h, w])); | |
} | |
Console.WriteLine(); | |
} | |
} | |
private static string GetFieldValue(FieldValue fieldValue) | |
{ | |
switch (fieldValue) | |
{ | |
case FieldValue.Empty: | |
return "X"; | |
case FieldValue.Ship: | |
return "S"; | |
} | |
return string.Empty; | |
} | |
} | |
public class BoardGenerator | |
{ | |
private int boardSize; | |
public FieldValue[,] GetBoard(int boardSize, ShipDetails[] shipDetails) | |
{ | |
this.boardSize = boardSize; | |
var board = new FieldValue[boardSize, boardSize]; | |
for (var i = 0; i < boardSize; i++) | |
for (var j = 0; j < boardSize; j++) | |
board[i, j] = FieldValue.Empty; | |
var rnd = new Random(); | |
foreach (var shipDetail in shipDetails.OrderByDescending(s => s.Length)) | |
{ | |
var shipsToAdd = shipDetail.Number; | |
while (shipsToAdd > 0) | |
{ | |
var column = rnd.Next(0, boardSize); | |
var row = rnd.Next(0, boardSize); | |
var direction = (Direction)(rnd.Next(0, 1000000)%4); | |
FieldValue[,] newBoard; | |
if (TryAddShip(board, column, row, shipDetail.Length, direction, out newBoard)) | |
{ | |
shipsToAdd--; | |
board = newBoard; | |
} | |
} | |
} | |
return board; | |
} | |
private bool TryAddShip(FieldValue[,] board, int column, int row, int length, Direction direction, out FieldValue[,] newBoard) | |
{ | |
newBoard = new FieldValue[boardSize,boardSize]; | |
for (var i = 0; i < boardSize; i++) | |
for (var j = 0; j < boardSize; j++) | |
newBoard[i, j] = board[i, j]; | |
if (board[column, row] != FieldValue.Empty) | |
return false; | |
for (var i = 0; i < length; i++) | |
{ | |
var columnToCheck = GetColumnValue(column, i, direction); | |
var rowToCheck = GetRowValue(row, i, direction); | |
if (columnToCheck >= 0 && columnToCheck < boardSize | |
&& rowToCheck >= 0 && rowToCheck < boardSize) | |
{ | |
if (board[columnToCheck, rowToCheck] == FieldValue.Ship) | |
return false; | |
newBoard[columnToCheck, rowToCheck] = FieldValue.Ship; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
private int GetColumnValue(int column, int shift, Direction direction) | |
{ | |
switch (direction) | |
{ | |
case Direction.Left: | |
return column - shift; | |
case Direction.Right: | |
return column + shift; | |
default: | |
return column; | |
} | |
} | |
private int GetRowValue(int row, int shift, Direction direction) | |
{ | |
switch (direction) | |
{ | |
case Direction.Up: | |
return row - shift; | |
case Direction.Down: | |
return row + shift; | |
default: | |
return row; | |
} | |
} | |
} | |
public enum Direction | |
{ | |
Left = 0, | |
Right = 1, | |
Up = 2, | |
Down = 3 | |
} | |
public enum FieldValue | |
{ | |
Ship, | |
Empty | |
} | |
public struct ShipDetails | |
{ | |
public int Length; | |
public int Number; | |
public ShipDetails(int length, int number) | |
{ | |
Length = length; | |
Number = number; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment