Advertisement
Guest User

TicTacToe in the console

a guest
Jun 14th, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.94 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int turn; /* flag for the player's turn. 0 = player X, 1 = player O */
  4. char* board[9]; /* cells in the tic-tac-toe board */
  5.  
  6. /* get input from the user for what cell to play in */
  7. int get_input() {
  8.     /* get user input */
  9.     printf("Position to play: ");
  10.     int pos;
  11.     scanf("%d", &pos);
  12.     pos--;
  13.  
  14.     /* error check the input */
  15.     if (pos < 0 || pos > 8) {
  16.         printf("Invalid position (choose between 1 and 9)\n");
  17.         return get_input();
  18.     }
  19.  
  20.     /* make sure nobody has played in the cell before */
  21.     if (board[pos] != "-") {
  22.         printf("Somebody has already played there. Try again...\n");
  23.         return get_input();
  24.     }
  25.  
  26.     return pos;
  27. }
  28.  
  29. /* display game board to the console */
  30. void display_board() {
  31.     for (int x=0; x<9; x++) {
  32.         /* new line after every 3 cells to form the grid output */
  33.         if (x > 0 && x % 3 == 0) {
  34.             printf("\n");
  35.         }
  36.  
  37.         /* print either X or O depending whats in the cell. */
  38.         /* print the cell number if the cell is empty */
  39.         if (board[x] == "-") {
  40.             printf("%d ", x+1);
  41.         } else {
  42.             printf("%s ", board[x]);
  43.         }
  44.     }
  45.  
  46.     printf("\n\n");
  47. }
  48.  
  49. /* make a player move (place X or O in a cell) */
  50. void make_move(int pos) {
  51.     board[pos] = turn ? "O" : "X";
  52.     turn = !turn;
  53. }
  54.  
  55. /* check if the game is over */
  56. int game_over() {
  57.     /* horizontal wins */
  58.     if (board[0] != "-" && board[0] == board[1] && board[1] == board[2]) {
  59.         printf("Player %s has won!\n", board[0]);
  60.         return 1;
  61.     }
  62.     if (board[3] != "-" && board[3] == board[4] && board[4] == board[5]) {
  63.         printf("Player %s has won!\n", board[3]);
  64.         return 1;
  65.     }
  66.     if (board[5] != "-" && board[5] == board[6] && board[6] == board[7]) {
  67.         printf("Player %s has won!\n", board[5]);
  68.         return 1;
  69.     }
  70.  
  71.     /* vertical wins */
  72.     if (board[0] != "-" && board[0] == board[3] && board[3] == board[6]) {
  73.         printf("Player %s has won!\n", board[0]);
  74.         return 1;
  75.     }
  76.     if (board[1] != "-" && board[1] == board[4] && board[4] == board[7]) {
  77.         printf("Player %s has won!\n", board[1]);
  78.         return 1;
  79.     }
  80.     if (board[2] != "-" && board[2] == board[5] && board[5] == board[8]) {
  81.         printf("Player %s has won!\n", board[2]);
  82.         return 1;
  83.     }
  84.  
  85.     /* diagonal wins */
  86.     if (board[0] != "-" && board[0] == board[4] && board[4] == board[8]) {
  87.         printf("Player %s has won!\n", board[0]);
  88.         return 1;
  89.     }
  90.     if (board[2] != "-" && board[2] == board[4] && board[4] == board[6]) {
  91.         printf("Player %s has won!\n", board[2]);
  92.         return 1;
  93.     }
  94.  
  95.     /* its a tie when all cells are filled */
  96.     for (int i=0; i<9; i++) {
  97.         if (board[i] == "-") {
  98.             return 0;
  99.         }
  100.     }
  101.     printf("It's a tie!\n");
  102.     return 1;
  103. }
  104.  
  105. int main() {
  106.     /* setup default game board */
  107.     for (int i=0; i<9; i++) {
  108.         board[i] = "-";
  109.     }
  110.  
  111.     printf("Welcome to Tie-Tac-Toe\n");
  112.     printf("Player X will go first.\n\n");
  113.     display_board();
  114.  
  115.     /* game loop until somebody wins or you both tie */
  116.     while (!game_over()) {
  117.         int position = get_input();
  118.         make_move(position);
  119.         display_board();
  120.     }
  121.  
  122.     printf("The game has ended.\n");
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement