Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.83 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <time.h>
  6. #include < string.h >
  7. int FourOnBoard(char** arr, int m, int n);
  8. int RowFour(char** arr, int m, int n);
  9. int ColFour(char** arr, int m, int n);
  10. int FdiagonalFour(char** arr, int m, int n);
  11. int SdiagonalFour(char** arr, int m, int n);
  12. int checkAvailability(char** arr, int m, int n, int i, int j);
  13. void togglePlayer();
  14. char player = 'O';
  15. void printBoard(int** board, char** boardMove, int m,int n);
  16. char initGame(int** board, char** boardMove, int m, int n);
  17. void moveOfPlayers(char** arr, int m, int n);
  18.  
  19.  
  20. int main()
  21. {
  22.     char* player1;
  23.     char* player2;
  24.     int sizep1 = 0, sizep2 = 0, countp1 = 0, countp2 = 0;
  25.     int row, col, i, j,move,rows,cols,flag=0;
  26. //  printf("How many characters is your name? (Player1):");
  27. //  scanf("%d", &sizep1);
  28.     player1 = (char*)malloc(sizep1 * sizeof(char));
  29.     printf("\n Player1 - What is your name please?");
  30.     scanf("%s", player1);
  31. //  printf("\nHow many characters is your name? (Player2):");
  32. //  scanf("%d", &sizep2);
  33.     player2 = (char*)malloc(sizep2 * sizeof(char));
  34.     printf("\n Player2 - What is your name please?");
  35.     scanf("%s", player2);
  36.     printf("\n Please enter number of rows:");
  37.     scanf("%d", &row);
  38.     printf("\n Please enter number of columns:");
  39.     scanf("%d", &col);
  40.     printf("\n\n Ok, %s and %s Let's go!!\n", player1, player2);
  41.     system("pause");
  42.  
  43.     int** board;
  44.     char** boardMoves;
  45.     board= (int**)malloc(row * sizeof(int*));
  46.     boardMoves = (char**)malloc(row * sizeof(char*));
  47.     for (i = 0; i < row; i++)
  48.     {
  49.         board[i] = (int*)malloc(col * sizeof(int));
  50.         boardMoves[i] = (char*)malloc(col * sizeof(char));
  51.     }
  52.     system("cls");
  53.     initGame(board, boardMoves, row, col);
  54.     printBoard(board, boardMoves, row, col);
  55.  
  56.     while (flag==0)
  57.     {
  58.        
  59.         togglePlayer(player);
  60.         printf("\n%c is your turn.\n", player);
  61.         printf("\n Score:\n %s - %s \n   %d : %d", player1, player2, countp1, countp2);
  62.         moveOfPlayers(boardMoves, row, col);
  63.         system("cls");
  64.         printBoard(board, boardMoves, row, col);
  65.         if (FourOnBoard(boardMoves,row,col) == 1)
  66.         {
  67.             printf("\n-------The Winner is %c-------\n", player);
  68.             initGame(board, boardMoves, row, col);
  69.  
  70.             if (player == 'X')
  71.                 countp1++;
  72.             else
  73.                 countp2++;
  74.         }
  75.     }
  76.  
  77.     for (i = 0; i < row; i++) {
  78.         free(board[i]);
  79.         free(boardMoves[i]);
  80.     }
  81.     free(board);
  82.     free(boardMoves);
  83.     return 0;
  84. }
  85.  
  86. int RowFour(char** arr,int m, int n)
  87. {
  88.     int i, j, count=0;
  89.     for (i = 0; i < m; i++)
  90.     {
  91.         for (j = 0; j < n - 1; j++)
  92.         {
  93.  
  94.             if (arr[i][j] == player && arr[i][j + 1] == player)
  95.             {
  96.                 count++;
  97.                 if (count == 3)
  98.                     return 1;
  99.             }
  100.             if (arr[i][j] != arr[i][j + 1])
  101.                 count = 0;
  102.         }
  103.     }
  104.     return 0;  
  105.  
  106. }
  107.  
  108. int ColFour(char** arr, int m, int n)
  109. {
  110.     int i, j, count=0;
  111.     for (j = 0; j < n; j++)
  112.     {
  113.         for (i = 0; i < m - 1; i++)
  114.         {
  115.             if (arr[i][j] == player && arr[i+1][j] == player)
  116.             {
  117.                 count++;
  118.                 if (count == 3)
  119.                     return 1;
  120.             }
  121.             if (arr[i][j] != player && arr[i + 1][j] != player)
  122.                 count = 0;
  123.         }
  124.     }
  125.     return 0;
  126. }
  127.  
  128. int FdiagonalFour(char** arr, int m, int n)
  129. {
  130.     int i, j, count=0;
  131.     for (i = 0; i < m-1; i++)
  132.     {
  133.        
  134.         for (j = 0; j < n - 1; j++)
  135.         {
  136.             if (arr[i][j] == player && arr[i+1][j+1] == player)
  137.             {
  138.             while(arr[i][j] == player && arr[i + 1][j + 1] == player && i+2 != m && j+2 != n)
  139.                 {
  140.                 count++;
  141.                 i++;
  142.                 j++;
  143.                     if (count == 3)
  144.                     return 1;
  145.                 }
  146.             if (arr[i][j] == arr[i + 1][j+1])
  147.             {
  148.                 count++;
  149.                 if (count == 3)
  150.                     return 1;
  151.             }
  152.  
  153.             }
  154.  
  155.             if (arr[i][j] != arr[i+1][j + 1])
  156.                 count = 0;
  157.         }
  158.     }
  159.     return 0;
  160. }
  161.  
  162. int SdiagonalFour(char** arr, int m, int n)
  163. {
  164.     int i, j, count = 0;
  165.  
  166.     for (i = 0; i < m -1; i++)
  167.     {
  168.         for (j = 0; j < n; j++)
  169.         {
  170.             if (arr[i][j] == player && arr[i+1][j-1] == player)
  171.             {
  172.                 while (arr[i][j] == player && arr[i + 1][j - 1] == player && i + 2 != m )
  173.                 {
  174.                     count++;
  175.                     i++;
  176.                     j--;
  177.                     if (count == 3)
  178.                         return 1;
  179.                 }
  180.                 if (arr[i][j] == arr[i + 1][j -1])
  181.                 {
  182.                     count++;
  183.                     if (count == 3)
  184.                         return 1;
  185.                 }
  186.  
  187.             }
  188.  
  189.             if (arr[i][j] != arr[i + 1][j - 1])
  190.                 count = 0;
  191.         }
  192.     }
  193.  
  194.     return 0;
  195.  
  196. }
  197. int FourOnBoard(char** arr, int m, int n)
  198. {
  199.     int back = 0;
  200.    
  201.     back = RowFour(arr, m, n);
  202.     if (back == 1) return 1;
  203.     back = ColFour(arr, m, n);
  204.     if (back == 1) return 1;
  205.     back = SdiagonalFour(arr, m, n);
  206.     if (back == 1) return 1;
  207.     back = FdiagonalFour(arr, m, n);
  208.     if (back == 1) return 1;
  209.     return 0;
  210. }
  211.  
  212. void togglePlayer()
  213. {
  214.     player = player == 'O' ? 'X' : 'O';
  215. }
  216.  
  217. void printBoard(int** board, char** boardMove, int m,int n)
  218. {
  219.     int i, j;
  220.     for (i = 0; i < m; i++)
  221.     {
  222.         printf("\n");
  223.         for (j = 0; j < n; j++)
  224.         {
  225.             if(boardMove[i][j] == ' ')
  226.                 printf("%d\t", board[i][j]);
  227.             else
  228.                 printf("%c\t", boardMove[i][j]);
  229.         }
  230.         printf("\n");
  231.     }
  232.  
  233. }
  234.  
  235. char initGame(int** board,char** boardMove, int m, int n)
  236. {
  237.     int i, j;
  238.     char num = 1;
  239.     for (i = 0; i < m; i++)
  240.     {
  241.         for (j = 0; j < n; j++)
  242.         {
  243.             board[i][j] = num;
  244.             boardMove[i][j] = ' ';
  245.             num++;
  246.         }
  247.     }
  248.  
  249. }
  250.  
  251. int checkAvailability(char** arr,int rows,int cols , int i,int j)
  252. {
  253.     if (i < 0 || i >= rows || j < 0 || j >= cols) return 0;
  254.     if (arr[i][j] == 'X' || arr[i][j] == 'O') return 0;
  255.     return 1;
  256. }
  257.  
  258. void moveOfPlayers(char** arr, int m, int n)
  259. {
  260.     int i, j,num;
  261.  
  262.     printf("\n\nGive me your move: ");
  263.     scanf("%d", &num);
  264.     i = givemeI(arr, m, n, num);
  265.     j = givemeJ(arr, m, n, num);
  266.     while (!checkAvailability(arr, m, n, i, j)) {
  267.         printf("\nWrong number please try again: ");
  268.         scanf("%d", &num);
  269.         i = givemeI(arr, m, n, num);
  270.         j = givemeJ(arr, m, n, num);
  271.     }
  272.     arr[i][j] = player;
  273. }
  274.  
  275. int givemeI(char** arr, int rows, int cols, int num)
  276. {
  277.     int i;
  278.     i = ((num - 1) / cols);
  279.     return i;
  280.  
  281. }
  282.  
  283. int givemeJ(char** arr, int rows, int cols, int num)
  284. {
  285.     int j;
  286.     j = ((num - 1) % cols);
  287.     return j;
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement