Advertisement
Guest User

tic-tac-toe

a guest
Aug 26th, 2011
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <windows.h>
  3.  
  4. #define b_col       3
  5. #define b_row       3
  6.  
  7. void clear_screen()
  8. {
  9.     HANDLE output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
  10.     DWORD bytes_write, size;
  11.     COORD coord = {0, 0};
  12.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  13.  
  14.     GetConsoleScreenBufferInfo(output_handle, &csbi);
  15.    
  16.     size = csbi.dwSize.X * csbi.dwSize.Y;
  17.    
  18.     FillConsoleOutputCharacter(output_handle, ' ', size, coord, &bytes_write);
  19.     SetConsoleCursorPosition(output_handle, coord);
  20. }
  21.  
  22. void clear_board(char game_board[][b_col])
  23. {
  24.     for(int i = 0; i < b_row; i++)
  25.         for(int j = 0; j < b_col; j++)
  26.             game_board[i][j] = '-';
  27. }
  28.  
  29. void print_board(char game_board[][b_col])
  30. {
  31.     for(int i = 0; i < b_row; i++)
  32.     {
  33.         for(int j = 0; j < b_col; j++)
  34.             printf("%c   ", game_board[i][j]);
  35.         printf("\n\n\n");
  36.     }
  37. }
  38.  
  39. bool place_xo(char game_board[][b_col], int row, int col, char x_o)
  40. {
  41.  
  42.     if(game_board[row - 1][col - 1] != '-') //if coordinates given is not empty
  43.             return false;
  44.     else
  45.     {
  46.         game_board[row - 1][col - 1] = x_o;
  47.         clear_screen(); //clear screen. don't use system("CLS");
  48.         print_board(game_board); //print board.
  49.         return true;
  50.     }
  51. }
  52.  
  53. int check_row_col(char game_board[][b_col], char x_o, unsigned int f_row = 0, unsigned int f_col = 0)
  54. {
  55.     int counter = 0;
  56.     if(f_row != 0) //checks rows
  57.     {
  58.         for(int i = 0; i < b_col; i++)
  59.             if(game_board[f_row - 1][i] == x_o)
  60.                 counter++;
  61.     }
  62.     else if(f_col != 0) //checks columns
  63.     {
  64.         for(int i = 0; i < b_row; i++)
  65.             if(game_board[i][f_col - 1] == x_o)
  66.                 counter++;
  67.     }
  68.     else if(f_col == 0 && f_col == 0) //diagonal
  69.     {
  70.         int d_col = 0;
  71.         for(int d_row = 0; d_row < b_row; d_row++, d_col++)
  72.             if(game_board[d_row][d_col] == x_o)
  73.                 counter++;
  74.         if(counter == b_row)
  75.             return counter;
  76.         else
  77.         {
  78.             d_col = b_col;
  79.             counter = 0;
  80.             for(int d_row = 0; d_row < b_row; d_row++, d_col--)
  81.                 if(game_board[d_row][d_col - 1] == x_o)
  82.                     counter++;
  83.         }
  84.     }
  85.     return counter;
  86. }
  87.  
  88. bool win_or_not(char game_board[][b_col], char x_o)
  89. {
  90.     for(int i = 1; i < b_col; i++) //checks all columns
  91.     {
  92.         if(check_row_col(game_board, x_o, 0, i) == b_row)
  93.         {
  94.             printf("\n\n\n%c won the game", x_o);
  95.             return true;
  96.         }
  97.     }
  98.  
  99.     for(int i = 1; i < b_row; i++) //checks all rows
  100.     {
  101.         if(check_row_col(game_board, x_o, i, 0) == b_row)
  102.         {
  103.             printf("\n\n\n%c won the game", x_o);
  104.             return true;
  105.         }
  106.     }
  107.  
  108.     if(check_row_col(game_board, x_o, 0, 0) == b_row) //diagonal checks
  109.     {
  110.             printf("\n\n\n%c won the  game", x_o);
  111.             return true;
  112.     }
  113.  
  114.     return false;
  115. }
  116.  
  117. int main()
  118. {
  119.     if(b_row != b_col)
  120.         return -1;
  121.     char game_board[b_row][b_col];
  122.     bool win_not = false;
  123.     int row, col;
  124.     clear_board(game_board); //clear board
  125.     print_board(game_board); //print board
  126.  
  127.     bool x_turn = true;
  128.  
  129.     do
  130.     {
  131.         printf("--It's %c's turn--\n\n", x_turn ? 'X' : 'O');
  132.  
  133.         printf("Row: ");
  134.         scanf("%d", &row);
  135.  
  136.         printf("Column: ");
  137.         scanf("%d", &col);
  138.  
  139.         if(row > b_row || col > b_col)
  140.         {
  141.             printf("\nOut of bounds.\n\n");
  142.             continue;
  143.         }
  144.  
  145.         if(!place_xo(game_board, row, col, x_turn ? 'X' : 'O')) //place the 'x' and 'o'. x_turn ? 'X' : 'O' returns based on the bool value of x_turn
  146.         {
  147.             printf("\nCannot put %c into row %d, col %d.\n\n", x_turn ? 'X' : 'O', row, col);
  148.             continue;
  149.         }
  150.  
  151.         win_not = win_or_not(game_board, x_turn ? 'X' : 'O');
  152.  
  153.         x_turn = !x_turn;
  154.     }while(win_not == false);
  155.     getchar();
  156.     getchar();
  157.     return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement