Click here to Skip to main content
15,844,498 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi, I have been writing a battleship game and I almost done. Just need one thing. I have followed all your advices and cut the code down a lot and understood how the game is supposed to be implemented I am struggling on the part where the computer randomly assigns ships to its map but does not show them to the user during the actual game. For example I click start and the computer should randomly assign ships (without displaying them on the board of course) with the user firing and the board does a show where the ship is or is not. Here is the snippet of code where I attempt to assign a value to each square that is supposed to hold a ship
public int[,] RandomShip(Grid grid)
{
    enemyShipArray = new int[row, col];
    for (int i = 0; i < 1; i++)
    {
        x = rand.Next(1, 97);
        //((Button)grid.Children[x]).Background = Brushes.Red;
        ((Button)grid.Children[x]).Tag = 1;
        x = x + 1;
        //((Button)grid.Children[x]).Background = Brushes.Red;
        ((Button)grid.Children[x]).Tag = 1;
        x = x + 1;
        //((Button)grid.Children[x]).Background = Brushes.Red;
        ((Button)grid.Children[x]).Tag = 1;
        x = x + 1;
        //((Button)grid.Children[x]).Background = Brushes.Red;
        ((Button)grid.Children[x]).Tag = 1;
    }
    for (int a = 0; a < 2; a++)
        {
            x = rand.Next(1, 98);
            //((Button)grid.Children[x]).Background = Brushes.Red;
        ((Button)grid.Children[x]).Tag = 1;
        x = x + 1;
            //((Button)grid.Children[x]).Background = Brushes.Red;
        ((Button)grid.Children[x]).Tag = 1;
        x = x + 1;
            //((Button)grid.Children[x]).Background = Brushes.Red;
        ((Button)grid.Children[x]).Tag = 1;
    }
        for (int a = 0; a < 3; a++)
        {
            x = rand.Next(1, 99);
            //((Button)grid.Children[x]).Background = Brushes.Red;
        ((Button)grid.Children[x]).Tag = 1;
        x = x + 1;
            //((Button)grid.Children[x]).Background = Brushes.Red;
        ((Button)grid.Children[x]).Tag = 1;
    }
            for (int a = 0; a < 4; a++)
            {
                x = rand.Next(1, 100);
                //((Button)grid.Children[x]).Background = Brushes.Red;
        ((Button)grid.Children[x]).Tag = 1;
    }
            return enemyShipArray;
    }

Note that this is a method within a seperate class. In the code it can be seen that I want to assign the square a Red background but only when the user clicks on it and there is a ship there. To solve this I assigned a tag to each square that contains a ship so it is marked with a 1. Now onto the function where I press the Start button and the computer must randomly assign the ships without showing.

private void StartButton_Click(object sender, RoutedEventArgs e)
     {
         enemyShips.RandomShip(gridEnemy);
     }

I am passing the enemy grid as the grid to execute the function RandomShip to randomize the ships. Now what I am struggling is how to pass the marked
((Button)grid.Children[x]).Tag = 1;

element into the function where the button click on the enemygrid occurs so it knows which square is marked and which is not.

private void ButtonEnemy_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
            if( (== 1)
            {
                ((Button)sender).Background = Brushes.Red;
            }
            else
            {
                ((Button)sender).Background = Brushes.Blue;
            }

}

Ideally I would want to put something like
if( ((Button)grid.Children[x]).Tag == 1)
then color the square Red and else color it Blue. Though I do not know how to make the compiler pick up the meaning of the tag equaling the 1. Thanks!

What I have tried:

I have tried even passing a button as an argument into the randomship function even though I know thats wrong but I do not know how to just glue this together.
Posted
Updated 21-Dec-22 21:20pm

1 solution

Make the enemyShips gridEnemy variable available inside ButtonEnemy_MouseLeftButtonUp.
How to do that, really depends on your actual scenario.
 
Share this answer
 
v3
Comments
Arthur Dragon 22-Dec-22 6:49am    
I am having trouble extracting the tag that I assigned to each random instance into an if statement like if the Tag equals 1 then color the background red upon button click if not 1 then color it blue
Arthur Dragon 22-Dec-22 6:50am    
how do I reach out to the tags that I assigned to the x's in the randomShip() function
CPallini 22-Dec-22 7:17am    
Sorry, I misread the original code. Now I've updated my solution. If you have access to gridEnemy then
if( ((Button)gridEnemy.Children[x]).Tag == 1)

would do the trick.
BTW what is the purpose of the enemyShipArray ?
Arthur Dragon 22-Dec-22 8:25am    
I tried putting them all into an Array but it didn't work out so I am using the arrays. This is what I have and it is giving me an error saying x does not exist in the current context. I know that it does not exist but how do I pass the meaning of the x's that were in the assigned to each button in the for loops of the random ship function



enemyShips.RandomShip(gridEnemy);

if (((Button)gridEnemy.Children[x]).Tag == 1)
{
((Button)gridEnemy.Children[enemyShips.x]).Background = Brushes.Red;
}
else
{
((Button)sender).Background = Brushes.Blue;
}
CPallini 22-Dec-22 12:35pm    
Shouldn't that be
for (int x=1; x<100; ++x)
{
  if (((Button)gridEnemy.Children[x]).Tag == 1)
  {
    //...
}

?

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