English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm working on a program that suppose to be like the game "Battleship" and it's known that when you hit one "cell", you can't hit it again. Similar in my program, how can I make it so you can't hit the same spot (deleting one object from an array)?

2007-02-23 10:35:36 · 4 answers · asked by deon7388 2 in Computers & Internet Programming & Design

4 answers

you can't delete one object from an array :(

you can, however, set the value in that cell of the array to something else, to let you know it's been modified

for example, in your game you can have your array be int[], and put 0 there if the cell wasn't hit, and 1 if it was. Then, to see if you should let someone hit a cell, check if the value is 0.

2007-02-23 10:40:41 · answer #1 · answered by iluxa 5 · 1 0

You can delete an item in an array, but you'd have to basically copy the array without the item you wanted to delete.

In your situation, it seems it'd be easier to create a multidimensional array. Make the first value the object, and the second value whether it's already been hit or not -- a simple 1 (not hit) or 0 (has been hit) would suffice if it's an integer.

2007-02-23 11:31:24 · answer #2 · answered by BDZot 6 · 0 0

Unlike C++ you dont really call delete in java. I think java handles memory management...you can set an object to null though. I guess you can call the garbage collector after, but you are not guaranteed that the memory is reclaimed.

2007-02-23 11:18:46 · answer #3 · answered by AL L 2 · 0 1

check if the array has a DELETE() or REMOVE() method.

2007-02-23 11:56:04 · answer #4 · answered by Richard H 7 · 0 1

fedest.com, questions and answers