I've been learning Objective C for a few days so to test my skills I went ahead and made a simple Tic Tac Toe game using the console. I already have some experience with languages such as C++, Java and Python so I'm familiar with most of the concepts in Obj C.
How it works
- The player chooses their token (x or o)
- The player puts in the x and y coordinates of their token piece
- The computer makes its move
- Repeat 2 and 3 until someone wins
#import <Foundation/Foundation.h>
#include <stdlib.h>
// Game class
@interface Game : NSObject{
BOOL isPlayersTurn;
// players tokens (x or o)
char playerToken;
char computerToken;
// 2d array stores tile values
char board[3][3];
}
@end
@implementation Game
-(id)init{
self = [super init];
isPlayersTurn = YES;
// choosing players token
while(!(playerToken=='x' || playerToken=='o')){
NSLog(@"Choose your token (must be either x or o)");
scanf("%c",&playerToken);
}
// computer token is opposite of player token
computerToken = playerToken=='x'?'o':'x';
// set tiles of board to empty
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
board[i][j] = ' ';
}
}
return self;
}
-(BOOL)finished{
// check if someone has won
for(int i = 0; i < 3; i++){
// checks columns and rows
if(board[i][0]==board[i][1]&&board[i][1]==board[i][2]&&board[i][0]!=' '){
return true;
}
if(board[0][i]==board[1][i]&&board[1][i]==board[2][i]&&board[0][i]!=' '){
return true;
}
// check diagonals
if(board[0][0]==board[1][1]&&board[1][1]==board[2][2]&&board[0][0]!=' '){
return true;
}
if(board[2][0]==board[1][1]&&board[1][1]==board[0][2]&&board[2][0]!=' '){
return true;
}
}
return false;
}
-(void)play{
while(![self finished]){
// print the current board
NSLog([self getBoard]);
// get the moves
[self makeMoves];
}
// print the finished game board
NSLog([self getBoard]);
// winner is person who just had their move
NSLog([isPlayersTurn?@"Computer":@"Player" stringByAppendingString:@" wins!"]);
[self reset];
}
-(void)makeMoves{
int x;
int y;
if(isPlayersTurn){
// check their coordinate is either 1,2 or 3
while(x!=1&&x!=2&&x!=3){
NSLog(@"Choose your x coordinate");
scanf("%i",&x);
}
while(y!=1&&y!=2&&y!=3){
NSLog(@"Choose your y coordinate");
scanf("%i",&y);
}
board[(int)y-1][(int)x-1] = playerToken;
}else{
// loop through until random tile is chosen that isn't already taken
while(board[(int)y-1][(int)x-1]!=' '){
x = arc4random()%3;
y = arc4random()%3;
}
board[(int)y-1][(int)x-1] = computerToken;
}
// change turns
isPlayersTurn = !isPlayersTurn;
}
// board in text form will be returned
-(NSString*)getBoard{
NSString* boardText = @"\n";
if(isPlayersTurn){
boardText = [boardText stringByAppendingString:@"Player's turn "];
}else{
boardText = [boardText stringByAppendingString:@"Computer's turn "];
}
// sets x axis
boardText = [boardText stringByAppendingString:@"\n | 1 | 2 | 3 |"];
// sets boardText layout and y axis
for(int i=0; i<3; i++){
boardText = [boardText stringByAppendingString:@"\n"];
boardText = [boardText stringByAppendingString:[[NSNumber numberWithInt:i+1] stringValue]];
boardText = [boardText stringByAppendingString:@" |"];
for(int j=0; j<3; j++){
boardText = [boardText stringByAppendingString:@" "];
boardText = [boardText stringByAppendingString:[NSString stringWithFormat:@"%c", board[i][j]]];
boardText = [boardText stringByAppendingString:@" |"];
}
}
return boardText;
}
// resets game
-(void)reset{
// reset variables
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
board[i][j] = ' ';
}
}
isPlayersTurn = YES;
[self play];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Game *game = [[Game alloc]init];
[game play];
}
return 0;
}
Sample
2016-02-28 15:55:10.298 Tic Tac Toe[7003:378858] Choose your token (must be either x or o) o 2016-02-28 15:55:25.133 Tic Tac Toe[7003:378858] Player's turn | 1 | 2 | 3 | 1 | | | | 2 | | | | 3 | | | | 2016-02-28 15:55:25.134 Tic Tac Toe[7003:378858] Choose your x coordinate 1 2016-02-28 15:55:28.980 Tic Tac Toe[7003:378858] Choose your y coordinate 1 2016-02-28 15:55:29.549 Tic Tac Toe[7003:378858] Computer's turn | 1 | 2 | 3 | 1 | o | | | 2 | | | | 3 | | | | 2016-02-28 15:55:29.550 Tic Tac Toe[7003:378858] Player's turn | 1 | 2 | 3 | 1 | o | | | 2 | x | | | 3 | | | | 2016-02-28 15:55:29.550 Tic Tac Toe[7003:378858] Choose your x coordinate 2 2016-02-28 15:55:30.588 Tic Tac Toe[7003:378858] Choose your y coordinate 2 2016-02-28 15:55:31.028 Tic Tac Toe[7003:378858] Computer's turn | 1 | 2 | 3 | 1 | o | | | 2 | x | o | | 3 | | | | 2016-02-28 15:55:31.029 Tic Tac Toe[7003:378858] Player's turn | 1 | 2 | 3 | 1 | o | | x | 2 | x | o | | 3 | | | | 2016-02-28 15:55:31.029 Tic Tac Toe[7003:378858] Choose your x coordinate 3 2016-02-28 15:55:33.956 Tic Tac Toe[7003:378858] Choose your y coordinate 3 2016-02-28 15:55:34.420 Tic Tac Toe[7003:378858] Computer's turn | 1 | 2 | 3 | 1 | o | | x | 2 | x | o | | 3 | | | o | 2016-02-28 15:55:34.420 Tic Tac Toe[7003:378858] Player wins! 2016-02-28 15:55:34.421 Tic Tac Toe[7003:378858] Player's turn | 1 | 2 | 3 | 1 | | | | 2 | | | | 3 | | | | 2016-02-28 15:55:34.421 Tic Tac Toe[7003:378858] Choose your x coordinate
I would appreciate it if you could give me feedback on how to make this program shorter/easier. I would also appreciate some advice on proper use of pointers and strings.