Permalink
Browse files

Adding config class for the game.

- making board size configurable
- adding selector UI widget
  • Loading branch information...
1 parent a9ecb20 commit 1432cd1a2b7cf5caf98bc710be8f30ae2ce99043 @kigster committed Feb 2, 2012
View
@@ -13,7 +13,6 @@
#define CELL_BLACK 1 // first player
#define CELL_WHITE 2 // second player
-#define GOMOKU_BOARD_SIZE 10
#define GOMOKU_PLAYERS 2
#define GOMOKU_REQUIRED_TO_WIN 5
View
@@ -30,7 +30,7 @@ -(Board *)initWithSize: (int)thisSize {
}
-(void) makeMove:(int) color At:(Move *) move {
- NSLog(@"received move to %d %@", color, 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);
@@ -48,20 +48,17 @@ - (BOOL) isMoveValid:(Move *) move {
- (BOOL) walkTheBoard: (MatrixDirection) block {
int i,j;
BOOL continuous;
- for (i = 0; i < GOMOKU_BOARD_SIZE; i++) {
+ for (i = 0; i < self.size; i++) {
int lastValue = CELL_EMPTY;
int lastValueCount = 0;
- for (j = 0; j < GOMOKU_BOARD_SIZE; j++) {
+ for (j = 0; j < self.size; j++) {
continuous = TRUE;
int currentValue = block(i,j, &continuous);
- if (continuous == FALSE){
- NSLog(@"continuety broken for i=%d, j=%d", i,j);
- }
if (lastValue != CELL_EMPTY && continuous == TRUE && lastValue == currentValue) {
lastValueCount ++;
if (lastValueCount == GOMOKU_REQUIRED_TO_WIN) {
- NSLog(@"found five");
+ NSLog(@"found five in a row!");
return YES;
}
} else {
@@ -85,15 +82,15 @@ - (BOOL) isGameOver {
return self.matrix[j][i];
};
MatrixDirection diagonalWalkLeftRight = ^(int i, int j, BOOL *continuous) {
- int index = (i + j + 1) % GOMOKU_BOARD_SIZE;
- if (index == 1 && (i + j + 1) > GOMOKU_BOARD_SIZE) {
+ int index = (i + j + 1) % self.size;
+ if (index == 1 && (i + j + 1) > self.size) {
*continuous = FALSE;
}
return self.matrix[index][j];
};
MatrixDirection diagonalWalkRightLeft = ^(int i, int j, BOOL *continuous) {
- int index = ((i - j) < 0) ? GOMOKU_BOARD_SIZE + i - j : (i - j);
- if ((index + 1) == GOMOKU_BOARD_SIZE) {
+ int index = ((i - j) < 0) ? self.size + i - j : (i - j);
+ if ((index + 1) == self.size) {
*continuous = FALSE;
}
return self.matrix[index][j];
View
@@ -0,0 +1,18 @@
+//
+// Config.h
+// gomoku
+//
+// Created by Konstantin Gredeskoul on 2/2/12.
+// Copyright (c) 2012 ModCloth Inc. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface Config : NSObject {
+ int boardSize;
+}
+
+@property(nonatomic, assign) int boardSize;
+
+@end
+
View
@@ -0,0 +1,15 @@
+//
+// Config.m - this class encapsulates game configuration (startup options).
+//
+// Created by Konstantin Gredeskoul on 2/2/12.
+//
+
+#import "Config.h"
+
+@implementation Config
+
+@synthesize boardSize;
+
+
+
+@end
View
@@ -9,7 +9,7 @@
#import <Foundation/Foundation.h>
#import "Player.h"
#import "Board.h"
-
+#import "Config.h"
@protocol GameDelegate
@@ -25,6 +25,7 @@
NSMutableArray *moves;
int currentPlayerIndex;
Board *board;
+ Config *config;
@private
BOOL gameStarted;
}
@@ -35,9 +36,10 @@
@property (nonatomic, assign) NSMutableArray *moves;
@property (nonatomic, assign) int currentPlayerIndex;
@property (nonatomic, assign) Board *board;
+@property (nonatomic, assign) Config *config;
@property (nonatomic, assign) BOOL gameStarted;
-- (Game *) initGame;
+- (Game *) initGameWithConfig: (Config *)config;
- (void) addPlayer:(id <Player>) player;
- (id<Player>) player:(int) index;
- (void) makeMove: (Move *) move;
View
@@ -15,18 +15,20 @@ @implementation Game
@synthesize moves;
@synthesize currentPlayerIndex;
@synthesize board;
+@synthesize config;
@synthesize gameStarted;
@synthesize delegate;
-- (Game *) initGame {
+- (Game *) initGameWithConfig: (Config *)gameConfig {
if (self = [super init]) {
+ self.config = gameConfig;
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.board = [[Board alloc] initWithSize:[config boardSize]];
self.gameStarted = NO;
self.currentPlayerIndex = 0;
}
@@ -71,13 +73,14 @@ - (void) makeMove: (Move *) move {
// save the move
[board makeMove:[self currentPlayerColor] At:move];
+ int playerJustMoved = self.currentPlayerIndex;
// change current player
self.currentPlayerIndex++;
self.currentPlayerIndex %= GOMOKU_PLAYERS;
// update the UI
- [delegate moveMade:move byPlayer:currentPlayerIndex];
-
+ [delegate moveMade:move byPlayer:playerJustMoved];
+
if ([board isGameOver]) {
[delegate gameOverWithWinner:currentPlayerIndex];
self.gameStarted = NO;
@@ -11,21 +11,25 @@
#define MAX_CELL_WIDTH 60.0
-@interface GameBoardViewController : UIViewController <UIScrollViewDelegate, BoardCellDelegate, GameDelegate> {
+@interface GameBoardViewController : UIViewController <UIScrollViewDelegate,
+ BoardCellDelegate, GameDelegate> {
+ Game *game;
UIScrollView *boardScrollView;
UIView *boardView;
NSMutableArray *cells;
- id mainController;
+ id mainController;
}
-@property(nonatomic, retain) IBOutlet UIScrollView *boardScrollView;
+@property(nonatomic, retain) Game *game;
+@property(nonatomic, retain) id mainController;
+
@property(nonatomic, retain) UIView *boardView;
@property(nonatomic, retain) NSMutableArray *cells;
-@property(nonatomic, retain) id mainController;
@property(nonatomic, retain) NSMutableArray *cellImages;
+@property(nonatomic, retain) IBOutlet UIScrollView *boardScrollView;
@property (nonatomic, retain) IBOutlet UILabel *gameStatus;
-- (void) initBoard;
+- (void) initBoardWithGame:(Game *)game;
@end
@@ -18,14 +18,18 @@ @implementation GameBoardViewController
@synthesize mainController;
@synthesize cellImages;
@synthesize gameStatus;
+@synthesize game;
- (void)viewDidLoad {
[super viewDidLoad];
}
-- (void) initBoard {
- self.boardView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, GOMOKU_BOARD_SIZE * MAX_CELL_WIDTH,
- GOMOKU_BOARD_SIZE * MAX_CELL_WIDTH)] autorelease];
+- (void) initBoardWithGame:(Game *) newGame {
+ self.game = newGame;
+ int boardSize = self.game.config.boardSize;
+
+ self.boardView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, boardSize * MAX_CELL_WIDTH,
+ boardSize * MAX_CELL_WIDTH)] autorelease];
[self.gameStatus setText:@"Game Starting!"];
if (self.cellImages == NULL) {
@@ -45,8 +49,8 @@ - (void) initBoard {
self.cells = [NSMutableArray array];
//setup squares of the board
- for (int x = 0; x < GOMOKU_BOARD_SIZE; x++) {
- for (int y = 0; y < GOMOKU_BOARD_SIZE; y++) {
+ for (int x = 0; x < boardSize; x++) {
+ for (int y = 0; y < boardSize; y++) {
BoardCell *currentCell = [[[BoardCell alloc] initWithFrame:CGRectMake(x * MAX_CELL_WIDTH,
y * MAX_CELL_WIDTH,
MAX_CELL_WIDTH,
@@ -60,9 +64,12 @@ - (void) initBoard {
[self.boardScrollView addSubview:self.boardView];
- self.boardScrollView.zoomScale = 0.535;
- self.boardScrollView.contentSize = CGSizeMake(GOMOKU_BOARD_SIZE * MAX_CELL_WIDTH,
- GOMOKU_BOARD_SIZE * MAX_CELL_WIDTH);
+ CGRect screenRect = [[UIScreen mainScreen] bounds];
+ CGFloat screenWidth = screenRect.size.width;
+ int viewSize = boardSize * MAX_CELL_WIDTH;
+
+ self.boardScrollView.zoomScale = screenWidth / viewSize;
+ self.boardScrollView.contentSize = CGSizeMake(viewSize, viewSize);
}
@@ -84,17 +91,17 @@ - (void)dealloc {
- (void) selectedBoardCell:(BoardCell *)theCell {
int index = [self.cells indexOfObject: theCell];
// calculate the move coordinates
- Move *move = [[[Move alloc] initWithX: index / GOMOKU_BOARD_SIZE
- AndY: index % GOMOKU_BOARD_SIZE ] retain];
+ Move *move = [[[Move alloc] initWithX: index / self.game.config.boardSize
+ AndY: index % self.game.config.boardSize ] retain];
// pass to the main controller for processing.
[self.mainController makeMove:move];
}
#pragma mark GameDelegate methods
- (void) moveMade:(Move *) move byPlayer:(int) playerIndex {
- NSLog(@"updating cell for playerIndex %d, at coordinates %@", playerIndex, move);
- BoardCell *cell = [self.cells objectAtIndex:(move.x * GOMOKU_BOARD_SIZE + move.y)];
+ NSLog(@"redrawing cell for playerIndex %d at %@", playerIndex, move);
+ BoardCell *cell = [self.cells objectAtIndex:(move.x * self.game.config.boardSize + move.y)];
cell.image = [self.cellImages objectAtIndex:(playerIndex + 1)];
NSString *nextPlayer = ([((GomokuViewController *)self.mainController) game]).currentPlayerIndex == 0 ? @"X" : @"O";
NSString *status = [NSString stringWithFormat:@"Next move: Player %@", nextPlayer];
@@ -91,7 +91,6 @@
<int key="NSvFlags">292</int>
<string key="NSFrame">{{20, 374}, {280, 36}}</string>
<reference key="NSSuperview" ref="191373211"/>
- <reference key="NSNextKeyView"/>
<bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClipsSubviews">YES</bool>
<bool key="IBUIUserInteractionEnabled">NO</bool>
@@ -10,13 +10,19 @@
#import "Game.h"
#import "Move.h"
-@interface GomokuViewController : UIViewController{
+@interface GomokuViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>{
GameBoardViewController* gameBoardController;
Game *game;
+ IBOutlet UIPickerView *pickerView;
+ NSMutableArray *boardSizes;
+ Config *config;
}
@property(nonatomic, retain) GameBoardViewController *gameBoardController;
@property(nonatomic, assign) Game *game;
+@property(nonatomic, retain) Config *config;
+@property(nonatomic, assign) NSMutableArray *boardSizes;
+@property(nonatomic, retain) IBOutlet UIPickerView *pickerView;
- (IBAction) startSinglePlayerGame:(id) sender;
- (IBAction) startTwoPlayerGame:(id) sender;
@@ -12,6 +12,9 @@ @implementation GomokuViewController
@synthesize gameBoardController;
@synthesize game;
+@synthesize pickerView;
+@synthesize boardSizes;
+@synthesize config;
- (IBAction) startSinglePlayerGame:(id) sender {
[self startGameWithPlayers:1];
@@ -22,12 +25,12 @@ - (IBAction) startTwoPlayerGame:(id) sender {
}
- (void) startGameWithPlayers: (int) playerCount {
- if (game != NULL) {
+ if (game != nil) {
[game release];
- game = NULL;
+ game = nil;
}
- game = [[Game alloc] initGame];
+ game = [[Game alloc] initGameWithConfig:config];
game.delegate = gameBoardController;
[game addPlayer:[[[UIPlayer alloc] initWithGame:game] autorelease]];
@@ -36,7 +39,7 @@ - (void) startGameWithPlayers: (int) playerCount {
NSLog(@"created game with players: %@", self.game);
[self.navigationController pushViewController:gameBoardController animated:YES];
- [gameBoardController initBoard];
+ [gameBoardController initBoardWithGame:game];
}
- (void) makeMove: (Move *) move {
@@ -45,6 +48,42 @@ - (void) makeMove: (Move *) move {
}
}
+-(void)viewDidLoad {
+ if (![self config])
+ self.config = [[Config alloc] init];
+
+ self.config.boardSize = 10;
+
+ [super viewDidLoad];
+ boardSizes = [[NSMutableArray alloc] initWithCapacity:3];
+ [boardSizes addObject:@"10 x 10"];
+ [boardSizes addObject:@"11 x 11"];
+ [boardSizes addObject:@"15 x 15"];
+ [boardSizes addObject:@"19 x 19"];
+}
+
+#pragma mark UIPickerViewDataSource methods
+
+- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
+ return 1 ;
+}
+
+- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
+ return [boardSizes count];
+}
+
+- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
+ return [boardSizes objectAtIndex:row];
+}
+
+#pragma mark UIPickerViewDelegate methods
+
+- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
+ NSLog(@"selected board size: %@. Index of selected color: %i", [boardSizes objectAtIndex:row], row);
+ NSString *label = [boardSizes objectAtIndex:row];
+ self.config.boardSize = label.intValue;
+}
+
- (void)dealloc {
[gameBoardController release];
[game release];
Oops, something went wrong.

0 comments on commit 1432cd1

Please sign in to comment.