Advertisement
Guest User

Untitled

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