2

I'm writing a chess game in Java have set it up with a Board class that has a 2d array of Square objects. Each square has a height and width (between 0 and 7 inclusive). I'm going to write a method that takes in a square object, a vertical offset, and a horizontal offset.

Ex: If I call getNearbySquare(mySquare, 3, 2), it should return the square object in my Board that is 3 above mysquare and 2 to the right of mySquare.

I originally wrote this as an instance method in the Square class and it just took in a vertical and horizontal offset. The problem is, I was creating and then returning a new square object, not the correct one from my 2d array in my Board class.

I see two options:

1) Make an instance method in Square class that takes in a Board object, vertical offset, and horizontal offset. I'll find the current height and width of the square object that calls this method, add the vertical and horizontal offsets to get the new location in the Board of the desired square, and then access it using Board[newHeight][newWidth].

2) Make an instance method in Board class does the same thing but doesn't require me to pass in the Board object as in 1). Instead, I'll need to pass in the square object.

Is there a generally accepted practice as far as making these decisions goes?

Thanks for the help, Mariogs

ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
anon_swe
  • 8,791
  • 24
  • 85
  • 145

2 Answers2

5

From a very simple view-point it makes more sense for the getNearbySquare() method to be an instance method for the Board rather than from the squares because the method will require intimate information about the layout and population of the board, which will be easier to do (and keep encapsulated/loosely coupled) from within the Board instance.

Having the method on a Square instance means giving the square objects access to information about the board that should probably be private.

Related Reading;

Community
  • 1
  • 1
Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
  • Here's the thing though: When a user clicks a square, I'm going to want to access the board (to call getNearbySquare(), like you suggest). But I don't have a reference to the board unless I make it a field of Square...but then Square knows about its board... – anon_swe Jan 09 '15 at 23:09
  • @Mariogs - Just to mirror Leandro's sentiment, it's fine for a square to *have-a* reference to the board it belongs to so long as the board has it's own internal components (fields) private. – Rudi Kershaw Jan 10 '15 at 09:38
1

It is fine for a Square to know the Board it belongs in as the Square is not in isolation. Otherwise, it wouldn't make sense for the Square to know its location. When a user clicks a Square, the Square may just communicate that event to its Board and the Board will be able to react to the user action based on the Square's location. Given that the Board should also know its Squares it would easily calculate which one would become the target of the next action.

See also OOP Design for Chess Game in Java (Trouble with Piece / Board Interaction)

Community
  • 1
  • 1
Leandro Caniglia
  • 14,495
  • 4
  • 29
  • 51