Để phát triển game cờ vua objective c ta làm theo hướng dẫn sau.
ChessViewController.m
PHP Code:
//
// ChessViewController.m
// Chess
//
// Created by Nicholas Haurus on 3/7/11.
// Copyright 2011 Haurus Inc. All rights reserved.
//
#import "ChessViewController.h"
@implementation ChessViewController
// Create getters and setters
@synthesize boardView, boardModel;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
// Add boardmodel
boardModel = [[BoardModel alloc] init];
[boardModel reset];
[boardModel print];
// Add background
UIImage *backgroundImage = [UIImage imageNamed:@"background.png"];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
[self.view addSubview:backgroundImageView];
// Add boardview
boardView = [[[BoardView alloc] initWithFrame:CGRectMake(0, 0, 640, 640)] init];
[boardView setCenter:CGPointMake(424, 420)];
[boardView render:boardModel];
[self.view addSubview:boardView];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
}
- (void)dealloc {
// Perform garbage collection
[boardModel dealloc];
[boardView dealloc];
[super dealloc];
}
@end
BoardModel.m
PHP Code:
//
// BoardModel.m
// Chess
//
// Created by Nicholas Haurus on 3/7/11.
// Copyright 2011 Haurus Inc. All rights reserved.
//
#import "BoardModel.h"
// Define your ids for the white pieces
NSString *const WHITE_KING = @"wk";
NSString *const WHITE_QUEEN = @"wq";
NSString *const WHITE_BISHOP = @"wb";
NSString *const WHITE_KNIGHT = @"wn";
NSString *const WHITE_ROOK = @"wr";
NSString *const WHITE_PAWN = @"wp";
// Define your ids for the black pieces
NSString *const BLACK_KING = @"bk";
NSString *const BLACK_QUEEN = @"bq";
NSString *const BLACK_BISHOP = @"bb";
NSString *const BLACK_KNIGHT = @"bn";
NSString *const BLACK_ROOK = @"br";
NSString *const BLACK_PAWN = @"bp";
// Define your id for the an empty space
NSString *const EMPTY = @"em";
@implementation BoardModel
@synthesize pieces;
-(id) init {
// This we hold which pieces should be on what square
pieces = [[NSMutableArray alloc] initWithCapacity:64];
return self;
}
-(void) clear {
// Removing any existing data
[pieces removeAllObjects];
// Setup the empty middle
for (int i=0; i < 64; i++) {
[pieces insertObject:EMPTY atIndex:i];
}
}
-(void) reset {
[self clear];
// Setup the white power pieces
[pieces replaceObjectAtIndex:0 withObject:WHITE_ROOK];
[pieces replaceObjectAtIndex:1 withObject:WHITE_KNIGHT];
[pieces replaceObjectAtIndex:2 withObject:WHITE_BISHOP];
[pieces replaceObjectAtIndex:3 withObject:WHITE_QUEEN];
[pieces replaceObjectAtIndex:4 withObject:WHITE_KING];
[pieces replaceObjectAtIndex:5 withObject:WHITE_BISHOP];
[pieces replaceObjectAtIndex:6 withObject:WHITE_KNIGHT];
[pieces replaceObjectAtIndex:7 withObject:WHITE_ROOK];
// Setup the white pawns
for (int i=8; i < 16; i++) {
[pieces replaceObjectAtIndex:i withObject:WHITE_PAWN];
}
// Setup the bacl pawns
for (int i=48; i < 56; i++) {
[pieces replaceObjectAtIndex:i withObject:BLACK_PAWN];
}
// Setup the black power pieces
[pieces replaceObjectAtIndex:56 withObject:BLACK_ROOK];
[pieces replaceObjectAtIndex:57 withObject:BLACK_KNIGHT];
[pieces replaceObjectAtIndex:58 withObject:BLACK_BISHOP];
[pieces replaceObjectAtIndex:59 withObject:BLACK_QUEEN];
[pieces replaceObjectAtIndex:60 withObject:BLACK_KING];
[pieces replaceObjectAtIndex:61 withObject:BLACK_BISHOP];
[pieces replaceObjectAtIndex:62 withObject:BLACK_KNIGHT];
[pieces replaceObjectAtIndex:63 withObject:BLACK_ROOK];
}
// A valuable tool for dumping what's in memory
// Example NSLog:
// --------- BOARD -----------
// | br bn bb bq bk bb bn br |
// | bp bp bp bp bp bp bp bp |
// | em em em em em em em em |
// | em em em em em em em em |
// | em em em em em em em em |
// | em em em em em em em em |
// | wp wp wp wp wp wp wp wp |
// | wr wn wb wq wk wb wn wr |
// ---------------------------
-(void) print {
NSLog(@"--------- BOARD -----------");
NSString * line;
// Start at the top of the row first
for( int i=7; i>=0; i--) {
line = @"";
// Simply move from left to right in that row
for( int j=0; j<8; j++) {
// Create a space between for readability
line = [line stringByAppendingString:[NSString stringWithFormat:@" %@",[pieces objectAtIndex:i*8+j]]];
}
NSLog(@"|%@ |",line);
}
NSLog(@"---------------------------");
}
@end
BoardView.m
PHP Code:
//
// BoardView.m
// Chess
//
// Created by Nicholas Haurus on 3/7/11.
// Copyright 2011 Haurus Inc. All rights reserved.
//
#import "BoardView.h"
#import "PieceButton.h"
@implementation BoardView
- (id)init {
squaresView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 480, 480)];
[self addSubview:squaresView];
piecesView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 480, 480)];
[self addSubview:piecesView];
UIImage *darksquareImage = [UIImage imageNamed:@"dark-square.png"];
UIImage *lightsquareImage = [UIImage imageNamed:@"light-square.png"];
UIImageView *squareImageView;
for (int i=0; i<64; i++) {
float fx = (float) ( i % 8 ) * SQUARE_SIZE;
float fy = floor( i / 8 ) * SQUARE_SIZE;
if((int)floor(i/8)%2) {
if( i%2 ) {
squareImageView = [[UIImageView alloc] initWithImage:darksquareImage];
}
else {
squareImageView = [[UIImageView alloc] initWithImage:lightsquareImage];
}
}
else {
if( i%2 ) {
squareImageView = [[UIImageView alloc] initWithImage:lightsquareImage];
}
else {
squareImageView = [[UIImageView alloc] initWithImage:darksquareImage];
}
}
[squareImageView setCenter:CGPointMake(fx,fy)];
[squaresView addSubview:squareImageView];
}
return self;
}
// Use this to render the model on to the board view
- (void)render:(BoardModel *)model {
PieceButton *pieceButton;
for (unsigned int i=0; i < 64; i++) {
if ([[model pieces] objectAtIndex:i] != EMPTY ) {
pieceButton = [[[PieceButton alloc] initWithFrame:CGRectMake(0, 0, SQUARE_SIZE, SQUARE_SIZE)] initWithPiece:[[model pieces] objectAtIndex:i]];
[pieceButton setSquare:i];
[self addSubview:pieceButton];
}
}
}
@end
PieceButton.m
PHP Code:
//
// PieceButton.m
// Chess
//
// Created by Nicholas Haurus on 3/7/11.
// Copyright 2011 Haurus Inc. All rights reserved.
//
#import "PieceButton.h"
#import "BoardView.h"
@implementation PieceButton
// Init the piece with it's correct image
-(id)initWithPiece:(NSString *)type {
NSString *path = [NSString stringWithFormat:@"%@.png",type];
UIImage *image = [UIImage imageNamed:path];
[self setBackgroundImage:image forState:UIControlStateNormal];
[self addTarget:self action:@selector(draggedOut:withEvent: ) forControlEvents:UIControlEventTouchDragOutside | UIControlEventTouchDragInside];
return self;
}
// Use this function to position the piece
-(void) setSquare:(unsigned int)squareValue {
square = squareValue;
float fx = (float) ( square % 8 ) * SQUARE_SIZE;
float fy = (SQUARE_SIZE * 7) -floor( square / 8 ) * SQUARE_SIZE;
[self setCenter:CGPointMake(fx,fy)];
}
// Makes the piece draggable
- (void)draggedOut:(UIControl *)c withEvent:(UIEvent *)ev {
CGPoint cord = [[[ev allTouches] anyObject] locationInView:nil];
// Center of the piece plus the offset of the board view
// Total iPad width 768, Board width 80*8= 640
// 768-640=128
// Half of the margin over and down
// 128/2=64
// Plus half of the piece size
// 80/2 = 40
// 40+64 = 104 net offset
cord.x -= 104;
cord.y -= 104;
c.center = cord;
}
@end
Chúc các bạn làm thành công
Các bạn có thể tải project link đính kèm để tham khảo
http://www.mediafire.com/?e5d3rln1pxlqid9
Resource: internet