Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 19-Dec-22 4:12am
Comments
Graeme_Grant 17-Dec-22 5:16am    
Understanding the rules will help with what you are trying to achieve. This should help: How to Play Battleship - YouTube[^]

I think you are making a big mistake: you seem to be assuming that just dumping the center of a ship on a single square is what you need to do, it isn't - because they are different shapes and sizes and they can't overlap at all.
Try this thread: C# Battleship gameboard & random assignment - C# Discussion Boards[^] he had very similar problems.
 
Share this answer
 
Comments
BillWoodruff 17-Dec-22 9:12am    
possible the OP is using Buttons, not images ?
Arthur Dragon 17-Dec-22 11:04am    
I am using buttons
Graeme_Grant 17-Dec-22 20:22pm    
The trick is do do the map in code, then apply to the grid manually or by using data binding. By using data binding, changes made to the models will automatically update the display, therefore you do not have to manually work with the UI elements.
 
Share this answer
 
Comments
Arthur Dragon 17-Dec-22 11:04am    
I did all this
BillWoodruff 17-Dec-22 11:16am    
... and ... ?
Arthur Dragon 17-Dec-22 18:29pm    
The setting the buttons into the grid rows and columns I have done but I do not know how to randomly generate the computer map
BillWoodruff 17-Dec-22 21:31pm    
that sounds like another question, and how would anyone help you when you don't describe exactly what the map is, or its behavior ?

you are not doing the work/study/experimentation needed to make a plan for your game, and specify its components and how they function.

we are not here to write your code for you, but to help you solve problems with your implementation.

Make a detailed plan, and get to work.

You're really not thinking about the problem of placing ships at all. You're adding all this extra garbage when you don't have to. You need to know three things about placing a ship on a board. That's it!

It's really easy. A ship is 1 to 5 squares in length. Each ship can also only be in one of two orientations, horizontal and vertical. The valid grid size for placing the ship is going to be determined by the size of the board, the length of the ship, and its orientation.

So what is the number of rows and columns to fit a ship of, say, length 5 and oriented horizontally on a standard 10x10 board? The number of rows, since a ship is only ever 1 square wide, is the number of board rows. The number of valid columns to start the ship is the width of the board minus the length of the ship, 10 minus 5, plus 1. The valid column selections are columns 1, 2, 3, 4, 5, and 6. The bow of the ship can start in column 6 and fit the stern in column 10, that's 5 squares (6, 7, 8, 9, 10). So you have to generate a random number between 1 and 6, inclusive.

You're so worried about the code and what it looks like, you're forgetting to think about the real problem. It helps to have the real table-top game in front of you to help you think about the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900