3

ok, here´s a first time noob question, sorry if that´s stupid.

I was just wondering, for a battleship kind of game, would it be a waste of memory to build a set of objects for each cell (10X10=100), with position(x,y) and state(empty,hit,missed) properties?

I was wondering if it would be better to only create an object Grid and use methods to calculate the cell positions whenever necessary (when handling cell selection with touches or drawing for example)..

user1347271
  • 127
  • 1
  • 8

2 Answers2

2

The former is problematic because you may have ships that sit side-by-side or end to end and it will become difficult to know when one is completely destroyed just from the data structures you described. Two hits side-by-side could be two hits on the same ship, two hits to two different ships, or even a sinking for the smallest ship.

Go with the latter for sanity's sake.

John Munsch
  • 19,530
  • 8
  • 42
  • 72
1

If I was doing this, I would keep it simple, Have a 2 dimensional array, that's your 10 by 10 grid.

When someone takes a turn, calculate the position and;

if it's a miss, insert a 0 in that array cell if it's a hit, insert a 1 in that array cell

Chris
  • 2,739
  • 4
  • 29
  • 57
  • ah yes, I forgot about identifying whole ships! – Chris Apr 20 '12 at 21:07
  • Here's another way to use the 10 by 10 array. When you place the ships give each ship a different number say 1 to 5 if you have five ships. then if you have a hit on ship 2 change that number to -2. Then search all adjacent cells - if you find any 2 the ship is not yet sunk. – James Thiele Apr 20 '12 at 23:33