From 4316a5401100828e7b8daa99397b2f361a7f7d12 Mon Sep 17 00:00:00 2001 From: Eric Sanchez Date: Sat, 27 Jun 2015 12:23:57 -0400 Subject: [PATCH 01/24] changed readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 082e4ed..81496b2 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,5 @@ All directions are here... https://github.com/accesscode-2-2/unit-0/tree/master/project Instructions on forking and pull requests here... https://github.com/accesscode-2-2/how-to-pr + +hi my name is eric \ No newline at end of file From 24d0f4c48bf17d239dd6ee8461f6e7ab74cd9fed Mon Sep 17 00:00:00 2001 From: Derek Date: Sat, 27 Jun 2015 12:29:38 -0400 Subject: [PATCH 02/24] yo --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 81496b2..6a94e9a 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,6 @@ https://github.com/accesscode-2-2/unit-0/tree/master/project Instructions on forking and pull requests here... https://github.com/accesscode-2-2/how-to-pr -hi my name is eric \ No newline at end of file +hi my name is eric + +lololololol \ No newline at end of file From 5e6c6efd2fb32059cf5aba27b5dd82b631270aa3 Mon Sep 17 00:00:00 2001 From: Eric Sanchez Date: Sat, 27 Jun 2015 14:49:05 -0400 Subject: [PATCH 03/24] first commit --- TicTacToe/TicTacToe/main.m | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 3a713ee..e887487 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -8,10 +8,41 @@ #import +@interface game:NSObject + +-(BOOL)checkWin; +-(void)startGame; +-(BOOL)isEmpty; +-(void)printGame; +-(void)turn; + + + + + + +@end + +@implementation game{ + +NSArray *gameBoard[9]; +BOOL *win; +NSInteger *difficult; + + +} + +-(BOOL)checkWin; +-(void)startGame; +-(BOOL)isEmpty; +-(void)printGame; +-(void)turn; + +@end + int main(int argc, const char * argv[]) { @autoreleasepool { - // insert code here... - NSLog(@"Hello, World!"); + } return 0; } From 9610b03a03a46521dbd620f4de3de344a7c0f47c Mon Sep 17 00:00:00 2001 From: Eric Sanchez Date: Sat, 27 Jun 2015 15:11:18 -0400 Subject: [PATCH 04/24] set up game class --- TicTacToe/TicTacToe/main.m | 43 +++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index e887487..18c3e2f 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -9,11 +9,11 @@ #import @interface game:NSObject - +//This is all the possible messages for the game class. -(BOOL)checkWin; -(void)startGame; -(BOOL)isEmpty; --(void)printGame; +-(void)printBoard; -(void)turn; @@ -30,19 +30,46 @@ @implementation game{ NSInteger *difficult; +} +-(void) printBoard { + NSLog(@"Blah"); +} +-(void)startGame { +NSLog(@"Welcome to Derek's and Eric's TicTacToe!"); +[self printBoard]; } --(BOOL)checkWin; --(void)startGame; --(BOOL)isEmpty; --(void)printGame; --(void)turn; + +//-(BOOL)isEmpty; +//-(void)printGame; +//-(void)turn; +//-(BOOL)checkWin; @end int main(int argc, const char * argv[]) { @autoreleasepool { - + + game *ticTacToe =[[game alloc]init]; + + [ticTacToe startGame]; + + + + + + + + + + + + + + + + + } return 0; } From d0b338e96ad9d5d8b3100d2cd4cf7dbc37e1a887 Mon Sep 17 00:00:00 2001 From: Derek Date: Sat, 27 Jun 2015 15:12:54 -0400 Subject: [PATCH 05/24] x --- TicTacToe/TicTacToe/main.m | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 3a713ee..788fc9c 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -8,10 +8,15 @@ #import + + +game{ + +} + int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... - NSLog(@"Hello, World!"); } return 0; } From 09600c8c829270d21249feb375c70d669cc821e6 Mon Sep 17 00:00:00 2001 From: Derek Date: Sat, 27 Jun 2015 17:30:19 -0400 Subject: [PATCH 06/24] made checkWin --- TicTacToe/TicTacToe/main.m | 114 ++++++++++++++++++++++++++++++------- 1 file changed, 93 insertions(+), 21 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 6ced71f..e443009 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -12,7 +12,6 @@ @interface game:NSObject //This is all the possible messages for the game class. -(BOOL)checkWin; -(void)startGame; --(BOOL)isEmpty; -(void)printBoard; -(void)printTutorialBoard; @@ -43,7 +42,7 @@ -(void) printBoard { } -(void)startGame { NSLog(@"Welcome to Derek's and Eric's TicTacToe!"); - win = false; + win = NO; //fill gameboard with spaces gameBoard = [[NSMutableArray alloc] init]; @@ -54,37 +53,114 @@ -(void)startGame { //print tutorial board [self printTutorialBoard]; + //Game Loop playerTurn = 1; - while (win == false) { + while (win == NO) { [self turn]; - [self printBoard]; } } -(void)turn{ - if (playerTurn == 1) { + while (playerTurn == 1) { NSLog(@"Player 1, make your move: "); int currentMove; scanf("%d",¤tMove); - // integer currentMove is cast as an NSUInteger - [gameBoard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"X"]; - playerTurn = 2; + + if (([gameBoard[currentMove] isEqual: @" "]) && (currentMove>=0) && (currentMove<=8)) { + [gameBoard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"X"]; + playerTurn = 2; + } + else { + NSLog(@"Invalid move. Enter a number from 0-8."); + } } - else { + + [self printBoard]; + + while (playerTurn == 2) { NSLog(@"Player 2, make your move: "); int currentMove; scanf("%d",¤tMove); - // integer currentMove is cast as an NSUInteger - [gameBoard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"O"]; - playerTurn = 1; + fpurge(stdin); + + if (([gameBoard[currentMove] isEqual: @" "]) && (currentMove>=0) && (currentMove<=8)) { + [gameBoard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"O"]; + playerTurn = 1; + } + else { + NSLog(@"Invalid move. Enter a number from 0-8."); + } + } - + [self printBoard]; } - -//-(BOOL)isEmpty; - -//-(BOOL)checkWin; +-(BOOL)checkWin{ + + NSString *playerPiece; + + for (int i = 0; i < 2; i++) { + + //sets player piece for each of the two loops + if (i == 1) { + playerPiece = @"X"; + } + else{ + playerPiece = @"O"; + } + + // |X|X|X| + // | | | | + // | | | | + if (([gameBoard[0] isEqual:playerPiece]) && ([gameBoard[1] isEqual:playerPiece]) && ([gameBoard[2] isEqual:playerPiece])){ + return YES; + } + // | | | | + // |X|X|X| + // | | | | + if (([gameBoard[3] isEqual:playerPiece]) && ([gameBoard[4] isEqual:playerPiece]) && ([gameBoard[6] isEqual:playerPiece])){ + return YES; + } + // | | | | + // | | | | + // |X|X|X| + if (([gameBoard[6] isEqual:playerPiece]) && ([gameBoard[7] isEqual:playerPiece]) && ([gameBoard[8] isEqual:playerPiece])){ + return YES; + } + // |X| | | + // |X| | | + // |X| | | + if (([gameBoard[0] isEqual:playerPiece]) && ([gameBoard[3] isEqual:playerPiece]) && ([gameBoard[6] isEqual:playerPiece])){ + return YES; + } + // | |X| | + // | |X| | + // | |X| | + if (([gameBoard[1] isEqual:playerPiece]) && ([gameBoard[4] isEqual:playerPiece]) && ([gameBoard[7] isEqual:playerPiece])){ + return YES; + } + // | | |X| + // | | |X| + // | | |X| + if (([gameBoard[2] isEqual:playerPiece]) && ([gameBoard[5] isEqual:playerPiece]) && ([gameBoard[8] isEqual:playerPiece])){ + return YES; + } + // |X| | | + // | |X| | + // | | |X| + if (([gameBoard[0] isEqual:playerPiece]) && ([gameBoard[4] isEqual:playerPiece]) && ([gameBoard[8] isEqual:playerPiece])){ + return YES; + } + // | | |X| + // | |X| | + // |X| | | + if (([gameBoard[2] isEqual:playerPiece]) && ([gameBoard[4] isEqual:playerPiece]) && ([gameBoard[6] isEqual:playerPiece])){ + return YES; + } + + } + return NO; +} @end @@ -92,13 +168,9 @@ int main(int argc, const char * argv[]) { @autoreleasepool { game *ticTacToe =[[game alloc]init]; - [ticTacToe startGame]; - - - } return 0; } From 429ed5462abe4fa7e99c04705054e7f94dc9c45d Mon Sep 17 00:00:00 2001 From: Eric Sanchez Date: Sat, 27 Jun 2015 17:33:31 -0400 Subject: [PATCH 07/24] yeah --- TicTacToe/TicTacToe/main.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 6ced71f..832851f 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -66,9 +66,12 @@ -(void)turn{ NSLog(@"Player 1, make your move: "); int currentMove; scanf("%d",¤tMove); + // integer currentMove is cast as an NSUInteger - [gameBoard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"X"]; + if ([gameBoard[currentMove] isEqual:@" "]) { + [gameBoard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"X"]; playerTurn = 2; + } else { NSLog(@"Player 2, make your move: "); From b191a04c4604bdbe00a7f0957aa61993dfbee5ef Mon Sep 17 00:00:00 2001 From: Derek Date: Sun, 28 Jun 2015 10:46:21 -0400 Subject: [PATCH 08/24] before making win message --- TicTacToe/TicTacToe/main.m | 71 +++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index e443009..efb1ac6 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -8,8 +8,8 @@ #import -@interface game:NSObject -//This is all the possible messages for the game class. +@interface Game:NSObject + -(BOOL)checkWin; -(void)startGame; @@ -20,11 +20,16 @@ -(void)turn; @end -@implementation game{ - NSMutableArray *gameBoard; - BOOL *win; - NSInteger *difficult; - int playerTurn; +@implementation Game{ + NSString *mode; + NSInteger *difficulty; + + int _playerTurn; + + BOOL *_win; + NSString *_winner; + + NSMutableArray *_gameboard; } -(void)printTutorialBoard{ @@ -35,40 +40,40 @@ -(void)printTutorialBoard{ -(void) printBoard { - NSLog(@"|%@|%@|%@|",[gameBoard objectAtIndex:0],[gameBoard objectAtIndex:1],[gameBoard objectAtIndex:2] ); - NSLog(@"|%@|%@|%@|",[gameBoard objectAtIndex:3],[gameBoard objectAtIndex:4],[gameBoard objectAtIndex:5] ); - NSLog(@"|%@|%@|%@|",[gameBoard objectAtIndex:6],[gameBoard objectAtIndex:7],[gameBoard objectAtIndex:8] ); + NSLog(@"|%@|%@|%@|",[_gameboard objectAtIndex:0],[_gameboard objectAtIndex:1],[_gameboard objectAtIndex:2] ); + NSLog(@"|%@|%@|%@|",[_gameboard objectAtIndex:3],[_gameboard objectAtIndex:4],[_gameboard objectAtIndex:5] ); + NSLog(@"|%@|%@|%@|",[_gameboard objectAtIndex:6],[_gameboard objectAtIndex:7],[_gameboard objectAtIndex:8] ); } -(void)startGame { NSLog(@"Welcome to Derek's and Eric's TicTacToe!"); - win = NO; + _win = NO; //fill gameboard with spaces - gameBoard = [[NSMutableArray alloc] init]; + _gameboard = [[NSMutableArray alloc] init]; for (int i = 0; i < 9; i++) { - [gameBoard addObject:@" "]; + [_gameboard addObject:@" "]; } //print tutorial board [self printTutorialBoard]; //Game Loop - playerTurn = 1; - while (win == NO) { + _playerTurn = 1; + while (_win == NO) { [self turn]; } } -(void)turn{ - while (playerTurn == 1) { + while (_playerTurn == 1) { NSLog(@"Player 1, make your move: "); int currentMove; scanf("%d",¤tMove); - if (([gameBoard[currentMove] isEqual: @" "]) && (currentMove>=0) && (currentMove<=8)) { - [gameBoard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"X"]; - playerTurn = 2; + if (([_gameboard[currentMove] isEqual: @" "]) && (currentMove>=0) && (currentMove<=8)) { + [_gameboard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"X"]; + _playerTurn = 2; } else { NSLog(@"Invalid move. Enter a number from 0-8."); @@ -77,15 +82,15 @@ -(void)turn{ [self printBoard]; - while (playerTurn == 2) { + while (_playerTurn == 2) { NSLog(@"Player 2, make your move: "); int currentMove; scanf("%d",¤tMove); fpurge(stdin); - if (([gameBoard[currentMove] isEqual: @" "]) && (currentMove>=0) && (currentMove<=8)) { - [gameBoard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"O"]; - playerTurn = 1; + if (([_gameboard[currentMove] isEqual: @" "]) && (currentMove>=0) && (currentMove<=8)) { + [_gameboard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"O"]; + _playerTurn = 1; } else { NSLog(@"Invalid move. Enter a number from 0-8."); @@ -104,57 +109,59 @@ -(BOOL)checkWin{ //sets player piece for each of the two loops if (i == 1) { playerPiece = @"X"; + _winner = @"Player 1"; } else{ playerPiece = @"O"; + _winner = @"Player 2"; } // |X|X|X| // | | | | // | | | | - if (([gameBoard[0] isEqual:playerPiece]) && ([gameBoard[1] isEqual:playerPiece]) && ([gameBoard[2] isEqual:playerPiece])){ + if (([_gameboard[0] isEqual:playerPiece]) && ([_gameboard[1] isEqual:playerPiece]) && ([_gameboard[2] isEqual:playerPiece])){ return YES; } // | | | | // |X|X|X| // | | | | - if (([gameBoard[3] isEqual:playerPiece]) && ([gameBoard[4] isEqual:playerPiece]) && ([gameBoard[6] isEqual:playerPiece])){ + if (([_gameboard[3] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[6] isEqual:playerPiece])){ return YES; } // | | | | // | | | | // |X|X|X| - if (([gameBoard[6] isEqual:playerPiece]) && ([gameBoard[7] isEqual:playerPiece]) && ([gameBoard[8] isEqual:playerPiece])){ + if (([_gameboard[6] isEqual:playerPiece]) && ([_gameboard[7] isEqual:playerPiece]) && ([_gameboard[8] isEqual:playerPiece])){ return YES; } // |X| | | // |X| | | // |X| | | - if (([gameBoard[0] isEqual:playerPiece]) && ([gameBoard[3] isEqual:playerPiece]) && ([gameBoard[6] isEqual:playerPiece])){ + if (([_gameboard[0] isEqual:playerPiece]) && ([_gameboard[3] isEqual:playerPiece]) && ([_gameboard[6] isEqual:playerPiece])){ return YES; } // | |X| | // | |X| | // | |X| | - if (([gameBoard[1] isEqual:playerPiece]) && ([gameBoard[4] isEqual:playerPiece]) && ([gameBoard[7] isEqual:playerPiece])){ + if (([_gameboard[1] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[7] isEqual:playerPiece])){ return YES; } // | | |X| // | | |X| // | | |X| - if (([gameBoard[2] isEqual:playerPiece]) && ([gameBoard[5] isEqual:playerPiece]) && ([gameBoard[8] isEqual:playerPiece])){ + if (([_gameboard[2] isEqual:playerPiece]) && ([_gameboard[5] isEqual:playerPiece]) && ([_gameboard[8] isEqual:playerPiece])){ return YES; } // |X| | | // | |X| | // | | |X| - if (([gameBoard[0] isEqual:playerPiece]) && ([gameBoard[4] isEqual:playerPiece]) && ([gameBoard[8] isEqual:playerPiece])){ + if (([_gameboard[0] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[8] isEqual:playerPiece])){ return YES; } // | | |X| // | |X| | // |X| | | - if (([gameBoard[2] isEqual:playerPiece]) && ([gameBoard[4] isEqual:playerPiece]) && ([gameBoard[6] isEqual:playerPiece])){ + if (([_gameboard[2] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[6] isEqual:playerPiece])){ return YES; } @@ -167,7 +174,7 @@ -(BOOL)checkWin{ int main(int argc, const char * argv[]) { @autoreleasepool { - game *ticTacToe =[[game alloc]init]; + Game *ticTacToe =[[Game alloc]init]; [ticTacToe startGame]; From de64440869c21f2b72fb6e34b42d1d58e83fcd7d Mon Sep 17 00:00:00 2001 From: Derek Date: Sun, 28 Jun 2015 11:14:38 -0400 Subject: [PATCH 09/24] xy --- TicTacToe/TicTacToe/main.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index efb1ac6..b378289 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -21,8 +21,8 @@ -(void)turn; @end @implementation Game{ - NSString *mode; - NSInteger *difficulty; + NSString *_mode; + NSInteger *_difficulty; int _playerTurn; From 9b23cd6376ce3dd3ab7fdcd9eed55991bbdc6c42 Mon Sep 17 00:00:00 2001 From: Eric Sanchez Date: Sun, 28 Jun 2015 11:30:57 -0400 Subject: [PATCH 10/24] Fixed merge conflict --- TicTacToe/TicTacToe/main.m | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 2fc7771..7da25fa 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -70,14 +70,11 @@ -(void)turn{ NSLog(@"Player 1, make your move: "); int currentMove; scanf("%d",¤tMove); -<<<<<<< HEAD + - // integer currentMove is cast as an NSUInteger - if ([gameBoard[currentMove] isEqual:@" "]) { - [gameBoard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"X"]; - playerTurn = 2; + -======= + if (([_gameboard[currentMove] isEqual: @" "]) && (currentMove>=0) && (currentMove<=8)) { [_gameboard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"X"]; @@ -86,7 +83,7 @@ -(void)turn{ else { NSLog(@"Invalid move. Enter a number from 0-8."); } ->>>>>>> 09600c8c829270d21249feb375c70d669cc821e6 + } [self printBoard]; From 13a889c490bd2659ac7a222f27b329f5af863ec9 Mon Sep 17 00:00:00 2001 From: Eric Sanchez Date: Sun, 28 Jun 2015 12:09:33 -0400 Subject: [PATCH 11/24] Added draw, win state mod --- TicTacToe/TicTacToe/main.m | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 7da25fa..696e4bf 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -26,7 +26,7 @@ @implementation Game{ int _playerTurn; - BOOL *_win; + BOOL _win; NSString *_winner; NSMutableArray *_gameboard; @@ -63,20 +63,21 @@ -(void)startGame { while (_win == NO) { [self turn]; } + } -(void)turn{ + if (([self checkWin] == FALSE) && ([_gameboard containsObject:@" "]==FALSE)){ + _win = FALSE; + NSLog(@"It's a draw."); + } while (_playerTurn == 1) { NSLog(@"Player 1, make your move: "); int currentMove; scanf("%d",¤tMove); - - - - - if (([_gameboard[currentMove] isEqual: @" "]) && (currentMove>=0) && (currentMove<=8)) { + if (([_gameboard[currentMove] isEqual: @" "]) && (currentMove>=0) && (currentMove<=8)) { [_gameboard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"X"]; _playerTurn = 2; } @@ -87,6 +88,23 @@ -(void)turn{ } [self printBoard]; + + + + if ([self checkWin] == TRUE) { + _win = TRUE; + NSLog(@"Player 1 WINS!"); + _playerTurn = 0; + + + } + + if (([self checkWin] == FALSE) && ([_gameboard containsObject:@" "]==FALSE)){ + _win = FALSE; + NSLog(@"It's a draw."); + _playerTurn = 0; + + } while (_playerTurn == 2) { NSLog(@"Player 2, make your move: "); @@ -104,6 +122,14 @@ -(void)turn{ } [self printBoard]; + if ([self checkWin] == TRUE) { + _win = TRUE; + NSLog(@"Player 2 WINS!"); + _playerTurn = 0; + + } + + } -(BOOL)checkWin{ From 3d658a40c07028222f71eae84f1db026ab38561d Mon Sep 17 00:00:00 2001 From: Derek Date: Sun, 28 Jun 2015 12:17:34 -0400 Subject: [PATCH 12/24] q --- TicTacToe/TicTacToe/main.m | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index b378289..b22578d 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -26,7 +26,7 @@ @implementation Game{ int _playerTurn; - BOOL *_win; + BOOL _win; NSString *_winner; NSMutableArray *_gameboard; @@ -82,6 +82,11 @@ -(void)turn{ [self printBoard]; + if ([self checkWin] == TRUE) { + _win = TRUE; + NSLog(@"Player 1 WINS!"); + } + while (_playerTurn == 2) { NSLog(@"Player 2, make your move: "); int currentMove; @@ -98,6 +103,11 @@ -(void)turn{ } [self printBoard]; + + if ([self checkWin] == TRUE) { + _win = TRUE; + NSLog(@"Player 2 WINS!"); + } } -(BOOL)checkWin{ From e2b8ede4642aee1f8610f3411bca6f8e1214f8bc Mon Sep 17 00:00:00 2001 From: Derek Date: Sun, 28 Jun 2015 13:55:48 -0400 Subject: [PATCH 13/24] before game size --- TicTacToe/TicTacToe/main.m | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 71ffe60..786becc 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -11,6 +11,9 @@ @interface Game:NSObject -(BOOL)checkWin; +-(BOOL)getWin; + + -(BOOL)checkDraw; -(void)startGame; @@ -20,6 +23,9 @@ -(void)printTutorialBoard; -(void)turn; +-(void)setGameSize:(int)size; +-(int)getGameSize; + @end @implementation Game{ @@ -33,6 +39,15 @@ @implementation Game{ NSString *_winner; NSMutableArray *_gameboard; + int _gameSize; +} + +-(void)setGameSize:(int)size{ + _gameSize = size; +} + +-(int)getGameSize{ + return _gameSize; } -(void)printTutorialBoard{ @@ -49,8 +64,9 @@ -(void) printBoard { } - - +-(BOOL)getWin{ + return _win; +} -(void)startGame { NSLog(@"Welcome to Derek's and Eric's TicTacToe!"); @@ -208,7 +224,19 @@ int main(int argc, const char * argv[]) { Game *ticTacToe =[[Game alloc]init]; [ticTacToe startGame]; - + // while ([ticTacToe win] == NO) { + // ask user input + // validate the users move + // if user move valid + // make move + // validate board + // if won + // + // // ask game to priint board } + return 0; } + + + From 5e95a273d9e237b9d60f61612c95dd06ba0590cd Mon Sep 17 00:00:00 2001 From: Derek Date: Sun, 28 Jun 2015 14:55:16 -0400 Subject: [PATCH 14/24] ww --- TicTacToe/TicTacToe/main.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 786becc..1bd9fe3 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -72,6 +72,9 @@ -(void)startGame { NSLog(@"Welcome to Derek's and Eric's TicTacToe!"); _win = NO; + NSLog(@"Set a game size:"); + + //fill gameboard with spaces _gameboard = [[NSMutableArray alloc] init]; for (int i = 0; i < 9; i++) { From 0391bf9d58dad18ef80ecb14fc594d547057f260 Mon Sep 17 00:00:00 2001 From: Derek Date: Sun, 28 Jun 2015 17:03:03 -0400 Subject: [PATCH 15/24] game sizes --- TicTacToe/TicTacToe/main.m | 76 +++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 1bd9fe3..dc098a3 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -13,7 +13,6 @@ @interface Game:NSObject -(BOOL)checkWin; -(BOOL)getWin; - -(BOOL)checkDraw; -(void)startGame; @@ -26,11 +25,20 @@ -(void)turn; -(void)setGameSize:(int)size; -(int)getGameSize; +//Easy, Medium, Hard +-(void)setDifficulty:(int)level; +-(int)getDifficulty; + +//Player vs. Player OR Player vs. Computer +-(void)setMode:(int)mode; +-(int)getMode; + @end @implementation Game{ - NSString *_mode; - NSInteger *_difficulty; + int _mode; + + int _difficulty; int _playerTurn; int _turnCount; @@ -42,6 +50,14 @@ @implementation Game{ int _gameSize; } +-(void)setDifficulty:(int)level{ + _difficulty = level; +} + +-(int)getDifficulty{ + return _difficulty; +} + -(void)setGameSize:(int)size{ _gameSize = size; } @@ -50,34 +66,60 @@ -(int)getGameSize{ return _gameSize; } --(void)printTutorialBoard{ - NSLog(@"|0|1|2|"); - NSLog(@"|3|4|5|"); - NSLog(@"|6|7|8|"); +-(void)setMode:(int)mode{ + _mode = mode; +} +-(int)getMode{ + return _mode; +} + +-(BOOL)getWin{ + return _win; } +-(void)printTutorialBoard{ + for (int i = 0; i < pow (_gameSize,2); i++) { + + // create a string from i + NSString *placeNumber = [NSString stringWithFormat:@"%d",i]; + int lengthOfPlaceNumber = (int)[placeNumber length]; + + // check the length of the sting + if (lengthOfPlaceNumber == 1) { + NSString *zero = [NSString stringWithFormat:@"0"]; + placeNumber = [zero stringByAppendingString:placeNumber]; + } + //convert NSString to char string + const char *placeNumberCharString = [placeNumber cStringUsingEncoding: NSUTF8StringEncoding]; + + if ((i >= _gameSize) && (i % _gameSize == 0)) { + printf("\n|%s|", placeNumberCharString); + + } + else{ + printf("|%s|", placeNumberCharString); + } + } + printf("\n"); +} -(void) printBoard { NSLog(@"|%@|%@|%@|",[_gameboard objectAtIndex:0],[_gameboard objectAtIndex:1],[_gameboard objectAtIndex:2] ); NSLog(@"|%@|%@|%@|",[_gameboard objectAtIndex:3],[_gameboard objectAtIndex:4],[_gameboard objectAtIndex:5] ); NSLog(@"|%@|%@|%@|",[_gameboard objectAtIndex:6],[_gameboard objectAtIndex:7],[_gameboard objectAtIndex:8] ); - -} - --(BOOL)getWin{ - return _win; } -(void)startGame { NSLog(@"Welcome to Derek's and Eric's TicTacToe!"); _win = NO; - - NSLog(@"Set a game size:"); - + NSLog(@"Set a game size, fool:"); + int userGameSize; + scanf("%d",&userGameSize); + [self setGameSize:userGameSize]; //fill gameboard with spaces _gameboard = [[NSMutableArray alloc] init]; - for (int i = 0; i < 9; i++) { + for (int i = 0; i < pow (_gameSize,2); i++) { [_gameboard addObject:@" "]; } @@ -241,5 +283,3 @@ int main(int argc, const char * argv[]) { return 0; } - - From 45881c9783dfb859e335a35a14a6440e2abfe4f8 Mon Sep 17 00:00:00 2001 From: Derek Date: Sun, 28 Jun 2015 18:04:42 -0400 Subject: [PATCH 16/24] print any size board --- TicTacToe/TicTacToe/main.m | 65 ++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 13 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index dc098a3..fb3b8b9 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -48,6 +48,7 @@ @implementation Game{ NSMutableArray *_gameboard; int _gameSize; + int _maxIndex; } -(void)setDifficulty:(int)level{ @@ -104,23 +105,46 @@ -(void)printTutorialBoard{ } -(void) printBoard { - NSLog(@"|%@|%@|%@|",[_gameboard objectAtIndex:0],[_gameboard objectAtIndex:1],[_gameboard objectAtIndex:2] ); - NSLog(@"|%@|%@|%@|",[_gameboard objectAtIndex:3],[_gameboard objectAtIndex:4],[_gameboard objectAtIndex:5] ); - NSLog(@"|%@|%@|%@|",[_gameboard objectAtIndex:6],[_gameboard objectAtIndex:7],[_gameboard objectAtIndex:8] ); + for (int i = 0; i < pow (_gameSize,2); i++) { + + NSString *placeHolderString = [_gameboard objectAtIndex:i]; + const char *placeHolderCharString = [placeHolderString cStringUsingEncoding:NSUTF8StringEncoding]; + + if ((i >= _gameSize) && (i % _gameSize == 0)) { + printf("\n|%s|", placeHolderCharString); + } + else{ + printf("|%s|", placeHolderCharString); + } + } + printf("\n"); } -(void)startGame { NSLog(@"Welcome to Derek's and Eric's TicTacToe!"); _win = NO; - NSLog(@"Set a game size, fool:"); + NSLog(@"Set a game size from 3-10, fool!:"); int userGameSize; scanf("%d",&userGameSize); [self setGameSize:userGameSize]; - //fill gameboard with spaces + //calculate max index + _maxIndex = (pow(_gameSize, 2) - 1); + + //fill gameboard with the index of the space _gameboard = [[NSMutableArray alloc] init]; for (int i = 0; i < pow (_gameSize,2); i++) { - [_gameboard addObject:@" "]; + + // create a string from i + NSString *placeNumber = [NSString stringWithFormat:@"%d",i]; + int lengthOfPlaceNumber = (int)[placeNumber length]; + + // check the length of the string + if (lengthOfPlaceNumber == 1) { + NSString *zero = [NSString stringWithFormat:@"0"]; + placeNumber = [zero stringByAppendingString:placeNumber]; + } + [_gameboard addObject: placeNumber]; } //print tutorial board @@ -154,9 +178,17 @@ -(void)turn{ NSLog(@"Player 1, make your move: "); int currentMove; scanf("%d",¤tMove); + fpurge(stdin); + + NSString *stringOfCurrentMove = [NSString stringWithFormat:@"%d",currentMove]; + int lengthOfStringOfCurrentMove = (int)[stringOfCurrentMove length]; + if (lengthOfStringOfCurrentMove == 1) { + NSString *zero = [NSString stringWithFormat:@"0"]; + stringOfCurrentMove = [zero stringByAppendingString:stringOfCurrentMove]; + } - if (([_gameboard[currentMove] isEqual: @" "]) && (currentMove>=0) && (currentMove<=8)) { - [_gameboard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"X"]; + if (([_gameboard[currentMove] isEqual: stringOfCurrentMove]) && (currentMove>=0) && (currentMove<=_maxIndex)) { + [_gameboard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"~X"]; _playerTurn = 2; } else { @@ -172,8 +204,15 @@ -(void)turn{ scanf("%d",¤tMove); fpurge(stdin); - if (([_gameboard[currentMove] isEqual: @" "]) && (currentMove>=0) && (currentMove<=8)) { - [_gameboard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"O"]; + NSString *stringOfCurrentMove = [NSString stringWithFormat:@"%d",currentMove]; + int lengthOfStringOfCurrentMove = (int)[stringOfCurrentMove length]; + if (lengthOfStringOfCurrentMove == 1) { + NSString *zero = [NSString stringWithFormat:@"0"]; + stringOfCurrentMove = [zero stringByAppendingString:stringOfCurrentMove]; + } + + if (([_gameboard[currentMove] isEqual: stringOfCurrentMove]) && (currentMove>=0) && (currentMove<=_maxIndex)) { + [_gameboard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"~O"]; _playerTurn = 1; } else { @@ -193,11 +232,11 @@ -(BOOL)checkWin{ //sets player piece for each of the two loops if (i == 1) { - playerPiece = @"X"; + playerPiece = @"~X"; _winner = @"Player 1"; } else{ - playerPiece = @"O"; + playerPiece = @"~O"; _winner = @"Player 2"; } @@ -255,7 +294,7 @@ -(BOOL)checkWin{ } -(BOOL)checkDraw{ - if (([self checkWin] == FALSE) && ([_gameboard containsObject:@" "]==FALSE)){ + if ((_win == NO) && (_turnCount == (_maxIndex + 1))){ return TRUE; } return FALSE; From 3dac00558a20f5333167da9a839f1300d307df33 Mon Sep 17 00:00:00 2001 From: Derek Date: Tue, 30 Jun 2015 17:44:24 -0400 Subject: [PATCH 17/24] before making general checkWin --- TicTacToe/TicTacToe/main.m | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index fb3b8b9..6b045f8 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -51,6 +51,7 @@ @implementation Game{ int _maxIndex; } + -(void)setDifficulty:(int)level{ _difficulty = level; } @@ -123,15 +124,28 @@ -(void) printBoard { -(void)startGame { NSLog(@"Welcome to Derek's and Eric's TicTacToe!"); _win = NO; - NSLog(@"Set a game size from 3-10, fool!:"); - int userGameSize; - scanf("%d",&userGameSize); - [self setGameSize:userGameSize]; + + BOOL gameSizeSet = NO; + while (gameSizeSet == NO) { + NSLog(@"Set a game size from 3-10, fool!:"); + int userGameSize; + scanf("%d",&userGameSize); + fpurge(stdin); + + + if (userGameSize >=3 && userGameSize <= 10) { + [self setGameSize:userGameSize]; + gameSizeSet = YES; + } + else{ + NSLog(@"Invalid size."); + } + } //calculate max index _maxIndex = (pow(_gameSize, 2) - 1); - //fill gameboard with the index of the space + //fill gameboard with the indices of the positions to occupy _gameboard = [[NSMutableArray alloc] init]; for (int i = 0; i < pow (_gameSize,2); i++) { @@ -139,7 +153,7 @@ -(void)startGame { NSString *placeNumber = [NSString stringWithFormat:@"%d",i]; int lengthOfPlaceNumber = (int)[placeNumber length]; - // check the length of the string + // if the string is of a single digit number, padd with 0 for formatting purposes if (lengthOfPlaceNumber == 1) { NSString *zero = [NSString stringWithFormat:@"0"]; placeNumber = [zero stringByAppendingString:placeNumber]; @@ -187,12 +201,13 @@ -(void)turn{ stringOfCurrentMove = [zero stringByAppendingString:stringOfCurrentMove]; } - if (([_gameboard[currentMove] isEqual: stringOfCurrentMove]) && (currentMove>=0) && (currentMove<=_maxIndex)) { + //NOTE that the order of the conditionals matters. WHY????? + if ((currentMove<=_maxIndex) && (currentMove>=0) && ([_gameboard[currentMove] isEqual: stringOfCurrentMove])) { [_gameboard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"~X"]; _playerTurn = 2; } else { - NSLog(@"Invalid move. Enter a number from 0-8."); + NSLog(@"Invalid move. Enter a number from 0-%d.",_maxIndex); } } @@ -211,12 +226,12 @@ -(void)turn{ stringOfCurrentMove = [zero stringByAppendingString:stringOfCurrentMove]; } - if (([_gameboard[currentMove] isEqual: stringOfCurrentMove]) && (currentMove>=0) && (currentMove<=_maxIndex)) { + if ((currentMove<=_maxIndex) && (currentMove>=0) && ([_gameboard[currentMove] isEqual: stringOfCurrentMove])) { [_gameboard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"~O"]; _playerTurn = 1; } else { - NSLog(@"Invalid move. Enter a number from 0-8."); + NSLog(@"Invalid move. Enter a number from 0-%d.",_maxIndex); } } From 359c70b2bae0f9a9c25e3110149e6da88c134712 Mon Sep 17 00:00:00 2001 From: Derek Date: Tue, 30 Jun 2015 21:57:23 -0400 Subject: [PATCH 18/24] made win checker work with any size of game. made easy computer opponent --- TicTacToe/TicTacToe/main.m | 344 ++++++++++++++++++++++++++++--------- 1 file changed, 260 insertions(+), 84 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 6b045f8..f7af6b0 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -8,6 +8,20 @@ #import +@interface NSArray (LineChecker) + +-(BOOL) areAllObjectsAreIdentical; + +@end + +@implementation NSArray (LineChecker) + +-(BOOL) areAllObjectsAreIdentical{ //category method for NSArray + NSSet *set = [NSSet setWithArray:self]; + return ([set count] <= 1); +} +@end + @interface Game:NSObject -(BOOL)checkWin; @@ -122,26 +136,57 @@ -(void) printBoard { } -(void)startGame { + //greeting NSLog(@"Welcome to Derek's and Eric's TicTacToe!"); _win = NO; - BOOL gameSizeSet = NO; - while (gameSizeSet == NO) { - NSLog(@"Set a game size from 3-10, fool!:"); - int userGameSize; - scanf("%d",&userGameSize); + //set game mode + BOOL modeSet = NO; + while (modeSet == NO) { + NSLog(@"Select Game Mode:"); + NSLog(@" 1) Player vs. Player"); + NSLog(@" 2) Player vs. Computer"); + int userGameMode; + scanf("%d",&userGameMode); fpurge(stdin); - - if (userGameSize >=3 && userGameSize <= 10) { - [self setGameSize:userGameSize]; - gameSizeSet = YES; + if (userGameMode == 1) { + [self setMode:1]; + modeSet = YES; } - else{ - NSLog(@"Invalid size."); + else if (userGameMode == 2){ + [self setMode:2]; + modeSet = YES; + } + else { + NSLog(@"Invalid game mode."); } } + //set game size + if (_mode == 1) { + BOOL gameSizeSet = NO; + while (gameSizeSet == NO) { + NSLog(@"Set a game size from 3-10, fool!:"); + int userGameSize; + scanf("%d",&userGameSize); + fpurge(stdin); + + + if (userGameSize >=3 && userGameSize <= 10) { + [self setGameSize:userGameSize]; + gameSizeSet = YES; + } + else{ + NSLog(@"Invalid size."); + } + } + } + else{ + [self setGameSize:3]; //Computer only works with game size of 3 + } + + //calculate max index _maxIndex = (pow(_gameSize, 2) - 1); @@ -173,15 +218,35 @@ -(void)startGame { [self printBoard]; - if ([self checkDraw] == TRUE) { - _win = TRUE; //set win to true to break game loop - NSLog(@"It's a draw."); - } - if ([self checkWin] == TRUE) { _win = TRUE; + + if (_turnCount % 2 == 0) { + + if (_mode == 1) { + _winner = @"Player X"; + } + else{ + _winner = @"Player"; + } + } + else{ + + if (_mode == 1) { + _winner = @"Player O"; + } + else{ + _winner = @"Computer"; + } + + } NSLog(@"%@ WINS!",_winner); } + + if ([self checkDraw] == TRUE) { + _win = TRUE; //set win to true to break game loop + NSLog(@"It's a draw."); + } } } @@ -214,24 +279,37 @@ -(void)turn{ } else{ while (_playerTurn == 2) { - NSLog(@"Player 2, make your move: "); - int currentMove; - scanf("%d",¤tMove); - fpurge(stdin); - - NSString *stringOfCurrentMove = [NSString stringWithFormat:@"%d",currentMove]; - int lengthOfStringOfCurrentMove = (int)[stringOfCurrentMove length]; - if (lengthOfStringOfCurrentMove == 1) { - NSString *zero = [NSString stringWithFormat:@"0"]; - stringOfCurrentMove = [zero stringByAppendingString:stringOfCurrentMove]; - } - if ((currentMove<=_maxIndex) && (currentMove>=0) && ([_gameboard[currentMove] isEqual: stringOfCurrentMove])) { - [_gameboard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"~O"]; - _playerTurn = 1; + int currentMove; + if (_mode == 1) { + NSLog(@"Player 2, make your move: "); + scanf("%d",¤tMove); + fpurge(stdin); + + NSString *stringOfCurrentMove = [NSString stringWithFormat:@"%d",currentMove]; + int lengthOfStringOfCurrentMove = (int)[stringOfCurrentMove length]; + if (lengthOfStringOfCurrentMove == 1) { + NSString *zero = [NSString stringWithFormat:@"0"]; + stringOfCurrentMove = [zero stringByAppendingString:stringOfCurrentMove]; + } + + if ((currentMove<=_maxIndex) && (currentMove>=0) && ([_gameboard[currentMove] isEqual: stringOfCurrentMove])) { + [_gameboard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"~O"]; + _playerTurn = 1; + } + else { + NSLog(@"Invalid move. Enter a number from 0-%d.",_maxIndex); + } } - else { - NSLog(@"Invalid move. Enter a number from 0-%d.",_maxIndex); + else{ + int computerMove = arc4random_uniform(_maxIndex + 1); + if ((![[_gameboard objectAtIndex:computerMove] isEqual: @"~X"]) && (![[_gameboard objectAtIndex:computerMove] isEqual: @"~O"])) { + NSLog(@"Computer is making a move..."); + sleep(1.5); + [_gameboard replaceObjectAtIndex:(NSUInteger)computerMove withObject:@"~O"]; + _playerTurn = 1; + } + } } @@ -239,77 +317,175 @@ -(void)turn{ _turnCount = _turnCount + 1; } +//-(BOOL)checkWin{ +// +// NSString *playerPiece; +// +// for (int i = 0; i < 2; i++) { +// +// //sets player piece for each of the two loops +// if (i == 1) { +// playerPiece = @"~X"; +// _winner = @"Player 1"; +// } +// else{ +// playerPiece = @"~O"; +// _winner = @"Player 2"; +// } +// +// // |X|X|X| +// // | | | | +// // | | | | +// if (([_gameboard[0] isEqual:playerPiece]) && ([_gameboard[1] isEqual:playerPiece]) && ([_gameboard[2] isEqual:playerPiece])){ +// return YES; +// } +// // | | | | +// // |X|X|X| +// // | | | | +// if (([_gameboard[3] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[5] isEqual:playerPiece])){ +// return YES; +// } +// // | | | | +// // | | | | +// // |X|X|X| +// if (([_gameboard[6] isEqual:playerPiece]) && ([_gameboard[7] isEqual:playerPiece]) && ([_gameboard[8] isEqual:playerPiece])){ +// return YES; +// } +// // |X| | | +// // |X| | | +// // |X| | | +// if (([_gameboard[0] isEqual:playerPiece]) && ([_gameboard[3] isEqual:playerPiece]) && ([_gameboard[6] isEqual:playerPiece])){ +// return YES; +// } +// // | |X| | +// // | |X| | +// // | |X| | +// if (([_gameboard[1] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[7] isEqual:playerPiece])){ +// return YES; +// } +// // | | |X| +// // | | |X| +// // | | |X| +// if (([_gameboard[2] isEqual:playerPiece]) && ([_gameboard[5] isEqual:playerPiece]) && ([_gameboard[8] isEqual:playerPiece])){ +// return YES; +// } +// // |X| | | +// // | |X| | +// // | | |X| +// if (([_gameboard[0] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[8] isEqual:playerPiece])){ +// return YES; +// } +// // | | |X| +// // | |X| | +// // |X| | | +// if (([_gameboard[2] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[6] isEqual:playerPiece])){ +// return YES; +// } +// +// } +// return NO; +//} + -(BOOL)checkWin{ - NSString *playerPiece; + BOOL gameWon = NO; + + NSMutableArray *line = [[NSMutableArray alloc] init]; //array of the line we are checking for a win - for (int i = 0; i < 2; i++) { + //HORIZONAL WIN TEST + for (int i = 0; i < pow(_gameSize, 2); i++) { - //sets player piece for each of the two loops - if (i == 1) { - playerPiece = @"~X"; - _winner = @"Player 1"; - } - else{ - playerPiece = @"~O"; - _winner = @"Player 2"; - } + [line addObject:[_gameboard objectAtIndex:i]]; - // |X|X|X| - // | | | | - // | | | | - if (([_gameboard[0] isEqual:playerPiece]) && ([_gameboard[1] isEqual:playerPiece]) && ([_gameboard[2] isEqual:playerPiece])){ - return YES; - } - // | | | | - // |X|X|X| - // | | | | - if (([_gameboard[3] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[5] isEqual:playerPiece])){ - return YES; + if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize + gameWon = [line areAllObjectsAreIdentical]; + + if (gameWon == YES) { + return YES; + } + else{ + [line removeAllObjects]; //empty the line array + } } - // | | | | - // | | | | - // |X|X|X| - if (([_gameboard[6] isEqual:playerPiece]) && ([_gameboard[7] isEqual:playerPiece]) && ([_gameboard[8] isEqual:playerPiece])){ - return YES; + } + + int currentIndex; + int lastIndex; + + //VERTICAL WIN TEST + for (int i = 0; i < _gameSize; i++) { + currentIndex = i; + lastIndex = 0; + + for (int i = 0; i < _gameSize; i++) { + [line addObject:[_gameboard objectAtIndex:currentIndex]]; + lastIndex = currentIndex; + currentIndex = lastIndex + _gameSize; } - // |X| | | - // |X| | | - // |X| | | - if (([_gameboard[0] isEqual:playerPiece]) && ([_gameboard[3] isEqual:playerPiece]) && ([_gameboard[6] isEqual:playerPiece])){ - return YES; + + if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize + gameWon = [line areAllObjectsAreIdentical]; + + if (gameWon == YES) { + return YES; + } + else{ + [line removeAllObjects]; //empty the line array + } } - // | |X| | - // | |X| | - // | |X| | - if (([_gameboard[1] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[7] isEqual:playerPiece])){ + } + + //DIAGONAL WIN TEST + + // |X| | | + // | |X| | + // | | |X| + currentIndex = 0; + lastIndex = 0; + for (int i = 0; i < _gameSize; i++) { + [line addObject:[_gameboard objectAtIndex:currentIndex]]; + lastIndex = currentIndex; + currentIndex = lastIndex + _gameSize + 1; + } + + if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize + gameWon = [line areAllObjectsAreIdentical]; + + if (gameWon == YES) { return YES; } - // | | |X| - // | | |X| - // | | |X| - if (([_gameboard[2] isEqual:playerPiece]) && ([_gameboard[5] isEqual:playerPiece]) && ([_gameboard[8] isEqual:playerPiece])){ - return YES; + else{ + [line removeAllObjects]; //empty the line array } - // |X| | | - // | |X| | - // | | |X| - if (([_gameboard[0] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[8] isEqual:playerPiece])){ + } + + // | | |X| + // | |X| | + // |X| | | + currentIndex = _gameSize - 1; + lastIndex = _gameSize - 1; + for (int i = 0; i < _gameSize; i++) { + [line addObject:[_gameboard objectAtIndex:currentIndex]]; + lastIndex = currentIndex; + currentIndex = lastIndex + _gameSize - 1; + } + + if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize + gameWon = [line areAllObjectsAreIdentical]; + + if (gameWon == YES) { return YES; } - // | | |X| - // | |X| | - // |X| | | - if (([_gameboard[2] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[6] isEqual:playerPiece])){ - return YES; + else{ + [line removeAllObjects]; //empty the line array } - } + return NO; } -(BOOL)checkDraw{ - if ((_win == NO) && (_turnCount == (_maxIndex + 1))){ + if ((_win == NO) && (_turnCount == (_maxIndex + 2))){ return TRUE; } return FALSE; From bfe95334a1d5f3211e2f21695ae1e089be3b48c6 Mon Sep 17 00:00:00 2001 From: Derek Date: Sun, 12 Jul 2015 12:34:48 -0400 Subject: [PATCH 19/24] restructed code --- TicTacToe/TicTacToe.xcodeproj/project.pbxproj | 31 + TicTacToe/TicTacToe/ComputerPlayer.h | 13 + TicTacToe/TicTacToe/ComputerPlayer.m | 92 +++ TicTacToe/TicTacToe/Game.h | 41 ++ TicTacToe/TicTacToe/Game.m | 266 +++++++++ TicTacToe/TicTacToe/MyTicTacToeGame.h | 18 + TicTacToe/TicTacToe/MyTicTacToeGame.m | 55 ++ TicTacToe/TicTacToe/NSArray+LineChecker.h | 15 + TicTacToe/TicTacToe/NSArray+LineChecker.m | 18 + TicTacToe/TicTacToe/Player.h | 13 + TicTacToe/TicTacToe/Player.m | 13 + TicTacToe/TicTacToe/main.m | 541 +++--------------- 12 files changed, 648 insertions(+), 468 deletions(-) create mode 100644 TicTacToe/TicTacToe/ComputerPlayer.h create mode 100644 TicTacToe/TicTacToe/ComputerPlayer.m create mode 100644 TicTacToe/TicTacToe/Game.h create mode 100644 TicTacToe/TicTacToe/Game.m create mode 100644 TicTacToe/TicTacToe/MyTicTacToeGame.h create mode 100644 TicTacToe/TicTacToe/MyTicTacToeGame.m create mode 100644 TicTacToe/TicTacToe/NSArray+LineChecker.h create mode 100644 TicTacToe/TicTacToe/NSArray+LineChecker.m create mode 100644 TicTacToe/TicTacToe/Player.h create mode 100644 TicTacToe/TicTacToe/Player.m diff --git a/TicTacToe/TicTacToe.xcodeproj/project.pbxproj b/TicTacToe/TicTacToe.xcodeproj/project.pbxproj index 71ad89c..47a8908 100644 --- a/TicTacToe/TicTacToe.xcodeproj/project.pbxproj +++ b/TicTacToe/TicTacToe.xcodeproj/project.pbxproj @@ -7,6 +7,11 @@ objects = { /* Begin PBXBuildFile section */ + 695078051B4C867600735A8D /* Game.m in Sources */ = {isa = PBXBuildFile; fileRef = 695078041B4C867600735A8D /* Game.m */; }; + 695078081B4C880600735A8D /* NSArray+LineChecker.m in Sources */ = {isa = PBXBuildFile; fileRef = 695078071B4C880600735A8D /* NSArray+LineChecker.m */; }; + 6950780B1B4C893600735A8D /* MyTicTacToeGame.m in Sources */ = {isa = PBXBuildFile; fileRef = 6950780A1B4C893600735A8D /* MyTicTacToeGame.m */; }; + 6950780E1B4C97FD00735A8D /* Player.m in Sources */ = {isa = PBXBuildFile; fileRef = 6950780D1B4C97FD00735A8D /* Player.m */; }; + 6965035B1B51B1CD00300B68 /* ComputerPlayer.m in Sources */ = {isa = PBXBuildFile; fileRef = 6965035A1B51B1CD00300B68 /* ComputerPlayer.m */; }; 8D9789E41B3C9A70007CF4CF /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D9789E31B3C9A70007CF4CF /* main.m */; }; /* End PBXBuildFile section */ @@ -23,6 +28,16 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 695078031B4C867600735A8D /* Game.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Game.h; sourceTree = ""; }; + 695078041B4C867600735A8D /* Game.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Game.m; sourceTree = ""; }; + 695078061B4C880600735A8D /* NSArray+LineChecker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+LineChecker.h"; sourceTree = ""; }; + 695078071B4C880600735A8D /* NSArray+LineChecker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+LineChecker.m"; sourceTree = ""; }; + 695078091B4C893600735A8D /* MyTicTacToeGame.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MyTicTacToeGame.h; sourceTree = ""; }; + 6950780A1B4C893600735A8D /* MyTicTacToeGame.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MyTicTacToeGame.m; sourceTree = ""; }; + 6950780C1B4C97FD00735A8D /* Player.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Player.h; sourceTree = ""; }; + 6950780D1B4C97FD00735A8D /* Player.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Player.m; sourceTree = ""; }; + 696503591B51B1CD00300B68 /* ComputerPlayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ComputerPlayer.h; sourceTree = ""; }; + 6965035A1B51B1CD00300B68 /* ComputerPlayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ComputerPlayer.m; sourceTree = ""; }; 8D9789E01B3C9A70007CF4CF /* TicTacToe */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = TicTacToe; sourceTree = BUILT_PRODUCTS_DIR; }; 8D9789E31B3C9A70007CF4CF /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; /* End PBXFileReference section */ @@ -58,6 +73,16 @@ isa = PBXGroup; children = ( 8D9789E31B3C9A70007CF4CF /* main.m */, + 6950780C1B4C97FD00735A8D /* Player.h */, + 6950780D1B4C97FD00735A8D /* Player.m */, + 695078031B4C867600735A8D /* Game.h */, + 695078041B4C867600735A8D /* Game.m */, + 696503591B51B1CD00300B68 /* ComputerPlayer.h */, + 6965035A1B51B1CD00300B68 /* ComputerPlayer.m */, + 695078091B4C893600735A8D /* MyTicTacToeGame.h */, + 6950780A1B4C893600735A8D /* MyTicTacToeGame.m */, + 695078061B4C880600735A8D /* NSArray+LineChecker.h */, + 695078071B4C880600735A8D /* NSArray+LineChecker.m */, ); path = TicTacToe; sourceTree = ""; @@ -118,7 +143,12 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 6950780B1B4C893600735A8D /* MyTicTacToeGame.m in Sources */, 8D9789E41B3C9A70007CF4CF /* main.m in Sources */, + 695078081B4C880600735A8D /* NSArray+LineChecker.m in Sources */, + 6965035B1B51B1CD00300B68 /* ComputerPlayer.m in Sources */, + 695078051B4C867600735A8D /* Game.m in Sources */, + 6950780E1B4C97FD00735A8D /* Player.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -235,6 +265,7 @@ 8D9789E91B3C9A70007CF4CF /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/TicTacToe/TicTacToe/ComputerPlayer.h b/TicTacToe/TicTacToe/ComputerPlayer.h new file mode 100644 index 0000000..b9ecca1 --- /dev/null +++ b/TicTacToe/TicTacToe/ComputerPlayer.h @@ -0,0 +1,13 @@ +// +// ComputerPlayer.h +// TicTacToe +// +// Created by Z on 7/11/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import + +@interface ComputerPlayer : NSObject + +@end diff --git a/TicTacToe/TicTacToe/ComputerPlayer.m b/TicTacToe/TicTacToe/ComputerPlayer.m new file mode 100644 index 0000000..c126da7 --- /dev/null +++ b/TicTacToe/TicTacToe/ComputerPlayer.m @@ -0,0 +1,92 @@ +// +// ComputerPlayer.m +// TicTacToe +// +// Created by Z on 7/11/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import "ComputerPlayer.h" + +@implementation ComputerPlayer + + +// //Easy Mode computer move +// if (_difficulty == 1) { +// int computerMove = arc4random_uniform(_maxIndex + 1); +// if ((![[_gameboard objectAtIndex:computerMove] isEqual: @"~X"]) && (![[_gameboard objectAtIndex:computerMove] isEqual: @"~O"])) { +// NSLog(@"Computer is making a move..."); +// sleep(1.5); +// [_gameboard replaceObjectAtIndex:(NSUInteger)computerMove withObject:@"~O"]; + + + +//-(BOOL)checkWin{ +// +// NSString *playerPiece; +// +// for (int i = 0; i < 2; i++) { +// +// //sets player piece for each of the two loops +// if (i == 1) { +// playerPiece = @"~X"; +// _winner = @"Player 1"; +// } +// else{ +// playerPiece = @"~O"; +// _winner = @"Player 2"; +// } +// +// // |X|X|X| +// // | | | | +// // | | | | +// if (([_gameboard[0] isEqual:playerPiece]) && ([_gameboard[1] isEqual:playerPiece]) && ([_gameboard[2] isEqual:playerPiece])){ +// return YES; +// } +// // | | | | +// // |X|X|X| +// // | | | | +// if (([_gameboard[3] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[5] isEqual:playerPiece])){ +// return YES; +// } +// // | | | | +// // | | | | +// // |X|X|X| +// if (([_gameboard[6] isEqual:playerPiece]) && ([_gameboard[7] isEqual:playerPiece]) && ([_gameboard[8] isEqual:playerPiece])){ +// return YES; +// } +// // |X| | | +// // |X| | | +// // |X| | | +// if (([_gameboard[0] isEqual:playerPiece]) && ([_gameboard[3] isEqual:playerPiece]) && ([_gameboard[6] isEqual:playerPiece])){ +// return YES; +// } +// // | |X| | +// // | |X| | +// // | |X| | +// if (([_gameboard[1] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[7] isEqual:playerPiece])){ +// return YES; +// } +// // | | |X| +// // | | |X| +// // | | |X| +// if (([_gameboard[2] isEqual:playerPiece]) && ([_gameboard[5] isEqual:playerPiece]) && ([_gameboard[8] isEqual:playerPiece])){ +// return YES; +// } +// // |X| | | +// // | |X| | +// // | | |X| +// if (([_gameboard[0] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[8] isEqual:playerPiece])){ +// return YES; +// } +// // | | |X| +// // | |X| | +// // |X| | | +// if (([_gameboard[2] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[6] isEqual:playerPiece])){ +// return YES; +// } +// +// } +// return NO; +//} +@end diff --git a/TicTacToe/TicTacToe/Game.h b/TicTacToe/TicTacToe/Game.h new file mode 100644 index 0000000..afa0332 --- /dev/null +++ b/TicTacToe/TicTacToe/Game.h @@ -0,0 +1,41 @@ +// +// Game.h +// TicTacToe +// +// Created by Z on 7/7/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import + +@interface Game : NSObject + +-(BOOL)checkWin; +-(BOOL)getWin; + +-(BOOL)checkDraw; + +-(void)setupGame; + +- (NSMutableArray *)getGameBoard; + + +-(void)printBoard; +-(void)printTutorialBoard; + +-(void)turn; +-(int)getTurnCount; + +-(void)setGameSize:(int)size; +-(int)getGameSize; + +//Easy, Medium, Hard +-(void)setDifficulty:(int)level; +-(int)getDifficulty; + +//Player vs. Player OR Player vs. Computer +-(void)setMode:(int)mode; +-(int)getMode; + + +@end diff --git a/TicTacToe/TicTacToe/Game.m b/TicTacToe/TicTacToe/Game.m new file mode 100644 index 0000000..7fc55b9 --- /dev/null +++ b/TicTacToe/TicTacToe/Game.m @@ -0,0 +1,266 @@ +// +// Game.m +// TicTacToe +// +// Created by Z on 7/7/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import "Game.h" +#import "Player.h" +#import "NSArray+LineChecker.h" + +@implementation Game { + int _mode; + + int _difficulty; + + int _playerTurn; + int _turnCount; + + BOOL _win; + NSString *_winner; + + NSMutableArray *_gameboard; + int _gameSize; + int _maxIndex; + + int _currentMove; +} + +- (NSMutableArray *)getGameBoard { + return _gameboard; +} + +-(void)setDifficulty:(int)level{ + _difficulty = level; +} + +-(int)getDifficulty{ + return _difficulty; +} + +-(void)setGameSize:(int)size{ + _gameSize = size; +} + +-(int)getGameSize{ + return _gameSize; +} + +-(void)setMode:(int)mode{ + _mode = mode; +} +-(int)getMode{ + return _mode; +} + +-(BOOL)getWin{ + return _win; +} + +-(int)getTurnCount{ + return _turnCount; +} + +-(void)setupGame { + + //greeting + NSLog(@"Welcome to Derek's and Eric's TicTacToe!"); + _win = NO; + _playerTurn = 1; + + //calculate max index + _maxIndex = (pow(_gameSize, 2) - 1); + + //fill gameboard with the indices of the positions to occupy + _gameboard = [[NSMutableArray alloc] init]; + for (int i = 0; i < pow (_gameSize,2); i++) { + + // create a string from i + NSString *placeNumber = [NSString stringWithFormat:@"%d",i]; + int lengthOfPlaceNumber = (int)[placeNumber length]; + + // if the string is of a single digit number, padd with 0 for formatting purposes + if (lengthOfPlaceNumber == 1) { + NSString *zero = [NSString stringWithFormat:@"0"]; + placeNumber = [zero stringByAppendingString:placeNumber]; + } + [_gameboard addObject: placeNumber]; + } +} + +//-(void)movePadder:(NSString *)stringOfCurrentMove { +// int lengthOfStringOfCurrentMove = (int)[stringOfCurrentMove length]; +// if (lengthOfStringOfCurrentMove == 1) { +// stringOfCurrentMove = [NSString stringWithFormat:@"%02d", stringOfCurrentMove]; +// } +//} + +-(NSMutableString *)makeMove{ + NSLog(@"Player %d make your move:",_playerTurn); + scanf("%d",&_currentMove); + fpurge(stdin); + + NSMutableString *stringOfCurrentMove = [NSMutableString stringWithFormat:@"%02d",_currentMove]; +// [self movePadder:stringOfCurrentMove]; + + return stringOfCurrentMove; +} + +-(NSMutableString *)computerMakeMove{ + //Easy Mode computer move + if (_difficulty == 1) { + _currentMove = arc4random_uniform(_maxIndex + 1); + NSMutableString *stringOfCurrentMove = [NSMutableString stringWithFormat:@"%02d",_currentMove]; +// [self movePadder:stringOfCurrentMove]; + return stringOfCurrentMove; + } + return @"HEHEE"; +} + +-(NSString *)getPlayerPiece{ + NSString *playerPiece; + if (_playerTurn == 2) { + playerPiece = @"~O"; + } + else{ + playerPiece = @"~X"; + } + return playerPiece; +} + +-(void)turn{ + BOOL moveMade = NO; + while (moveMade == NO) { + + NSString *stringOfCurrentMove; + + if (_mode == 1) { + stringOfCurrentMove = [self makeMove]; + } + else if (_mode == 2){ + stringOfCurrentMove = [self computerMakeMove]; + } + + if (([_gameboard[_currentMove] isEqual: stringOfCurrentMove]) && (_currentMove<=_maxIndex) && (_currentMove>=0)) { + [_gameboard replaceObjectAtIndex:(NSUInteger)_currentMove withObject:[self getPlayerPiece]]; + moveMade = YES; + } + else { + NSLog(@"Invalid move. Enter a number from 0-%d.",_maxIndex); + } + } + _turnCount = _turnCount + 1; + + if (_turnCount % 2 != 0) { + _playerTurn = 2; + } + else{ + _playerTurn = 1; + } +} + +-(BOOL)checkWin{ + + NSMutableArray *line = [[NSMutableArray alloc] init]; //array of the line we are checking for a win + + //HORIZONAL WIN TEST + for (int i = 0; i < pow(_gameSize, 2); i++) { + + [line addObject:[_gameboard objectAtIndex:i]]; + + if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize + _win = [line areAllObjectsAreIdentical]; + + if (_win == YES) { + return YES; + } + else{ + [line removeAllObjects]; //empty the line array + } + } + } + + int currentIndex; + int lastIndex; + + //VERTICAL WIN TEST + for (int i = 0; i < _gameSize; i++) { + currentIndex = i; + lastIndex = 0; + + for (int i = 0; i < _gameSize; i++) { + [line addObject:[_gameboard objectAtIndex:currentIndex]]; + lastIndex = currentIndex; + currentIndex = lastIndex + _gameSize; + } + + if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize + _win = [line areAllObjectsAreIdentical]; + + if (_win == YES) { + return YES; + } + else{ + [line removeAllObjects]; //empty the line array + } + } + } + + //DIAGONAL WIN TEST + + // |X| | | + // | |X| | + // | | |X| + currentIndex = 0; + lastIndex = 0; + for (int i = 0; i < _gameSize; i++) { + [line addObject:[_gameboard objectAtIndex:currentIndex]]; + lastIndex = currentIndex; + currentIndex = lastIndex + _gameSize + 1; + } + + if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize + _win = [line areAllObjectsAreIdentical]; + + if (_win == YES) { + return YES; + } + else{ + [line removeAllObjects]; //empty the line array + } + } + + // | | |X| + // | |X| | + // |X| | | + currentIndex = _gameSize - 1; + lastIndex = _gameSize - 1; + for (int i = 0; i < _gameSize; i++) { + [line addObject:[_gameboard objectAtIndex:currentIndex]]; + lastIndex = currentIndex; + currentIndex = lastIndex + _gameSize - 1; + } + + if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize + _win = [line areAllObjectsAreIdentical]; + + if (_win == YES) { + return YES; + } + else{ + [line removeAllObjects]; //empty the line array + } + } + return NO; +} + +-(BOOL)checkDraw{ + if ((_win == NO) && (_turnCount == (_maxIndex + 2))){ + return TRUE; + } + return FALSE; +} + +@end diff --git a/TicTacToe/TicTacToe/MyTicTacToeGame.h b/TicTacToe/TicTacToe/MyTicTacToeGame.h new file mode 100644 index 0000000..c7efb06 --- /dev/null +++ b/TicTacToe/TicTacToe/MyTicTacToeGame.h @@ -0,0 +1,18 @@ +// +// MyTicTacToeGame.h +// TicTacToe +// +// Created by Z on 7/7/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import + +@class Game; + +@interface MyTicTacToeGame: NSObject + ++ (void)printBoard:(Game *)game; ++ (void)printTutorialBoard:(Game *)game; + +@end diff --git a/TicTacToe/TicTacToe/MyTicTacToeGame.m b/TicTacToe/TicTacToe/MyTicTacToeGame.m new file mode 100644 index 0000000..211aee8 --- /dev/null +++ b/TicTacToe/TicTacToe/MyTicTacToeGame.m @@ -0,0 +1,55 @@ +// +// MyTicTacToeGame.m +// TicTacToe +// +// Created by Z on 7/7/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import "MyTicTacToeGame.h" +#import "Game.h" + +@implementation MyTicTacToeGame + ++ (void)printBoard:(Game *)game { //class method + for (int i = 0; i < pow ([game getGameSize],2); i++) { + + NSString *placeHolderString = [[game getGameBoard] objectAtIndex:i]; + const char *placeHolderCharString = [placeHolderString cStringUsingEncoding:NSUTF8StringEncoding]; + + if ((i >= [game getGameSize]) && (i % [game getGameSize] == 0)) { + printf("\n|%s|", placeHolderCharString); + } + else{ + printf("|%s|", placeHolderCharString); + } + } + printf("\n"); +} + ++ (void)printTutorialBoard:(Game *)game{ //class method + for (int i = 0; i < pow ([game getGameSize],2); i++) { + + // create a string from i + NSString *placeNumber = [NSString stringWithFormat:@"%d",i]; + int lengthOfPlaceNumber = (int)[placeNumber length]; + + // check the length of the sting + if (lengthOfPlaceNumber == 1) { + NSString *zero = [NSString stringWithFormat:@"0"]; + placeNumber = [zero stringByAppendingString:placeNumber]; + } + //convert NSString to char string + const char *placeNumberCharString = [placeNumber cStringUsingEncoding: NSUTF8StringEncoding]; + + if ((i >= [game getGameSize]) && (i % [game getGameSize] == 0)) { + printf("\n|%s|", placeNumberCharString); + } + else{ + printf("|%s|", placeNumberCharString); + } + } + printf("\n"); +} + +@end diff --git a/TicTacToe/TicTacToe/NSArray+LineChecker.h b/TicTacToe/TicTacToe/NSArray+LineChecker.h new file mode 100644 index 0000000..63d45e3 --- /dev/null +++ b/TicTacToe/TicTacToe/NSArray+LineChecker.h @@ -0,0 +1,15 @@ +// +// NSArray+LineChecker.h +// TicTacToe +// +// Created by Z on 7/7/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import + +@interface NSArray (LineChecker) + +-(BOOL) areAllObjectsAreIdentical; + +@end diff --git a/TicTacToe/TicTacToe/NSArray+LineChecker.m b/TicTacToe/TicTacToe/NSArray+LineChecker.m new file mode 100644 index 0000000..083ed3f --- /dev/null +++ b/TicTacToe/TicTacToe/NSArray+LineChecker.m @@ -0,0 +1,18 @@ +// +// NSArray+LineChecker.m +// TicTacToe +// +// Created by Z on 7/7/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import "NSArray+LineChecker.h" + +@implementation NSArray (LineChecker) + +-(BOOL) areAllObjectsAreIdentical{ //category method for NSArray + NSSet *set = [NSSet setWithArray:self]; + return ([set count] <= 1); +} + +@end diff --git a/TicTacToe/TicTacToe/Player.h b/TicTacToe/TicTacToe/Player.h new file mode 100644 index 0000000..0eb5747 --- /dev/null +++ b/TicTacToe/TicTacToe/Player.h @@ -0,0 +1,13 @@ +// +// Player.h +// TicTacToe +// +// Created by Z on 7/7/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import + +@interface Player : NSObject +//seperate humman and computer +@end diff --git a/TicTacToe/TicTacToe/Player.m b/TicTacToe/TicTacToe/Player.m new file mode 100644 index 0000000..73a288e --- /dev/null +++ b/TicTacToe/TicTacToe/Player.m @@ -0,0 +1,13 @@ +// +// Player.m +// TicTacToe +// +// Created by Z on 7/7/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import "Player.h" + +@implementation Player + +@end diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index f7af6b0..d80dbbd 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -7,509 +7,114 @@ // #import +#import "MyTicTacToeGame.h" +#import "Game.h" -@interface NSArray (LineChecker) --(BOOL) areAllObjectsAreIdentical; - -@end - -@implementation NSArray (LineChecker) - --(BOOL) areAllObjectsAreIdentical{ //category method for NSArray - NSSet *set = [NSSet setWithArray:self]; - return ([set count] <= 1); -} -@end - -@interface Game:NSObject - --(BOOL)checkWin; --(BOOL)getWin; - --(BOOL)checkDraw; - --(void)startGame; - --(void)printBoard; --(void)printTutorialBoard; - --(void)turn; - --(void)setGameSize:(int)size; --(int)getGameSize; - -//Easy, Medium, Hard --(void)setDifficulty:(int)level; --(int)getDifficulty; - -//Player vs. Player OR Player vs. Computer --(void)setMode:(int)mode; --(int)getMode; - -@end - -@implementation Game{ - int _mode; - - int _difficulty; - - int _playerTurn; - int _turnCount; - - BOOL _win; - NSString *_winner; - - NSMutableArray *_gameboard; - int _gameSize; - int _maxIndex; -} - - --(void)setDifficulty:(int)level{ - _difficulty = level; -} - --(int)getDifficulty{ - return _difficulty; -} - --(void)setGameSize:(int)size{ - _gameSize = size; -} - --(int)getGameSize{ - return _gameSize; -} - --(void)setMode:(int)mode{ - _mode = mode; -} --(int)getMode{ - return _mode; -} - --(BOOL)getWin{ - return _win; -} - --(void)printTutorialBoard{ - for (int i = 0; i < pow (_gameSize,2); i++) { - - // create a string from i - NSString *placeNumber = [NSString stringWithFormat:@"%d",i]; - int lengthOfPlaceNumber = (int)[placeNumber length]; - - // check the length of the sting - if (lengthOfPlaceNumber == 1) { - NSString *zero = [NSString stringWithFormat:@"0"]; - placeNumber = [zero stringByAppendingString:placeNumber]; - } - //convert NSString to char string - const char *placeNumberCharString = [placeNumber cStringUsingEncoding: NSUTF8StringEncoding]; - - if ((i >= _gameSize) && (i % _gameSize == 0)) { - printf("\n|%s|", placeNumberCharString); - - } - else{ - printf("|%s|", placeNumberCharString); - } - } - printf("\n"); -} - --(void) printBoard { - for (int i = 0; i < pow (_gameSize,2); i++) { - - NSString *placeHolderString = [_gameboard objectAtIndex:i]; - const char *placeHolderCharString = [placeHolderString cStringUsingEncoding:NSUTF8StringEncoding]; +int main(int argc, const char * argv[]) { + @autoreleasepool { - if ((i >= _gameSize) && (i % _gameSize == 0)) { - printf("\n|%s|", placeHolderCharString); - } - else{ - printf("|%s|", placeHolderCharString); - } - } - printf("\n"); -} - --(void)startGame { - //greeting - NSLog(@"Welcome to Derek's and Eric's TicTacToe!"); - _win = NO; - - //set game mode - BOOL modeSet = NO; - while (modeSet == NO) { - NSLog(@"Select Game Mode:"); - NSLog(@" 1) Player vs. Player"); - NSLog(@" 2) Player vs. Computer"); - int userGameMode; - scanf("%d",&userGameMode); - fpurge(stdin); + Game *ticTacToe =[[Game alloc]init]; - if (userGameMode == 1) { - [self setMode:1]; - modeSet = YES; - } - else if (userGameMode == 2){ - [self setMode:2]; - modeSet = YES; - } - else { - NSLog(@"Invalid game mode."); - } - } - - //set game size - if (_mode == 1) { - BOOL gameSizeSet = NO; - while (gameSizeSet == NO) { - NSLog(@"Set a game size from 3-10, fool!:"); - int userGameSize; - scanf("%d",&userGameSize); + //set game mode + BOOL modeSet = NO; + while (modeSet == NO) { + NSLog(@"Select Game Mode:"); + NSLog(@" 1) Player vs. Player"); + NSLog(@" 2) Player vs. Computer"); + NSLog(@" 3) Computer vs. Computer"); + int userGameMode; + scanf("%d",&userGameMode); fpurge(stdin); - - if (userGameSize >=3 && userGameSize <= 10) { - [self setGameSize:userGameSize]; - gameSizeSet = YES; + if (userGameMode == 1) { + [ticTacToe setMode:1]; + modeSet = YES; } - else{ - NSLog(@"Invalid size."); + else if (userGameMode == 2){ + [ticTacToe setMode:2]; + modeSet = YES; + } + else if (userGameMode == 3){ + [ticTacToe setMode:3]; + modeSet = YES; + } + else { + NSLog(@"Invalid game mode."); } } - } - else{ - [self setGameSize:3]; //Computer only works with game size of 3 - } - - - //calculate max index - _maxIndex = (pow(_gameSize, 2) - 1); - - //fill gameboard with the indices of the positions to occupy - _gameboard = [[NSMutableArray alloc] init]; - for (int i = 0; i < pow (_gameSize,2); i++) { - - // create a string from i - NSString *placeNumber = [NSString stringWithFormat:@"%d",i]; - int lengthOfPlaceNumber = (int)[placeNumber length]; - - // if the string is of a single digit number, padd with 0 for formatting purposes - if (lengthOfPlaceNumber == 1) { - NSString *zero = [NSString stringWithFormat:@"0"]; - placeNumber = [zero stringByAppendingString:placeNumber]; - } - [_gameboard addObject: placeNumber]; - } - - //print tutorial board - [self printTutorialBoard]; - - //Game Loop - _playerTurn = 1; - _turnCount = 1; - while (_win == NO) { - - [self turn]; - - [self printBoard]; - if ([self checkWin] == TRUE) { - _win = TRUE; - - if (_turnCount % 2 == 0) { + //set difficulty + if ([ticTacToe getMode] == 2) { + BOOL difficultySet = NO; + while (difficultySet == NO && [ticTacToe getMode] != 1) { + NSLog(@"Choose a difficulty."); + NSLog(@" 1) Easy"); + NSLog(@" 2) Medium"); + NSLog(@" 3) Hard"); + int userDifficulty; + scanf("%d",&userDifficulty); + fpurge(stdin); - if (_mode == 1) { - _winner = @"Player X"; + if (userDifficulty == 1) { + [ticTacToe setDifficulty:1]; + difficultySet = YES; } - else{ - _winner = @"Player"; - } - } - else{ - - if (_mode == 1) { - _winner = @"Player O"; + else if (userDifficulty == 2){ + [ticTacToe setDifficulty:2]; + difficultySet = YES; } - else{ - _winner = @"Computer"; + else if (userDifficulty == 3){ + [ticTacToe setDifficulty:3]; + difficultySet = YES; } - } - NSLog(@"%@ WINS!",_winner); } - - if ([self checkDraw] == TRUE) { - _win = TRUE; //set win to true to break game loop - NSLog(@"It's a draw."); - } - } -} - --(void)turn{ - - if (_turnCount % 2 == 1) { - while (_playerTurn == 1) { - NSLog(@"Player 1, make your move: "); - int currentMove; - scanf("%d",¤tMove); - fpurge(stdin); - - NSString *stringOfCurrentMove = [NSString stringWithFormat:@"%d",currentMove]; - int lengthOfStringOfCurrentMove = (int)[stringOfCurrentMove length]; - if (lengthOfStringOfCurrentMove == 1) { - NSString *zero = [NSString stringWithFormat:@"0"]; - stringOfCurrentMove = [zero stringByAppendingString:stringOfCurrentMove]; - } - - //NOTE that the order of the conditionals matters. WHY????? - if ((currentMove<=_maxIndex) && (currentMove>=0) && ([_gameboard[currentMove] isEqual: stringOfCurrentMove])) { - [_gameboard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"~X"]; - _playerTurn = 2; - } - else { - NSLog(@"Invalid move. Enter a number from 0-%d.",_maxIndex); - } - - } - } - else{ - while (_playerTurn == 2) { - - int currentMove; - if (_mode == 1) { - NSLog(@"Player 2, make your move: "); - scanf("%d",¤tMove); + //set game size + if ([ticTacToe getMode] == 1) { + BOOL gameSizeSet = NO; + while (gameSizeSet == NO) { + NSLog(@"Set a game size from 3-10, fool!:"); + int userGameSize; + scanf("%d",&userGameSize); fpurge(stdin); - NSString *stringOfCurrentMove = [NSString stringWithFormat:@"%d",currentMove]; - int lengthOfStringOfCurrentMove = (int)[stringOfCurrentMove length]; - if (lengthOfStringOfCurrentMove == 1) { - NSString *zero = [NSString stringWithFormat:@"0"]; - stringOfCurrentMove = [zero stringByAppendingString:stringOfCurrentMove]; - } - - if ((currentMove<=_maxIndex) && (currentMove>=0) && ([_gameboard[currentMove] isEqual: stringOfCurrentMove])) { - [_gameboard replaceObjectAtIndex:(NSUInteger)currentMove withObject:@"~O"]; - _playerTurn = 1; - } - else { - NSLog(@"Invalid move. Enter a number from 0-%d.",_maxIndex); + if (userGameSize >=3 && userGameSize <= 10) { + [ticTacToe setGameSize:userGameSize]; + gameSizeSet = YES; } - } - else{ - int computerMove = arc4random_uniform(_maxIndex + 1); - if ((![[_gameboard objectAtIndex:computerMove] isEqual: @"~X"]) && (![[_gameboard objectAtIndex:computerMove] isEqual: @"~O"])) { - NSLog(@"Computer is making a move..."); - sleep(1.5); - [_gameboard replaceObjectAtIndex:(NSUInteger)computerMove withObject:@"~O"]; - _playerTurn = 1; + else{ + NSLog(@"Invalid size."); } - } - } - } - _turnCount = _turnCount + 1; -} - -//-(BOOL)checkWin{ -// -// NSString *playerPiece; -// -// for (int i = 0; i < 2; i++) { -// -// //sets player piece for each of the two loops -// if (i == 1) { -// playerPiece = @"~X"; -// _winner = @"Player 1"; -// } -// else{ -// playerPiece = @"~O"; -// _winner = @"Player 2"; -// } -// -// // |X|X|X| -// // | | | | -// // | | | | -// if (([_gameboard[0] isEqual:playerPiece]) && ([_gameboard[1] isEqual:playerPiece]) && ([_gameboard[2] isEqual:playerPiece])){ -// return YES; -// } -// // | | | | -// // |X|X|X| -// // | | | | -// if (([_gameboard[3] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[5] isEqual:playerPiece])){ -// return YES; -// } -// // | | | | -// // | | | | -// // |X|X|X| -// if (([_gameboard[6] isEqual:playerPiece]) && ([_gameboard[7] isEqual:playerPiece]) && ([_gameboard[8] isEqual:playerPiece])){ -// return YES; -// } -// // |X| | | -// // |X| | | -// // |X| | | -// if (([_gameboard[0] isEqual:playerPiece]) && ([_gameboard[3] isEqual:playerPiece]) && ([_gameboard[6] isEqual:playerPiece])){ -// return YES; -// } -// // | |X| | -// // | |X| | -// // | |X| | -// if (([_gameboard[1] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[7] isEqual:playerPiece])){ -// return YES; -// } -// // | | |X| -// // | | |X| -// // | | |X| -// if (([_gameboard[2] isEqual:playerPiece]) && ([_gameboard[5] isEqual:playerPiece]) && ([_gameboard[8] isEqual:playerPiece])){ -// return YES; -// } -// // |X| | | -// // | |X| | -// // | | |X| -// if (([_gameboard[0] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[8] isEqual:playerPiece])){ -// return YES; -// } -// // | | |X| -// // | |X| | -// // |X| | | -// if (([_gameboard[2] isEqual:playerPiece]) && ([_gameboard[4] isEqual:playerPiece]) && ([_gameboard[6] isEqual:playerPiece])){ -// return YES; -// } -// -// } -// return NO; -//} - --(BOOL)checkWin{ - - BOOL gameWon = NO; - - NSMutableArray *line = [[NSMutableArray alloc] init]; //array of the line we are checking for a win - - //HORIZONAL WIN TEST - for (int i = 0; i < pow(_gameSize, 2); i++) { + else{ + [ticTacToe setGameSize:3]; //Computer only works with game size of 3 + } - [line addObject:[_gameboard objectAtIndex:i]]; + [ticTacToe setupGame]; - if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize - gameWon = [line areAllObjectsAreIdentical]; + [MyTicTacToeGame printTutorialBoard:ticTacToe]; + + while (![ticTacToe checkWin] && ![ticTacToe checkDraw]) { - if (gameWon == YES) { - return YES; - } - else{ - [line removeAllObjects]; //empty the line array - } - } - } - - int currentIndex; - int lastIndex; - - //VERTICAL WIN TEST - for (int i = 0; i < _gameSize; i++) { - currentIndex = i; - lastIndex = 0; - - for (int i = 0; i < _gameSize; i++) { - [line addObject:[_gameboard objectAtIndex:currentIndex]]; - lastIndex = currentIndex; - currentIndex = lastIndex + _gameSize; + [ticTacToe turn]; + [MyTicTacToeGame printBoard:ticTacToe]; } - if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize - gameWon = [line areAllObjectsAreIdentical]; - - if (gameWon == YES) { - return YES; + if ([ticTacToe getWin]) { + if ([ticTacToe getTurnCount] % 2 != 0) { + NSLog(@"Player ~X Wins!"); } else{ - [line removeAllObjects]; //empty the line array + NSLog(@"Player ~O Wins!"); } } - } - - //DIAGONAL WIN TEST - - // |X| | | - // | |X| | - // | | |X| - currentIndex = 0; - lastIndex = 0; - for (int i = 0; i < _gameSize; i++) { - [line addObject:[_gameboard objectAtIndex:currentIndex]]; - lastIndex = currentIndex; - currentIndex = lastIndex + _gameSize + 1; - } - - if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize - gameWon = [line areAllObjectsAreIdentical]; - - if (gameWon == YES) { - return YES; - } - else{ - [line removeAllObjects]; //empty the line array - } - } - - // | | |X| - // | |X| | - // |X| | | - currentIndex = _gameSize - 1; - lastIndex = _gameSize - 1; - for (int i = 0; i < _gameSize; i++) { - [line addObject:[_gameboard objectAtIndex:currentIndex]]; - lastIndex = currentIndex; - currentIndex = lastIndex + _gameSize - 1; - } - - if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize - gameWon = [line areAllObjectsAreIdentical]; - - if (gameWon == YES) { - return YES; - } else{ - [line removeAllObjects]; //empty the line array + NSLog(@"It's a draw."); } } - return NO; -} - --(BOOL)checkDraw{ - if ((_win == NO) && (_turnCount == (_maxIndex + 2))){ - return TRUE; - } - return FALSE; -} - -@end - -int main(int argc, const char * argv[]) { - @autoreleasepool { - - Game *ticTacToe =[[Game alloc]init]; - [ticTacToe startGame]; - - // while ([ticTacToe win] == NO) { - // ask user input - // validate the users move - // if user move valid - // make move - // validate board - // if won - // - // // ask game to priint board - } - return 0; } From b2bd8c4077626a5d66d2cce401f2dbc931bf31fa Mon Sep 17 00:00:00 2001 From: Derek Date: Sun, 12 Jul 2015 21:12:19 -0400 Subject: [PATCH 20/24] horizontal block working on medium difficulty --- TicTacToe/TicTacToe.xcodeproj/project.pbxproj | 6 + TicTacToe/TicTacToe/Game.m | 188 ++++++++++++++++-- TicTacToe/TicTacToe/NSString+isNumeric.h | 14 ++ TicTacToe/TicTacToe/NSString+isNumeric.m | 33 +++ 4 files changed, 224 insertions(+), 17 deletions(-) create mode 100644 TicTacToe/TicTacToe/NSString+isNumeric.h create mode 100644 TicTacToe/TicTacToe/NSString+isNumeric.m diff --git a/TicTacToe/TicTacToe.xcodeproj/project.pbxproj b/TicTacToe/TicTacToe.xcodeproj/project.pbxproj index 47a8908..653e63d 100644 --- a/TicTacToe/TicTacToe.xcodeproj/project.pbxproj +++ b/TicTacToe/TicTacToe.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 692E08551B533C8300A52FCC /* NSString+isNumeric.m in Sources */ = {isa = PBXBuildFile; fileRef = 692E08541B533C8300A52FCC /* NSString+isNumeric.m */; }; 695078051B4C867600735A8D /* Game.m in Sources */ = {isa = PBXBuildFile; fileRef = 695078041B4C867600735A8D /* Game.m */; }; 695078081B4C880600735A8D /* NSArray+LineChecker.m in Sources */ = {isa = PBXBuildFile; fileRef = 695078071B4C880600735A8D /* NSArray+LineChecker.m */; }; 6950780B1B4C893600735A8D /* MyTicTacToeGame.m in Sources */ = {isa = PBXBuildFile; fileRef = 6950780A1B4C893600735A8D /* MyTicTacToeGame.m */; }; @@ -28,6 +29,8 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 692E08531B533C8300A52FCC /* NSString+isNumeric.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+isNumeric.h"; sourceTree = ""; }; + 692E08541B533C8300A52FCC /* NSString+isNumeric.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+isNumeric.m"; sourceTree = ""; }; 695078031B4C867600735A8D /* Game.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Game.h; sourceTree = ""; }; 695078041B4C867600735A8D /* Game.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Game.m; sourceTree = ""; }; 695078061B4C880600735A8D /* NSArray+LineChecker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+LineChecker.h"; sourceTree = ""; }; @@ -77,6 +80,8 @@ 6950780D1B4C97FD00735A8D /* Player.m */, 695078031B4C867600735A8D /* Game.h */, 695078041B4C867600735A8D /* Game.m */, + 692E08531B533C8300A52FCC /* NSString+isNumeric.h */, + 692E08541B533C8300A52FCC /* NSString+isNumeric.m */, 696503591B51B1CD00300B68 /* ComputerPlayer.h */, 6965035A1B51B1CD00300B68 /* ComputerPlayer.m */, 695078091B4C893600735A8D /* MyTicTacToeGame.h */, @@ -144,6 +149,7 @@ buildActionMask = 2147483647; files = ( 6950780B1B4C893600735A8D /* MyTicTacToeGame.m in Sources */, + 692E08551B533C8300A52FCC /* NSString+isNumeric.m in Sources */, 8D9789E41B3C9A70007CF4CF /* main.m in Sources */, 695078081B4C880600735A8D /* NSArray+LineChecker.m in Sources */, 6965035B1B51B1CD00300B68 /* ComputerPlayer.m in Sources */, diff --git a/TicTacToe/TicTacToe/Game.m b/TicTacToe/TicTacToe/Game.m index 7fc55b9..1b1f742 100644 --- a/TicTacToe/TicTacToe/Game.m +++ b/TicTacToe/TicTacToe/Game.m @@ -9,6 +9,7 @@ #import "Game.h" #import "Player.h" #import "NSArray+LineChecker.h" +#import "NSString+isNumeric.h" @implementation Game { int _mode; @@ -90,30 +91,34 @@ -(void)setupGame { } } -//-(void)movePadder:(NSString *)stringOfCurrentMove { -// int lengthOfStringOfCurrentMove = (int)[stringOfCurrentMove length]; -// if (lengthOfStringOfCurrentMove == 1) { -// stringOfCurrentMove = [NSString stringWithFormat:@"%02d", stringOfCurrentMove]; -// } -//} - --(NSMutableString *)makeMove{ +-(NSString *)makeMove{ NSLog(@"Player %d make your move:",_playerTurn); scanf("%d",&_currentMove); fpurge(stdin); - NSMutableString *stringOfCurrentMove = [NSMutableString stringWithFormat:@"%02d",_currentMove]; -// [self movePadder:stringOfCurrentMove]; + NSString *stringOfCurrentMove = [NSString stringWithFormat:@"%02d",_currentMove]; return stringOfCurrentMove; } --(NSMutableString *)computerMakeMove{ +-(NSString *)computerMakeMove{ + //Easy Mode computer move if (_difficulty == 1) { _currentMove = arc4random_uniform(_maxIndex + 1); - NSMutableString *stringOfCurrentMove = [NSMutableString stringWithFormat:@"%02d",_currentMove]; -// [self movePadder:stringOfCurrentMove]; + NSString *stringOfCurrentMove = [NSString stringWithFormat:@"%02d",_currentMove]; + return stringOfCurrentMove; + } + + //Medium Mode computer move + else if (_difficulty == 2){ + _currentMove = [self winBlocker]; + NSString *stringOfCurrentMove = [NSString stringWithFormat:@"%02d",_currentMove]; + + if ([stringOfCurrentMove isEqualToString:@"9999"]) { //if win blocker found no blocks to make + _currentMove = arc4random_uniform(_maxIndex + 1); + stringOfCurrentMove = [NSString stringWithFormat:@"%02d",_currentMove]; + } return stringOfCurrentMove; } return @"HEHEE"; @@ -136,19 +141,33 @@ -(void)turn{ NSString *stringOfCurrentMove; - if (_mode == 1) { + if (_mode == 1) { //Player vs. Player stringOfCurrentMove = [self makeMove]; } - else if (_mode == 2){ - stringOfCurrentMove = [self computerMakeMove]; + else if (_mode == 2){ //Player vs. Computer + if (_playerTurn == 1) { + stringOfCurrentMove = [self makeMove]; + } + else{ + stringOfCurrentMove = [self computerMakeMove]; + } } if (([_gameboard[_currentMove] isEqual: stringOfCurrentMove]) && (_currentMove<=_maxIndex) && (_currentMove>=0)) { [_gameboard replaceObjectAtIndex:(NSUInteger)_currentMove withObject:[self getPlayerPiece]]; moveMade = YES; + + if ((_mode != 1) && (_playerTurn == 2)) { + NSLog(@"Computer is thinking..."); + usleep(1e6); + NSLog(@"Computer has made a move!"); + } + } else { - NSLog(@"Invalid move. Enter a number from 0-%d.",_maxIndex); + if (_mode == 1) { + NSLog(@"Invalid move. Enter a number from 0-%d.",_maxIndex); + } } } _turnCount = _turnCount + 1; @@ -263,4 +282,139 @@ -(BOOL)checkDraw{ return FALSE; } +//-(int)returnIndexOfSpaceToBlock:(NSMutableArray*)line{ +// int xPieceCount = 0; +// int spaceCount = 0; +// int indexOfSpace = 0; +// +// for (int j = 0; j < [line count]; j++) { +// if ([[line objectAtIndex:j] isEqual: @"~X"]) { +// xPieceCount++; +// } +// else if ([[line objectAtIndex:j] isEqual: @"~O"]) { +// } +// else { +// spaceCount++; +// } +// } +// +// if ((xPieceCount == _gameSize - 1) && (spaceCount == 1)) { +// return indexOfSpace; +// } +// return 9999; +//} + +-(int)winBlocker{ //returns index of gameBoard to block + NSMutableArray *line = [[NSMutableArray alloc] init]; //array of the line we are checking for a win + NSMutableString *stringOfLineObject = [[NSMutableString alloc] init]; + int xPieceCount = 0; + int spaceCount = 0; + int indexOfSpace = 0; + + //HORIZONAL BLOCK TEST + for (int i = 0; i < pow(_gameSize, 2); i++) { + + stringOfLineObject = [_gameboard objectAtIndex:i]; + [line addObject:stringOfLineObject]; + + if ([stringOfLineObject isNumeric]) { + indexOfSpace = i; + } + + if ([line count] == _gameSize) { //line array reaches the gameSize + + for (int j = 0; j < [line count]; j++) { + if ([[line objectAtIndex:j] isEqual: @"~X"]) { + xPieceCount++; + } + else if ([[line objectAtIndex:j] isEqual: @"~O"]) { + } + else { + spaceCount++; + } + } + + if ((xPieceCount == _gameSize - 1) && (spaceCount == 1)) { + return indexOfSpace; + } + + [line removeAllObjects]; //empty the line array + xPieceCount = 0; spaceCount = 0; indexOfSpace = 0; + } + } + + int currentIndex; + int lastIndex; + + //VERTICAL WIN TEST + for (int i = 0; i < _gameSize; i++) { + currentIndex = i; + lastIndex = 0; + + for (int i = 0; i < _gameSize; i++) { + [line addObject:[_gameboard objectAtIndex:currentIndex]]; + lastIndex = currentIndex; + currentIndex = lastIndex + _gameSize; + } + + if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize + _win = [line areAllObjectsAreIdentical]; + + if (_win == YES) { + return YES; + } + else{ + [line removeAllObjects]; //empty the line array + } + } + } + + //DIAGONAL WIN TEST + + // |X| | | + // | |X| | + // | | |X| + currentIndex = 0; + lastIndex = 0; + for (int i = 0; i < _gameSize; i++) { + [line addObject:[_gameboard objectAtIndex:currentIndex]]; + lastIndex = currentIndex; + currentIndex = lastIndex + _gameSize + 1; + } + + if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize + _win = [line areAllObjectsAreIdentical]; + + if (_win == YES) { + return YES; + } + else{ + [line removeAllObjects]; //empty the line array + } + } + + // | | |X| + // | |X| | + // |X| | | + currentIndex = _gameSize - 1; + lastIndex = _gameSize - 1; + for (int i = 0; i < _gameSize; i++) { + [line addObject:[_gameboard objectAtIndex:currentIndex]]; + lastIndex = currentIndex; + currentIndex = lastIndex + _gameSize - 1; + } + + if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize + _win = [line areAllObjectsAreIdentical]; + + if (_win == YES) { + return YES; + } + else{ + [line removeAllObjects]; //empty the line array + } + } + return 9999; +} + @end diff --git a/TicTacToe/TicTacToe/NSString+isNumeric.h b/TicTacToe/TicTacToe/NSString+isNumeric.h new file mode 100644 index 0000000..9bc3f76 --- /dev/null +++ b/TicTacToe/TicTacToe/NSString+isNumeric.h @@ -0,0 +1,14 @@ +// +// NSString+isNumeric.h +// TicTacToe +// +// Created by Z on 7/12/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import + +@interface NSString (isNumeric) +- (BOOL) isAllDigits; +- (BOOL) isNumeric; +@end diff --git a/TicTacToe/TicTacToe/NSString+isNumeric.m b/TicTacToe/TicTacToe/NSString+isNumeric.m new file mode 100644 index 0000000..a4f1753 --- /dev/null +++ b/TicTacToe/TicTacToe/NSString+isNumeric.m @@ -0,0 +1,33 @@ +// +// NSString+isNumeric.m +// TicTacToe +// +// Created by Z on 7/12/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import "NSString+isNumeric.h" + +@implementation NSString (isNumeric) + +- (BOOL) isAllDigits{ + NSCharacterSet* nonNumbers = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; + NSRange r = [self rangeOfCharacterFromSet: nonNumbers]; + return r.location == NSNotFound; +} + +-(BOOL) isNumeric{ + NSScanner *sc = [NSScanner scannerWithString: self]; + // We can pass NULL because we don't actually need the value to test + // for if the string is numeric. This is allowable. + if ( [sc scanFloat:NULL] ) + { + // Ensure nothing left in scanner so that "42foo" is not accepted. + // ("42" would be consumed by scanFloat above leaving "foo".) + return [sc isAtEnd]; + } + // Couldn't even scan a float :( + return NO; +} + +@end From 7db745b4a7e5f2f2d8390a1f0680c432ebfda01e Mon Sep 17 00:00:00 2001 From: Derek Date: Mon, 13 Jul 2015 18:49:11 -0400 Subject: [PATCH 21/24] mike refactor --- TicTacToe/TicTacToe.xcodeproj/project.pbxproj | 6 + TicTacToe/TicTacToe/Game.h | 4 +- TicTacToe/TicTacToe/Game.m | 50 ++++---- TicTacToe/TicTacToe/TicTacToeRowGenerator.h | 22 ++++ TicTacToe/TicTacToe/TicTacToeRowGenerator.m | 109 ++++++++++++++++++ TicTacToe/TicTacToe/main.m | 3 +- 6 files changed, 170 insertions(+), 24 deletions(-) create mode 100644 TicTacToe/TicTacToe/TicTacToeRowGenerator.h create mode 100644 TicTacToe/TicTacToe/TicTacToeRowGenerator.m diff --git a/TicTacToe/TicTacToe.xcodeproj/project.pbxproj b/TicTacToe/TicTacToe.xcodeproj/project.pbxproj index 653e63d..343c979 100644 --- a/TicTacToe/TicTacToe.xcodeproj/project.pbxproj +++ b/TicTacToe/TicTacToe.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ 6950780B1B4C893600735A8D /* MyTicTacToeGame.m in Sources */ = {isa = PBXBuildFile; fileRef = 6950780A1B4C893600735A8D /* MyTicTacToeGame.m */; }; 6950780E1B4C97FD00735A8D /* Player.m in Sources */ = {isa = PBXBuildFile; fileRef = 6950780D1B4C97FD00735A8D /* Player.m */; }; 6965035B1B51B1CD00300B68 /* ComputerPlayer.m in Sources */ = {isa = PBXBuildFile; fileRef = 6965035A1B51B1CD00300B68 /* ComputerPlayer.m */; }; + 698005621B547295008F3E8A /* TicTacToeRowGenerator.m in Sources */ = {isa = PBXBuildFile; fileRef = 698005611B547295008F3E8A /* TicTacToeRowGenerator.m */; }; 8D9789E41B3C9A70007CF4CF /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D9789E31B3C9A70007CF4CF /* main.m */; }; /* End PBXBuildFile section */ @@ -41,6 +42,8 @@ 6950780D1B4C97FD00735A8D /* Player.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Player.m; sourceTree = ""; }; 696503591B51B1CD00300B68 /* ComputerPlayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ComputerPlayer.h; sourceTree = ""; }; 6965035A1B51B1CD00300B68 /* ComputerPlayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ComputerPlayer.m; sourceTree = ""; }; + 698005601B547295008F3E8A /* TicTacToeRowGenerator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TicTacToeRowGenerator.h; sourceTree = ""; }; + 698005611B547295008F3E8A /* TicTacToeRowGenerator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TicTacToeRowGenerator.m; sourceTree = ""; }; 8D9789E01B3C9A70007CF4CF /* TicTacToe */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = TicTacToe; sourceTree = BUILT_PRODUCTS_DIR; }; 8D9789E31B3C9A70007CF4CF /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; /* End PBXFileReference section */ @@ -88,6 +91,8 @@ 6950780A1B4C893600735A8D /* MyTicTacToeGame.m */, 695078061B4C880600735A8D /* NSArray+LineChecker.h */, 695078071B4C880600735A8D /* NSArray+LineChecker.m */, + 698005601B547295008F3E8A /* TicTacToeRowGenerator.h */, + 698005611B547295008F3E8A /* TicTacToeRowGenerator.m */, ); path = TicTacToe; sourceTree = ""; @@ -149,6 +154,7 @@ buildActionMask = 2147483647; files = ( 6950780B1B4C893600735A8D /* MyTicTacToeGame.m in Sources */, + 698005621B547295008F3E8A /* TicTacToeRowGenerator.m in Sources */, 692E08551B533C8300A52FCC /* NSString+isNumeric.m in Sources */, 8D9789E41B3C9A70007CF4CF /* main.m in Sources */, 695078081B4C880600735A8D /* NSArray+LineChecker.m in Sources */, diff --git a/TicTacToe/TicTacToe/Game.h b/TicTacToe/TicTacToe/Game.h index afa0332..5451a7e 100644 --- a/TicTacToe/TicTacToe/Game.h +++ b/TicTacToe/TicTacToe/Game.h @@ -8,6 +8,7 @@ #import + @interface Game : NSObject -(BOOL)checkWin; @@ -19,7 +20,6 @@ - (NSMutableArray *)getGameBoard; - -(void)printBoard; -(void)printTutorialBoard; @@ -29,6 +29,8 @@ -(void)setGameSize:(int)size; -(int)getGameSize; +- (void)getGameBoard:(NSArray *)gameBoard; + //Easy, Medium, Hard -(void)setDifficulty:(int)level; -(int)getDifficulty; diff --git a/TicTacToe/TicTacToe/Game.m b/TicTacToe/TicTacToe/Game.m index 1b1f742..5a8e870 100644 --- a/TicTacToe/TicTacToe/Game.m +++ b/TicTacToe/TicTacToe/Game.m @@ -11,6 +11,7 @@ #import "NSArray+LineChecker.h" #import "NSString+isNumeric.h" + @implementation Game { int _mode; @@ -282,27 +283,28 @@ -(BOOL)checkDraw{ return FALSE; } -//-(int)returnIndexOfSpaceToBlock:(NSMutableArray*)line{ -// int xPieceCount = 0; -// int spaceCount = 0; -// int indexOfSpace = 0; -// -// for (int j = 0; j < [line count]; j++) { -// if ([[line objectAtIndex:j] isEqual: @"~X"]) { -// xPieceCount++; -// } -// else if ([[line objectAtIndex:j] isEqual: @"~O"]) { -// } -// else { -// spaceCount++; -// } -// } -// -// if ((xPieceCount == _gameSize - 1) && (spaceCount == 1)) { -// return indexOfSpace; -// } -// return 9999; -//} +-(int)returnIndexOfSpaceToBlock:(NSMutableArray*)line{ + int xPieceCount = 0; + int spaceCount = 0; + int indexOfSpace = 0; + + for (int j = 0; j < [line count]; j++) { + if ([[line objectAtIndex:j] isEqual: @"~X"]) { + xPieceCount++; + } + else if ([[line objectAtIndex:j] isEqual: @"~O"]) { + } + else { + spaceCount++; + } + } + + if ((xPieceCount == _gameSize - 1) && (spaceCount == 1)) { + return indexOfSpace; + } + return 9999; +} + -(int)winBlocker{ //returns index of gameBoard to block NSMutableArray *line = [[NSMutableArray alloc] init]; //array of the line we are checking for a win @@ -311,6 +313,8 @@ -(int)winBlocker{ //returns index of gameBoard to block int spaceCount = 0; int indexOfSpace = 0; + NSMutableArray *lines = [[NSMutableArray alloc] init]; + //HORIZONAL BLOCK TEST for (int i = 0; i < pow(_gameSize, 2); i++) { @@ -322,7 +326,7 @@ -(int)winBlocker{ //returns index of gameBoard to block } if ([line count] == _gameSize) { //line array reaches the gameSize - + [lines addObject:line]; for (int j = 0; j < [line count]; j++) { if ([[line objectAtIndex:j] isEqual: @"~X"]) { xPieceCount++; @@ -343,6 +347,8 @@ -(int)winBlocker{ //returns index of gameBoard to block } } + return indexOfSpace; + int currentIndex; int lastIndex; diff --git a/TicTacToe/TicTacToe/TicTacToeRowGenerator.h b/TicTacToe/TicTacToe/TicTacToeRowGenerator.h new file mode 100644 index 0000000..cbc9dba --- /dev/null +++ b/TicTacToe/TicTacToe/TicTacToeRowGenerator.h @@ -0,0 +1,22 @@ +// +// TicTacToeRowGenerator.h +// TicTacToe +// +// Created by Z on 7/13/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import + +typedef NS_ENUM(NSUInteger, Direction) { + Horizonal, + Vertical, + Diagonal +}; + +@interface TicTacToeRowGenerator : NSObject + ++ (NSMutableArray *)allLinesForDirection:(Direction)direction + withGame:(NSArray *)gameBoard; + +@end diff --git a/TicTacToe/TicTacToe/TicTacToeRowGenerator.m b/TicTacToe/TicTacToe/TicTacToeRowGenerator.m new file mode 100644 index 0000000..359ee62 --- /dev/null +++ b/TicTacToe/TicTacToe/TicTacToeRowGenerator.m @@ -0,0 +1,109 @@ +// +// TicTacToeRowGenerator.m +// TicTacToe +// +// Created by Z on 7/13/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import "TicTacToeRowGenerator.h" +#import "NSString+isNumeric.h" + +@implementation TicTacToeRowGenerator + ++ (NSMutableArray *)allLinesForDirection:(Direction)direction + withGame:(NSArray *)gameBoard { + + // check if valid gameboard + // check if valid direction + + switch (direction) { + case Horizonal: + return [self horizontalLinesForGame:gameBoard]; + case Vertical: + return [self verticalLinesForGame:gameBoard]; + case Diagonal: + return [self diagonalLinesForGame:gameBoard]; + } +} + ++ (NSMutableArray *)horizontalLinesForGame:(NSArray *)gameBoard { + + NSInteger sizeOfGame = sqrt(gameBoard.count); + NSMutableArray *lines = [[NSMutableArray alloc] init]; + + + // this sucks it's state + NSMutableArray *line = [[NSMutableArray alloc] init]; + + for (int i = 0; i < gameBoard.count; i++) { + + NSString *currentVal = [gameBoard objectAtIndex:i]; + [line addObject:currentVal]; + + if ([line count] == sizeOfGame) { //line array reaches the gameSize + [lines addObject:[NSArray arrayWithArray:line]]; + [line removeAllObjects]; + } + } + + return lines; +} + ++ (NSMutableArray *)verticalLinesForGame:(NSArray *)gameBoard { + NSInteger sizeOfGame = sqrt(gameBoard.count); + + NSMutableArray *lines = [[NSMutableArray alloc] init]; + + NSInteger currentIndex; + NSInteger lastIndex; + + for (int i = 0; i < sizeOfGame; i++) { + currentIndex = i; + lastIndex = 0; + NSMutableArray *line = [[NSMutableArray alloc] init]; + for (int i = 0; i < sizeOfGame; i++) { + [line addObject:[gameBoard objectAtIndex:currentIndex]]; + lastIndex = currentIndex; + currentIndex = lastIndex + sizeOfGame; + } + + [lines addObject:line]; + } + + return lines; +} + ++ (NSMutableArray *)diagonalLinesForGame:(NSArray *)gameBoard { + + NSInteger sizeOfGame = sqrt(gameBoard.count); + NSMutableArray *lines = [[NSMutableArray alloc] init]; + + NSInteger currentIndex = 0; + NSInteger lastIndex = 0; + + NSMutableArray *line = [[NSMutableArray alloc] init]; + for (int i = 0; i < sizeOfGame; i++) { + [line addObject:[gameBoard objectAtIndex:currentIndex]]; + lastIndex = currentIndex; + currentIndex = lastIndex + sizeOfGame + 1; + } + + [lines addObject:line]; + + currentIndex = sizeOfGame - 1; + lastIndex = sizeOfGame - 1; + + for (int i = 0; i < sizeOfGame; i++) { + [line addObject:[gameBoard objectAtIndex:currentIndex]]; + lastIndex = currentIndex; + currentIndex = lastIndex + sizeOfGame - 1; + } + + [lines addObject:line]; + + return lines; +} + + +@end diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index d80dbbd..1c6e897 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -8,6 +8,7 @@ #import #import "MyTicTacToeGame.h" +#import "TicTacToeRowGenerator.h" #import "Game.h" @@ -97,7 +98,7 @@ int main(int argc, const char * argv[]) { [MyTicTacToeGame printTutorialBoard:ticTacToe]; while (![ticTacToe checkWin] && ![ticTacToe checkDraw]) { - + NSLog(@"%@", [TicTacToeRowGenerator allLinesForDirection:Horizonal withGame:[ticTacToe getGameBoard]]); [ticTacToe turn]; [MyTicTacToeGame printBoard:ticTacToe]; } From f81c354a7ebb03016e5fa7f546b784524f7f2d98 Mon Sep 17 00:00:00 2001 From: Derek Date: Mon, 13 Jul 2015 21:01:58 -0400 Subject: [PATCH 22/24] made line inspector --- TicTacToe/TicTacToe.xcodeproj/project.pbxproj | 8 +- TicTacToe/TicTacToe/Game.h | 7 + TicTacToe/TicTacToe/Game.m | 259 ++++++++++-------- TicTacToe/TicTacToe/TicTacToeRowGenerator.m | 1 - TicTacToe/TicTacToe/main.m | 2 +- 5 files changed, 152 insertions(+), 125 deletions(-) diff --git a/TicTacToe/TicTacToe.xcodeproj/project.pbxproj b/TicTacToe/TicTacToe.xcodeproj/project.pbxproj index 343c979..d7e93bd 100644 --- a/TicTacToe/TicTacToe.xcodeproj/project.pbxproj +++ b/TicTacToe/TicTacToe.xcodeproj/project.pbxproj @@ -83,16 +83,16 @@ 6950780D1B4C97FD00735A8D /* Player.m */, 695078031B4C867600735A8D /* Game.h */, 695078041B4C867600735A8D /* Game.m */, - 692E08531B533C8300A52FCC /* NSString+isNumeric.h */, - 692E08541B533C8300A52FCC /* NSString+isNumeric.m */, 696503591B51B1CD00300B68 /* ComputerPlayer.h */, 6965035A1B51B1CD00300B68 /* ComputerPlayer.m */, 695078091B4C893600735A8D /* MyTicTacToeGame.h */, 6950780A1B4C893600735A8D /* MyTicTacToeGame.m */, - 695078061B4C880600735A8D /* NSArray+LineChecker.h */, - 695078071B4C880600735A8D /* NSArray+LineChecker.m */, 698005601B547295008F3E8A /* TicTacToeRowGenerator.h */, 698005611B547295008F3E8A /* TicTacToeRowGenerator.m */, + 695078061B4C880600735A8D /* NSArray+LineChecker.h */, + 695078071B4C880600735A8D /* NSArray+LineChecker.m */, + 692E08531B533C8300A52FCC /* NSString+isNumeric.h */, + 692E08541B533C8300A52FCC /* NSString+isNumeric.m */, ); path = TicTacToe; sourceTree = ""; diff --git a/TicTacToe/TicTacToe/Game.h b/TicTacToe/TicTacToe/Game.h index 5451a7e..10b8061 100644 --- a/TicTacToe/TicTacToe/Game.h +++ b/TicTacToe/TicTacToe/Game.h @@ -8,6 +8,13 @@ #import +typedef NS_ENUM(NSUInteger, PossibleWinner) { + PlayerX, + PlayerO, + ComputerX, + ComputerO, + noWinNextMove +}; @interface Game : NSObject diff --git a/TicTacToe/TicTacToe/Game.m b/TicTacToe/TicTacToe/Game.m index 5a8e870..fc6f43a 100644 --- a/TicTacToe/TicTacToe/Game.m +++ b/TicTacToe/TicTacToe/Game.m @@ -10,6 +10,7 @@ #import "Player.h" #import "NSArray+LineChecker.h" #import "NSString+isNumeric.h" +#import "TicTacToeRowGenerator.h" @implementation Game { @@ -283,144 +284,164 @@ -(BOOL)checkDraw{ return FALSE; } --(int)returnIndexOfSpaceToBlock:(NSMutableArray*)line{ - int xPieceCount = 0; - int spaceCount = 0; - int indexOfSpace = 0; +-(NSDictionary *)lineInspect:(NSMutableArray*)line{ + NSNumber *xPieceCount = 0; + NSNumber *oPieceCount = 0; + NSNumber *spaceCount = 0; + NSMutableArray *IndicesOfSpaces = [[NSMutableArray alloc] init]; for (int j = 0; j < [line count]; j++) { if ([[line objectAtIndex:j] isEqual: @"~X"]) { - xPieceCount++; + xPieceCount = [NSNumber numberWithInt: [xPieceCount intValue] + 1 ]; } else if ([[line objectAtIndex:j] isEqual: @"~O"]) { + oPieceCount = [NSNumber numberWithInt: [oPieceCount intValue] + 1 ]; } else { - spaceCount++; + spaceCount = [NSNumber numberWithInt: [spaceCount intValue] + 1 ]; } } - if ((xPieceCount == _gameSize - 1) && (spaceCount == 1)) { - return indexOfSpace; - } - return 9999; + NSArray *keyArray = [NSArray arrayWithObjects: @"xPieceCount", @"oPieceCount", @"spaceCount", @"IndicesOfSpaces",nil]; + NSArray *valArray = [NSArray arrayWithObjects: xPieceCount, oPieceCount, spaceCount, IndicesOfSpaces, nil]; + + NSDictionary *countsForElementsInLine = [NSDictionary dictionaryWithObjects:valArray forKeys:keyArray]; + return countsForElementsInLine; } - --(int)winBlocker{ //returns index of gameBoard to block - NSMutableArray *line = [[NSMutableArray alloc] init]; //array of the line we are checking for a win - NSMutableString *stringOfLineObject = [[NSMutableString alloc] init]; - int xPieceCount = 0; - int spaceCount = 0; - int indexOfSpace = 0; +-(PossibleWinner) whoCanWinOnNextMove { - NSMutableArray *lines = [[NSMutableArray alloc] init]; + NSArray *horizontals = [TicTacToeRowGenerator allLinesForDirection:Horizonal withGame:_gameboard]; + NSArray *verticals = [TicTacToeRowGenerator allLinesForDirection:Vertical withGame:_gameboard]; + NSArray *diagonals = [TicTacToeRowGenerator allLinesForDirection:Diagonal withGame:_gameboard]; - //HORIZONAL BLOCK TEST - for (int i = 0; i < pow(_gameSize, 2); i++) { - - stringOfLineObject = [_gameboard objectAtIndex:i]; - [line addObject:stringOfLineObject]; + for (int i = 0; i< horizontals.count; i++) { - if ([stringOfLineObject isNumeric]) { - indexOfSpace = i; - } - - if ([line count] == _gameSize) { //line array reaches the gameSize - [lines addObject:line]; - for (int j = 0; j < [line count]; j++) { - if ([[line objectAtIndex:j] isEqual: @"~X"]) { - xPieceCount++; - } - else if ([[line objectAtIndex:j] isEqual: @"~O"]) { - } - else { - spaceCount++; - } - } - - if ((xPieceCount == _gameSize - 1) && (spaceCount == 1)) { - return indexOfSpace; - } - - [line removeAllObjects]; //empty the line array - xPieceCount = 0; spaceCount = 0; indexOfSpace = 0; - } - } - - return indexOfSpace; - - int currentIndex; - int lastIndex; - - //VERTICAL WIN TEST - for (int i = 0; i < _gameSize; i++) { - currentIndex = i; - lastIndex = 0; - - for (int i = 0; i < _gameSize; i++) { - [line addObject:[_gameboard objectAtIndex:currentIndex]]; - lastIndex = currentIndex; - currentIndex = lastIndex + _gameSize; - } - - if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize - _win = [line areAllObjectsAreIdentical]; - - if (_win == YES) { - return YES; - } - else{ - [line removeAllObjects]; //empty the line array - } - } - } - - //DIAGONAL WIN TEST - - // |X| | | - // | |X| | - // | | |X| - currentIndex = 0; - lastIndex = 0; - for (int i = 0; i < _gameSize; i++) { - [line addObject:[_gameboard objectAtIndex:currentIndex]]; - lastIndex = currentIndex; - currentIndex = lastIndex + _gameSize + 1; - } - - if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize - _win = [line areAllObjectsAreIdentical]; + NSDictionary *countsForElementsInLine = [self lineInspect:[horizontals objectAtIndex:i]]; - if (_win == YES) { - return YES; - } - else{ - [line removeAllObjects]; //empty the line array + if (([[countsForElementsInLine valueForKey:@"xPieceCount"] intValue] == _gameSize - 1) && ([[countsForElementsInLine valueForKey: @"IndicesOfSpaces"] length] == 1)) { + <#statements#> } } - // | | |X| - // | |X| | - // |X| | | - currentIndex = _gameSize - 1; - lastIndex = _gameSize - 1; - for (int i = 0; i < _gameSize; i++) { - [line addObject:[_gameboard objectAtIndex:currentIndex]]; - lastIndex = currentIndex; - currentIndex = lastIndex + _gameSize - 1; - } - - if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize - _win = [line areAllObjectsAreIdentical]; - - if (_win == YES) { - return YES; - } - else{ - [line removeAllObjects]; //empty the line array - } - } - return 9999; } + +//-(int)winBlocker{ //returns index of gameBoard to block +// NSMutableArray *line = [[NSMutableArray alloc] init]; //array of the line we are checking for a win +// NSMutableString *stringOfLineObject = [[NSMutableString alloc] init]; +// int xPieceCount = 0; +// int spaceCount = 0; +// int indexOfSpace = 0; +// +// NSMutableArray *lines = [[NSMutableArray alloc] init]; +// +// //HORIZONAL BLOCK TEST +// for (int i = 0; i < pow(_gameSize, 2); i++) { +// +// stringOfLineObject = [_gameboard objectAtIndex:i]; +// [line addObject:stringOfLineObject]; +// +// if ([stringOfLineObject isNumeric]) { +// indexOfSpace = i; +// } +// +// if ([line count] == _gameSize) { //line array reaches the gameSize +// [lines addObject:line]; +// for (int j = 0; j < [line count]; j++) { +// if ([[line objectAtIndex:j] isEqual: @"~X"]) { +// xPieceCount++; +// } +// else if ([[line objectAtIndex:j] isEqual: @"~O"]) { +// } +// else { +// spaceCount++; +// } +// } +// +// if ((xPieceCount == _gameSize - 1) && (spaceCount == 1)) { +// return indexOfSpace; +// } +// +// [line removeAllObjects]; //empty the line array +// xPieceCount = 0; spaceCount = 0; indexOfSpace = 0; +// } +// } +// +// return indexOfSpace; +// +// int currentIndex; +// int lastIndex; +// +// //VERTICAL WIN TEST +// for (int i = 0; i < _gameSize; i++) { +// currentIndex = i; +// lastIndex = 0; +// +// for (int i = 0; i < _gameSize; i++) { +// [line addObject:[_gameboard objectAtIndex:currentIndex]]; +// lastIndex = currentIndex; +// currentIndex = lastIndex + _gameSize; +// } +// +// if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize +// _win = [line areAllObjectsAreIdentical]; +// +// if (_win == YES) { +// return YES; +// } +// else{ +// [line removeAllObjects]; //empty the line array +// } +// } +// } +// +// //DIAGONAL WIN TEST +// +// // |X| | | +// // | |X| | +// // | | |X| +// currentIndex = 0; +// lastIndex = 0; +// for (int i = 0; i < _gameSize; i++) { +// [line addObject:[_gameboard objectAtIndex:currentIndex]]; +// lastIndex = currentIndex; +// currentIndex = lastIndex + _gameSize + 1; +// } +// +// if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize +// _win = [line areAllObjectsAreIdentical]; +// +// if (_win == YES) { +// return YES; +// } +// else{ +// [line removeAllObjects]; //empty the line array +// } +// } +// +// // | | |X| +// // | |X| | +// // |X| | | +// currentIndex = _gameSize - 1; +// lastIndex = _gameSize - 1; +// for (int i = 0; i < _gameSize; i++) { +// [line addObject:[_gameboard objectAtIndex:currentIndex]]; +// lastIndex = currentIndex; +// currentIndex = lastIndex + _gameSize - 1; +// } +// +// if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize +// _win = [line areAllObjectsAreIdentical]; +// +// if (_win == YES) { +// return YES; +// } +// else{ +// [line removeAllObjects]; //empty the line array +// } +// } +// return 9999; +//} +// @end diff --git a/TicTacToe/TicTacToe/TicTacToeRowGenerator.m b/TicTacToe/TicTacToe/TicTacToeRowGenerator.m index 359ee62..6c66a29 100644 --- a/TicTacToe/TicTacToe/TicTacToeRowGenerator.m +++ b/TicTacToe/TicTacToe/TicTacToeRowGenerator.m @@ -32,7 +32,6 @@ + (NSMutableArray *)horizontalLinesForGame:(NSArray *)gameBoard { NSInteger sizeOfGame = sqrt(gameBoard.count); NSMutableArray *lines = [[NSMutableArray alloc] init]; - // this sucks it's state NSMutableArray *line = [[NSMutableArray alloc] init]; diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 1c6e897..9e1c45f 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -98,7 +98,7 @@ int main(int argc, const char * argv[]) { [MyTicTacToeGame printTutorialBoard:ticTacToe]; while (![ticTacToe checkWin] && ![ticTacToe checkDraw]) { - NSLog(@"%@", [TicTacToeRowGenerator allLinesForDirection:Horizonal withGame:[ticTacToe getGameBoard]]); +// NSLog(@"%@", [TicTacToeRowGenerator allLinesForDirection:Horizonal withGame:[ticTacToe getGameBoard]]); [ticTacToe turn]; [MyTicTacToeGame printBoard:ticTacToe]; } From dd29eac806760b9ce67628e7258df7f67d6bb4a9 Mon Sep 17 00:00:00 2001 From: Derek Date: Fri, 17 Jul 2015 15:56:13 -0400 Subject: [PATCH 23/24] fixed medium difficulty --- TicTacToe/TicTacToe/Game.h | 12 + TicTacToe/TicTacToe/Game.m | 348 ++++++-------------- TicTacToe/TicTacToe/TicTacToeRowGenerator.m | 14 +- TicTacToe/TicTacToe/main.m | 30 +- 4 files changed, 141 insertions(+), 263 deletions(-) diff --git a/TicTacToe/TicTacToe/Game.h b/TicTacToe/TicTacToe/Game.h index 10b8061..95aa6dd 100644 --- a/TicTacToe/TicTacToe/Game.h +++ b/TicTacToe/TicTacToe/Game.h @@ -16,6 +16,18 @@ typedef NS_ENUM(NSUInteger, PossibleWinner) { noWinNextMove }; +typedef NS_ENUM(NSUInteger, Difficulty) { + Easy = 1, + Medium = 2, + Hard =3 +}; + +typedef NS_ENUM(NSUInteger, Mode) { + PlayerVsPlayer = 1, + PlayerVsComputer = 2, + ComputerVsComputer =3 +}; + @interface Game : NSObject -(BOOL)checkWin; diff --git a/TicTacToe/TicTacToe/Game.m b/TicTacToe/TicTacToe/Game.m index fc6f43a..868fda8 100644 --- a/TicTacToe/TicTacToe/Game.m +++ b/TicTacToe/TicTacToe/Game.m @@ -12,6 +12,7 @@ #import "NSString+isNumeric.h" #import "TicTacToeRowGenerator.h" +NSString * const PossibleWinnerString = @"PossibleWinnerString"; @implementation Game { int _mode; @@ -93,39 +94,6 @@ -(void)setupGame { } } --(NSString *)makeMove{ - NSLog(@"Player %d make your move:",_playerTurn); - scanf("%d",&_currentMove); - fpurge(stdin); - - NSString *stringOfCurrentMove = [NSString stringWithFormat:@"%02d",_currentMove]; - - return stringOfCurrentMove; -} - --(NSString *)computerMakeMove{ - - //Easy Mode computer move - if (_difficulty == 1) { - _currentMove = arc4random_uniform(_maxIndex + 1); - NSString *stringOfCurrentMove = [NSString stringWithFormat:@"%02d",_currentMove]; - return stringOfCurrentMove; - } - - //Medium Mode computer move - else if (_difficulty == 2){ - _currentMove = [self winBlocker]; - NSString *stringOfCurrentMove = [NSString stringWithFormat:@"%02d",_currentMove]; - - if ([stringOfCurrentMove isEqualToString:@"9999"]) { //if win blocker found no blocks to make - _currentMove = arc4random_uniform(_maxIndex + 1); - stringOfCurrentMove = [NSString stringWithFormat:@"%02d",_currentMove]; - } - return stringOfCurrentMove; - } - return @"HEHEE"; -} - -(NSString *)getPlayerPiece{ NSString *playerPiece; if (_playerTurn == 2) { @@ -182,122 +150,122 @@ -(void)turn{ } } --(BOOL)checkWin{ +-(NSString *)makeMove{ + NSLog(@"Player %d make your move:",_playerTurn); + scanf("%d",&_currentMove); + fpurge(stdin); - NSMutableArray *line = [[NSMutableArray alloc] init]; //array of the line we are checking for a win + NSString *stringOfCurrentMove = [NSString stringWithFormat:@"%02d",_currentMove]; - //HORIZONAL WIN TEST - for (int i = 0; i < pow(_gameSize, 2); i++) { - - [line addObject:[_gameboard objectAtIndex:i]]; - - if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize - _win = [line areAllObjectsAreIdentical]; - - if (_win == YES) { - return YES; - } - else{ - [line removeAllObjects]; //empty the line array - } - } - } + return stringOfCurrentMove; +} + +-(NSString *)computerMakeMove{ - int currentIndex; - int lastIndex; + //Easy Mode computer move + if (_difficulty == Easy) { + _currentMove = arc4random_uniform(_maxIndex + 1); + NSString *stringOfCurrentMove = [NSString stringWithFormat:@"%02d",_currentMove]; + return stringOfCurrentMove; + } - //VERTICAL WIN TEST - for (int i = 0; i < _gameSize; i++) { - currentIndex = i; - lastIndex = 0; + //Medium Mode computer move + else if (_difficulty == Medium){ - for (int i = 0; i < _gameSize; i++) { - [line addObject:[_gameboard objectAtIndex:currentIndex]]; - lastIndex = currentIndex; - currentIndex = lastIndex + _gameSize; - } + NSString *stringOfCurrentMove = [NSString stringWithString:[self makeWinMove]]; - if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize - _win = [line areAllObjectsAreIdentical]; + if ([stringOfCurrentMove isEqualToString:@"none"]) { //if no win move to make - if (_win == YES) { - return YES; - } - else{ - [line removeAllObjects]; //empty the line array + stringOfCurrentMove = [self makeBlockMove]; + + if ([stringOfCurrentMove isEqualToString:@"none"]) { //if no block move to make + + _currentMove = arc4random_uniform(_maxIndex + 1); + stringOfCurrentMove = [NSString stringWithFormat:@"%02d",_currentMove]; //random move } } + + _currentMove = [stringOfCurrentMove intValue]; + return stringOfCurrentMove; } - //DIAGONAL WIN TEST + return @"HEHEE"; +} + +-(NSString *)makeBlockMove{ - // |X| | | - // | |X| | - // | | |X| - currentIndex = 0; - lastIndex = 0; - for (int i = 0; i < _gameSize; i++) { - [line addObject:[_gameboard objectAtIndex:currentIndex]]; - lastIndex = currentIndex; - currentIndex = lastIndex + _gameSize + 1; - } + NSDictionary *possibleWinnerAndWinningMove = [self whoCanWinOnNextMove]; - if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize - _win = [line areAllObjectsAreIdentical]; - - if (_win == YES) { - return YES; - } - else{ - [line removeAllObjects]; //empty the line array - } + if ([[possibleWinnerAndWinningMove objectForKey:@"PossibleWinner"] isEqualToString:@"PlayerX"] ) { + return [possibleWinnerAndWinningMove objectForKey:@"WinningMove"]; } + return @"none"; +} + +-(NSString *)makeWinMove{ - // | | |X| - // | |X| | - // |X| | | - currentIndex = _gameSize - 1; - lastIndex = _gameSize - 1; - for (int i = 0; i < _gameSize; i++) { - [line addObject:[_gameboard objectAtIndex:currentIndex]]; - lastIndex = currentIndex; - currentIndex = lastIndex + _gameSize - 1; + NSDictionary *possibleWinnerAndWinningMove = [self whoCanWinOnNextMove]; + + if ([[possibleWinnerAndWinningMove objectForKey:@"PossibleWinner"] isEqualToString:@"PlayerO"] ) { + return [possibleWinnerAndWinningMove objectForKey:@"WinningMove"]; } + return @"none"; +} + +-(NSDictionary *) whoCanWinOnNextMove { + + NSArray *allLines = [self makeAllLines]; - if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize - _win = [line areAllObjectsAreIdentical]; + for (int i = 0; i< allLines.count; i++) { - if (_win == YES) { - return YES; - } - else{ - [line removeAllObjects]; //empty the line array + for (int j = 0; j < [[allLines objectAtIndex:i] count]; j++){ + + NSDictionary *countsForElementsInLine = [self lineInspect:[[allLines objectAtIndex:i] objectAtIndex:j]]; + + if (([[countsForElementsInLine valueForKey:@"xPieceCount"] intValue] == _gameSize - 1) && ([[countsForElementsInLine valueForKey: @"IndicesOfSpaces"] count] == 1)) { + NSDictionary *possibleWinnerAndWinningMove = [NSDictionary dictionaryWithObjectsAndKeys:[[countsForElementsInLine valueForKey: @"IndicesOfSpaces"]objectAtIndex:0] , @"WinningMove", @"PlayerX", @"PossibleWinner", nil]; + return possibleWinnerAndWinningMove; //returns PlayerX and WinningMove + } + else if (([[countsForElementsInLine valueForKey:@"oPieceCount"] intValue] == _gameSize - 1) && ([[countsForElementsInLine valueForKey: @"IndicesOfSpaces"] count] == 1)) { + NSDictionary *possibleWinnerAndWinningMove = [NSDictionary dictionaryWithObjectsAndKeys:[[countsForElementsInLine valueForKey: @"IndicesOfSpaces"] objectAtIndex:0] , @"WinningMove", @"PlayerO", @"PossibleWinner", nil]; //returns PlayerO and WinningMove + return possibleWinnerAndWinningMove; + + } } } - return NO; + + NSDictionary *noWinner = @{ + @"WinningMove" : @"none", + @"PossibleWinner":@"none" + }; + + return noWinner; } --(BOOL)checkDraw{ - if ((_win == NO) && (_turnCount == (_maxIndex + 2))){ - return TRUE; - } - return FALSE; +-(NSArray *)makeAllLines{ + NSArray *horizontals = [TicTacToeRowGenerator allLinesForDirection:Horizonal withGame:_gameboard]; + NSArray *verticals = [TicTacToeRowGenerator allLinesForDirection:Vertical withGame:_gameboard]; + NSArray *diagonals = [TicTacToeRowGenerator allLinesForDirection:Diagonal withGame:_gameboard]; + NSArray *allLines = [NSArray arrayWithObjects:horizontals,verticals,diagonals, nil]; + + return allLines; } -(NSDictionary *)lineInspect:(NSMutableArray*)line{ - NSNumber *xPieceCount = 0; - NSNumber *oPieceCount = 0; - NSNumber *spaceCount = 0; + NSNumber *xPieceCount = [NSNumber numberWithInt:0]; + NSNumber *oPieceCount = [NSNumber numberWithInt:0]; + NSNumber *spaceCount = [NSNumber numberWithInt:0]; NSMutableArray *IndicesOfSpaces = [[NSMutableArray alloc] init]; for (int j = 0; j < [line count]; j++) { if ([[line objectAtIndex:j] isEqual: @"~X"]) { - xPieceCount = [NSNumber numberWithInt: [xPieceCount intValue] + 1 ]; + xPieceCount = [NSNumber numberWithInt: [xPieceCount intValue] + 1 ]; } else if ([[line objectAtIndex:j] isEqual: @"~O"]) { oPieceCount = [NSNumber numberWithInt: [oPieceCount intValue] + 1 ]; } else { + [IndicesOfSpaces addObject:[line objectAtIndex:j]]; spaceCount = [NSNumber numberWithInt: [spaceCount intValue] + 1 ]; } } @@ -309,139 +277,35 @@ -(NSDictionary *)lineInspect:(NSMutableArray*)line{ return countsForElementsInLine; } --(PossibleWinner) whoCanWinOnNextMove { +-(BOOL)checkWin{ - NSArray *horizontals = [TicTacToeRowGenerator allLinesForDirection:Horizonal withGame:_gameboard]; - NSArray *verticals = [TicTacToeRowGenerator allLinesForDirection:Vertical withGame:_gameboard]; - NSArray *diagonals = [TicTacToeRowGenerator allLinesForDirection:Diagonal withGame:_gameboard]; + NSArray *allLines = [self makeAllLines]; - for (int i = 0; i< horizontals.count; i++) { - - NSDictionary *countsForElementsInLine = [self lineInspect:[horizontals objectAtIndex:i]]; + for (int i = 0; i< allLines.count; i++) { - if (([[countsForElementsInLine valueForKey:@"xPieceCount"] intValue] == _gameSize - 1) && ([[countsForElementsInLine valueForKey: @"IndicesOfSpaces"] length] == 1)) { - <#statements#> + for (int j = 0; j < [[allLines objectAtIndex:i] count]; j++){ + + NSDictionary *countsForElementsInLine = [self lineInspect:[[allLines objectAtIndex:i] objectAtIndex:j]]; + + if ([[countsForElementsInLine valueForKey:@"xPieceCount"] intValue] == _gameSize) { + _win = TRUE; + return TRUE; + } + else if ([[countsForElementsInLine valueForKey:@"oPieceCount"] intValue] == _gameSize) { + _win = TRUE; + return TRUE; + } } } - + return FALSE; +} + +-(BOOL)checkDraw{ + if ((_win == NO) && (_turnCount == (_maxIndex + 2))){ + return TRUE; + } + return FALSE; } -//-(int)winBlocker{ //returns index of gameBoard to block -// NSMutableArray *line = [[NSMutableArray alloc] init]; //array of the line we are checking for a win -// NSMutableString *stringOfLineObject = [[NSMutableString alloc] init]; -// int xPieceCount = 0; -// int spaceCount = 0; -// int indexOfSpace = 0; -// -// NSMutableArray *lines = [[NSMutableArray alloc] init]; -// -// //HORIZONAL BLOCK TEST -// for (int i = 0; i < pow(_gameSize, 2); i++) { -// -// stringOfLineObject = [_gameboard objectAtIndex:i]; -// [line addObject:stringOfLineObject]; -// -// if ([stringOfLineObject isNumeric]) { -// indexOfSpace = i; -// } -// -// if ([line count] == _gameSize) { //line array reaches the gameSize -// [lines addObject:line]; -// for (int j = 0; j < [line count]; j++) { -// if ([[line objectAtIndex:j] isEqual: @"~X"]) { -// xPieceCount++; -// } -// else if ([[line objectAtIndex:j] isEqual: @"~O"]) { -// } -// else { -// spaceCount++; -// } -// } -// -// if ((xPieceCount == _gameSize - 1) && (spaceCount == 1)) { -// return indexOfSpace; -// } -// -// [line removeAllObjects]; //empty the line array -// xPieceCount = 0; spaceCount = 0; indexOfSpace = 0; -// } -// } -// -// return indexOfSpace; -// -// int currentIndex; -// int lastIndex; -// -// //VERTICAL WIN TEST -// for (int i = 0; i < _gameSize; i++) { -// currentIndex = i; -// lastIndex = 0; -// -// for (int i = 0; i < _gameSize; i++) { -// [line addObject:[_gameboard objectAtIndex:currentIndex]]; -// lastIndex = currentIndex; -// currentIndex = lastIndex + _gameSize; -// } -// -// if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize -// _win = [line areAllObjectsAreIdentical]; -// -// if (_win == YES) { -// return YES; -// } -// else{ -// [line removeAllObjects]; //empty the line array -// } -// } -// } -// -// //DIAGONAL WIN TEST -// -// // |X| | | -// // | |X| | -// // | | |X| -// currentIndex = 0; -// lastIndex = 0; -// for (int i = 0; i < _gameSize; i++) { -// [line addObject:[_gameboard objectAtIndex:currentIndex]]; -// lastIndex = currentIndex; -// currentIndex = lastIndex + _gameSize + 1; -// } -// -// if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize -// _win = [line areAllObjectsAreIdentical]; -// -// if (_win == YES) { -// return YES; -// } -// else{ -// [line removeAllObjects]; //empty the line array -// } -// } -// -// // | | |X| -// // | |X| | -// // |X| | | -// currentIndex = _gameSize - 1; -// lastIndex = _gameSize - 1; -// for (int i = 0; i < _gameSize; i++) { -// [line addObject:[_gameboard objectAtIndex:currentIndex]]; -// lastIndex = currentIndex; -// currentIndex = lastIndex + _gameSize - 1; -// } -// -// if ([line count] == _gameSize) { //check if all objects in line are identical when line array reaches the gameSize -// _win = [line areAllObjectsAreIdentical]; -// -// if (_win == YES) { -// return YES; -// } -// else{ -// [line removeAllObjects]; //empty the line array -// } -// } -// return 9999; -//} -// @end diff --git a/TicTacToe/TicTacToe/TicTacToeRowGenerator.m b/TicTacToe/TicTacToe/TicTacToeRowGenerator.m index 6c66a29..a28dbc0 100644 --- a/TicTacToe/TicTacToe/TicTacToeRowGenerator.m +++ b/TicTacToe/TicTacToe/TicTacToeRowGenerator.m @@ -50,8 +50,8 @@ + (NSMutableArray *)horizontalLinesForGame:(NSArray *)gameBoard { } + (NSMutableArray *)verticalLinesForGame:(NSArray *)gameBoard { + NSInteger sizeOfGame = sqrt(gameBoard.count); - NSMutableArray *lines = [[NSMutableArray alloc] init]; NSInteger currentIndex; @@ -60,6 +60,7 @@ + (NSMutableArray *)verticalLinesForGame:(NSArray *)gameBoard { for (int i = 0; i < sizeOfGame; i++) { currentIndex = i; lastIndex = 0; + NSMutableArray *line = [[NSMutableArray alloc] init]; for (int i = 0; i < sizeOfGame; i++) { [line addObject:[gameBoard objectAtIndex:currentIndex]]; @@ -81,25 +82,26 @@ + (NSMutableArray *)diagonalLinesForGame:(NSArray *)gameBoard { NSInteger currentIndex = 0; NSInteger lastIndex = 0; - NSMutableArray *line = [[NSMutableArray alloc] init]; + NSMutableArray *lineLR = [[NSMutableArray alloc] init]; for (int i = 0; i < sizeOfGame; i++) { - [line addObject:[gameBoard objectAtIndex:currentIndex]]; + [lineLR addObject:[gameBoard objectAtIndex:currentIndex]]; lastIndex = currentIndex; currentIndex = lastIndex + sizeOfGame + 1; } - [lines addObject:line]; + [lines addObject:lineLR]; currentIndex = sizeOfGame - 1; lastIndex = sizeOfGame - 1; + NSMutableArray *lineRL = [[NSMutableArray alloc] init]; for (int i = 0; i < sizeOfGame; i++) { - [line addObject:[gameBoard objectAtIndex:currentIndex]]; + [lineRL addObject:[gameBoard objectAtIndex:currentIndex]]; lastIndex = currentIndex; currentIndex = lastIndex + sizeOfGame - 1; } - [lines addObject:line]; + [lines addObject:lineRL]; return lines; } diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 9e1c45f..b8212d0 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -28,16 +28,16 @@ int main(int argc, const char * argv[]) { scanf("%d",&userGameMode); fpurge(stdin); - if (userGameMode == 1) { - [ticTacToe setMode:1]; + if (userGameMode == PlayerVsPlayer) { + [ticTacToe setMode:PlayerVsPlayer]; modeSet = YES; } - else if (userGameMode == 2){ - [ticTacToe setMode:2]; + else if (userGameMode == PlayerVsComputer){ + [ticTacToe setMode:PlayerVsComputer]; modeSet = YES; } - else if (userGameMode == 3){ - [ticTacToe setMode:3]; + else if (userGameMode == ComputerVsComputer){ + [ticTacToe setMode:ComputerVsComputer]; modeSet = YES; } else { @@ -46,7 +46,7 @@ int main(int argc, const char * argv[]) { } //set difficulty - if ([ticTacToe getMode] == 2) { + if ([ticTacToe getMode] == PlayerVsComputer) { BOOL difficultySet = NO; while (difficultySet == NO && [ticTacToe getMode] != 1) { NSLog(@"Choose a difficulty."); @@ -57,22 +57,22 @@ int main(int argc, const char * argv[]) { scanf("%d",&userDifficulty); fpurge(stdin); - if (userDifficulty == 1) { - [ticTacToe setDifficulty:1]; + if (userDifficulty == Easy) { + [ticTacToe setDifficulty:Easy]; difficultySet = YES; } - else if (userDifficulty == 2){ - [ticTacToe setDifficulty:2]; + else if (userDifficulty == Medium){ + [ticTacToe setDifficulty:Medium]; difficultySet = YES; } - else if (userDifficulty == 3){ - [ticTacToe setDifficulty:3]; + else if (userDifficulty == Hard){ + [ticTacToe setDifficulty:Hard]; difficultySet = YES; } } } //set game size - if ([ticTacToe getMode] == 1) { + if ([ticTacToe getMode] == PlayerVsPlayer) { BOOL gameSizeSet = NO; while (gameSizeSet == NO) { NSLog(@"Set a game size from 3-10, fool!:"); @@ -98,7 +98,7 @@ int main(int argc, const char * argv[]) { [MyTicTacToeGame printTutorialBoard:ticTacToe]; while (![ticTacToe checkWin] && ![ticTacToe checkDraw]) { -// NSLog(@"%@", [TicTacToeRowGenerator allLinesForDirection:Horizonal withGame:[ticTacToe getGameBoard]]); +// NSLog(@"%@", [TicTacToeRowGenerator allLinesForDirection:Diagonal withGame:[ticTacToe getGameBoard]]); [ticTacToe turn]; [MyTicTacToeGame printBoard:ticTacToe]; } From a483f2109482f8170a9b03f7a72a920cee40e87a Mon Sep 17 00:00:00 2001 From: Derek Date: Sun, 19 Jul 2015 17:47:30 -0400 Subject: [PATCH 24/24] difficulties --- TicTacToe/TicTacToe/Game.m | 49 +++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/TicTacToe/TicTacToe/Game.m b/TicTacToe/TicTacToe/Game.m index 868fda8..30e8d75 100644 --- a/TicTacToe/TicTacToe/Game.m +++ b/TicTacToe/TicTacToe/Game.m @@ -162,36 +162,63 @@ -(NSString *)makeMove{ -(NSString *)computerMakeMove{ - //Easy Mode computer move + NSString *stringOfCurrentMove = [[NSString alloc] init]; + if (_difficulty == Easy) { _currentMove = arc4random_uniform(_maxIndex + 1); - NSString *stringOfCurrentMove = [NSString stringWithFormat:@"%02d",_currentMove]; + stringOfCurrentMove = [NSString stringWithFormat:@"%02d",_currentMove]; return stringOfCurrentMove; } - //Medium Mode computer move else if (_difficulty == Medium){ - NSString *stringOfCurrentMove = [NSString stringWithString:[self makeWinMove]]; + stringOfCurrentMove = [NSString stringWithString:[self makeWinMove]]; if ([stringOfCurrentMove isEqualToString:@"none"]) { //if no win move to make stringOfCurrentMove = [self makeBlockMove]; + } + + if ([stringOfCurrentMove isEqualToString:@"none"]) { //if no block move to make - if ([stringOfCurrentMove isEqualToString:@"none"]) { //if no block move to make - - _currentMove = arc4random_uniform(_maxIndex + 1); - stringOfCurrentMove = [NSString stringWithFormat:@"%02d",_currentMove]; //random move - } + _currentMove = arc4random_uniform(_maxIndex + 1); + stringOfCurrentMove = [NSString stringWithFormat:@"%02d",_currentMove]; //random move } _currentMove = [stringOfCurrentMove intValue]; return stringOfCurrentMove; } - return @"HEHEE"; + else if (_difficulty == Hard){ + + } + + +return @"HEHEE"; } +//-(NSString *)makePowerMove{ //checks board and outputs available move of highest power +// NSDictionary * const movePowers = @{ +// @"00" : @300, +// @"01" : @200, +// @"02" : @300, +// @"03" : @200, +// @"04" : @400, +// @"05" : @200, +// @"06" : @300, +// @"07" : @200, +// @"08" : @300, +// }; +// +// NSString *powerMove = [[NSString alloc] init]; +// for (int i = 0; [_gameboard count]; i++) { +// <#statements#> +// } +// +//} + + + -(NSString *)makeBlockMove{ NSDictionary *possibleWinnerAndWinningMove = [self whoCanWinOnNextMove]; @@ -301,7 +328,7 @@ -(BOOL)checkWin{ } -(BOOL)checkDraw{ - if ((_win == NO) && (_turnCount == (_maxIndex + 2))){ + if ((_win == NO) && (_turnCount == (_maxIndex + 1))){ return TRUE; } return FALSE;