2

I'm writing a board game to teach myself Objective C for iPhone and I have a "Piece" class.

Now I would like to make pieces of different shapes, so I would like to add a field "int shape" to the ".h" file of my Piece class. Let's assume I would like to have 3 shapes: square, triangle and circle.

My question is: what is the nicest way to define these sorts of shapes? And where is it best to define them?

Edit: With nicest here I mean most real world, most common, least code and most performant.

Edit No. 2: I used an enum.

For example: I could make a separate header file "PieceConstants.h" and make an enumeration in there. Or I could add the enumeration in the file "Piece.m" so that I could use them like this [Piece squareShapeType].

Many thanks for your help!

Matt N.
  • 1,239
  • 2
  • 11
  • 26

1 Answers1

2

Objective C is an object oriented language. So what you would do is define a separate class for each of the shapes of the game pieces, that inherits from the "Piece" class. Then you would have a draw function implemented in each of the shape classes that draws the different shapes. If you want to read about object oriented programming, here is a good resource.
http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/OOP_ObjC/Introduction/Introduction.html%23//apple_ref/doc/uid/TP40005149-CH1-SW2

Skyler Saleh
  • 3,961
  • 1
  • 22
  • 35
  • Thanks, but I think this would blow up my code. I can see your point but I think in this case I will not do it "the object oriented way", even though it's also correct. – Matt N. Feb 28 '11 at 15:27
  • Blowing up your code shouldn't be your concern when learning the features of a new language. This is the correct way to do what you want. Don't use enums. – Marc W Mar 06 '11 at 15:23