Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d60324b
Showing
110 changed files
with
17,755 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
**/*.db | ||
*.??# | ||
*.~?~ | ||
*~ | ||
*.swp | ||
.#* | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// Board.h | ||
// Gomoku | ||
// | ||
// Created by Konstantin Gredeskoul on 5/3/10. | ||
// Copyright 2010 Konstantin Gredeskoul, shared under MIT license. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "Move.h" | ||
|
||
#define CELL_EMPTY 0 | ||
#define CELL_BLACK 1 | ||
#define CELL_WHITE 2 | ||
|
||
@interface Board : NSObject { | ||
int size; | ||
// two dimensional int array filled with values above. | ||
int **matrix; | ||
} | ||
|
||
@property(nonatomic) int size; | ||
@property(nonatomic) int** matrix; | ||
|
||
-(Board *)initWithSize: (int)size; | ||
-(void) makeMove:(int) color At:(Move *) move; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// Board.m | ||
// Gomoku | ||
// | ||
// Created by Konstantin Gredeskoul on 5/3/10. | ||
// Copyright 2010 Konstantin Gredeskoul, shared under MIT license. All rights reserved. | ||
// | ||
|
||
#import "Board.h" | ||
|
||
@implementation Board | ||
@synthesize size; | ||
@synthesize matrix; | ||
|
||
-(Board *)initWithSize: (int)thisSize { | ||
if (self = [super init]) { | ||
self.size = thisSize; | ||
self.matrix = malloc(self.size * sizeof(int *)); | ||
// TODO: check for NULL? | ||
for(int i = 0; i < self.size; i++) { | ||
self.matrix[i] = malloc(self.size * sizeof(int)); | ||
// TODO: check for NULL | ||
for (int j = 0; j < self.size; j++) { | ||
self.matrix[i][j] = 0; | ||
} | ||
} | ||
} | ||
NSLog(@"initialized %@", self); | ||
return self; | ||
} | ||
|
||
-(void) makeMove:(int) color At:(Move *) move { | ||
NSLog(@"received move to %d %@", color, move); | ||
// update the board at move.x, move.y | ||
if (move.x < 0 || move.x >= self.size || move.y < 0 || move.y >= self.size) { | ||
NSLog(@"invalid move parameters %@ with color %d", move, color); | ||
return; | ||
} | ||
// else all is good. | ||
self.matrix[move.x][move.y] = color; | ||
} | ||
|
||
|
||
- (NSString *)description { | ||
// TODO: provide toString that dumps the full board using space, O and X characters | ||
return [NSString stringWithFormat:@"Board of Size %d", self.size]; | ||
} | ||
|
||
|
||
- (void)dealloc { | ||
for(int i = 0; i < self.size; i++) | ||
free(self.matrix[i]); | ||
free(self.matrix); | ||
self.matrix = NULL; | ||
[super dealloc]; | ||
} | ||
|
||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// BoardCell.h | ||
// gomoku | ||
// | ||
// Created by Konstantin Gredeskoul on 5/2/10. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@class BoardCell; | ||
|
||
@protocol BoardCellDelegate | ||
|
||
- (void) selectedBoardCell:(BoardCell *)theCell; | ||
|
||
@end | ||
|
||
|
||
@interface BoardCell : UIImageView { | ||
id<BoardCellDelegate> delegate; | ||
} | ||
|
||
@property(nonatomic, retain) id<BoardCellDelegate> delegate; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// BoardCell.m | ||
// gomoku | ||
// | ||
// Created by Konstantin Gredeskoul on 5/2/10. | ||
// | ||
|
||
#import "BoardCell.h" | ||
|
||
|
||
@implementation BoardCell | ||
|
||
@synthesize delegate; | ||
|
||
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { | ||
//NSLog(@"touches began %@, %@", touches, event); | ||
[super touchesBegan:touches withEvent:event]; | ||
} | ||
|
||
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { | ||
//NSLog(@"touches ended %@, %@", touches, event); | ||
[super touchesEnded:touches withEvent:event]; | ||
[self.delegate selectedBoardCell:self]; | ||
} | ||
|
||
- (id)initWithFrame:(CGRect)frame { | ||
if (self = [super initWithFrame:frame]) { | ||
self.userInteractionEnabled = YES; | ||
} | ||
return self; | ||
} | ||
|
||
|
||
//- (void)drawRect:(CGRect)rect { | ||
// // Drawing code | ||
//} | ||
|
||
|
||
- (void)dealloc { | ||
[super dealloc]; | ||
} | ||
|
||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// Game.h | ||
// gomoku | ||
// | ||
// Created by Konstantin Gredeskoul on 5/3/10. | ||
// Copyright 2010 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
|
||
@protocol Game | ||
|
||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// Game.h | ||
// gomoku | ||
// | ||
// Created by Konstantin Gredeskoul on 5/3/10. | ||
// Copyright 2010 Konstantin Gredeskoul, shared under MIT license. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "Player.h" | ||
#import "Board.h" | ||
|
||
#define GOMOKU_BOARD_SIZE 19 | ||
#define GOMOKU_PLAYERS 2 | ||
|
||
@protocol GameDelegate | ||
|
||
- (void) moveMade:(Move *) move byPlayer:(int) playerIndex; | ||
- (void) gameOverWithWinner:(int) playerIndex; | ||
|
||
@end | ||
|
||
|
||
@interface Game : NSObject { | ||
NSMutableArray *players; | ||
NSMutableArray *moves; | ||
int currentPlayerIndex; | ||
Board *board; | ||
@private | ||
BOOL gameStarted; | ||
} | ||
|
||
@property (nonatomic, retain) id<GameDelegate> delegate; | ||
|
||
@property (nonatomic, assign) NSMutableArray *players; | ||
@property (nonatomic, assign) NSMutableArray *moves; | ||
@property (nonatomic, assign) int currentPlayerIndex; | ||
@property (nonatomic, assign) Board *board; | ||
@property (nonatomic, assign) BOOL gameStarted; | ||
|
||
- (Game *) initGame; | ||
- (void) addPlayer:(id <Player>) player; | ||
- (id<Player>) player:(int) index; | ||
- (void) makeMove: (Move *) move; | ||
- (BOOL) isMoveValid: (Move *) move; | ||
- (void) startGame; | ||
- (void) stopGame; | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// | ||
// Game.m | ||
// gomoku | ||
// | ||
// Created by Konstantin Gredeskoul on 5/3/10. | ||
// | ||
|
||
#import "Game.h" | ||
#import "UIPlayer.h" | ||
|
||
|
||
@implementation Game | ||
|
||
@synthesize players; | ||
@synthesize moves; | ||
@synthesize currentPlayerIndex; | ||
@synthesize board; | ||
@synthesize gameStarted; | ||
@synthesize delegate; | ||
|
||
|
||
- (Game *) initGame { | ||
if (self = [super init]) { | ||
self.players = [[NSMutableArray alloc] initWithCapacity:GOMOKU_PLAYERS]; | ||
self.moves = [[NSMutableArray alloc] initWithCapacity:GOMOKU_PLAYERS]; | ||
for (int i = 0; i < GOMOKU_PLAYERS; i++) { | ||
[self.moves addObject: [[NSMutableArray alloc] initWithCapacity:20]]; | ||
} | ||
self.board = [[Board alloc] initWithSize:GOMOKU_BOARD_SIZE]; | ||
self.gameStarted = NO; | ||
self.currentPlayerIndex = 0; | ||
} | ||
return self; | ||
} | ||
|
||
- (void) addPlayer:(id <Player>) player{ | ||
if ([self.players count] < GOMOKU_PLAYERS) { | ||
[self.players addObject:player]; | ||
} else { | ||
NSLog(@"already have enough players!"); | ||
} | ||
} | ||
|
||
- (void) startGame { | ||
if ([self.players count] != GOMOKU_PLAYERS) { | ||
NSLog(@"not enough players added!"); | ||
return; | ||
} | ||
self.gameStarted = YES; | ||
NSLog(@"starting %@", self); | ||
// call first player | ||
} | ||
|
||
- (id<Player>) player:(int) index { | ||
if (index < [self.players count]) | ||
return [self.players objectAtIndex:index]; | ||
else | ||
return nil; | ||
} | ||
|
||
|
||
- (void) makeMove: (Move *) move { | ||
if (self.gameStarted != YES) { | ||
NSLog(@"game is not started, can't make this move %@", move); | ||
return; | ||
} | ||
if ([self isMoveValid:move] == YES) { | ||
// add move to the history | ||
[[self.moves objectAtIndex:currentPlayerIndex] addObject:move]; | ||
|
||
// [board makeMove:CELL_WHITE At:move]; | ||
// update the board | ||
[delegate moveMade:move byPlayer:currentPlayerIndex]; | ||
|
||
// change current player | ||
self.currentPlayerIndex++; | ||
self.currentPlayerIndex %= GOMOKU_PLAYERS; | ||
} else { | ||
NSLog(@"move %@ is NOT valid, ignored", move); | ||
} | ||
return; | ||
} | ||
|
||
- (BOOL) isMoveValid:(Move *)move { | ||
for (int player = 0; player < GOMOKU_PLAYERS; player++) { | ||
id previousMove; | ||
for (previousMove in [moves objectAtIndex:player]) { | ||
if ([move isEqual:previousMove]) { | ||
return NO; | ||
} | ||
} | ||
} | ||
return YES; | ||
} | ||
|
||
- (void) stopGame { | ||
gameStarted = NO; | ||
NSLog(@"stopping %@", self); | ||
} | ||
|
||
- (NSString *)description { | ||
return [NSString stringWithFormat:@"game: player1:%@, player2:%@, board:%@", | ||
[self player:0], | ||
[self player:1], | ||
self.board]; | ||
} | ||
|
||
- (void) dealloc { | ||
id player; | ||
for (player in players) { [player release]; } | ||
[players release]; | ||
|
||
id move; | ||
for (move in moves) { [move release]; } | ||
[moves release]; | ||
[super dealloc]; | ||
} | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// GameBoardViewController.h | ||
// gomoku | ||
// | ||
// Created by Konstantin Gredeskoul on 5/2/10. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
#import "BoardCell.h" | ||
#import "Game.h" | ||
|
||
#define MAX_CELL_WIDTH 60.0 | ||
|
||
@interface GameBoardViewController : UIViewController <UIScrollViewDelegate, BoardCellDelegate, GameDelegate> { | ||
UIScrollView *boardScrollView; | ||
UIView *boardView; | ||
NSMutableArray *cells; | ||
id mainController; | ||
} | ||
|
||
@property(nonatomic, retain) IBOutlet UIScrollView *boardScrollView; | ||
@property(nonatomic, retain) UIView *boardView; | ||
@property(nonatomic, retain) NSMutableArray *cells; | ||
@property(nonatomic, retain) id mainController; | ||
@property(nonatomic, retain) NSMutableArray *cellImages; | ||
|
||
@end |
Oops, something went wrong.