ulvis.paste.net

Paste Search Dynamic
Recent pastes
the tic tac toe game board
  1. /*      
  2.                        ***Variables info table***
  3.   char board[row][column] = the tic tac toe game board, comprised of 3 rows and 3 columns.
  4.   char p1[35] = player 1's name.
  5.   char p2[35] = player 2's name.
  6.   char p1marker = 'X' self explanatory
  7.   char p2marker = 'O' self explanatory
  8.   int row = board row selected by the player
  9.   int column = board column selected by the player
  10.   int i = used to print the board columns
  11.   int j = used to print the board rows
  12.   int playcounter = counts how many turns have passed, if the counter is greater than 9, it means that the entire board has been filled up without a winner, so, in other words, it's a draw.
  13.   int x = receives the value returned by the wincheck function, if it receives 1, the player 1 has won, if it receives 2, the player 2 has won, if it receives 3, the game ended in a draw.
  14.   int save = variable that saves de result of the match to a file depending on the value given by the player (1-save, 2-don't save)
  15.   int opt = executes one of the command chains depending on the value given by the player.
  16.   int replay = returns to the start of int main() if the player wishes to use the main menu again, if he doesn't, closes the program.
  17.   char buffer [loadprintmatchhistory func] = variable responsible for holding each line of the file being opened and then printing them one by one on the screen.
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22.  
  23. void matrixzeroed(char board[3][3]); //Function that zeroes all the positions of the board matrix.
  24. void markboard(char board[3][3], char p1marker, char p2marker, int row, int column);// This one checks whether or not the position chosen by the player has already been marked, if not, marks that position with his marker.
  25. void skynet(char board[3][3], char p1marker, char p2marker, int row, int column, int i, char p2[]); // The reasonably simple AI which serves as a opponent to the player 1.
  26. void printmatrix(char board[3][3], char p1[], char p2[], int i, int j); //Function responsible for printing the tic tac toe board and the positions marked by the players in it.
  27. int winnercheck(char board[3][3], char p1marker, char p2marker); // Check which one of the players was victorious, or if the game ended in a draw.(Barebones fix applied, keep looking for bugs)
  28. int printresult (char board[3][3], char p1marker, char p2marker, int row, int column, int i, int j, int x, char p1[], char p2[]); //saves the match result into a file named jooj.txt (YTPBR)
  29. int loadprintmatchhistory (); //Loads previous match results from a file named jooj.txt
  30.  
  31. int main ()
  32. {
  33.   char board[3][3], p1[35], p2[] = {"SkyNet"}, p1marker = 'X', p2marker = 'O';
  34.   int row,column, i, j, playcounter, x, save, opt, replay;
  35.   matrixzeroed(board);
  36.   system ("cls");
  37.                 while (opt != 1){
  38.         printf("                    ----------------------------------------------------------------------------------\n");
  39.         printf("                                                          Tic Tac Toe                                 \n");
  40.         printf("                    ----------------------------------------------------------------------------------\n");
  41.        
  42.         printf ("Choose one of the following options:\n 1 - Exit\n 2 - Read previously saved matches\n 3 - Play.\nOption:");
  43.         setbuf (stdin,null);
  44.         scanf ("%d", &opt);
  45.         printf("Voce escolheu: %d", opt);
  46.         setbuf (stdin,null);
  47.        
  48.         if (opt != 1 && opt != 2 && opt != 3)
  49.         {
  50.             printf ("Incorrect parameter! Choose one of the following options:\n");  sleep(2); main();
  51.         }
  52.                        
  53.         if (opt == 2)
  54.                 loadprintmatchhistory();
  55.                
  56.    
  57.                 if (opt == 3){
  58.  
  59.       printf ("\nType in Player 1's name:"); //only asks for player 1's name since player 2 is the CPU (named SkyNet by one of the devs)
  60.        gets (p1);
  61.        system("cls");
  62.        
  63.     while(playcounter < 9) //keeps the game going until it reaches the turn limit if none of the players has won the game until then.
  64.     {
  65.       printmatrix(board, p1, p2, i, j);
  66.       printf("\n\nIt's %s's turn!\n", p1);
  67.         printf("\nType the number of the row that you want to mark: \n");
  68.           scanf("%d", &row);
  69.            row = row - 1;
  70.         printf("\nType the number of the column that you want to mark: \n");
  71.           scanf("%d", &column);
  72.            column = column - 1;
  73.        
  74.         markboard(board, p1marker, p2marker, row, column); //Player 1's turn command chain
  75.         system("cls");
  76.          printmatrix(board, p1, p2, i, j);
  77.           x = winnercheck(board, p1marker, p2marker);
  78.           if (x == 1) break;
  79.           playcounter++;
  80.            
  81.         skynet(board, p1marker, p2marker, row, column, i, p2); //Player 2's turn command chain
  82.         system("cls");
  83.          printmatrix(board, p1, p2, i, j); printf("\n");
  84.           sleep (1);
  85.         x = winnercheck(board, p1marker, p2marker);
  86.          system("cls");  
  87.           if (x == 2) playcounter = 10;
  88.            playcounter++;
  89.     }
  90.  
  91.  
  92.  
  93.  
  94.     if (x == 1)
  95.     {
  96.     printmatrix(board, p1, p2, i, j);
  97.     printf("\n\nPlayer %s has won!\n\n", p1);
  98.     }
  99.     if (x == 2)
  100.     {
  101.     printmatrix(board, p1, p2, i, j);
  102.     printf("\n\nPlayer %s has won!\n\n", p2);
  103.     }
  104.     if (x == 3)
  105.     {
  106.     printmatrix(board, p1, p2, i, j);
  107.     printf ("\n\nDeu velha!!!\n\n");
  108.     }
  109.    
  110.    printf ("\n\nDo you wish to save the match results into a file?\n 1 - Yes. \n 2 - No. \n Option:");
  111.    scanf ("%d", &save);
  112.    setbuf (stdin,null);
  113.    
  114.    while (save != 1 && save != 2)
  115.    {
  116.     printf ("This option does not exist, select one of those:\n1 - Yes\n 2 - No\nOption:");
  117.     scanf ("%d", &save);
  118.     setbuf(stdin,null);
  119.    }
  120.    if (save == 1)
  121.    {
  122.    printresult (board, p1marker, p2marker, row, column, i, j, x, p1, p2);
  123.    printf ("\n\n\nThe match has been saved into a file named jooj.txt in the same folder of the game executable.\n\n");
  124.    }
  125.    if (save == 2) printf ("\nThe match has not been saved.\n\n");
  126.    
  127.    printf ("\nDo you want to go back to the main menu?\n1 - Yes \n2 - No(closes the program automatically)\nOption:");
  128.    scanf ("%d", &replay);
  129.    while (replay != 1 && replay != 2)
  130.    {
  131.     printf ("\nIncorrect option, there are only 2 options:\n1 - Yes\n 2 -No(closes the program automatically)\nOption:");
  132.     scanf ("%d", &replay);
  133.     setbuf(stdin,null);
  134.    }
  135.  
  136.    if (replay == 1) main();
  137.    if (replay == 2)
  138.         opt = 1;
  139.    }
  140. }
  141. }
  142.  
  143.  
  144.  
  145.  
  146. void matrixzeroed(char board[3][3]) // The cleaning order has been organized to better resemble the look of the board, making it easier to understand the code.
  147. {
  148.     board[0][0] = '.'; board[0][1] = '.'; board[0][2] = '.';
  149.     board[1][0] = '.'; board[1][1] = '.'; board[1][2] = '.';
  150.     board[2][0] = '.'; board[2][1] = '.'; board[2][2] = '.';
  151. }
  152.  
  153. void markboard(char board[3][3], char p1marker, char p2marker, int row, int column)
  154. {
  155.     if (board[row][column] == '.')  board[row][column] = p1marker;  
  156.     else
  157.     {
  158.         printf("\nThis position is occupied by either X or O, or goes beyond the board's positions, try again: \n");
  159.         printf("\nType the number of the row that you want to mark: \n");
  160.         scanf("%d", &row);
  161.         if (row > 3)
  162.             while (row > 3)
  163.             {
  164.              printf("\nThis row position does not exist, please try again: \n");
  165.              scanf("%d", &row);
  166.             }    
  167.         row = row - 1;
  168.        
  169.         printf("\nType the number of the column that you want to mark: \n");
  170.         scanf("%d", &column);
  171.             while (column > 3)
  172.             {
  173.              printf("\nThis column position does not exist, please try again: \n");
  174.              scanf("%d", &column);
  175.             }
  176.         column = column - 1;
  177.        
  178.         markboard(board, p1marker, p2marker, row, column);
  179.     }
  180. }
  181.  
  182. void skynet(char board[3][3], char p1marker, char p2marker, int row, int column, int i, char p2[]) // reduced from 133 to 73 lines in a single swift swoop.
  183. {
  184.     printf("\n\nIt's %s's turn!\n", p2);
  185.     printf("\nComputing ");
  186.     for (i=0; i<8; i++)
  187.     {
  188.         usleep(100000);
  189.         printf(".");
  190.     }
  191. //For the AI, offensive tactics are the top priority, preceding anything else...              
  192.   if      (board[0][0] == p2marker && board[0][1] == p2marker && board[0][2] == '.') board[0][2] = p2marker;//Start of the offense oriented Row commands
  193.    else if (board[0][0] == p2marker && board[0][2] == p2marker && board[0][1] == '.') board[0][1] = p2marker;
  194.     else if (board[0][1] == p2marker && board[0][2] == p2marker && board[0][0] == '.') board[0][0] = p2marker;
  195.      else if (board[1][0] == p2marker && board[1][1] == p2marker && board[1][2] == '.') board[1][2] = p2marker;
  196.       else if (board[1][0] == p2marker && board[1][2] == p2marker && board[1][1] == '.') board[1][1] = p2marker;
  197.        else if (board[1][1] == p2marker && board[1][2] == p2marker && board[1][0] == '.') board[1][0] = p2marker;
  198.         else if (board[2][0] == p2marker && board[2][1] == p2marker && board[2][2] == '.') board[2][2] = p2marker;
  199.          else if (board[2][0] == p2marker && board[2][2] == p2marker && board[2][1] == '.') board[2][1] = p2marker;
  200.           else if (board[2][1] == p2marker && board[2][2] == p2marker && board[2][0] == '.') board[2][0] = p2marker;//End of the offense oriented Row commands
  201.            else if (board[0][0] == p2marker && board[1][0] == p2marker && board[2][0] == '.') board[2][0] = p2marker;//Start of the offense oriented Column commands
  202.             else if (board[0][0] == p2marker && board[2][0] == p2marker && board[1][0] == '.') board[1][0] = p2marker;
  203.              else if (board[1][0] == p2marker && board[2][0] == p2marker && board[0][0] == '.') board[0][0] = p2marker;
  204.               else if (board[0][1] == p2marker && board[1][1] == p2marker && board[2][1] == '.') board[2][1] = p2marker;
  205.                else if (board[0][1] == p2marker && board[2][1] == p2marker && board[1][1] == '.') board[1][1] = p2marker;
  206.                 else if (board[2][1] == p2marker && board[1][1] == p2marker && board[0][1] == '.') board[0][1] = p2marker;
  207.                  else if (board[0][2] == p2marker && board[1][2] == p2marker && board[2][2] == '.') board[2][2] = p2marker;
  208.                   else if (board[0][2] == p2marker && board[2][2] == p2marker && board[1][2] == '.') board[1][2] = p2marker;
  209.                    else if (board[1][2] == p2marker && board[2][2] == p2marker && board[0][2] == '.') board[0][2] = p2marker; //End of the offense oriented Column commands
  210.                     else if (board[0][0] == p2marker && board[1][1] == p2marker && board[2][2] == '.') board[2][2] = p2marker;//Start of the offense oriented Diagonal commands
  211.                      else if (board[0][0] == p2marker && board[2][2] == p2marker && board[1][1] == '.') board[1][1] = p2marker;
  212.                       else if (board[1][1] == p2marker && board[2][2] == p2marker && board[0][0] == '.') board[0][0] = p2marker;
  213.                        else if (board[0][2] == p2marker && board[1][1] == p2marker && board[2][0] == '.') board[2][0] = p2marker;
  214.                         else if (board[0][2] == p2marker && board[2][0] == p2marker && board[1][1] == '.') board[1][1] = p2marker;
  215.                          else if (board[2][0] == p2marker && board[1][1] == p2marker && board[0][2] == '.') board[0][2] = p2marker;//End of the offense oriented Digaonal commands
  216. //...followed by defensive tactics...  
  217.   else if (board[0][0] == p1marker && board[0][1] == p1marker && board[0][2] == '.') board[0][2] = p2marker; //Start of the defense oriented Row commands
  218.    else if (board[0][0] == p1marker && board[0][2] == p1marker && board[0][1] == '.') board[0][1] = p2marker;
  219.     else if (board[0][1] == p1marker && board[0][2] == p1marker && board[0][0] == '.') board[0][0] = p2marker;  
  220.      else if (board[1][0] == p1marker && board[1][1] == p1marker && board[1][2] == '.') board[1][2] = p2marker;
  221.       else if (board[1][0] == p1marker && board[1][2] == p1marker && board[1][1] == '.') board[1][1] = p2marker;
  222.        else if (board[1][1] == p1marker && board[1][2] == p1marker && board[1][0] == '.') board[1][0] = p2marker;
  223.         else if (board[2][0] == p1marker && board[2][1] == p1marker && board[2][2] == '.') board[2][2] = p2marker;
  224.          else if (board[2][0] == p1marker && board[2][2] == p1marker && board[2][1] == '.') board[2][1] = p2marker;
  225.           else if (board[2][1] == p1marker && board[2][2] == p1marker && board[2][0] == '.') board[2][0] = p2marker;//End of the defense oriented Row commands
  226.            else if (board[0][0] == p1marker && board[1][0] == p1marker && board[2][0] == '.') board[2][0] = p2marker;// Start of the defense oriented Column commands
  227.             else if (board[0][0] == p1marker && board[2][0] == p1marker && board[1][0] == '.') board[1][0] = p2marker;
  228.              else if (board[1][0] == p1marker && board[2][0] == p1marker && board[0][0] == '.') board[0][0] = p2marker;
  229.               else if (board[0][1] == p1marker && board[1][1] == p1marker && board[2][1] == '.') board[2][1] = p2marker;
  230.                else if (board[0][1] == p1marker && board[2][1] == p1marker && board[1][1] == '.') board[1][1] = p2marker;
  231.                 else if (board[2][1] == p1marker && board[1][1] == p1marker && board[0][1] == '.') board[0][1] = p2marker;
  232.                  else if (board[0][2] == p1marker && board[1][2] == p1marker && board[2][2] == '.') board[2][2] = p2marker;
  233.                   else if (board[0][2] == p1marker && board[2][2] == p1marker && board[1][2] == '.') board[1][2] = p2marker;
  234.                    else if (board[1][2] == p1marker && board[2][2] == p1marker && board[0][2] == '.') board[0][2] = p2marker; //End of the defense oriented Column commands
  235.                     else if (board[0][0] == p1marker && board[1][1] == p1marker && board[2][2] == '.') board[2][2] = p2marker;// Start of the defense oriented Diagonal commands
  236.                      else if (board[0][0] == p1marker && board[2][2] == p1marker && board[1][1] == '.') board[1][1] = p2marker;
  237.                       else if (board[1][1] == p1marker && board[2][2] == p1marker && board[0][0] == '.') board[0][0] = p2marker;
  238.                        else if (board[0][2] == p1marker && board[1][1] == p1marker && board[2][0] == '.') board[2][0] = p2marker;
  239.                         else if (board[0][2] == p1marker && board[2][0] == p1marker && board[1][1] == '.') board[1][1] = p2marker;
  240.                          else if (board[2][0] == p1marker && board[1][1] == p1marker && board[0][2] == '.') board[0][2] = p2marker; //End of the defense oriented Diagonal commands
  241. //...and ending in marking the empty spaces on the board, as a measure to bring the game to, at least, a draw.
  242.                  else if (board[0][0] == '.') board[0][0] = p2marker;
  243.                   else if (board[0][1] == '.') board[0][1] = p2marker;
  244.                    else if (board[0][2] == '.') board[0][2] = p2marker;
  245.                     else if (board[1][0] == '.') board[1][0] = p2marker;
  246.                      else if (board[1][1] == '.') board[1][1] = p2marker;
  247.                       else if (board[1][2] == '.') board[1][2] = p2marker;
  248.                        else if (board[2][0] == '.') board[2][0] = p2marker;
  249.                         else if (board[2][1] == '.') board[2][1] = p2marker;
  250.                          else if (board[2][2] == '.') board[2][2] = p2marker;
  251. //Disconnected from the AI offensive and defensive tactics, we have the starting game tactics.                      
  252. else if (board[0][0] == '.' && board[0][1] == '.' && board[0][2] == '.') board[0][0] = p2marker;
  253.  else if (board[1][0] == '.' && board[1][1] == '.' && board[1][2] == '.') board[1][1] = p2marker;
  254.   else if (board[2][0] == '.' && board[2][1] == '.' && board[2][2] == '.') board[2][2] = p2marker;
  255. }
  256.  
  257. void printmatrix(char board[3][3], char p1[], char p2[], int i, int j)
  258. {
  259.     system("cls");
  260.         printf("                    ----------------------------------------------------------------------------------\n");
  261.         printf("                                                          Tic Tac Toe                                 \n");
  262.         printf("                    ----------------------------------------------------------------------------------\n");
  263.     printf ("\nPlayer 1's name is: %s\t\t    Player 2's name is: %s\n\n\t\t\t     ", p1, p2);
  264.       for (i=0;i<3;i++)
  265.       {
  266.          printf ("C %d",i+1); // prints he number of the column above the board
  267.          if (i<2) printf ("|"); // additional command to improve the visual aspect of the board's columns.
  268.       }    
  269.     printf ("\n\n");
  270.        for (j=0;j<3;j++)
  271.        {
  272.     printf ("\t\t\tR %d  ",j+1); // prints the number of the row at the left of the board.
  273.         for (i=0;i<3;i++)
  274.         {
  275.           printf (" %c ", board[j][i]);
  276.           if (i<2) printf ("|"); // pretty much the same as above
  277.         }
  278.        if (j <2) printf("\n\t\t\t     ___________\n"); // additional command to improve the visual aspect of the board's rows
  279.        }
  280. }
  281.  
  282. int winnercheck(char board[3][3], char p1marker, char p2marker)
  283. {
  284.    //Checking if player 1 has won the game...
  285.     if     (board[0][0] == p1marker && board[0][1] == p1marker && board[0][2] == p1marker) return 1;        // <- Row 1 formed by player 1  
  286.     else if (board[1][0] == p1marker && board[1][1] == p1marker && board[1][2] == p1marker) return 1;       // <- Row 2 formed by player 1
  287.      else if (board[2][0] == p1marker && board[2][1] == p1marker && board[2][2] == p1marker) return 1;      // <- Row 3 formed by player 1
  288.       else if (board[0][0] == p1marker && board[1][0] == p1marker && board[2][0] == p1marker) return 1;     // <- Column 1 formed by player 1
  289.        else if (board[0][1] == p1marker && board[1][1] == p1marker && board[2][1] == p1marker) return 1;    // <- Column 2 formed by player 1
  290.         else if (board[0][2] == p1marker && board[1][2] == p1marker && board[2][2] == p1marker) return 1;   // <- Column 2 formed by player 1
  291.          else if (board[0][0] == p1marker && board[1][1] == p1marker && board[2][2] == p1marker) return 1;  // <- Main diagonal formed by player 1
  292.           else if (board[0][2] == p1marker && board[1][1] == p1marker && board[2][0] == p1marker) return 1; // <- Secondary diagonal formed by player 1
  293. //Checking if player 2 has won the game...  
  294.    else if (board[0][0] == p2marker && board[0][1] == p2marker && board[0][2] == p2marker) return 2;        // <- Row 1 formed by player 2
  295.     else if (board[1][0] == p2marker && board[1][1] == p2marker && board[1][2] == p2marker) return 2;       // <- Row 2 formed by player 2
  296.      else if (board[2][0] == p2marker && board[2][1] == p2marker && board[2][2] == p2marker) return 2;      // <- Row 3 formed by player 2
  297.       else if (board[0][0] == p2marker && board[1][0] == p2marker && board[2][0] == p2marker) return 2;     // <- Column 1 formed by player 2
  298.        else if (board[0][1] == p2marker && board[1][1] == p2marker && board[2][1] == p2marker) return 2;    // <- Column 2 formed by player 2
  299.         else if (board[0][2] == p2marker && board[1][2] == p2marker && board[2][2] == p2marker) return 2;   // <- Column 3 formed by player 2
  300.          else if (board[0][0] == p2marker && board[1][1] == p2marker && board[2][2] == p2marker) return 2;  // <- Main diagonal formed by player 2
  301.           else if (board[0][2] == p2marker && board[1][1] == p2marker && board[2][0] == p2marker) return 2; // <- Secondary diagonal formed by player 2
  302.     //And, if none of them has won the game...            
  303.         else return 3;  
  304. }
  305.  
  306. int printresult (char board[3][3], char p1marker, char p2marker, int row, int column, int i, int j, int x, char p1[], char p2[])
  307. {
  308.     system ("cls");
  309.      FILE *r;
  310.      r = fopen("jooj.txt", "a");
  311.      printf ("Saving");
  312.      while (i<15) {
  313.                       printf (".");
  314.                       usleep(50000);
  315.                       i++;
  316.                   }
  317.         fprintf(r, "                    ----------------------------------------------------------------------------------\n");
  318.         fprintf(r, "                                                          Tic Tac Toe                                 \n");
  319.         fprintf(r, "                    ----------------------------------------------------------------------------------\n");
  320.     fprintf (r, "\nPlayer 1's name is: %s\t\t    Player 2's name is: %s\n\n\t\t\t     ", p1, p2);
  321.       for (i=0;i<3;i++)
  322.       {
  323.          fprintf (r, "C %d",i+1);
  324.          if (i<2) fprintf (r, "|");
  325.       }    
  326.     fprintf (r, "\n\n");
  327.        for (j=0;j<3;j++)
  328.        {
  329.     fprintf (r, "\t\t\tR %d  ",j+1);
  330.         for (i=0;i<3;i++)
  331.         {
  332.           fprintf (r, " %c ", board[j][i]);
  333.           if (i<2) fprintf (r, "|");
  334.         }
  335.        if (j <2) fprintf(r, "\n\t\t\t     ___________\n");
  336.        }
  337.     if (x == 1) fprintf(r, "\n\nPlayer %s has won!\n\n", p1);
  338.     if (x == 2) fprintf(r, "\n\nPlayer %s has won!\n\n", p2);
  339.     if (x == 3) fprintf(r, "\n\nDeu velha!!!\n\n");
  340.     fprintf (r, "\n\n\n");
  341.     fclose (r);
  342.     return 0;
  343. }
  344.  
  345. loadprintmatchhistory ()
  346. {
  347.     FILE *fread;
  348.     fread = fopen("jooj.txt", "r");
  349.     if (fread == null)
  350.    {
  351.     system ("cls");
  352.       perror("Error while opening the file.\n\n");
  353.       sleep(2);
  354.       main();
  355.    }
  356.         else{
  357.     char buffer[100];
  358.     printf ("\nPrinting previous matches history:\n\n");
  359.     while (!feof(fread)) //feof = file end of file, ! = not
  360.     {
  361.         fgets (buffer, 100, fread);
  362.         printf(buffer);
  363.         usleep(80000);
  364.     }
  365.     fclose (fread);
  366.     printf ("Press enter to return to the menu."); getchar();
  367.     main();
  368. }
  369. }
Parsed in 0.122 seconds