Funny question: In this particular case, I agree with Luke in the UIButton subclass. Thus, you can give each button (X, Y) in its grid, as well as a list (Xnext, Ynext) for all possible expected next places to print (if the button itself can be used to create several words). Outwardly, you compare the current hit with the expected one (Xnext, Ynext). If the two do not match, this is the signal you are looking for.
This is an answer that takes into account all your situations: back and forth horizontally (if you decide to implement backware), up and down vertically (if you decide to implement up) and any diagonal or any other combination that you can think of!
It also means that you can say that pressing D, then O, then trying to press D again compared to hitting G. He will also make sure to hit the wrong G.
Create a new .mh pair of files (new object) and give it your name.
Sample code for implementing a custom UIButton (h file):
@interface myConnectedUIButton : UIButton { BOOL isAWordBeginCharacter; unsigned int beginWordKey; unsigned int myGridX; unsigned int myGridY; NSMutableArray * myConnectedSet; } -(id)init; -(void)initWithGridX:(unsigned int)X GridY:(unsigned int)Y BeginChar:(BOOL)yesNo BeginWordKey:(unsigned int)key; -(void)setGridPosWithX:(unsigned int)XY:(unsigned int)Y; -(void)setGridX:(unsigned int)X; -(void)setGridY:(unsigned int)Y; -(unsigned int)getGridX; -(unsigned int)getGridY; -(void)setIsABeginChar:(BOOL)yesNo; -(BOOL)getIsABeginChar; -(void)addPosToConnectedSetGridX:(unsigned int)X GridY:(unsigned int)Y WordKey:(unsigned int)key; -(NSArray *)getMyConnectedSetArray; -(void)clearConnectedSet; @end
In your .m file
@implementation myConnectedUIButton -(id)init{ [super init]; // Lets go ahead and initialize the NSMutableArray here also IFF it hasnt already been allocated if( nil == myConnectedSet ){ myConnectedSet = [[NSMutableArray alloc] init]; } // Lets also zero out the x, y position myGridX = 0; myGridY = 0; // Lets also state that this is NOT a begin char for the time being and 0 for the begin char key isAWordBeginCharacter = NO; beginWordKey = 0; return self; } -(void)initWithGridX:(unsigned int)X GridY:(unsigned int)Y BeginChar:(BOOL)yesNo BeginWordKey:(unsigned int)key{ // Lets go ahead and initialize the NSMutableArray here also IFF it hasnt already been allocated if( nil == myConnectedSet ){ myConnectedSet = [[NSMutableArray alloc] init]; } myGridX = X; myGridY = Y; isAWordBeginCharacter = yesNo; beginWordKey = key; } -(void)setGridPosWithX:(unsigned int)XY:(unsigned int)Y{ myGridX = X; myGridY = Y; } -(void)setGridX:(unsigned int)X{ myGridX = X; } -(void)setGridY:(unsigned int)Y{ myGridY = Y; } -(unsigned int)getGridX{ return myGridX; } -(unsigned int)getGridY{ return myGridY; } -(void)setIsABeginChar:(BOOL)yesNo{ isAWordBeginCharacter = yesNo; } -(BOOL)getIsABeginChar{ return isAWordBeginCharacter; } -(void)addPosToConnectedSetGridX:(unsigned int)X GridY:(unsigned int)Y WordKey:(unsigned int)key{ [myConnectedSet addObject:[GridPointNext GridPointNextWithX:X GridPointY:Y NextWordKey:key]]; } -(NSArray *)getMyConnectedSetArray{ return myConnectedSet; } -(void)clearConnectedSet{ [myConnectedSet removeAllObjects]; } -(void)dealloc{ [myConnectedSet release]; [super dealloc]; } @end
Now you also need the "GridPointNext" object.
The title of the Grid object should look like this:
@interface GridPointNext : NSObject { unsigned int GridPointX; unsigned int GridPointY; unsigned int nextWordKey; } +(GridPointNext *)GridPointNextWithX:(unsigned int)X GridPointY:(unsigned int)Y NextWordKey:(unsigned int)key; -(id)initWithX:(unsigned int)X GridPointY:(unsigned int)Y NextWordKey:(unsigned int)key; -(unsigned int)getGridX; -(unsigned int)getGridY; -(unsigned int)getNextWordKey; @end
The m file for the object should look like this:
@implementation GridPointNext +(GridPointNext *)GridPointNextWithX:(unsigned int)X GridPointY:(unsigned int)Y NextWordKey:(unsigned int)key{ GridPointNext * aPoint = [[GridPointNext alloc] initWithX:X GridPointY:Y NextWordKey:key]; [aPoint autorelease]; return aPoint; } -(id)initWithX:(unsigned int)X GridPointY:(unsigned int)Y NextWordKey:(unsigned int)key{ GridPointX = X; GridPointY = Y; nextWordKey = key; return self; } -(unsigned int)getGridX{ return GridPointX; } -(unsigned int)getGridY{ return GridPointY; } -(unsigned int)getNextWordKey{ return nextWordKey; } @end
You will have to deal with the dealloc part. This will at least give you some tools to create your custom button and your word list algorithm around it.