Permalink
Browse files

adding Undo functionality

  • Loading branch information...
1 parent ed63036 commit b3f8f2c77151653a716750ffb4209f1aba91a5a1 @kigster committed Feb 20, 2012
View
@@ -20,15 +20,18 @@
int size;
// two dimensional int array filled with values above.
int **matrix;
+ int moveCount;
}
@property(nonatomic) int size;
+@property(nonatomic) int moveCount;
@property(nonatomic) int** matrix;
typedef int (^MatrixDirection)(int,int, BOOL*);
- (Board *)initWithSize: (int)size;
- (void) makeMove:(int) color At:(Move *) move;
+- (void) undoMove:(int) color At:(Move *) move;
- (BOOL) isMoveValid:(Move *) move;
- (BOOL) isGameOver;
- (BOOL) walkTheBoard: (MatrixDirection) block;
View
@@ -11,6 +11,7 @@
@implementation Board
@synthesize size;
@synthesize matrix;
+@synthesize moveCount;
-(Board *)initWithSize: (int)thisSize {
if (self = [super init]) {
@@ -24,11 +25,17 @@ -(Board *)initWithSize: (int)thisSize {
self.matrix[i][j] = CELL_EMPTY;
}
}
+ self.moveCount = 0;
}
NSLog(@"initialized %@", self);
return self;
}
-
+
+-(void) undoMove:(int) color At:(Move *) move {
+ self.matrix[move.x][move.y] = CELL_EMPTY;
+ self.moveCount --;
+}
+
-(void) makeMove:(int) color At:(Move *) move {
//NSLog(@"received move to %d %@", color, move);
// update the board at move.x, move.y
@@ -38,6 +45,7 @@ -(void) makeMove:(int) color At:(Move *) move {
}
// else all is good.
self.matrix[move.x][move.y] = color;
+ self.moveCount ++;
}
- (BOOL) isMoveValid:(Move *) move {
View
@@ -13,16 +13,16 @@
@protocol GameDelegate
-
- (void) moveMade:(Move *) move byPlayer:(int) playerIndex;
+- (void) undoMove:(Move *) move byPlayer:(int) playerIndex;
- (void) gameOverWithWinner:(int) playerIndex;
-
@end
@interface Game : NSObject {
NSMutableArray *players;
NSMutableArray *moves;
+ NSMutableArray *redoMoves;
int currentPlayerIndex;
Board *board;
Config *config;
@@ -33,6 +33,7 @@
@property (strong, nonatomic) id<GameDelegate> delegate;
@property (strong, nonatomic) NSMutableArray *players;
@property (strong, nonatomic) NSMutableArray *moves;
+@property (strong, nonatomic) NSMutableArray *redoMoves;
@property (nonatomic) int currentPlayerIndex;
@property (strong, nonatomic) Board *board;
@property (strong, nonatomic) Config *config;
@@ -42,8 +43,12 @@
- (void) addPlayer:(id <Player>) player;
- (id<Player>) player:(int) index;
- (void) makeMove: (Move *) move;
+- (Move *) lastMove;
+- (void) undoLastMove;
- (BOOL) isMoveValid: (Move *) move;
- (void) startGame;
- (void) stopGame;
- (int) currentPlayerColor;
+- (int) otherPlayerColor;
+- (void) moveToNextPlayer ;
@end
View
@@ -14,6 +14,7 @@ @implementation Game
@synthesize players;
@synthesize moves;
+@synthesize redoMoves;
@synthesize currentPlayerIndex;
@synthesize board;
@synthesize config;
@@ -28,6 +29,7 @@ - (Game *) initGameWithConfig: (Config *)gameConfig {
self.moves = [[NSMutableArray alloc] initWithCapacity:GOMOKU_PLAYERS];
for (int i = 0; i < GOMOKU_PLAYERS; i++) {
[self.moves addObject: [[NSMutableArray alloc] initWithCapacity:20]];
+ [self.redoMoves addObject: [[NSMutableArray alloc] initWithCapacity:20]];
}
self.board = [[Board alloc] initWithSize:[config boardSize]];
self.gameStarted = NO;
@@ -61,6 +63,25 @@ - (void) startGame {
return nil;
}
+- (Move *) lastMove {
+ Move *undoMove = [[self.moves objectAtIndex:[self otherPlayerColor]] lastObject];
+ return undoMove;
+}
+
+- (void) undoLastMove {
+ if (self.board.moveCount > 0) {
+ [self moveToNextPlayer];
+
+ Move *undoMove = [[self.moves objectAtIndex:currentPlayerIndex] lastObject];
+ [[self.moves objectAtIndex:currentPlayerIndex] removeObject:undoMove];
+
+ [board undoMove:currentPlayerIndex At:undoMove];
+ [delegate undoMove:undoMove byPlayer:currentPlayerIndex];
+ if (self.gameStarted == NO) {
+ self.gameStarted = YES;
+ }
+ }
+}
- (void) makeMove: (Move *) move {
if (self.gameStarted != YES) {
@@ -75,9 +96,8 @@ - (void) makeMove: (Move *) move {
[board makeMove:[self currentPlayerColor] At:move];
int playerJustMoved = self.currentPlayerIndex;
- // change current player
- self.currentPlayerIndex++;
- self.currentPlayerIndex %= GOMOKU_PLAYERS;
+ // change current player
+ [self moveToNextPlayer];
// update the UI
[delegate moveMade:move byPlayer:playerJustMoved];
@@ -94,6 +114,14 @@ - (void) makeMove: (Move *) move {
return;
}
+- (void) moveToNextPlayer {
+ self.currentPlayerIndex = [self otherPlayerColor];
+}
+
+- (int) otherPlayerColor {
+ return (self.currentPlayerIndex + 1) % GOMOKU_PLAYERS;
+}
+
- (BOOL) isMoveValid:(Move *)move {
return [self.board isMoveValid:move];
}
@@ -17,6 +17,8 @@
UIScrollView *boardScrollView;
UIView *boardView;
NSMutableArray *cells;
+ UIBarButtonItem *undoButton;
+ UIBarButtonItem *redoButton;
id mainController;
}
@@ -26,10 +28,13 @@
@property(nonatomic, strong) UIView *boardView;
@property(nonatomic, strong) NSMutableArray *cells;
@property(nonatomic, strong) NSMutableArray *cellImages;
+@property(nonatomic, strong) UIBarButtonItem *undoButton;
+@property(nonatomic, strong) UIBarButtonItem *redoButton;
@property(nonatomic, strong) IBOutlet UIScrollView *boardScrollView;
-@property (nonatomic, strong) IBOutlet UILabel *gameStatus;
+@property(nonatomic, strong) IBOutlet UILabel *gameStatus;
- (void) initBoardWithGame:(Game *)game;
+- (void) undoRedoMove:(id) sender;
@end
@@ -10,6 +10,7 @@
#import "Game.h"
#import "Move.h"
+
@implementation GameBoardViewController
@synthesize boardScrollView;
@@ -19,9 +20,14 @@ @implementation GameBoardViewController
@synthesize cellImages;
@synthesize gameStatus;
@synthesize game;
+@synthesize undoButton;
+@synthesize redoButton;
+
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
+
+ //[[self navigationItem] setTitle:@"Some Title"];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
@@ -76,7 +82,20 @@ - (void) initBoardWithGame:(Game *) newGame {
self.boardScrollView.zoomScale = screenWidth / viewSize;
self.boardScrollView.contentSize = CGSizeMake(viewSize, viewSize);
-
+
+ // create Back button
+ undoButton = [[UIBarButtonItem alloc] initWithTitle:@"Undo"
+ style:UIBarButtonItemStylePlain
+ target:self
+ action:@selector(undoRedoMove:)];
+
+// redoButton = [[UIBarButtonItem alloc] initWithTitle:@"Redo"
+// style:UIBarButtonItemStylePlain
+// target:self
+// action:@selector(undoRedoMove:)];
+
+ NSArray *buttons = [NSArray arrayWithObjects: undoButton, redoButton, nil];
+ self.navigationItem.rightBarButtonItems = buttons;
}
@@ -85,6 +104,17 @@ - (void)viewDidUnload {
self.boardScrollView = nil;
}
+- (void)undoRedoMove:(id)sender {
+ if (sender == undoButton) {
+ NSLog(@"undo UNDO pressed");
+ for (int i = 0; i <= 1; i++) {
+ [[self game] undoLastMove];
+ }
+
+ } else if (sender == redoButton) {
+ NSLog(@"undo redo pressed");
+ }
+}
#pragma mark BoardCellDelegate methods
@@ -107,12 +137,20 @@ - (void) moveMade:(Move *) move byPlayer:(int) playerIndex {
NSString *nextPlayer = self.game.currentPlayerIndex == 0 ? @"X" : @"O";
NSString *status = [NSString stringWithFormat:@"Next move: Player %@", nextPlayer];
[self.gameStatus setText:status];
+}
-
+- (void) undoMove:(Move *) move byPlayer:(int) player {
+ BoardCell *cell = [self.cells objectAtIndex:(move.x * self.game.config.boardSize + move.y)];
+ cell.image = [self.cellImages objectAtIndex:0];
+ NSString *nextPlayer = self.game.currentPlayerIndex == 0 ? @"X" : @"O";
+ NSString *status = [NSString stringWithFormat:@"Next move: Player %@", nextPlayer];
+ [self.gameStatus setText:status];
}
+
- (void) gameOverWithWinner:(int) playerIndex {
- NSString *status = [NSString stringWithFormat:@"Player %d Won!", (playerIndex + 1)];
+ NSString *winner = [self.game otherPlayerColor] == 0 ? @"X" : @"O";
+ NSString *status = [NSString stringWithFormat:@"Player '%@' Won Yo!", winner];
[self.gameStatus setText:status];
NSLog(@"GAME OVER, player %d won!", playerIndex);
}
@@ -2,7 +2,7 @@
<archive type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="7.10">
<data>
<int key="IBDocument.SystemTarget">1280</int>
- <string key="IBDocument.SystemVersion">11D50</string>
+ <string key="IBDocument.SystemVersion">11D50b</string>
<string key="IBDocument.InterfaceBuilderVersion">1938</string>
<string key="IBDocument.AppKitVersion">1138.32</string>
<string key="IBDocument.HIToolboxVersion">568.00</string>
@@ -59,7 +59,7 @@
<object class="IBUIScrollView" id="408326628">
<reference key="NSNextResponder" ref="191373211"/>
<int key="NSvFlags">268</int>
- <string key="NSFrame">{{0, 42}, {320, 320}}</string>
+ <string key="NSFrame">{{0, 43}, {320, 320}}</string>
<reference key="NSSuperview" ref="191373211"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="504897353"/>
@@ -75,7 +75,7 @@
<object class="IBUILabel" id="504897353">
<reference key="NSNextResponder" ref="191373211"/>
<int key="NSvFlags">292</int>
- <string key="NSFrame">{{20, 391}, {280, 36}}</string>
+ <string key="NSFrame">{{20, 386}, {280, 54}}</string>
<reference key="NSSuperview" ref="191373211"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView"/>
@@ -95,17 +95,19 @@
</object>
<string key="IBUIShadowOffset">{1, 1}</string>
<int key="IBUIBaselineAdjustment">1</int>
+ <bool key="IBUIAdjustsFontSizeToFit">NO</bool>
<float key="IBUIMinimumFontSize">15</float>
+ <int key="IBUINumberOfLines">3</int>
<int key="IBUITextAlignment">1</int>
<object class="IBUIFontDescription" key="IBUIFontDescription">
<string key="name">AmericanTypewriter-Bold</string>
<string key="family">American Typewriter</string>
<int key="traits">2</int>
- <double key="pointSize">20</double>
+ <double key="pointSize">22</double>
</object>
<object class="NSFont" key="IBUIFont">
<string key="NSName">AmericanTypewriter-Bold</string>
- <double key="NSSize">20</double>
+ <double key="NSSize">22</double>
<int key="NSfFlags">16</int>
</object>
</object>
@@ -175,8 +177,8 @@
<reference key="object" ref="191373211"/>
<object class="NSMutableArray" key="children">
<bool key="EncodedWithXMLCoder">YES</bool>
- <reference ref="408326628"/>
<reference ref="652275340"/>
+ <reference ref="408326628"/>
<reference ref="504897353"/>
</object>
<reference key="parent" ref="0"/>
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<VariablesViewState
+ version = "1.0">
+ <ContextStates>
+ <ContextState
+ contextName = "calc_score_at:basic_ai.c">
+ <PersistentStrings>
+ <PersistentString
+ value = "row">
+ </PersistentString>
+ </PersistentStrings>
+ </ContextState>
+ <ContextState
+ contextName = "calc_score_in_one_dimension:basic_ai.c">
+ <PersistentStrings>
+ <PersistentString
+ value = "*(row+2)">
+ </PersistentString>
+ <PersistentString
+ value = "*(row + 3)">
+ </PersistentString>
+ <PersistentString
+ value = "*(row+1)">
+ </PersistentString>
+ </PersistentStrings>
+ </ContextState>
+ </ContextStates>
+</VariablesViewState>

0 comments on commit b3f8f2c

Please sign in to comment.