I have been trying to randomly arrange ships for the computer but it does not work when I click on the random button it just colors the entire grid. This is the random button click
private void Button_Click_1(object sender, RoutedEventArgs e)
{
int[,] enemyArrayPlace = new int[row, col];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (enemyArrayPlace[i,j] == 0)
{
enemyShips.RandomShip(gridEnemy);
gridEnemy.Background = Brushes.Red;
}
else
{
gridEnemy.Background = Brushes.Blue;
}
}
}
}
and this is the EnemyShips class where I do the placement although I do not know how to write the method to randomly place them 4,3,2 in a row so they don't overlap and are all one cell away from another. Also I do not know how to pass this classes contents to the random button which randomizes the ships and places them onto the grid
class EnemyShips
{
Random rand = new Random();
public int fourman = 4;
public int threeman = 3;
public int twoman = 2;
public int oneman = 1;
private readonly int row = 10;
private readonly int col = 10;
public int[,] ship;
public int[,] enemyShipArray;
int x;
int y;
public int[,] RandomShip(Grid grid)
{
enemyShipArray = new int[row, col];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
for (int a = 0; a < 1; a++)
{
ship = enemyShipArray;
if (x + 4 <= 10 || x + 4 <= 1 || y + 4 <= 10 || y - 4 >= 1)
{
x = rand.Next(0, 10);
y = rand.Next(0, 10);
fourman = enemyShipArray[x,y];
enemyShipArray[x, y] = 1;
return enemyShipArray;
}
for (int b = 0; b < 2; b++)
{
if (x + 3 <= 10 || x + 3 <= 1 || y + 3 <= 10 || y - 3 >= 1)
{
x = rand.Next(0, 10);
y = rand.Next(0, 10);
threeman = enemyShipArray[x,y];
enemyShipArray[x, y] = 1;
return enemyShipArray;
}
}
for (int c = 0; c < 3; c++)
{
if (x + 2 <= 10 || x + 2 <= 1 || y + 2 <= 10 || y - 2 >= 1)
{
x = rand.Next(0, 10);
y = rand.Next(0, 10);
twoman = enemyShipArray[x,y];
enemyShipArray[x, y] = 1;
return enemyShipArray;
}
}
for (int d = 0; d < 4; d++)
{
if (x + 1 <= 10 || x + 1 <= 1 || y + 1 <= 10 || y - 1 >= 1)
{
x = rand.Next(0, 10);
y = rand.Next(0, 10);
oneman = enemyShipArray[x,y];
enemyShipArray[x, y] = 1;
return enemyShipArray;
}
}
}
}
}
return enemyShipArray;
}
}
What I have tried:
I have tried making an empty array filled with 0's in the enemyShips class and also doing the random generation to put the ships on random cells but in my code it would choose a random cell one by one not the entire ship to place in a row or column.