jhylands

an attempt at tic tac toe in c

Oct 2nd, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 KB | None | 0 0
  1. // ConsoleApplication3.cpp : Defines the entry point for the console application.
  2. //
  3. #include <stdio.h>
  4. int setUpBoard(char *board);
  5. int printBoard(char board);
  6. int playerMove(char *board, char player);
  7. char checkForWin(char board);
  8. struct vector getCoord();
  9.  
  10. struct vector
  11. {
  12.     int x;
  13.     int y;
  14. };
  15.  
  16. int main(void)
  17. {
  18.     printf("Welcome to tic tac toe!");
  19.     char board[9];
  20.     setUpBoard((char*)board);
  21.     while (checkForWin(board) == '-'){
  22.         playerMove(&board, 'X');
  23.         if (checkForWin(board) == '-') {
  24.             playerMove(&board, 'O');
  25.         }
  26.     }
  27.     return 0;
  28. }
  29.  
  30. int setUpBoard(char *board) {
  31.     for (int i = 0; i < 3; i++) {
  32.         for (int n = 0; n < 3; i++) {
  33.             board[i*3+n] = '-';
  34.         }
  35.     }
  36.     return 0;
  37. }
  38. int printBoard(char board) {
  39.     for (int i = 0; i < 3; i++) {
  40.         printf("\n");
  41.         for (int n = 0; n < 3; n++) {
  42.             printf(board[i*3+n]);
  43.             printf("|");
  44.         }
  45.     }
  46.     return 0
  47. }
  48. int playerMove(char *board, char player) {
  49.     struct vector coord;
  50.     do {
  51.         coord = getCoord();
  52.     } while (*board[coord.x][coord.y] != '-')
  53.     *board[coord.x][coord.y] = player;
  54.     return 0;
  55. }
  56. char checkForWin(char board) {
  57.     /*check rows*/
  58.     for (int i = 0; i < 3; i++) {
  59.         if (board[i] == board[3+i] && board[2*3+i] = board[3+i]) {
  60.             return board[i];
  61.         }
  62.     }
  63.     /*check columns*/
  64.     for (int i = 0; i < 3; i++) {
  65.         if (board[i*3] == board[i*3+1] && board[i*3+2] = board[i*3+1]) {
  66.             return board[i*3];
  67.         }
  68.     }
  69.     if (board[0] == board[4] && board[4] == board[9]) {
  70.         return board[4];
  71.     }
  72.     return '-';
  73. }
  74. struct vector getCoord() {
  75.     printf("Please enter the coordinates of where you would like to go:\n");
  76.     printf("0,0|1,0|2,0\n0,1|1,1|2,1\n0,2|1,2|2,2");
  77.     printf("x:");
  78.     struct vector coord;
  79.     scanf("%d", coord.x);
  80.     printf("/ny:");
  81.     scanf("%d", coord.y);
  82.     return coord;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment