// ********* TICTACTOE DEMO IN OBJECTIVE-C ********* // using printf instead of NSLog // using 1 instead of X // using 2 instead of O #import // ********* TICTACTOE INTERFACE ******************* @interface TicTacToe: NSObject { int playerTurn; // 0 for player X, 1 for player O int board[10]; // only using 1-9 } -(void) drawBoard; -(void) changePlayerTurn; -(void) make1In: (int) position; -(void) make2In: (int) position; @end // end of TicTacToe interface // ********* TICTACTOE IMPLEMENTATION ************** @implementation TicTacToe -(void) drawBoard { printf("\n%i | %i | %i\n", board[1], board[2], board[3]); printf("%i | %i | %i\n", board[4], board[5], board[6]); printf("%i | %i | %i\n", board[7], board[8], board[9]); } -(void) changePlayerTurn { if (playerTurn == 1) { playerTurn = 2; } else { playerTurn = 0; } } -(void) make1In: (int) position { board[position] = 1; } -(void) make2In: (int) position { board[position] = 2; } @end // end of TicTacToe implementation // ****************** CLIENT ******************* int main(int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int pos = 0; // board position TicTacToe *ttt = [TicTacToe alloc] ; printf("Welcome to TicTacToe"); printf("Board Positions:"); printf("\n1 | 2 | 3\n"); printf("4 | 5 | 6\n"); printf("7 | 8 | 9\n\n\n"); printf("Enter a position number: "); scanf("%i", &pos); [ttt make1In: pos]; [ttt drawBoard]; [ttt changePlayerTurn]; printf("Enter a position number: "); scanf("%i", &pos); [ttt make2In: pos]; [ttt drawBoard]; [ttt changePlayerTurn]; // free memory [ttt release]; [pool drain]; return 0; }// end of main