Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TicTacToeState.h
- //
- // TicTacToeState.h
- // CocoaTTT
- //
- // Created by Zachary Becker on 2/14/11.
- // Copyright 2011 Home. All rights reserved.
- //
- #import <Cocoa/Cocoa.h>
- typedef enum { PIECE_X=1, NONE=0, PIECE_O=-1 } Piece;
- @interface TicTacToeState : NSObject {
- Piece board[9];
- Piece winner;
- bool gameover;
- }
- @property (readonly) Piece winner;
- @property (readonly) bool gameover;
- - (Piece)turn;
- - (void)setPieceAtRow:(int)row column:(int)col;
- - (Piece)pieceAtRow:(int)row column:(int)col;
- - (bool)isValidMoveAtRow:(int)row column:(int)col;
- @end
- // TicTacToeState.m
- //
- // TicTacToeState.m
- // CocoaTTT
- //
- // Created by Zachary Becker on 2/14/11.
- // Copyright 2011 Home. All rights reserved.
- //
- #import "TicTacToeState.h"
- @implementation TicTacToeState
- @synthesize winner;
- @synthesize gameover;
- int possibleWins[8][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8},
- {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}};
- - (id)init {
- if (self = [super init]) {
- // Initialize tic tac toe board
- for (int i = 0; i < 9; ++i) {
- board[i] = NONE;
- }
- winner = NONE;
- gameover = FALSE;
- }
- return self;
- }
- - (void)dealloc {
- [super dealloc];
- }
- - (Piece)turn {
- if (gameover)
- return NONE;
- int count = 0;
- for (int i = 0; i < 9; ++i) {
- if (board[i] == NONE) {
- ++count;
- }
- }
- if (count % 2)
- return PIECE_X;
- else
- return PIECE_O;
- }
- - (void)setPieceAtRow:(int)row column:(int)col {
- if (gameover)
- return;
- int index = row * 3 + col;
- if (board[index] != NONE)
- return;
- board[index] = self.turn;
- // Check for tic tac toe
- for (int i = 0; i < 8; ++i) {
- int a = possibleWins[i][0];
- int b = possibleWins[i][1];
- int c = possibleWins[i][2];
- if (board[a] == board[b] && board[a] == board[c] && board[a] != NONE) {
- winner = board[a];
- gameover = TRUE;
- }
- }
- // Check if there are any open spots on board
- for (int i = 0; i < 9; ++i) {
- if (board[i] == NONE)
- return; // Exit if spots remain
- }
- winner = NONE;
- gameover = TRUE;
- }
- - (Piece)pieceAtRow:(int)row column:(int)col {
- int index = row * 3 + col;
- return board[index];
- }
- - (bool)isValidMoveAtRow:(int)row column:(int)col {
- int index = row * 3 + col;
- return board[index] == NONE;
- }
- @end
- // main.m
- // This is mostly C, haven't done any graphics stuff in Cocoa yet
- //
- // main.m
- // CocoaTTT
- //
- // Created by Zachary Becker on 2/14/11.
- // Copyright 2011 Home. All rights reserved.
- //
- #import <Cocoa/Cocoa.h>
- #import "TicTacToeState.h"
- #import <stdio.h>
- void printTicTacToeState(TicTacToeState *state)
- {
- for (int i = 0; i < 3; ++i) {
- for (int j = 0; j < 3; ++j) {
- Piece cur = [state pieceAtRow:i column:j];
- if (cur == PIECE_X)
- printf(" X ");
- else if (cur == PIECE_O)
- printf(" O ");
- else
- printf(" ");
- if (j != 2)
- printf("|");
- else
- printf("\n");
- }
- if (i != 2)
- printf("---|---|---\n");
- }
- }
- int main(int argc, char *argv[])
- {
- TicTacToeState *tictactoe = [TicTacToeState new];
- [tictactoe retain];
- while (!tictactoe.gameover) {
- printTicTacToeState(tictactoe);
- int row, col;
- printf("Enter Row: ");
- scanf("%d", &row);
- printf("Enter Col: ");
- scanf("%d", &col);
- if (![tictactoe isValidMoveAtRow:row column:col]) {
- printf("Invalid move! Try again...\n");
- continue;
- }
- [tictactoe setPieceAtRow:row column:col];
- }
- printTicTacToeState(tictactoe);
- if (tictactoe.winner == PIECE_X) {
- printf("X Won!\n");
- } else if (tictactoe.winner == PIECE_O) {
- printf("O Won!\n");
- } else {
- printf("Cats game!\n");
- }
- [tictactoe release];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement