From f582347cb0870b91d24577834305d926a92d5d7a Mon Sep 17 00:00:00 2001 From: Fatima Date: Sat, 27 Jun 2015 16:20:48 -0400 Subject: [PATCH 1/7] tic tac toe --- TicTacToe/TicTacToe/main.m | 111 ++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 2 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 3a713ee..a29c82d 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -8,10 +8,117 @@ #import +@interface TicTacToe :NSObject; + +- (void)printIntroudction; + +- (void)printBoard; + +- (int)playerMove:(int)x; + +- (NSMutableArray *)board; + + +@end + + +@implementation TicTacToe { + NSMutableArray * _board; +} + +- (void)printIntroduction{ + + printf( "\n\n\tTic Tac Toe\n\n"); + printf( "Player 1 (X) - Player 2 (O) \n \n"); + printf("\n"); + printf( " | | \n" ); + printf( " [1] | [2] | [3] \n"); + printf( "_____|_____|_____\n") ; + printf( " | | \n") ; + printf( " [4 | [5] | [6] \n"); + printf( "_____|_____|_____\n") ; + printf( " | | \n") ; + printf( " [7] | [8] | [9] \n"); + printf( " | | \n \n") ; + +}; + +- (int)playerMove:(int)x{ + + printf("Please pick a number where you want to put your move: "); + int num; + scanf("%d", &num); + printf("\n"); + + return num; +} + +- (NSMutableArray *)board{ + + _board = [NSMutableArray arrayWithObjects: @"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil]; + + return _board; +} + +- (void)printBoard { + + + printf("\n"); + printf(" | | \n" ); + printf(" [%s] | [%s] | [%s] \n", [_board[0] UTF8String], [_board[1] UTF8String], [_board[2]UTF8String]); + printf("_____|_____|_____\n") ; + printf(" | | \n") ; + printf(" [%s] | [%s] | [%s] \n", [_board[3] UTF8String], [_board[4] UTF8String], [_board[5]UTF8String]); + printf("_____|_____|_____\n") ; + printf(" | | \n") ; + printf(" [%s] | [%s] | [%s] \n", [_board[6] UTF8String], [_board[7] UTF8String],[_board[8] UTF8String]); + printf(" | | \n \n") ; + + +} + + + +@end + + int main(int argc, const char * argv[]) { @autoreleasepool { - // insert code here... - NSLog(@"Hello, World!"); + + + + TicTacToe *tictactoe = [[TicTacToe alloc]init]; + + [tictactoe printIntroduction]; + + [tictactoe board]; + + [tictactoe printBoard]; + + + + + + + for (int i = 0; i < 9; i++){ + NSLog(@"user selection: %d, current value: %d", x, i); + NSLog(@"%@", board); + if (x == i+1){ + board[i] = @"x"; + } + } + + +// make object tic tac toe +// in while loop - +// hey tic tac toe print board +// inside while loop get # from user +// hey tic tac toe make move + + } return 0; } + + + From 73fe5b568f66c5bcea972751806d477cfdaf6eb4 Mon Sep 17 00:00:00 2001 From: Fatima Date: Sun, 28 Jun 2015 10:25:51 -0400 Subject: [PATCH 2/7] adding conditions to check for win --- TicTacToe/TicTacToe/main.m | 111 +++++++++++++++++++++++++------------ 1 file changed, 76 insertions(+), 35 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index a29c82d..42ce3c3 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -14,10 +14,11 @@ - (void)printIntroudction; - (void)printBoard; -- (int)playerMove:(int)x; +- (void)playerMove; - (NSMutableArray *)board; +- (BOOL)gameState:(int)tries; @end @@ -26,6 +27,8 @@ @implementation TicTacToe { NSMutableArray * _board; } + + - (void)printIntroduction{ printf( "\n\n\tTic Tac Toe\n\n"); @@ -41,16 +44,6 @@ - (void)printIntroduction{ printf( " [7] | [8] | [9] \n"); printf( " | | \n \n") ; -}; - -- (int)playerMove:(int)x{ - - printf("Please pick a number where you want to put your move: "); - int num; - scanf("%d", &num); - printf("\n"); - - return num; } - (NSMutableArray *)board{ @@ -60,6 +53,27 @@ - (NSMutableArray *)board{ return _board; } + + +- (void)playerMove{ + + printf("Please pick a number where you want to put your move: "); + int x; + scanf("%d", &x); + printf("\n"); + + for (int i = 0; i < 9; i++){ + NSLog(@"user selection: %d, current value: %d", x, i); + NSLog(@"%@", _board); + if (x == i+1){ + _board[i] = @"x"; + } + } + + +} + + - (void)printBoard { @@ -78,6 +92,55 @@ - (void)printBoard { } +- (BOOL)gameState:(int)tries{ + + [self board]; + int maxTries = 9; + while (tries < maxTries){ + + [self playerMove]; + + [self printBoard]; + + int horizontalCorrect1 = 0; + int horizontalCorrect2 = 0; + int horizontalCorrect3 = 0; + + // vertical check + for (int i = 0; i < [_board count]; i = i + 3) { + + if([ _board[i] isEqualToString:@"x"]){ + horizontalCorrect1++; + } + + if([_board[i+1] isEqualToString:@"x"]){ + horizontalCorrect2++; + } + + if ([_board[i+2] isEqualToString:@"x"]){ + horizontalCorrect3++; + } + + if (horizontalCorrect1 == 3 || horizontalCorrect2 == 3 || horizontalCorrect3 == 3){ + printf("WIN WIN WIN WIN WIN!"); + break; + } + + + } + + // diagonal check + + for (int i = 0; i < [_board count]; i = i + 4) { + + if ([_board[i] isEqualToString: ]) + } + + tries++; + } + return true; +} + @end @@ -86,34 +149,12 @@ int main(int argc, const char * argv[]) { @autoreleasepool { - TicTacToe *tictactoe = [[TicTacToe alloc]init]; - - [tictactoe printIntroduction]; - - [tictactoe board]; - - [tictactoe printBoard]; - - + [tictactoe printIntroduction]; - + [tictactoe gameState:0]; - for (int i = 0; i < 9; i++){ - NSLog(@"user selection: %d, current value: %d", x, i); - NSLog(@"%@", board); - if (x == i+1){ - board[i] = @"x"; - } - } - - -// make object tic tac toe -// in while loop - -// hey tic tac toe print board -// inside while loop get # from user -// hey tic tac toe make move } From 8104c2137057850b31a36090efffa93cfcc8fc56 Mon Sep 17 00:00:00 2001 From: Fatima Date: Mon, 29 Jun 2015 08:44:08 -0400 Subject: [PATCH 3/7] tic tac toe updated --- TicTacToe/TicTacToe/main.m | 394 ++++++++++++++++++++++++++++++++----- 1 file changed, 347 insertions(+), 47 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 42ce3c3..317e1f6 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -8,27 +8,42 @@ #import + + +/////// TIC TAC TOE DECLATRATION /////// @interface TicTacToe :NSObject; - (void)printIntroudction; +- (int)introMessage; + - (void)printBoard; -- (void)playerMove; +- (void)playerOne; + +- (void)playerTwo; - (NSMutableArray *)board; -- (BOOL)gameState:(int)tries; +- (BOOL)correctCheck; + +- (BOOL)oneVoneGameState:(int)num; + +- (BOOL)onePlayerGameState:(int)num; + +- (void)computerEasy; + + @end +/////// TIC TAC TOE DEFINITION /////// @implementation TicTacToe { NSMutableArray * _board; } - - (void)printIntroduction{ printf( "\n\n\tTic Tac Toe\n\n"); @@ -46,6 +61,50 @@ - (void)printIntroduction{ } +- (int)introMessage { + printf(" \n\n"); + printf("///////////////////////////////////////////////////////////////////////////////// \n\n"); + printf("######## #### ###### ######## ### ###### ######## ####### ########\n"); + printf(" ## ## ## ## ## ## ## ## ## ## ## ## ##\n"); + printf(" ## ## ## ## ## ## ## ## ## ## ##\n"); + printf(" ## ## ## ## ## ## ## ## ## ## ######\n"); + printf(" ## ## ## ## ######### ## ## ## ## ##\n"); + printf(" ## ## ## ## ## ## ## ## ## ## ## ## ##\n"); + printf(" ## #### ###### ## ## ## ###### ## ####### ########\n"); + printf(" \n\n"); + printf("///////////////////////////////////////////////////////////////////////////////// \n"); + printf("//// //// \n"); + printf("//// PRESS 1 for ONE PLAYER MODE //// \n"); + printf("//// //// \n"); + printf("//// PRESS 2 for TWO PLAYER MODE //// \n"); + printf("//// //// \n"); + printf("//// //// \n"); + printf("//// //// \n"); + printf("///////////////////////////////////////////////////////////////////////////////// \n\n"); + + int playerMode; + printf("ENTER: "); + scanf("%d", &playerMode); + + + int mode; + + if (playerMode == 1){ + + mode = 1; + + } else if (playerMode == 2) { + + mode = 2; + + } else { + + mode = 0; + } + + return mode; +} + - (NSMutableArray *)board{ _board = [NSMutableArray arrayWithObjects: @"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil]; @@ -55,25 +114,93 @@ - (NSMutableArray *)board{ -- (void)playerMove{ +- (void)playerOne{ - printf("Please pick a number where you want to put your move: "); + printf("PLAYER ONE MOVE: "); int x; scanf("%d", &x); printf("\n"); - for (int i = 0; i < 9; i++){ - NSLog(@"user selection: %d, current value: %d", x, i); - NSLog(@"%@", _board); - if (x == i+1){ - _board[i] = @"x"; + if (x < 10){ + if (![[_board objectAtIndex:x - 1] isEqualToString:@"x"] && ![[_board objectAtIndex:x - 1] isEqualToString:@"o"]) { + // valid input + _board[x - 1] = @"x"; + } else { + // already taken + NSLog(@"this is already taken"); } + } else { + printf("You inputted twice!"); } +} + +- (void)playerTwo { + + printf("PLAYER TWO MOVE: "); + int o; + scanf("%d", &o); + printf("\n"); + while(!(o < 10)){ + printf("Please pick a valid number: "); + scanf("%d", &o); + printf("\n"); + + } + + do { + + if (!(o < 10)){ + printf("This is already taken!\n"); + printf("Please pick a valid number: "); + scanf("%d", &o); + printf("\n"); + } + + if (o < 10){ + if (![[_board objectAtIndex:o - 1] isEqualToString:@"x"] && ![[_board objectAtIndex:o - 1] isEqualToString:@"o"]) { + // valid input + _board[o - 1] = @"o"; + } + else { + printf("This is already taken!\n"); + printf("Please pick a valid number: "); + scanf("%d", &o); + printf("\n"); + } + + } + } while (!(o < 10) || ([[_board objectAtIndex:o - 1] isEqualToString:@"x"] && ![[_board objectAtIndex:o - 1] isEqualToString:@"o"]) ); + +} + +- (void)computerEasy { + printf("COMPUTER MOVE: "); + + NSMutableArray *emptySpaces = [[NSMutableArray alloc]init]; + + for (int i = 0; i < [_board count]; i++){ + NSString *currentVal = [_board objectAtIndex:i]; + + if (![currentVal isEqualToString:@"x"] && ![currentVal isEqualToString:@"o"]){ + + [emptySpaces addObject:@(i)]; + } + } + + int fromNumber = 1; + int toNumber = (int)[emptySpaces count]; + int randomNumber = (arc4random()%(toNumber-fromNumber))+fromNumber; + + int computerSelection = [[emptySpaces objectAtIndex:randomNumber] integerValue]; + _board[computerSelection] = @"o"; + + } + - (void)printBoard { @@ -92,55 +219,216 @@ - (void)printBoard { } -- (BOOL)gameState:(int)tries{ +- (BOOL)oneVoneGameState:(int)num{ - [self board]; - int maxTries = 9; + [self board]; // array + int tries = 0; + int maxTries = 20; while (tries < maxTries){ - [self playerMove]; + [self playerOne]; [self printBoard]; - int horizontalCorrect1 = 0; - int horizontalCorrect2 = 0; - int horizontalCorrect3 = 0; - - // vertical check - for (int i = 0; i < [_board count]; i = i + 3) { - - if([ _board[i] isEqualToString:@"x"]){ - horizontalCorrect1++; - } - - if([_board[i+1] isEqualToString:@"x"]){ - horizontalCorrect2++; - } - - if ([_board[i+2] isEqualToString:@"x"]){ - horizontalCorrect3++; - } - - if (horizontalCorrect1 == 3 || horizontalCorrect2 == 3 || horizontalCorrect3 == 3){ - printf("WIN WIN WIN WIN WIN!"); - break; - } - - + if ([self correctCheck]) { + break; } - // diagonal check - for (int i = 0; i < [_board count]; i = i + 4) { - - if ([_board[i] isEqualToString: ]) + [self playerTwo]; + + [self printBoard]; + + if ([self correctCheck]) { + break; } tries++; + } + return true; } +- (BOOL)correctCheck { + + ////// HORIZONTAL CHECK + + BOOL win = false; + + // FIRST ROW + + if([[_board objectAtIndex:0] isEqualToString:@"x"] && [[_board objectAtIndex:1] isEqualToString:@"x"] && [[_board objectAtIndex:2] isEqualToString:@"x"]){ + + printf("Player X wins!"); + win = true; + } + + else if ([[_board objectAtIndex:0] isEqualToString:@"o"] && [[_board objectAtIndex:1] isEqualToString:@"o"] && [[_board objectAtIndex:2] isEqualToString:@"o"]) { + + printf("Player O wins!"); + win = true; + + } + + // SECOND ROW + + if([[_board objectAtIndex:3] isEqualToString:@"x"] && [[_board objectAtIndex:4] isEqualToString:@"x"] && [[_board objectAtIndex:5] isEqualToString:@"x"]){ + + printf("Player X wins!"); + win = true; + } + + else if ([[_board objectAtIndex:3] isEqualToString:@"o"] && [[_board objectAtIndex:4] isEqualToString:@"o"] && [[_board objectAtIndex:5] isEqualToString:@"o"]) { + + printf("Player O wins!"); + win = true; + + } + + // THIRD ROW + + if([[_board objectAtIndex:6] isEqualToString:@"x"] && [[_board objectAtIndex:7] isEqualToString:@"x"] && [[_board objectAtIndex:8] isEqualToString:@"x"]){ + + printf("Player X wins!"); + win = true; + } + + else if ([[_board objectAtIndex:6] isEqualToString:@"o"] && [[_board objectAtIndex:7] isEqualToString:@"o"] && [[_board objectAtIndex:8] isEqualToString:@"o"]) { + + printf("Player O wins!"); + win = true; + + } + + + ////// VERTICAL CHECK + + // FIRST COLUMN + + if([[_board objectAtIndex:0] isEqualToString:@"x"] && [[_board objectAtIndex:3] isEqualToString:@"x"] && [[_board objectAtIndex:6] isEqualToString:@"x"]){ + + printf("Player X wins!"); + win = true; + } + + else if ([[_board objectAtIndex:0] isEqualToString:@"o"] && [[_board objectAtIndex:3] isEqualToString:@"o"] && [[_board objectAtIndex:6] isEqualToString:@"o"]) { + + printf("Player O wins!"); + win = true; + + } + + // SECOND COLUMN + + if([[_board objectAtIndex:1] isEqualToString:@"x"] && [[_board objectAtIndex:4] isEqualToString:@"x"] && [[_board objectAtIndex:7] isEqualToString:@"x"]){ + + printf("Player X wins!"); + win = true; + } + + else if ([[_board objectAtIndex:1] isEqualToString:@"o"] && [[_board objectAtIndex:4] isEqualToString:@"o"] && [[_board objectAtIndex:7] isEqualToString:@"o"]) { + + printf("Player O wins!"); + win = true; + + } + + // THIRD COLUMN + + if([[_board objectAtIndex:2] isEqualToString:@"x"] && [[_board objectAtIndex:5] isEqualToString:@"x"] && [[_board objectAtIndex:8] isEqualToString:@"x"]){ + + printf("Player X wins!"); + win = true; + } + + else if ([[_board objectAtIndex:2] isEqualToString:@"o"] && [[_board objectAtIndex:5] isEqualToString:@"o"] && [[_board objectAtIndex:8] isEqualToString:@"o"]) { + + printf("Player O wins!"); + win = true; + + } + + + ////// DIAGONAL CHECK + + // \\\\ + // \\\\ + // -------- + // \ \ \ \ + // \ \ \ + // \ \ + // \ + + if([[_board objectAtIndex:0] isEqualToString:@"x"] && [[_board objectAtIndex:4] isEqualToString:@"x"] && [[_board objectAtIndex:8] isEqualToString:@"x"]){ + + printf("Player X wins!"); + win = true; + } + + else if ([[_board objectAtIndex:0] isEqualToString:@"o"] && [[_board objectAtIndex:4] isEqualToString:@"o"] && [[_board objectAtIndex:8] isEqualToString:@"o"]) { + + printf("Player O wins!"); + win = true; + + } + + // //// + // //// + // -------- + // / / / / + // / / / + // / / + // / + + if([[_board objectAtIndex:2] isEqualToString:@"x"] && [[_board objectAtIndex:4] isEqualToString:@"x"] && [[_board objectAtIndex:6] isEqualToString:@"x"]){ + + printf("Player X wins!"); + win = true; + } + + else if ([[_board objectAtIndex:2] isEqualToString:@"o"] && [[_board objectAtIndex:4] isEqualToString:@"o"] && [[_board objectAtIndex:6] isEqualToString:@"o"]) { + + printf("Player O wins!"); + win = true; + + } + + return win; +} + +- (BOOL)onePlayerGameState:(int)num{ + + [self board]; // array + int tries = 0; + int maxTries = 20; + while (tries < maxTries){ + + [self playerOne]; + + [self printBoard]; + + if ([self correctCheck]) { + break; + } + + + [self computerEasy]; + + [self printBoard]; + + if ([self correctCheck]) { + break; + } + + + + tries++; + + } + + return true; +} @end @@ -148,12 +436,24 @@ - (BOOL)gameState:(int)tries{ int main(int argc, const char * argv[]) { @autoreleasepool { - + TicTacToe *tictactoe = [[TicTacToe alloc]init]; - - [tictactoe printIntroduction]; - [tictactoe gameState:0]; + int playerChoose = [tictactoe introMessage]; + + if (playerChoose == 2){ + + [tictactoe printIntroduction]; + + [tictactoe oneVoneGameState:0]; + } + + else if (playerChoose == 1){ + + [tictactoe printIntroduction]; + + [tictactoe onePlayerGameState:0]; + } From d699b4b7a7688b034f962d9431d5f9a9b783bb0a Mon Sep 17 00:00:00 2001 From: Fatima Date: Tue, 30 Jun 2015 18:21:17 -0400 Subject: [PATCH 4/7] final project with tic tac toe --- TicTacToe/TicTacToe/main.m | 122 +++++++++++++++++++++++++------------ 1 file changed, 82 insertions(+), 40 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 317e1f6..fb048c2 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -33,6 +33,9 @@ - (BOOL)onePlayerGameState:(int)num; - (void)computerEasy; +- (void)printWinnerX; + +- (void)printWinnerO; @end @@ -43,6 +46,37 @@ @implementation TicTacToe { NSMutableArray * _board; } +- (void)printWinnerX { + + + printf(" `8.`8888. ,8' `8.`888b ,8' 8 8888 b. 8 d888888o.\n"); + printf(" `8.`8888. ,8' `8.`888b ,8' 8 8888 888o. 8 .`8888:' `88.\n"); + printf(" `8.`8888. ,8' `8.`888b ,8' 8 8888 Y88888o. 8 8.`8888. Y8\n"); + printf(" `8.`8888.,8' `8.`888b .b ,8' 8 8888 .`Y888888o. 8 `8.`8888.\n"); + printf(" `8.`88888' `8.`888b 88b ,8' 8 8888 8o. `Y888888o. 8 `8.`8888.\n"); + printf(" .88.`8888. `8.`888b .`888b,8' 8 8888 8`Y8o. `Y88888o8 `8.`8888.\n"); + printf(" .8'`8.`8888. `8.`888b8.`8888' 8 8888 8 `Y8o. `Y8888 `8.`8888.\n"); + printf(" .8' `8.`8888. `8.`888`8.`88' 8 8888 8 `Y8o. `Y8 8b `8.`8888.\n"); + printf(" .8' `8.`8888. `8.`8' `8,`' 8 8888 8 `Y8o.` `8b. ;8.`8888\n"); + printf(" .8' `8.`8888. `8.` `8' 8 8888 8 `Yo `Y8888P ,88P'\n"); +} + +- (void)PrinterWinnerO { + + + printf(" ,o888888o. `8.`888b ,8' 8 8888 b. 8 d888888o.\n"); + printf(" . 8888 `88. `8.`888b ,8' 8 8888 888o. 8 .`8888:' `88.\n"); + printf(" ,8 8888 `8b `8.`888b ,8' 8 8888 Y88888o. 8 8.`8888. Y8\n"); + printf(" 88 8888 `8b `8.`888b .b ,8' 8 8888 .`Y888888o. 8 `8.`8888.\n"); + printf(" 88 8888 88 `8.`888b 88b ,8' 8 8888 8o. `Y888888o. 8 `8.`8888.\n"); + printf(" 88 8888 88 `8.`888b .`888b,8' 8 8888 8`Y8o. `Y88888o8 `8.`8888.\n"); + printf(" 88 8888 ,8P `8.`888b8.`8888' 8 8888 8 `Y8o. `Y8888 `8.`8888.\n"); + printf(" `8 8888 ,8P `8.`888`8.`88' 8 8888 8 `Y8o. `Y8 8b `8.`8888.\n"); + printf(" ` 8888 ,88' `8.`8' `8,`' 8 8888 8 `Y8o.` `8b. ;8.`8888\n"); + printf(" `8888888P' `8.` `8' 8 8888 8 `Yo `Y8888P ,88P'\n"); + + +} - (void)printIntroduction{ @@ -113,25 +147,38 @@ - (NSMutableArray *)board{ } - - (void)playerOne{ printf("PLAYER ONE MOVE: "); int x; scanf("%d", &x); printf("\n"); + BOOL wrongMove = false; - if (x < 10){ - if (![[_board objectAtIndex:x - 1] isEqualToString:@"x"] && ![[_board objectAtIndex:x - 1] isEqualToString:@"o"]) { - // valid input - _board[x - 1] = @"x"; - } else { - // already taken - NSLog(@"this is already taken"); + do { + if (!(x < 10)){ + printf("Please pick a valid number: "); + scanf("%d", &x); + printf("\n"); + wrongMove = true; } - } else { - printf("You inputted twice!"); - } + + if (x < 10){ + if (![[_board objectAtIndex:x - 1] isEqualToString:@"o"] && ![[_board objectAtIndex:x - 1] isEqualToString:@"x"]) { + // valid input + _board[x - 1] = @"x"; + break; + } + else { + // already taken + printf("Please pick a valid number: "); + scanf("%d", &x); + printf("\n"); + wrongMove = true; + } + } + + } while (wrongMove); } @@ -141,36 +188,31 @@ - (void)playerTwo { int o; scanf("%d", &o); printf("\n"); - while(!(o < 10)){ - printf("Please pick a valid number: "); - scanf("%d", &o); - printf("\n"); - - } + BOOL wrongMove = false; do { - if (!(o < 10)){ - printf("This is already taken!\n"); printf("Please pick a valid number: "); scanf("%d", &o); printf("\n"); + wrongMove = true; } - if (o < 10){ - if (![[_board objectAtIndex:o - 1] isEqualToString:@"x"] && ![[_board objectAtIndex:o - 1] isEqualToString:@"o"]) { + if (![[_board objectAtIndex:o - 1] isEqualToString:@"o"] && ![[_board objectAtIndex:o - 1] isEqualToString:@"x"]) { // valid input _board[o - 1] = @"o"; + break; } else { - printf("This is already taken!\n"); + // already taken printf("Please pick a valid number: "); scanf("%d", &o); printf("\n"); + wrongMove = true; } - } - } while (!(o < 10) || ([[_board objectAtIndex:o - 1] isEqualToString:@"x"] && ![[_board objectAtIndex:o - 1] isEqualToString:@"o"]) ); + + } while (wrongMove); } @@ -260,13 +302,13 @@ - (BOOL)correctCheck { if([[_board objectAtIndex:0] isEqualToString:@"x"] && [[_board objectAtIndex:1] isEqualToString:@"x"] && [[_board objectAtIndex:2] isEqualToString:@"x"]){ - printf("Player X wins!"); + [self printWinnerX]; win = true; } else if ([[_board objectAtIndex:0] isEqualToString:@"o"] && [[_board objectAtIndex:1] isEqualToString:@"o"] && [[_board objectAtIndex:2] isEqualToString:@"o"]) { - printf("Player O wins!"); + [self printWinnerO]; win = true; } @@ -275,13 +317,13 @@ - (BOOL)correctCheck { if([[_board objectAtIndex:3] isEqualToString:@"x"] && [[_board objectAtIndex:4] isEqualToString:@"x"] && [[_board objectAtIndex:5] isEqualToString:@"x"]){ - printf("Player X wins!"); + [self printWinnerX]; win = true; } else if ([[_board objectAtIndex:3] isEqualToString:@"o"] && [[_board objectAtIndex:4] isEqualToString:@"o"] && [[_board objectAtIndex:5] isEqualToString:@"o"]) { - printf("Player O wins!"); + [self printWinnerO]; win = true; } @@ -290,13 +332,13 @@ - (BOOL)correctCheck { if([[_board objectAtIndex:6] isEqualToString:@"x"] && [[_board objectAtIndex:7] isEqualToString:@"x"] && [[_board objectAtIndex:8] isEqualToString:@"x"]){ - printf("Player X wins!"); + [self printWinnerX]; win = true; } else if ([[_board objectAtIndex:6] isEqualToString:@"o"] && [[_board objectAtIndex:7] isEqualToString:@"o"] && [[_board objectAtIndex:8] isEqualToString:@"o"]) { - printf("Player O wins!"); + [self printWinnerO]; win = true; } @@ -308,13 +350,13 @@ - (BOOL)correctCheck { if([[_board objectAtIndex:0] isEqualToString:@"x"] && [[_board objectAtIndex:3] isEqualToString:@"x"] && [[_board objectAtIndex:6] isEqualToString:@"x"]){ - printf("Player X wins!"); + [self printWinnerX]; win = true; } else if ([[_board objectAtIndex:0] isEqualToString:@"o"] && [[_board objectAtIndex:3] isEqualToString:@"o"] && [[_board objectAtIndex:6] isEqualToString:@"o"]) { - printf("Player O wins!"); + [self printWinnerO]; win = true; } @@ -323,13 +365,13 @@ - (BOOL)correctCheck { if([[_board objectAtIndex:1] isEqualToString:@"x"] && [[_board objectAtIndex:4] isEqualToString:@"x"] && [[_board objectAtIndex:7] isEqualToString:@"x"]){ - printf("Player X wins!"); + [self printWinnerX]; win = true; } else if ([[_board objectAtIndex:1] isEqualToString:@"o"] && [[_board objectAtIndex:4] isEqualToString:@"o"] && [[_board objectAtIndex:7] isEqualToString:@"o"]) { - printf("Player O wins!"); + [self printWinnerO]; win = true; } @@ -338,13 +380,13 @@ - (BOOL)correctCheck { if([[_board objectAtIndex:2] isEqualToString:@"x"] && [[_board objectAtIndex:5] isEqualToString:@"x"] && [[_board objectAtIndex:8] isEqualToString:@"x"]){ - printf("Player X wins!"); + [self printWinnerX]; win = true; } else if ([[_board objectAtIndex:2] isEqualToString:@"o"] && [[_board objectAtIndex:5] isEqualToString:@"o"] && [[_board objectAtIndex:8] isEqualToString:@"o"]) { - printf("Player O wins!"); + [self printWinnerO]; win = true; } @@ -362,13 +404,13 @@ - (BOOL)correctCheck { if([[_board objectAtIndex:0] isEqualToString:@"x"] && [[_board objectAtIndex:4] isEqualToString:@"x"] && [[_board objectAtIndex:8] isEqualToString:@"x"]){ - printf("Player X wins!"); + [self printWinnerX]; win = true; } else if ([[_board objectAtIndex:0] isEqualToString:@"o"] && [[_board objectAtIndex:4] isEqualToString:@"o"] && [[_board objectAtIndex:8] isEqualToString:@"o"]) { - printf("Player O wins!"); + [self printWinnerO]; win = true; } @@ -383,13 +425,13 @@ - (BOOL)correctCheck { if([[_board objectAtIndex:2] isEqualToString:@"x"] && [[_board objectAtIndex:4] isEqualToString:@"x"] && [[_board objectAtIndex:6] isEqualToString:@"x"]){ - printf("Player X wins!"); + [self printWinnerX]; win = true; } else if ([[_board objectAtIndex:2] isEqualToString:@"o"] && [[_board objectAtIndex:4] isEqualToString:@"o"] && [[_board objectAtIndex:6] isEqualToString:@"o"]) { - printf("Player O wins!"); + [self printWinnerO]; win = true; } From 8322dd58ba7b663733c74800d7c8c479487207ad Mon Sep 17 00:00:00 2001 From: Fatima Date: Wed, 1 Jul 2015 20:54:16 -0400 Subject: [PATCH 5/7] final project tic tac toe -Kaira and -Christella --- TicTacToe/TicTacToe/main.m | 172 ++++++++++++++++++++++++++++++++----- 1 file changed, 151 insertions(+), 21 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index fb048c2..d35b3b6 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -2,9 +2,8 @@ // main.m // TicTacToe // -// Created by Michael Kavouras on 6/25/15. -// Copyright (c) 2015 Mike Kavouras. All rights reserved. -// +// Kaira and Christella +// Tic Tac Toe Project #import @@ -25,6 +24,12 @@ - (void)playerTwo; - (NSMutableArray *)board; +- (NSString *)returnCoin:(char)headsTails; + +- (NSArray *)coinFaces; + +- (NSString *)playerSelection; + - (BOOL)correctCheck; - (BOOL)oneVoneGameState:(int)num; @@ -37,6 +42,12 @@ - (void)printWinnerX; - (void)printWinnerO; +- (void)coinCalculating:(NSArray *)coins; + +- (char)headsOrtails; + +- (BOOL)draw; + @end @@ -44,10 +55,80 @@ - (void)printWinnerO; /////// TIC TAC TOE DEFINITION /////// @implementation TicTacToe { NSMutableArray * _board; + NSArray * _coinFaces; + NSString * _playerSelection; + char _headsTails; +} + + + +- (NSArray *)coinFaces{ + + _coinFaces = @[@"h",@"t"]; + + return _coinFaces; + +} +- (NSString *)playerSelection { + + return _playerSelection; +} + +- (char)headsOrtails { + + do + { + printf("\nChoose heads(h) or tails(t): "); + scanf("%c", &_headsTails); + fpurge(stdin); + if(_headsTails == 'h') { + printf("\nYou chose Heads\n"); + } else if (_headsTails == 't'){ + printf("You chose Tails\n"); + } + + } while (_headsTails != 'h' && _headsTails != 't'); + return _headsTails; + +} +- (NSString *)returnCoin:(char)coin { + + _playerSelection = [NSString stringWithFormat:@"%c", _headsTails]; + + return _playerSelection; + + +} + +- (void)coinCalculating:(NSArray *)coins { + + NSUInteger randomNumber = arc4random_uniform((int)[_coinFaces count]); + + NSString *result = [_coinFaces objectAtIndex:randomNumber]; + + if ([result isEqualToString:[_coinFaces firstObject]]) { + printf("\nCoin is on Heads\n\n"); + } else if ([result isEqualToString:[_coinFaces lastObject]]) { + printf("\nCoin is on Tails\n\n"); + } + + + if([result isEqualToString: _playerSelection]) { + printf("Nice! you are Player 1. You get to go first. :D\n\n"); + } else { + printf("Bummer, Looks like you are Player 2\n\n"); + } + + const char *c = [_playerSelection UTF8String]; + const char *results = [result UTF8String]; + + printf("PLAYER INPUT: %s\n", c); + + printf("RANDOM TOSS: %s\n", results); } - (void)printWinnerX { - + printf(" `8.`8888. ,8' `8.`888b ,8' 8 8888 b. 8 d888888o.\n"); printf(" `8.`8888. ,8' `8.`888b ,8' 8 8888 888o. 8 .`8888:' `88.\n"); @@ -64,17 +145,17 @@ - (void)printWinnerX { - (void)PrinterWinnerO { - printf(" ,o888888o. `8.`888b ,8' 8 8888 b. 8 d888888o.\n"); - printf(" . 8888 `88. `8.`888b ,8' 8 8888 888o. 8 .`8888:' `88.\n"); - printf(" ,8 8888 `8b `8.`888b ,8' 8 8888 Y88888o. 8 8.`8888. Y8\n"); - printf(" 88 8888 `8b `8.`888b .b ,8' 8 8888 .`Y888888o. 8 `8.`8888.\n"); - printf(" 88 8888 88 `8.`888b 88b ,8' 8 8888 8o. `Y888888o. 8 `8.`8888.\n"); - printf(" 88 8888 88 `8.`888b .`888b,8' 8 8888 8`Y8o. `Y88888o8 `8.`8888.\n"); - printf(" 88 8888 ,8P `8.`888b8.`8888' 8 8888 8 `Y8o. `Y8888 `8.`8888.\n"); - printf(" `8 8888 ,8P `8.`888`8.`88' 8 8888 8 `Y8o. `Y8 8b `8.`8888.\n"); - printf(" ` 8888 ,88' `8.`8' `8,`' 8 8888 8 `Y8o.` `8b. ;8.`8888\n"); - printf(" `8888888P' `8.` `8' 8 8888 8 `Yo `Y8888P ,88P'\n"); - + printf(" ,o888888o. `8.`888b ,8' 8 8888 b. 8 d888888o.\n"); + printf(" . 8888 `88. `8.`888b ,8' 8 8888 888o. 8 .`8888:' `88.\n"); + printf(" ,8 8888 `8b `8.`888b ,8' 8 8888 Y88888o. 8 8.`8888. Y8\n"); + printf(" 88 8888 `8b `8.`888b .b ,8' 8 8888 .`Y888888o. 8 `8.`8888.\n"); + printf(" 88 8888 88 `8.`888b 88b ,8' 8 8888 8o. `Y888888o. 8 `8.`8888.\n"); + printf(" 88 8888 88 `8.`888b .`888b,8' 8 8888 8`Y8o. `Y88888o8 `8.`8888.\n"); + printf(" 88 8888 ,8P `8.`888b8.`8888' 8 8888 8 `Y8o. `Y8888 `8.`8888.\n"); + printf(" `8 8888 ,8P `8.`888`8.`88' 8 8888 8 `Y8o. `Y8 8b `8.`8888.\n"); + printf(" ` 8888 ,88' `8.`8' `8,`' 8 8888 8 `Y8o.` `8b. ;8.`8888\n"); + printf(" `8888888P' `8.` `8' 8 8888 8 `Yo `Y8888P ,88P'\n"); + } @@ -119,6 +200,7 @@ - (int)introMessage { int playerMode; printf("ENTER: "); scanf("%d", &playerMode); + fpurge(stdin); int mode; @@ -177,7 +259,7 @@ - (void)playerOne{ wrongMove = true; } } - + } while (wrongMove); } @@ -237,12 +319,9 @@ - (void)computerEasy { int computerSelection = [[emptySpaces objectAtIndex:randomNumber] integerValue]; _board[computerSelection] = @"o"; - - } - - (void)printBoard { @@ -263,7 +342,9 @@ - (void)printBoard { - (BOOL)oneVoneGameState:(int)num{ + [self board]; // array + int tries = 0; int maxTries = 20; while (tries < maxTries){ @@ -276,6 +357,11 @@ - (BOOL)oneVoneGameState:(int)num{ break; } + if ([self draw] == true){ + printf("It's a draw!"); + break; + } + [self playerTwo]; @@ -285,10 +371,18 @@ - (BOOL)oneVoneGameState:(int)num{ break; } + if ([self draw] == true){ + printf("It's a draw!"); + break; + } + tries++; + } + + return true; } @@ -472,6 +566,30 @@ - (BOOL)onePlayerGameState:(int)num{ return true; } +- (BOOL)draw { + + BOOL gameDraw = false; + NSMutableArray *emptySpaces = [[NSMutableArray alloc]init]; + for (int i = 0; i < [_board count]; i++){ + NSString *currentVal = [_board objectAtIndex:i]; + + if (![currentVal isEqualToString:@"x"] && ![currentVal isEqualToString:@"o"]){ + + [emptySpaces addObject:@(i)]; + + + } + } + + int toNumber = (int)[emptySpaces count]; + + if (toNumber == 0){ + gameDraw = true; + } + + return gameDraw; +} + @end @@ -481,14 +599,25 @@ int main(int argc, const char * argv[]) { TicTacToe *tictactoe = [[TicTacToe alloc]init]; + + int playerChoose = [tictactoe introMessage]; if (playerChoose == 2){ + char coinSide = [tictactoe headsOrtails]; + + NSArray *cointItself = [tictactoe coinFaces]; + + [tictactoe returnCoin:coinSide]; + + [tictactoe coinCalculating: cointItself]; + [tictactoe printIntroduction]; - [tictactoe oneVoneGameState:0]; - } + [tictactoe oneVoneGameState:0];} + + else if (playerChoose == 1){ @@ -499,6 +628,7 @@ int main(int argc, const char * argv[]) { + } return 0; } From b8e9fb8490078ceaa44df3341fc6019be9531d2a Mon Sep 17 00:00:00 2001 From: Christella Date: Thu, 2 Jul 2015 17:28:16 -0400 Subject: [PATCH 6/7] just adding a comment --- TicTacToe/TicTacToe/main.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index d35b3b6..9d50ca2 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -7,7 +7,7 @@ #import - +//HI HELLO /////// TIC TAC TOE DECLATRATION /////// @interface TicTacToe :NSObject; From 658ddbff98f3df2f63879b46e6ce1c4393b9ba4d Mon Sep 17 00:00:00 2001 From: Christella Date: Thu, 2 Jul 2015 18:10:21 -0400 Subject: [PATCH 7/7] Play again loop --- TicTacToe/TicTacToe/main.m | 56 +++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/TicTacToe/TicTacToe/main.m b/TicTacToe/TicTacToe/main.m index 9d50ca2..9f39c9a 100644 --- a/TicTacToe/TicTacToe/main.m +++ b/TicTacToe/TicTacToe/main.m @@ -12,7 +12,7 @@ /////// TIC TAC TOE DECLATRATION /////// @interface TicTacToe :NSObject; -- (void)printIntroudction; +- (void)printIntroduction; - (int)introMessage; @@ -142,7 +142,7 @@ - (void)printWinnerX { printf(" .8' `8.`8888. `8.` `8' 8 8888 8 `Yo `Y8888P ,88P'\n"); } -- (void)PrinterWinnerO { +- (void)printWinnerO { printf(" ,o888888o. `8.`888b ,8' 8 8888 b. 8 d888888o.\n"); @@ -345,6 +345,7 @@ - (BOOL)oneVoneGameState:(int)num{ [self board]; // array + int tries = 0; int maxTries = 20; while (tries < maxTries){ @@ -380,6 +381,7 @@ - (BOOL)oneVoneGameState:(int)num{ } + @@ -599,34 +601,44 @@ int main(int argc, const char * argv[]) { TicTacToe *tictactoe = [[TicTacToe alloc]init]; - - - int playerChoose = [tictactoe introMessage]; - - if (playerChoose == 2){ + int playAgain; + do { - char coinSide = [tictactoe headsOrtails]; - NSArray *cointItself = [tictactoe coinFaces]; + int playerChoose = [tictactoe introMessage]; - [tictactoe returnCoin:coinSide]; + if (playerChoose == 2){ + + char coinSide = [tictactoe headsOrtails]; + + NSArray *cointItself = [tictactoe coinFaces]; + + [tictactoe returnCoin:coinSide]; + + [tictactoe coinCalculating: cointItself]; + + [tictactoe printIntroduction]; + + [tictactoe oneVoneGameState:0];} - [tictactoe coinCalculating: cointItself]; - [tictactoe printIntroduction]; - [tictactoe oneVoneGameState:0];} - - - - else if (playerChoose == 1){ + else if (playerChoose == 1){ + + [tictactoe printIntroduction]; + + [tictactoe onePlayerGameState:0]; + } - [tictactoe printIntroduction]; - [tictactoe onePlayerGameState:0]; - } - - + printf("\n\nDo you want to play again?\n\n"); + printf("PRESS 1 for YES or PRESS 2 for NO\n\n"); + printf("ENTER: "); + scanf("%d", &playAgain); + fpurge(stdin); + + + } while (playAgain == 1); }