C++ Array Assignment

I already successfully made function 1 but I need to use function 1 to make the second function and I can't quite figure out how to get it to play and 'X' or 'f' on the surrounding shot elements.

1. Write a function called cover() that places some fish in a single given square of the battle grid; we will use this function multiple times in task 3 (below). This function takes three parameters: a battle grid array, a row offset, and a column offset. This function should check whether the given row and column offsets are valid for the battle grid array as follows:

If either one of the given offsets is not valid, the function should return without modifying the battle grid array.

If the row and column offsets are both valid, then use them to look at the element at the given row and column of the battle grid array, as follows:

If there is a penguin 'p' there, replace it with the 'X' character to represent that this penguin has been hit by fish and therefore that it has stopped attacking.

If the element was a space (no penguin) replace the space character with an 'f' character to represent that the area has been hit by fish.

If the element already contains an 'f' or an 'X' (previously hit by fish), do nothing. Design and test this function before going on.

2. Write a function called fishAPult() that simulates a single shot of the Fish-A-Pult. This function takes four parameters: a battle grid array, a row offset, a column offset, and the number of pounds of fish to fire. This function must first check whether the number of pounds of fish to fire is between 1 and 3. If it is not, output an error message and return. Otherwise, make the appropriate calls to the cover() function to cover the grid square specified by the given row and column offsets with fish, as well as any surrounding grid squares (depending on the number of pounds of fish) as determined by the rules given (https://moodle.cs.usask.ca/pluginfile.php/15676/mod_page/content/59/FishAPult.png). Design and test this function before going on.

This is my code for that section I've tried two different methods one for two fish and the other for three fish but neither seem to work..

void cover(char battleGrid[15][30], int row, int col)
{
populatePenguins(battleGrid);
if(battleGrid[row][col] == 'p')
{
battleGrid[row][col] = 'X';
}
else if(battleGrid[row][col] == ' ')
{
battleGrid[row][col] = 'f';
}
}

void fishAPult(char battleGrid[15][30], int row, int col, int fish)
{
if(fish < 1 || fish > 3)
{
cout << "ERROR";
exit (EXIT_FAILURE);
}
else if(fish == 1)
{
cover(battleGrid, row, col);
}
else if(fish == 2)
{
cover(battleGrid, row, col);
cover(battleGrid, row+=1, col);
cover(battleGrid, row-=1, col);
cover(battleGrid, row, col+=1);
cover(battleGrid, row, col-=1);
cover(battleGrid, row+=1, col+=1);
cover(battleGrid, row-=1, col-=1);
cover(battleGrid, row+=1, col-=1);
cover(battleGrid, row-=1, col+=1);
}
else if(fish == 3)
{
for(int i = 0; i < 15; i++)
{
for(int k = 0; k < 30; k++)
{
cover(battleGrid, i, k);
}
}
}
}
Last edited on
but neither seem to work..


What exactly doesn't work? Do you get error messages ?

What result do you expect and what do you get?



For example firing 2 pounds of fish is supposed to cover the grid square and all of its immediate horizontal, vertical and diagonal neighbours with fish but it only fires fish upon the row/col that I call upon.. there are no errors in the code
This is the description in the assignment file:

1. Firing one pound of fish will cover only the target grid square with fish, halting any penguin therein.

2. Firing two pounds of fish will cover the grid square and all of its immediate horizontal, vertical and diagonal neighbours with fish, halting all penguins in those squares.

3. Firing three pounds of fish will cover the same squares around the target square as 2 pounds of fish and also all squares horizontally, vertically, and diagonally adjacent to those squares.
Last edited on
These lines look a bit suspious.
1
2
3
4
5
6
7
8
cover(battleGrid, row+=1, col);
cover(battleGrid, row-=1, col);
cover(battleGrid, row, col+=1);
cover(battleGrid, row, col-=1);
cover(battleGrid, row+=1, col+=1);
cover(battleGrid, row-=1, col-=1);
cover(battleGrid, row+=1, col-=1);
cover(battleGrid, row-=1, col+=1);


Try it like this and see if it makes a difference.
1
2
3
4
5
6
7
8
cover(battleGrid, row+1, col);
cover(battleGrid, row-1, col);
cover(battleGrid, row, col+1);
cover(battleGrid, row, col-1);
cover(battleGrid, row+1, col+1);
cover(battleGrid, row-1, col-1);
cover(battleGrid, row+1, col-1);
cover(battleGrid, row-1, col+1);


Also be careful when you are in the first or last row, col to stay inside a array bounds.

Also it might help to write a function ShowBattleGrid(char grid[15][30]) that displays the grid before and after the call of fishAPult.
Last edited on
Topic archived. No new replies allowed.