First on the effect chart
-Function display
-Initial Advanced Board switching effect
Implementation of ideas and the main code detailed
1. Draw the Chessboard
Use quartz2d to draw the chessboard. The code is as follows
-(void) Drawbackground: (cgsize) size{self.gridwidth = (size.width-2 * kboardspace)/self.gridcount;
1. Open the image context Uigraphicsbeginimagecontext (size);
2. Get context Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Cgcontextsetlinewidth (CTX, 0.8f); 3.1 Draw 16 bar for (int i = 0; I <= self.gridcount i + +) {cgcontextmovetopoint (CTX, Kboardspace + i * self.gr
Idwidth, Kboardspace);
Cgcontextaddlinetopoint (CTX, Kboardspace + i * self.gridwidth, kboardspace + self.gridcount * self.gridwidth); //3.1 Draw 16 horizontal line for (int i = 0; I <= self.gridcount i + +) {cgcontextmovetopoint (CTX, Kboardspace, Kboard
Space + i * self.gridwidth);
Cgcontextaddlinetopoint (CTX, Kboardspace + self.gridcount * self.gridwidth, Kboardspace + i * self.gridwidth);
} cgcontextstrokepath (CTX);
4. Get the generated picture UIImage *image=uigraphicsgetimagefromcurrentimagecontext (); 5. Show the generated picture to ImageView Uiimageview * imageView = [[Uiimageview alloc]initwithimage:image];
[Self addsubview:imageview];
Uigraphicsendimagecontext (); }
2. Click on the Chessboard drop
1 according to the drop position to find the line number and column number of the piece.
2 to determine whether the position of drop has a piece, there is not under. If not, keep the pieces in the dictionary, and the string combined with the column number and line number is the key value.
The code is as follows:
Click on the chessboard, chess
-(void) Tapboard: (UITapGestureRecognizer *) tap{
cgpoint point = [tap LocationInView:tap.view];
Calculates the line number of the row number
Nsinteger col = (point.x-kboardspace + 0.5 * self.gridwidth)/self.gridwidth;
Nsinteger row = (point.y-kboardspace + 0.5 * self.gridwidth)/self.gridwidth;
NSString * key = [NSString stringwithformat:@ "%ld-%ld", Col,row];
if (![ Self.chessmanDict.allKeys Containsobject:key]) {
UIView * Chessman = [self Chessman];
Chessman.center = Cgpointmake (kboardspace + col * self.gridwidth, kboardspace + row * self.gridwidth);
[Self Addsubview:chessman];
[Self.chessmandict Setvalue:chessman Forkey:key];
Self.lastkey = key;
Check game results
[self checkresult:col androw:row andColor:self.isBlack];
Self.isblack =!self.isblack;
}
3. Test Game Results
Every piece will be more than a game results of a check to determine whether there are more than 5 in the direction of the same color of the pieces of a line, there is a hint of the game win or lose results, the game continues. The algorithm, from the current position of the pawn forward traversal, until encountered with their own different color pieces, accumulate the number of pieces of the same color, and then traverse, Until you encounter a different color of the pieces, the number of pieces of the same color. The total number of the same color pieces connected to the direction
The code is as follows
Determine if it is greater than or equal to five color-(BOOL) Checkresult: (Nsinteger) col androw: (nsinteger) Row andcolor: (bool) Isblack anddirection: (
gmkdirection) direction{if (Self.sameChessmanArray.count >= 5) {return YES; } uicolor * Currentchessmancolor = [self.chessmandict[[nsstring stringwithformat:@ "%ld-%ld", Col,row]] BackgroundColor
];
[Self.samechessmanarray Addobject:self.chessmandict[self.lastkey]]; switch (direction) {//Horizontal check results case gmkhorizontal:{//forward traversal for (Nsinteger i = col-1 i > 0;
I--) {NSString * key = [NSString stringwithformat:@ "%ld-%ld", I,row]; if (![ Self.chessmanDict.allKeys Containsobject:key] | |
[Self.chessmandict[key] backgroundcolor]!= currentchessmancolor) break;
[Self.samechessmanarray Addobject:self.chessmandict[key]]; }//Backward traversal for (Nsinteger i = col + 1; i < Kgridcount i + +) {NSString * key = [NSString Strin
gwithformat:@ "%ld-%ld", I,row]; If(! [Self.chessmanDict.allKeys Containsobject:key] | |
[Self.chessmandict[key] backgroundcolor]!= currentchessmancolor) break;
[Self.samechessmanarray Addobject:self.chessmandict[key]];
} if (Self.sameChessmanArray.count >= 5) {[Self alertresult];
return YES;
} [Self.samechessmanarray removeallobjects];
} break; Case gmkvertical:{//forward for (Nsinteger i = row-1 i > 0; I-) {NSString * key = [Nsstri
ng stringwithformat:@ "%ld-%ld", col,i]; if (![ Self.chessmanDict.allKeys Containsobject:key] | |
[Self.chessmandict[key] backgroundcolor]!= currentchessmancolor) break;
[Self.samechessmanarray Addobject:self.chessmandict[key]]; ///Backward traversal for (nsinteger i = row + 1; i < Kgridcount i + +) {NSString * key = [NSString Strin
gwithformat:@ "%ld-%ld", col,i]; if (![ Self.chessmanDict.allKeys Containsobject:key] ||
[Self.chessmandict[key] backgroundcolor]!= currentchessmancolor) break;
[Self.samechessmanarray Addobject:self.chessmandict[key]];
} if (Self.sameChessmanArray.count >= 5) {[Self alertresult];
return YES;
} [Self.samechessmanarray removeallobjects];
} break;
Case gmkobliquedown:{//forward traversal nsinteger j = col-1;
for (Nsinteger i = row-1 i >= 0; I--, j--) {NSString * key = [NSString stringwithformat:@ "%ld-%ld", j,i]; if (![ Self.chessmanDict.allKeys Containsobject:key] | | [Self.chessmandict[key] backgroundcolor]!= Currentchessmancolor | |
J < 0) break;
[Self.samechessmanarray Addobject:self.chessmandict[key]];
}//Backward traversal J = col + 1; for (Nsinteger i = row + 1; i < Kgridcount i++,j++) {NSString * key = [NSString stringwithformat:@]%ld-%ld
", J,i]; if (![ Self.chessmandicT.allkeys Containsobject:key] | | [Self.chessmandict[key] backgroundcolor]!= Currentchessmancolor | |
J > Kgridcount) break;
[Self.samechessmanarray Addobject:self.chessmandict[key]];
} if (Self.sameChessmanArray.count >= 5) {[Self alertresult];
return YES;
} [Self.samechessmanarray removeallobjects];
} break;
Case gmkobliqueup:{//forward traversal Nsinteger j = col + 1;
for (Nsinteger i = row-1 I >= 0 I--, j + +) {NSString * key = [NSString stringwithformat:@ "%ld-%ld", j,i]; if (![ Self.chessmanDict.allKeys Containsobject:key] | | [Self.chessmandict[key] backgroundcolor]!= Currentchessmancolor | |
J > Kgridcount) break;
[Self.samechessmanarray Addobject:self.chessmandict[key]];
}//Backward traversal j = col-1; for (Nsinteger i = row + 1; i < Kgridcount i++,j--) {NSString * key = [NSString Stringwithformat:@ "%ld-%ld", j,i]; if (![ Self.chessmanDict.allKeys Containsobject:key] | | [Self.chessmandict[key] backgroundcolor]!= Currentchessmancolor | |
J < 0) break;
[Self.samechessmanarray Addobject:self.chessmandict[key]];
} if (Self.sameChessmanArray.count >= 5) {[Self alertresult];
return YES;
} [Self.samechessmanarray removeallobjects];
} break;
} return NO; }
External offer, restart, undo, switch three interfaces of the first advanced board
Start again
-(void) newgame{
self.isover = NO;
Self.lastkey = nil;
[Self.samechessmanarray removeallobjects];
self.userinteractionenabled = YES;
[Self.chessmandict removeallobjects];
for (UIView * view in self.subviews) {
if ([View Iskindofclass:[uiimageview class]]) {
continue;
}
[View Removefromsuperview];
}
Self.isblack = NO;
}
Undo
Recall the first step chess-(void) Backonestep: (UIButton *) sender{if (self.isover) return;
if (Self.lastkey = = nil) {sender.enabled = NO;
CGFloat width = screen_width * 0.4 * screen_width_ratio;
UIView * tip = [[UIView alloc]initwithframe:cgrectmake (0, 0, Width, 0.6 * width)];
Tip.backgroundcolor = [Uicolor colorwithwhite:1 alpha:0.8];
Tip.layer.cornerRadius = 8.0f;
[Self addsubview:tip];
Tip.center = Cgpointmake (self.width * 0.5, Self.height * 0.5);
Uilabel * label = [[Uilabel alloc]init]; Label.text = self.chessmanDict.count > 0?
@ "only regret a move!!!": @ "please first drop!!!";
Label.font = [Uifont systemfontofsize:15];
[Label SizeToFit];
Label.center = Cgpointmake (tip.width * 0.5, Tip.height * 0.5);
[Tip Addsubview:label];
self.userinteractionenabled = NO; Dispatch_after (Dispatch_time (Dispatch_time_now, int64_t) (2.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{self . userinteractionenabled = YES;
sender.enabled = YES;
[Tip Removefromsuperview];
});
Return
} [Self.chessmandict RemoveObjectForKey:self.lastKey];
[Self.subviews.lastObject Removefromsuperview];
Self.isblack =!self.isblack;
Self.lastkey = nil; }
Toggle Initial Advanced Keyboard
Change keyboard level
-(void) changeboardlevel{for
(UIView * View in self.subviews) {
[view Removefromsuperview];
}
[self newgame];
Self.ishighlevel =!self.ishighlevel;
[Self drawBackground:self.bounds.size];
}
A little trick in the demo
To store pieces in a dictionary, the string with the number of pieces and the line number is the key value, and the value is the pawn view. This process makes it very easy to determine whether a column has a piece in a row.
Summarize
The above is the development of iOS game Gobang OC version of the full content, I hope this article for everyone to develop iOS help, if this article has deficiencies, you are welcome to provide advice and guidance!