From 056067a380ed3663c045ae434545d619c4b6d491 Mon Sep 17 00:00:00 2001 From: Henna Date: Thu, 25 Jun 2015 21:55:30 -0400 Subject: [PATCH 01/13] initial commit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 082e4ed..1765613 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # Unit 0 Final Project +Henna and Kaisha All directions are here... https://github.com/accesscode-2-2/unit-0/tree/master/project From 51173e26d8c6b67ddd7d4ed15371aa98859ee3be Mon Sep 17 00:00:00 2001 From: Henna Date: Sat, 27 Jun 2015 12:18:55 -0400 Subject: [PATCH 02/13] initialized board and printed it --- TicTacToe/TicTacToe/main.m | 49 ++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 3a713ee..1b3e399 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -2,16 +2,57 @@ // main.m // TicTacToe // -// Created by Michael Kavouras on 6/25/15. -// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// Created by Henna and Kaisha on 6/27/15. // #import +@interface ticTacToeManager : NSObject + +-(void) initializeArray: (int) dimension; +-(void) printBoard; + +@end + +@implementation ticTacToeManager{ + NSMutableArray *_board; +} + +-(void) initializeArray: (int) dimension{ + + _board = [[NSMutableArray alloc] init]; + + for (int i = 0; i Date: Sat, 27 Jun 2015 14:11:37 -0400 Subject: [PATCH 03/13] vPosition/hPosition if statements --- TicTacToe/TicTacToe/main.m | 58 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 1b3e399..d3c2b75 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -52,6 +52,64 @@ int main(int argc, const char * argv[]) { [game initializeArray:3]; [game printBoard]; + + char userXOrO; + char otherUserXOrO; + + while (true) { + + NSLog(@"Choose X or O: "); + fpurge(stdin); + scanf("%c", &userXOrO); + + if ( (userXOrO == 'x') || (userXOrO == 'X') || (userXOrO == 'o') || (userXOrO == 'O') ) { + + if (userXOrO == 'x' || userXOrO =='X') { + userXOrO = 'X'; + otherUserXOrO = 'O'; + } + else { + userXOrO = 'O'; + otherUserXOrO = 'X'; + } + + break; + } + } + + while (true) { + + int hPosition = -1; + int vPosition = -1; +// NSInteger *hPosition; +// NSInteger *vPosition; + NSLog(@"Choose your horizontal position (1-3): "); + fpurge(stdin); + scanf("%d", &hPosition); + + if (hPosition < 1 || hPosition > 3) { + NSLog(@"Please enter a valid position between 1 and 3"); + continue; + } + + NSLog(@"Choose your vertical position (1-3): "); + fpurge(stdin); + scanf("%d", &vPosition); + + if (vPosition < 1 || vPosition > 3) { + NSLog(@"Please enter a valid position betwwwn 1 and 3"); + } + + NSLog(@"%d %d", hPosition, vPosition); + + // Pass in this position into our class and then check if it's empty and if so place this person's x or o into the board + + break; + + + + } + } return 0; From de00124caad7d1be0ce7e798245b9eab0c5e0142 Mon Sep 17 00:00:00 2001 From: Henna Date: Sat, 27 Jun 2015 15:22:59 -0400 Subject: [PATCH 04/13] implemented computer choice and printing board --- TicTacToe/TicTacToe/main.m | 116 +++++++++++++++++++++++++++---------- 1 file changed, 86 insertions(+), 30 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index d3c2b75..3b1dd33 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -11,6 +11,7 @@ @interface ticTacToeManager : NSObject -(void) initializeArray: (int) dimension; -(void) printBoard; +-(BOOL) isPositionValidX: (int) x AndY: (int) y and: (char) userPosition; @end @@ -23,11 +24,9 @@ -(void) initializeArray: (int) dimension{ _board = [[NSMutableArray alloc] init]; for (int i = 0; i 3) { - NSLog(@"Please enter a valid position between 1 and 3"); - continue; + hPosition = arc4random_uniform(2) + 1; + vPosition = arc4random_uniform(2) + 1; + + //pick a hposition and vposition for computer } - NSLog(@"Choose your vertical position (1-3): "); - fpurge(stdin); - scanf("%d", &vPosition); - - if (vPosition < 1 || vPosition > 3) { - NSLog(@"Please enter a valid position betwwwn 1 and 3"); + else{ + + NSLog(@"Choose your horizontal position (1-3): "); + fpurge(stdin); + scanf("%d", &hPosition); + + if (hPosition < 1 || hPosition > 3) { + NSLog(@"Please enter a valid position between 1 and 3"); + continue; + } + + NSLog(@"Choose your vertical position (1-3): "); + fpurge(stdin); + scanf("%d", &vPosition); + + if (vPosition < 1 || vPosition > 3) { + NSLog(@"Please enter a valid position betwwwn 1 and 3"); + } } - NSLog(@"%d %d", hPosition, vPosition); // Pass in this position into our class and then check if it's empty and if so place this person's x or o into the board - break; + if ([game isPositionValidX:hPosition AndY:vPosition and:xOrO]){ + if (computerTurn == YES) { + computerTurn = NO; + } + else{ + computerTurn = YES; + } + if (xOrO == 'X') { //After each iteration we want to change the postion to X or back to O + xOrO = 'O'; + } + else{ + xOrO = 'X'; + } + + } + [game printBoard]; + + + + + + + //break; } - + } return 0; } From 0c05e6df35ed7eb8f0e365050892e482015c877b Mon Sep 17 00:00:00 2001 From: Kaisha Jones Date: Sat, 27 Jun 2015 15:56:50 -0400 Subject: [PATCH 05/13] 6 of 8 win cases --- TicTacToe/TicTacToe/main.m | 50 ++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 3b1dd33..83bd7b2 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -12,6 +12,7 @@ @interface ticTacToeManager : NSObject -(void) initializeArray: (int) dimension; -(void) printBoard; -(BOOL) isPositionValidX: (int) x AndY: (int) y and: (char) userPosition; +-(BOOL) isWinner; @end @@ -59,13 +60,49 @@ -(BOOL) isPositionValidX: (int) x AndY: (int) y and: (char) userPosition { return NO; } +} + +-(BOOL) isWinner { + NSString *winner; + + //horizontal cases + + for (int i =0; i<3; i++) { + if( [[[_board objectAtIndex:i] objectAtIndex:0] isEqualToString: [[_board objectAtIndex:i] objectAtIndex:1]] && + [[[_board objectAtIndex:i] objectAtIndex:1] isEqualToString: [[_board objectAtIndex:i] objectAtIndex:2]] && + ![[[_board objectAtIndex:i] objectAtIndex:0] isEqualToString: @" "] + ){ + + winner = [[_board objectAtIndex:i] objectAtIndex:0]; + NSLog(@"%@ won", winner); + return YES; + + } + + } + + //vertical cases + + for ( int i = 0; i < 3; i++) { + if( [[[_board objectAtIndex:0] objectAtIndex:i] isEqualToString: [[_board objectAtIndex:1] objectAtIndex:i]] && + [[[_board objectAtIndex:1] objectAtIndex:i] isEqualToString: [[_board objectAtIndex:2] objectAtIndex:i]] && + ![[[_board objectAtIndex:0] objectAtIndex:i] isEqualToString: @" "] + ){ + winner = [[_board objectAtIndex:0] objectAtIndex:i]; + NSLog(@"%@ won", winner); + return YES; + + + } + } + //diagnol cases + return NO; } - @end @@ -153,14 +190,13 @@ int main(int argc, const char * argv[]) { } } - [game printBoard]; - - - - + [game printBoard]; + BOOL won = [game isWinner]; - //break; + if (won) { + break; + } From 2901cd00665bf9a8f0672a124f597bca85706c84 Mon Sep 17 00:00:00 2001 From: Kaisha Jones Date: Sat, 27 Jun 2015 16:10:28 -0400 Subject: [PATCH 06/13] diagnol win cases --- TicTacToe/TicTacToe/main.m | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 83bd7b2..adcc8a3 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -96,8 +96,26 @@ -(BOOL) isWinner { } } - //diagnol cases - + //diagnol case + if ([[[_board objectAtIndex:0] objectAtIndex:0] isEqualToString: [[_board objectAtIndex:1] objectAtIndex:1]] && + [[[_board objectAtIndex:1] objectAtIndex:1] isEqualToString: [[_board objectAtIndex:2] objectAtIndex:2]] && + ![[[_board objectAtIndex:0] objectAtIndex:0] isEqualToString: @" "] + ){ + winner = [[_board objectAtIndex:0] objectAtIndex:0]; + NSLog(@"%@ won", winner); + return YES; + + } + //backwards diagnol case + if ([[[_board objectAtIndex:0] objectAtIndex:2] isEqualToString: [[_board objectAtIndex:1] objectAtIndex:1]] && + [[[_board objectAtIndex:1] objectAtIndex:1] isEqualToString: [[_board objectAtIndex:2] objectAtIndex:0]] && + ![[[_board objectAtIndex:0] objectAtIndex:2] isEqualToString: @" "] + ){ + winner = [[_board objectAtIndex:0] objectAtIndex:2]; + NSLog(@"%@ won", winner); + return YES; + + } return NO; } @@ -168,7 +186,8 @@ int main(int argc, const char * argv[]) { scanf("%d", &vPosition); if (vPosition < 1 || vPosition > 3) { - NSLog(@"Please enter a valid position betwwwn 1 and 3"); + NSLog(@"Please enter a valid position between 1 and 3"); + continue; } } From a668d2f1be052b5957eb00ba57ed6e1de2faf756 Mon Sep 17 00:00:00 2001 From: Kaisha Jones Date: Sat, 27 Jun 2015 16:49:44 -0400 Subject: [PATCH 07/13] fixed computer random number choice --- TicTacToe/TicTacToe/main.m | 41 ++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index adcc8a3..c2e685b 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -11,8 +11,9 @@ @interface ticTacToeManager : NSObject -(void) initializeArray: (int) dimension; -(void) printBoard; --(BOOL) isPositionValidX: (int) x AndY: (int) y and: (char) userPosition; +-(BOOL) isPositionValidX: (int) x AndY: (int) y and: (char) userPosition userType: (BOOL) isComputer; -(BOOL) isWinner; +-(BOOL) isFull; @end @@ -39,7 +40,7 @@ -(void)printBoard { NSLog(@"\n\t\t|%@|%@|%@| \n\t\t|%@|%@|%@| \n\t\t|%@|%@|%@|", [row1 objectAtIndex:0], [row1 objectAtIndex:1], [row1 objectAtIndex:2], [row2 objectAtIndex:0], [row2 objectAtIndex:1], [row2 objectAtIndex:2], [row3 objectAtIndex:0], [row3 objectAtIndex:1], [row3 objectAtIndex:2]); } --(BOOL) isPositionValidX: (int) x AndY: (int) y and: (char) userPosition { +-(BOOL) isPositionValidX: (int) x AndY: (int) y and: (char) userPosition userType: (BOOL) isComputer{ x = x-1; // this is the position on the row-array we want to check y = y-1; // this is the position in our board array we want the row-array from @@ -55,6 +56,9 @@ -(BOOL) isPositionValidX: (int) x AndY: (int) y and: (char) userPosition { } return YES; } + else if(isComputer) { + return NO; + } else { NSLog(@"This is a not valid position"); return NO; @@ -120,6 +124,21 @@ -(BOOL) isWinner { return NO; } +-(BOOL) isFull { + for (int i = 0; i < 3; i ++) { + if ( [ [ [_board objectAtIndex:i] objectAtIndex:0] isEqualToString: @" "] || + [ [ [_board objectAtIndex:i] objectAtIndex:1] isEqualToString: @" "] || + [ [ [_board objectAtIndex:i] objectAtIndex:2] isEqualToString: @" "] + ){ + + return NO; + } + + } + NSLog(@"No more plays, game over :("); + return YES; + +} @end @@ -154,6 +173,9 @@ int main(int argc, const char * argv[]) { } } + + [game printBoard]; + BOOL computerTurn = NO; while (true) { @@ -164,8 +186,10 @@ int main(int argc, const char * argv[]) { if (computerTurn) { - hPosition = arc4random_uniform(2) + 1; - vPosition = arc4random_uniform(2) + 1; + hPosition = 1 + arc4random_uniform(3); + vPosition = 1 + arc4random_uniform(3); + + NSLog(@"%d %d", hPosition, vPosition ); //pick a hposition and vposition for computer } @@ -194,7 +218,7 @@ int main(int argc, const char * argv[]) { // Pass in this position into our class and then check if it's empty and if so place this person's x or o into the board - if ([game isPositionValidX:hPosition AndY:vPosition and:xOrO]){ + if ([game isPositionValidX:hPosition AndY:vPosition and:xOrO userType:computerTurn ]){ if (computerTurn == YES) { computerTurn = NO; } @@ -208,12 +232,13 @@ int main(int argc, const char * argv[]) { xOrO = 'X'; } + [game printBoard]; } - - [game printBoard]; + BOOL won = [game isWinner]; + BOOL full = [game isFull]; - if (won) { + if (won || full) { break; } From ba579e0199fc49e6c4987a9863c1d23357738316 Mon Sep 17 00:00:00 2001 From: Henna Date: Sat, 27 Jun 2015 17:27:24 -0400 Subject: [PATCH 08/13] two-player implementation --- TicTacToe/TicTacToe/main.m | 77 +++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 27 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index c2e685b..2df2770 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -73,8 +73,8 @@ -(BOOL) isWinner { for (int i =0; i<3; i++) { if( [[[_board objectAtIndex:i] objectAtIndex:0] isEqualToString: [[_board objectAtIndex:i] objectAtIndex:1]] && - [[[_board objectAtIndex:i] objectAtIndex:1] isEqualToString: [[_board objectAtIndex:i] objectAtIndex:2]] && - ![[[_board objectAtIndex:i] objectAtIndex:0] isEqualToString: @" "] + [[[_board objectAtIndex:i] objectAtIndex:1] isEqualToString: [[_board objectAtIndex:i] objectAtIndex:2]] && + ![[[_board objectAtIndex:i] objectAtIndex:0] isEqualToString: @" "] ){ winner = [[_board objectAtIndex:i] objectAtIndex:0]; @@ -96,7 +96,7 @@ -(BOOL) isWinner { NSLog(@"%@ won", winner); return YES; - + } } @@ -109,7 +109,7 @@ -(BOOL) isWinner { NSLog(@"%@ won", winner); return YES; - } + } //backwards diagnol case if ([[[_board objectAtIndex:0] objectAtIndex:2] isEqualToString: [[_board objectAtIndex:1] objectAtIndex:1]] && [[[_board objectAtIndex:1] objectAtIndex:1] isEqualToString: [[_board objectAtIndex:2] objectAtIndex:0]] && @@ -127,17 +127,17 @@ -(BOOL) isWinner { -(BOOL) isFull { for (int i = 0; i < 3; i ++) { if ( [ [ [_board objectAtIndex:i] objectAtIndex:0] isEqualToString: @" "] || - [ [ [_board objectAtIndex:i] objectAtIndex:1] isEqualToString: @" "] || - [ [ [_board objectAtIndex:i] objectAtIndex:2] isEqualToString: @" "] + [ [ [_board objectAtIndex:i] objectAtIndex:1] isEqualToString: @" "] || + [ [ [_board objectAtIndex:i] objectAtIndex:2] isEqualToString: @" "] ){ return NO; } + + } + NSLog(@"No more plays, game over :("); + return YES; - } - NSLog(@"No more plays, game over :("); - return YES; - } @end @@ -153,6 +153,28 @@ int main(int argc, const char * argv[]) { char xOrO; + int onePlayerChoice; + BOOL onePlayer; + BOOL computerTurn = NO; + + while (true) { + + NSLog(@"Type 1 for a one player game and 2 for two-player game: "); + fpurge(stdin); + scanf("%d", &onePlayerChoice); + + if ( onePlayerChoice == 1 ) { + onePlayer = YES; + break; + } + else if (onePlayerChoice == 2){ + onePlayer = NO; + break; + } + else{ + NSLog(@"Please enter a valid number of players"); + } + } while (true) { @@ -174,28 +196,27 @@ int main(int argc, const char * argv[]) { } + + [game printBoard]; - BOOL computerTurn = NO; + while (true) { int hPosition = -1; int vPosition = -1; - - if (computerTurn) { - - hPosition = 1 + arc4random_uniform(3); - vPosition = 1 + arc4random_uniform(3); - - NSLog(@"%d %d", hPosition, vPosition ); - - //pick a hposition and vposition for computer + if (onePlayer) { + if (computerTurn) { + + hPosition = 1 + arc4random_uniform(3); + vPosition = 1 + arc4random_uniform(3); + } } else{ - + NSLog(@"Choose your horizontal position (1-3): "); fpurge(stdin); scanf("%d", &hPosition); @@ -219,11 +240,13 @@ int main(int argc, const char * argv[]) { // Pass in this position into our class and then check if it's empty and if so place this person's x or o into the board if ([game isPositionValidX:hPosition AndY:vPosition and:xOrO userType:computerTurn ]){ - if (computerTurn == YES) { - computerTurn = NO; - } - else{ - computerTurn = YES; + if (onePlayer) { + if (computerTurn == YES) { + computerTurn = NO; + } + else{ + computerTurn = YES; + } } if (xOrO == 'X') { //After each iteration we want to change the postion to X or back to O xOrO = 'O'; @@ -234,7 +257,7 @@ int main(int argc, const char * argv[]) { [game printBoard]; } - + BOOL won = [game isWinner]; BOOL full = [game isFull]; From 60b37e00cb49b73ca22055b8be0eed0e444aba16 Mon Sep 17 00:00:00 2001 From: Henna Date: Sun, 28 Jun 2015 10:37:05 -0400 Subject: [PATCH 09/13] new tasks --- TicTacToe/TicTacToe/main.m | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 2df2770..f838c63 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -5,6 +5,13 @@ // Created by Henna and Kaisha on 6/27/15. // +//Henna: +//include a way to check for no more plays + +//Kaisha: +//keep track of players and score +//take in player's name and let them know they won + #import @interface ticTacToeManager : NSObject From 1f04a940e0f7429709925b39767b3b3a1df83f56 Mon Sep 17 00:00:00 2001 From: Kaisha Jones Date: Sun, 28 Jun 2015 12:13:52 -0400 Subject: [PATCH 10/13] made person class --- TicTacToe/TicTacToe/main.m | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index f838c63..9e5fedd 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -22,8 +22,12 @@ -(BOOL) isPositionValidX: (int) x AndY: (int) y and: (char) userPosition userTyp -(BOOL) isWinner; -(BOOL) isFull; + + @end + + @implementation ticTacToeManager{ NSMutableArray *_board; } @@ -149,6 +153,38 @@ -(BOOL) isFull { @end +@interface Person : NSObject + +-(void)setName:(NSString *)name; + +-(void)won; + + + +@end + + +@implementation Person { // class + + // instance variable + NSString *_name; + int _pointsEarned; +} + // methods + +-(void)setName:(NSString *)name { + + _name = name; + _pointsEarned = 0; + +} + +-(void)won{ + _pointsEarned = _pointsEarned + 1; +} + + +@end int main(int argc, const char * argv[]) { @@ -158,7 +194,6 @@ int main(int argc, const char * argv[]) { [game initializeArray:3]; - char xOrO; int onePlayerChoice; BOOL onePlayer; @@ -278,5 +313,6 @@ int main(int argc, const char * argv[]) { } + return 0; } From e59bb034eeb488c118b14418babbb19cfde291f1 Mon Sep 17 00:00:00 2001 From: Henna Date: Sun, 28 Jun 2015 12:47:54 -0400 Subject: [PATCH 11/13] playing again option added --- TicTacToe/TicTacToe/main.m | 210 ++++++++++++++++++++++--------------- 1 file changed, 124 insertions(+), 86 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index f838c63..07ab59d 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -154,129 +154,167 @@ -(BOOL) isFull { int main(int argc, const char * argv[]) { @autoreleasepool { - ticTacToeManager * game = [[ticTacToeManager alloc] init]; - [game initializeArray:3]; + BOOL playingAgain = YES; - - char xOrO; - int onePlayerChoice; - BOOL onePlayer; - BOOL computerTurn = NO; - - while (true) { + while (playingAgain) { + ticTacToeManager * game = [[ticTacToeManager alloc] init]; - NSLog(@"Type 1 for a one player game and 2 for two-player game: "); - fpurge(stdin); - scanf("%d", &onePlayerChoice); + [game initializeArray:3]; - if ( onePlayerChoice == 1 ) { - onePlayer = YES; - break; - } - else if (onePlayerChoice == 2){ - onePlayer = NO; - break; - } - else{ - NSLog(@"Please enter a valid number of players"); - } - } - - while (true) { - NSLog(@"Choose X or O: "); - fpurge(stdin); - scanf("%c", &xOrO); + char xOrO; //first user chooses the position X or O + int onePlayerChoice; //user choice to play one player game or two player game + BOOL onePlayer; //onePlayer or twoPlayer + BOOL computerTurn = NO; //initialize computerTurn to NO + BOOL won; + BOOL full; + - if ( (xOrO == 'x') || (xOrO == 'X') || (xOrO == 'o') || (xOrO == 'O') ) { + + while (true) { + + NSLog(@"Type 1 for a one player game and 2 for two-player game: "); + fpurge(stdin); + scanf("%d", &onePlayerChoice); - if (xOrO == 'x' || xOrO =='X') { - xOrO = 'X'; + if ( onePlayerChoice == 1 ) { + onePlayer = YES; + break; } - else { - xOrO = 'O'; + else if (onePlayerChoice == 2){ + onePlayer = NO; + break; + } + else{ + NSLog(@"Please enter a valid number of players"); + continue; } + } + + while (true) { - break; + NSLog(@"Choose X or O: "); + fpurge(stdin); + scanf("%c", &xOrO); + + if ( (xOrO == 'x') || (xOrO == 'X') || (xOrO == 'o') || (xOrO == 'O') ) { + + if (xOrO == 'x' || xOrO =='X') { + xOrO = 'X'; + } + else { + xOrO = 'O'; + } + + break; + } + else { + continue; + } } - } - - - - - [game printBoard]; - - - - while (true) { - int hPosition = -1; - int vPosition = -1; - if (onePlayer) { - if (computerTurn) { + [game printBoard]; + + + + while (true) { + + int hPosition = -1; + int vPosition = -1; + + if (onePlayer && computerTurn) { hPosition = 1 + arc4random_uniform(3); vPosition = 1 + arc4random_uniform(3); } - } - - else{ - NSLog(@"Choose your horizontal position (1-3): "); - fpurge(stdin); - scanf("%d", &hPosition); + else{ + + NSLog(@"Choose your horizontal position (1-3): "); + fpurge(stdin); + scanf("%d", &hPosition); + + if (hPosition < 1 || hPosition > 3) { + NSLog(@"Please enter a valid position between 1 and 3"); + continue; + } + + NSLog(@"Choose your vertical position (1-3): "); + fpurge(stdin); + scanf("%d", &vPosition); + + if (vPosition < 1 || vPosition > 3) { + NSLog(@"Please enter a valid position between 1 and 3"); + continue; + } + } - if (hPosition < 1 || hPosition > 3) { - NSLog(@"Please enter a valid position between 1 and 3"); - continue; + + // Pass in this position into our class and then check if it's empty and if so place this person's x or o into the board + + if ([game isPositionValidX:hPosition AndY:vPosition and:xOrO userType:computerTurn ]){ + if (onePlayer) { + if (computerTurn == YES) { + computerTurn = NO; + } + else{ + computerTurn = YES; + } + } + if (xOrO == 'X') { //After each iteration we want to change the postion to X or back to O + xOrO = 'O'; + } + else{ + xOrO = 'X'; + } + + [game printBoard]; } - NSLog(@"Choose your vertical position (1-3): "); - fpurge(stdin); - scanf("%d", &vPosition); + won = [game isWinner]; + full = [game isFull]; - if (vPosition < 1 || vPosition > 3) { - NSLog(@"Please enter a valid position between 1 and 3"); + if (won || full) { + break; + + } + else{ continue; } } - // Pass in this position into our class and then check if it's empty and if so place this person's x or o into the board - if ([game isPositionValidX:hPosition AndY:vPosition and:xOrO userType:computerTurn ]){ - if (onePlayer) { - if (computerTurn == YES) { - computerTurn = NO; - } - else{ - computerTurn = YES; - } + + while (true) { + char playAgain; + NSLog(@"Do you want to play again?(Y for yes and N for no: "); + fpurge(stdin); + scanf("%c", &playAgain); + + if ( (playAgain == 'y') || (playAgain == 'Y') ) { + playingAgain = YES; + break; } - if (xOrO == 'X') { //After each iteration we want to change the postion to X or back to O - xOrO = 'O'; + else if ((playAgain == 'n') || (playAgain == 'N')){ + + playingAgain = NO; + NSLog(@"Thanks for playing!"); + return 0; + } else{ - xOrO = 'X'; + continue; } - [game printBoard]; } - BOOL won = [game isWinner]; - BOOL full = [game isFull]; - if (won || full) { - break; - } - - - - } + } // end of playingAgain while LOOP } - return 0; + //return 0; } From 361cba6623f308d5c645e22087d83425e9c9b423 Mon Sep 17 00:00:00 2001 From: Kaisha Jones Date: Sun, 28 Jun 2015 16:14:06 -0400 Subject: [PATCH 12/13] changed the horizontal and vertical variables --- TicTacToe/TicTacToe/main.m | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 8cd8c40..82e8633 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -12,13 +12,14 @@ //keep track of players and score //take in player's name and let them know they won + #import @interface ticTacToeManager : NSObject -(void) initializeArray: (int) dimension; -(void) printBoard; --(BOOL) isPositionValidX: (int) x AndY: (int) y and: (char) userPosition userType: (BOOL) isComputer; +-(BOOL) isPositionValid: (int) horizontal And: (int) vertical AndIs: (char) userPosition WithUserType: (BOOL) isComputer; -(BOOL) isWinner; -(BOOL) isFull; @@ -33,7 +34,7 @@ @implementation ticTacToeManager{ } -(void) initializeArray: (int) dimension{ - + //NSMutableArray *array = [[NSMutableArray alloc]init]; _board = [[NSMutableArray alloc] init]; for (int i = 0; i Date: Thu, 2 Jul 2015 15:03:14 -0400 Subject: [PATCH 13/13] added an NSLog to indicate who's turn it is --- TicTacToe/TicTacToe/main.m | 1 + 1 file changed, 1 insertion(+) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 82e8633..82f951d 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -271,6 +271,7 @@ int main(int argc, const char * argv[]) { else{ + NSLog(@"%c's turn", xOrO); NSLog(@"Choose your horizontal position (1-3): "); fpurge(stdin); scanf("%d", &hPosition);