jhylands

tic tac toe no compiler errors

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