Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // TicTacToeState.h
  2.  
  3.  
  4. //
  5. //  TicTacToeState.h
  6. //  CocoaTTT
  7. //
  8. //  Created by Zachary Becker on 2/14/11.
  9. //  Copyright 2011 Home. All rights reserved.
  10. //
  11.  
  12. #import <Cocoa/Cocoa.h>
  13.  
  14. typedef enum { PIECE_X=1, NONE=0, PIECE_O=-1 } Piece;
  15.  
  16. @interface TicTacToeState : NSObject {
  17.     Piece board[9];
  18.     Piece winner;
  19.     bool gameover;
  20. }
  21.  
  22. @property (readonly) Piece winner;
  23. @property (readonly) bool gameover;
  24.  
  25. - (Piece)turn;
  26. - (void)setPieceAtRow:(int)row column:(int)col;
  27. - (Piece)pieceAtRow:(int)row column:(int)col;
  28. - (bool)isValidMoveAtRow:(int)row column:(int)col;
  29.  
  30. @end
  31.  
  32.  
  33. // TicTacToeState.m
  34.  
  35. //
  36. //  TicTacToeState.m
  37. //  CocoaTTT
  38. //
  39. //  Created by Zachary Becker on 2/14/11.
  40. //  Copyright 2011 Home. All rights reserved.
  41. //
  42.  
  43. #import "TicTacToeState.h"
  44.  
  45.  
  46. @implementation TicTacToeState
  47.  
  48. @synthesize winner;
  49. @synthesize gameover;
  50.  
  51. int possibleWins[8][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8},
  52.     {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}};
  53.  
  54. - (id)init {
  55.     if (self = [super init]) {        
  56.         // Initialize tic tac toe board
  57.         for (int i = 0; i < 9; ++i) {
  58.             board[i] = NONE;            
  59.         }
  60.         winner = NONE;
  61.         gameover = FALSE;
  62.     }
  63.    
  64.     return self;
  65. }
  66.  
  67. - (void)dealloc {
  68.     [super dealloc];
  69. }
  70.  
  71. - (Piece)turn {
  72.     if (gameover)
  73.         return NONE;
  74.    
  75.     int count = 0;
  76.     for (int i = 0; i < 9; ++i) {
  77.         if (board[i] == NONE) {
  78.             ++count;
  79.         }
  80.     }
  81.    
  82.     if (count % 2)
  83.         return PIECE_X;
  84.     else
  85.         return PIECE_O;        
  86. }
  87.  
  88. - (void)setPieceAtRow:(int)row column:(int)col {
  89.     if (gameover)
  90.         return;
  91.    
  92.     int index = row * 3 + col;
  93.     if (board[index] != NONE)
  94.         return;
  95.    
  96.     board[index] = self.turn;
  97.    
  98.     // Check for tic tac toe
  99.     for (int i = 0; i < 8; ++i) {
  100.         int a = possibleWins[i][0];
  101.         int b = possibleWins[i][1];
  102.         int c = possibleWins[i][2];
  103.         if (board[a] == board[b] && board[a] == board[c] && board[a] != NONE) {
  104.             winner = board[a];
  105.             gameover = TRUE;
  106.         }
  107.     }
  108.    
  109.     // Check if there are any open spots on board
  110.     for (int i = 0; i < 9; ++i) {
  111.         if (board[i] == NONE)
  112.             return; // Exit if spots remain
  113.     }
  114.    
  115.     winner = NONE;
  116.     gameover = TRUE;
  117. }
  118.  
  119. - (Piece)pieceAtRow:(int)row column:(int)col {
  120.     int index = row * 3 + col;
  121.     return board[index];
  122. }
  123.  
  124. - (bool)isValidMoveAtRow:(int)row column:(int)col {
  125.     int index = row * 3 + col;
  126.     return board[index] == NONE;
  127. }
  128.  
  129. @end
  130.  
  131.  
  132. // main.m
  133. // This is mostly C, haven't done any graphics stuff in Cocoa yet
  134.  
  135.  
  136. //
  137. //  main.m
  138. //  CocoaTTT
  139. //
  140. //  Created by Zachary Becker on 2/14/11.
  141. //  Copyright 2011 Home. All rights reserved.
  142. //
  143.  
  144. #import <Cocoa/Cocoa.h>
  145.  
  146. #import "TicTacToeState.h"
  147. #import <stdio.h>
  148.  
  149. void printTicTacToeState(TicTacToeState *state)
  150. {
  151.     for (int i = 0; i < 3; ++i) {
  152.         for (int j = 0; j < 3; ++j) {
  153.             Piece cur = [state pieceAtRow:i column:j];
  154.             if (cur == PIECE_X)
  155.                 printf(" X ");
  156.             else if (cur == PIECE_O)
  157.                 printf(" O ");
  158.             else
  159.                 printf("   ");
  160.             if (j != 2)
  161.                 printf("|");
  162.             else
  163.                 printf("\n");
  164.         }
  165.         if (i != 2)
  166.             printf("---|---|---\n");
  167.     }
  168. }
  169.  
  170. int main(int argc, char *argv[])
  171. {
  172.     TicTacToeState *tictactoe = [TicTacToeState new];
  173.     [tictactoe retain];
  174.    
  175.     while (!tictactoe.gameover) {
  176.         printTicTacToeState(tictactoe);
  177.         int row, col;
  178.         printf("Enter Row: ");
  179.         scanf("%d", &row);
  180.         printf("Enter Col: ");
  181.         scanf("%d", &col);
  182.         if (![tictactoe isValidMoveAtRow:row column:col]) {
  183.             printf("Invalid move! Try again...\n");
  184.             continue;
  185.         }
  186.         [tictactoe setPieceAtRow:row column:col];
  187.     }
  188.    
  189.     printTicTacToeState(tictactoe);
  190.    
  191.     if (tictactoe.winner == PIECE_X) {
  192.         printf("X Won!\n");
  193.     } else if (tictactoe.winner == PIECE_O) {
  194.         printf("O Won!\n");
  195.     } else {
  196.         printf("Cats game!\n");
  197.     }
  198.    
  199.     [tictactoe release];
  200.    
  201.     return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement