I’m programming in xcode for fun. I got the idea to create a console app where the computer plays train (a game using dominoes) against other generated “players”, and counts up the number of times it wins and loses, based on the strategies I program it to follow. My problem is, I want the computer to create 79 dominoes for the game. I have now programmed it to make one domino and print the value of dots, just for a test. I would like to know how to generate a set number without having to write “domino *dom1… domino *dom2…” and so on.
I was thinking i could make the number of the domino an integer (int num) and create a loop creating objects and num++ until num == 79. Obviously that didn’t work. I’m 16 and doing this mostly to practice objective c and OOP. If anyone could help me out with this early stage in development, I’d really appreciate it.
Here’s the code i’ve got so far:
//obj1.h
#import <Foundation/Foundation.h>
#import <stdlib.h>
@interface Domino : NSObject {
int dotS1; //number of upper dots on domino
int dotS2; //number of lower dots on domino
bool played; //is the domino on the board (not yet implemented)
bool inHand; //is the domino in your hand (not yet implemented)
}
-(void) createDomino;
@property(getter=updom) int dotS1; //returns the integer representing the upper dots on the domino
@property(getter=ddom) int dotS2; //returns the integer representing the lower dots on the domino
@end
@implementation Domino
-(void) createDomino{
dotS1 = arc4random() % 12; //randomizes the number of dots on the upper part of the domino between 0 and 12
dotS2 = arc4random() % 12; //randomizes the number of dots on the lower part of the domino between 0 and 12
};
@synthesize dotS1;
@synthesize dotS2;
@end
// main.m
#import "obj1.h" //imports the header where i keep objects, for organization
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Domino *dom1; //creates domino object "dom1"
dom1 = [[Domino alloc] init]; //allocates space in ram for "dom1"
[dom1 createDomino]; //makes "dom1" a domino with upper and lower dots
NSLog(@"TOP: %i", [dom1 updom]); //prints the number of dots on the top of the domino
NSLog(@"BOTTOM: %i", [dom1 ddom]); //prints the number of dots on the bottom of the domino
[pool drain];
return 0;
}
That’d be a class method on your Domino class. Call it by something like
NSArray dSet = [Domino setOfDominoes];.I would suggest against doing
getter=in your@property; just name the ivar and the property the same for consistency’s sake (actually, you can drop the ivar declaration, too, as it is no longer needed in modern Objective-C).