Reputation: 1061
Currently working on a little game for my last task for the university.
My current problem is that the gameboard is a 2D Array of Cells
(selfmade-class). The board is created with a txt.file I read in. Now there are multiple commands the user can type in. One command is to create a new Game Object and place it on the board using specific x and y parameters.
Is there any way to check if the field is big enough for the parameters they entered besides this one:
try {
gameField.getField()[Integer
parseInt(splittedCom[1])][Integer
parseInt(splittedCom[2])].getStatus();
} catch (ArrayIndexOutOfBoundsException e) {
Terminal.printLine("Error, this cell isn't in the gameField");
}
I'm currently using this to check it for another command. Is there a way to use this without an Exception?
Upvotes: 1
Views: 53
Reputation: 52195
Since your array is already constructed, you should have the dimensions of the array already, so you would use gameBoard.length
for the width and gameBoard[0].length
for the height, and use those to compare with whatever it is that the user has provided.
EDIT: As per your question:
If you have your gameBoard
object accessible, you would not need to save them, since that information would be available through the gameBoard
object itself. Alternatively, you would have to persist them some way. Note that although you are wasting some extra memory to store the values, throwing exceptions also has its cost, and should be avoided (in this case). A situation where you expect the user to provide incorrect values does not fall under the criterion of an exception case, since that is something you expect.
Upvotes: 1
Reputation: 37624
Check the input as long as it fits into your gamefield.
boolean checker = false;
while(!checker){
if( /* check if the input is inside the gamefield */){
checker = true;
}
}
Upvotes: 0