I program a battleship console game and one of the rules is that you cant place a ship next to another ship. So I have a 2D-array for the field where the ships are placed with an Index number (1) and where the "tiles" are marked which cant have a ship in them (25), an array of coordinates and the direction I want to place a ship and in my function I loop over the surrounding "tiles" of the ship to check if I can change the index to the occupied one (25). So I check if the number I have moved to the place where I want to change the index number is in bounds. Because of visuals the board is a 11 * 11 field and the playground is from 1 - 10. In the code the coordinates I wanted to place the ship were 3,1
and with the direction "right"
and a length (saved in l) of 2. So this is totally in bounds all of the ships tiles are placed but when it comes to the changing of the surrounding tiles it just spits out the "Index out of bounds" exeption and marks the if-statement.
This is all the code required to run this part of the script, if not please ask and I can provide more of it. If you want to see the full code, I can also provide the GitHub-Link
int[,] P1board = { //The board of Player 1
//x = 0 1 2 3 4 5 6 7 8 9 10
{ 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, //y = 0
{ 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //y = 1
{ 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //y = 2
{ 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //y = 3
{ 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //y = 4
{ 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //y = 5
{ 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //y = 6
{ 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //y = 7
{ 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //y = 8
{ 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //y = 9
{ 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} //y = 10
};
public int[][] P1Schiffe = {
new int[] { 0, 0},
new int[] { 0, 0},
new int[] { 0, 0},
new int[] { 0, 0},
new int[] { 0, 0, 0, 0},
new int[] { 0, 0, 0, 0},
new int[] { 0, 0, 0, 0},
new int[] { 0, 0, 0, 0, 0, 0},
new int[] { 0, 0, 0, 0, 0, 0},
new int[] { 0, 0, 0, 0, 0, 0, 0, 0},
new int[] { 4, 3, 2, 1}
};
public void PlaceP1(int l, int r)
{
bool input = true; //Saves if an input was provided by the user. false means there was an input
int temp = 0;
int[] position = get(); //The input of the user is converted into numbers via the get() function
position = check(position, l, sub[2]); //check() checks if the ship is in the designated play area
position = P1checkO(position, l, sub[2]); //P1checkO() checks if the ship collides with another ship
while (input)
{
switch (sub[2]) //based on the length and orientation the ship will be placed
{
case "up": //to place the ship upwards the y-coordinate will be subtracted by i
if (position[0] 1 < 11)
{
P1board[position[0] 1, position[1]] = 25;
if (position[1] 1 < 11) P1board[position[0] 1, position[1] 1] = 25;
if (position[1] - 1 > 0) P1board[position[0] 1, position[1] - 1] = 25;
}
for (int i = 0; i < l; i )
{
P1board[position[0] - i, position[1]] = 1;
if (position[1] 1 < 11) P1board[position[0] - i, position[1] 1] = 25;
if (position[1] - 1 > 0) P1board[position[0] - i, position[1] - 1] = 25;
P1Schiffe[r][temp] = position[0] - i;
P1Schiffe[r][temp 1] = position[1];
temp = temp 2;
}
if (position[0] - l > 0)
{
P1board[position[0] - l, position[1]] = 25;
if (position[1] - 1 > 0) P1board[position[0] - l, position[1] - 1] = 25;
if (position[1] 1 < 11) P1board[position[0] - l, position[1] 1] = 25;
}
input = false;
break;
case "down": //to place the ship downwards the y-coordinate will be added by i
if (position[0] - 1 > 0)
{
P1board[position[0] - 1, position[1]] = 25;
if (position[1] - 1 > 0) P1board[position[0] - 1, position[1] - 1] = 25;
if (position[1] 1 < 11) P1board[position[0] - 1, position[1] 1] = 25;
}
for (int i = 0; i < l; i )
{
P1board[position[0] i, position[1]] = 1;
if (position[1] 1 < 11) P1board[position[0] i, position[1] 1] = 25;
if (position[1] - 1 > 0) P1board[position[0] i, position[1] - 1] = 25;
P1Schiffe[r][temp] = position[0] i;
P1Schiffe[r][temp 1] = position[1];
temp = temp 2;
}
if (position[0] l < 11)
{
P1board[position[0] l, position[1]] = 25;
if (position[1] - 1 > 0) P1board[position[0] l, position[1] - 1] = 25;
if (position[0] 1 < 11) P1board[position[0] l, position[1] 1] = 25;
}
input = false;
break;
case "left": //to place the ship left, the x-coordinate will be subtracted by i
if (position[1] 1 < 11)
{
P1board[position[0], position[1] 1] = 25;
if (position[0] - 1 > 0) P1board[position[0] - 1, position[1] 1] = 25;
if (position[0] 1 < 11) P1board[position[0] 1, position[1] 1] = 25;
}
for (int i = 0; i < l; i )
{
P1board[position[0], position[1] - i] = 1;
if (position[0] 1 < 11) P1board[position[0] 1, position[1] - i] = 25;
if (position[0] - 1 > 0) P1board[position[0] - 1, position[1] - i] = 25;
P1Schiffe[r][temp] = position[0];
P1Schiffe[r][temp 1] = position[1] - i;
temp = temp 2;
}
if (position[1] - l > 0)
{
P1board[position[0], position[1] - l] = 25;
if (position[0] - 1 > 0) P1board[position[0] - 1, position[1] - l] = 25;
if (position[0] 1 < 11) P1board[position[0] 1, position[1] - l] = 25;
}
input = false;
break;
case "right": //to place the ship to the right, the x-coordinate will be added by i
if (position[1] - 1 > 0)
{
P1board[position[0], position[1] - 1] = 25;
if (position[0] - 1 > 0) P1board[position[0] - 1, position[1] - 1] = 25;
if (position[0] 1 < 11) P1board[position[0] 1, position[1] - 1] = 25;
}
for (int i = 0; i < l; i )
{
P1board[position[0], position[1] i] = 1;
if (position[0] 1 < 11) P1board[position[0] 1, position[1] i] = 25;
if (position[0] - 1 > 0) P1board[position[0] - 1, position[1] i] = 25;
P1Schiffe[r][temp] = position[0];
P1Schiffe[r][temp 1] = position[1] i;
}
if (position[1] l < 11)
{
P1board[position[0], position[1] - l] = 25;
if (position[0] - 1 > 0) P1board[position[0] - 1, position[1] l] = 25;
if (position[0] 1 < 11) P1board[position[0] 1, position[1] l] = 25;
}
input = false;
break;
default: //If there was no direction provided or a wrong one, all will be checked again and the process starts again. input is still true
Console.WriteLine(d.text(27, lang));
sub[2] = Console.ReadLine();
position = check(position, l, sub[2]);
position = P1checkO(position, l, sub[2]);
break;
}
}
}
For some further explanation get()
just converts the user input into an int[] so it can be used. check(position, l, sub[2])
checks if the provided direction(sub[] ist the string the user provided as input) has the space to place the ship. P1checkO(position, l, sub[2])
checks if there is a ship already placed or adjacent to the ship you want to place. d.text()
is a function, which access a big library of strings for providing multiple supportet languages. I hope this explains my garbage piece of code so far to help me with this error. Here is my GitHub, project if you want to see the full code (Warning, its a mess and a lot of it):
github.com/Livesi5e/Schiffe-Versenken
CodePudding user response:
Okay I found the error. Somehow the line P1board[position[0], position[1] - l] = 25;
found in the case: "right"
, last if-statement, needed to be P1board[position[0], position[1] l] = 25;
and now the whole code works flawless