Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- #include < string.h >
- int FourOnBoard(char** arr, int m, int n);
- int RowFour(char** arr, int m, int n);
- int ColFour(char** arr, int m, int n);
- int FdiagonalFour(char** arr, int m, int n);
- int SdiagonalFour(char** arr, int m, int n);
- int checkAvailability(char** arr, int m, int n, int i, int j);
- void togglePlayer();
- char player = 'O';
- void printBoard(int** board, char** boardMove, int m,int n);
- char initGame(int** board, char** boardMove, int m, int n);
- void moveOfPlayers(char** arr, int m, int n);
- int main()
- {
- char* player1;
- char* player2;
- int sizep1 = 0, sizep2 = 0, countp1 = 0, countp2 = 0;
- int row, col, i, j,move,rows,cols,flag=0;
- // printf("How many characters is your name? (Player1):");
- // scanf("%d", &sizep1);
- player1 = (char*)malloc(sizep1 * sizeof(char));
- printf("\n Player1 - What is your name please?");
- scanf("%s", player1);
- // printf("\nHow many characters is your name? (Player2):");
- // scanf("%d", &sizep2);
- player2 = (char*)malloc(sizep2 * sizeof(char));
- printf("\n Player2 - What is your name please?");
- scanf("%s", player2);
- printf("\n Please enter number of rows:");
- scanf("%d", &row);
- printf("\n Please enter number of columns:");
- scanf("%d", &col);
- printf("\n\n Ok, %s and %s Let's go!!\n", player1, player2);
- system("pause");
- int** board;
- char** boardMoves;
- board= (int**)malloc(row * sizeof(int*));
- boardMoves = (char**)malloc(row * sizeof(char*));
- for (i = 0; i < row; i++)
- {
- board[i] = (int*)malloc(col * sizeof(int));
- boardMoves[i] = (char*)malloc(col * sizeof(char));
- }
- system("cls");
- initGame(board, boardMoves, row, col);
- printBoard(board, boardMoves, row, col);
- while (flag==0)
- {
- togglePlayer(player);
- printf("\n%c is your turn.\n", player);
- printf("\n Score:\n %s - %s \n %d : %d", player1, player2, countp1, countp2);
- moveOfPlayers(boardMoves, row, col);
- system("cls");
- printBoard(board, boardMoves, row, col);
- if (FourOnBoard(boardMoves,row,col) == 1)
- {
- printf("\n-------The Winner is %c-------\n", player);
- initGame(board, boardMoves, row, col);
- if (player == 'X')
- countp1++;
- else
- countp2++;
- }
- }
- for (i = 0; i < row; i++) {
- free(board[i]);
- free(boardMoves[i]);
- }
- free(board);
- free(boardMoves);
- return 0;
- }
- int RowFour(char** arr,int m, int n)
- {
- int i, j, count=0;
- for (i = 0; i < m; i++)
- {
- for (j = 0; j < n - 1; j++)
- {
- if (arr[i][j] == player && arr[i][j + 1] == player)
- {
- count++;
- if (count == 3)
- return 1;
- }
- if (arr[i][j] != arr[i][j + 1])
- count = 0;
- }
- }
- return 0;
- }
- int ColFour(char** arr, int m, int n)
- {
- int i, j, count=0;
- for (j = 0; j < n; j++)
- {
- for (i = 0; i < m - 1; i++)
- {
- if (arr[i][j] == player && arr[i+1][j] == player)
- {
- count++;
- if (count == 3)
- return 1;
- }
- if (arr[i][j] != player && arr[i + 1][j] != player)
- count = 0;
- }
- }
- return 0;
- }
- int FdiagonalFour(char** arr, int m, int n)
- {
- int i, j, count=0;
- for (i = 0; i < m-1; i++)
- {
- for (j = 0; j < n - 1; j++)
- {
- if (arr[i][j] == player && arr[i+1][j+1] == player)
- {
- while(arr[i][j] == player && arr[i + 1][j + 1] == player && i+2 != m && j+2 != n)
- {
- count++;
- i++;
- j++;
- if (count == 3)
- return 1;
- }
- if (arr[i][j] == arr[i + 1][j+1])
- {
- count++;
- if (count == 3)
- return 1;
- }
- }
- if (arr[i][j] != arr[i+1][j + 1])
- count = 0;
- }
- }
- return 0;
- }
- int SdiagonalFour(char** arr, int m, int n)
- {
- int i, j, count = 0;
- for (i = 0; i < m -1; i++)
- {
- for (j = 0; j < n; j++)
- {
- if (arr[i][j] == player && arr[i+1][j-1] == player)
- {
- while (arr[i][j] == player && arr[i + 1][j - 1] == player && i + 2 != m )
- {
- count++;
- i++;
- j--;
- if (count == 3)
- return 1;
- }
- if (arr[i][j] == arr[i + 1][j -1])
- {
- count++;
- if (count == 3)
- return 1;
- }
- }
- if (arr[i][j] != arr[i + 1][j - 1])
- count = 0;
- }
- }
- return 0;
- }
- int FourOnBoard(char** arr, int m, int n)
- {
- int back = 0;
- back = RowFour(arr, m, n);
- if (back == 1) return 1;
- back = ColFour(arr, m, n);
- if (back == 1) return 1;
- back = SdiagonalFour(arr, m, n);
- if (back == 1) return 1;
- back = FdiagonalFour(arr, m, n);
- if (back == 1) return 1;
- return 0;
- }
- void togglePlayer()
- {
- player = player == 'O' ? 'X' : 'O';
- }
- void printBoard(int** board, char** boardMove, int m,int n)
- {
- int i, j;
- for (i = 0; i < m; i++)
- {
- printf("\n");
- for (j = 0; j < n; j++)
- {
- if(boardMove[i][j] == ' ')
- printf("%d\t", board[i][j]);
- else
- printf("%c\t", boardMove[i][j]);
- }
- printf("\n");
- }
- }
- char initGame(int** board,char** boardMove, int m, int n)
- {
- int i, j;
- char num = 1;
- for (i = 0; i < m; i++)
- {
- for (j = 0; j < n; j++)
- {
- board[i][j] = num;
- boardMove[i][j] = ' ';
- num++;
- }
- }
- }
- int checkAvailability(char** arr,int rows,int cols , int i,int j)
- {
- if (i < 0 || i >= rows || j < 0 || j >= cols) return 0;
- if (arr[i][j] == 'X' || arr[i][j] == 'O') return 0;
- return 1;
- }
- void moveOfPlayers(char** arr, int m, int n)
- {
- int i, j,num;
- printf("\n\nGive me your move: ");
- scanf("%d", &num);
- i = givemeI(arr, m, n, num);
- j = givemeJ(arr, m, n, num);
- while (!checkAvailability(arr, m, n, i, j)) {
- printf("\nWrong number please try again: ");
- scanf("%d", &num);
- i = givemeI(arr, m, n, num);
- j = givemeJ(arr, m, n, num);
- }
- arr[i][j] = player;
- }
- int givemeI(char** arr, int rows, int cols, int num)
- {
- int i;
- i = ((num - 1) / cols);
- return i;
- }
- int givemeJ(char** arr, int rows, int cols, int num)
- {
- int j;
- j = ((num - 1) % cols);
- return j;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement