Please tell me why the code is not working properly as I am not able to figure out the problem.
#include<stdio.h>
#include<stdlib.h>
#define true 1
#define false 0
int m = 5; //rows
int n = 5; //columns
int i,j;
char ai = 'R';
char human = 'B';
char currentPlayer;
//functions used
_Bool equals5(char , char ,char , char , char );
char checkWinner(char board[5][5]);
void showBoard(char board[5][5]);
void bestMove(char board[5][5]) ;
void humanChance(char board[5][5]);
void setUp(char board[5][5]);
int MIN(int , int );
int MAX(int , int );
int minimax(char board[m][n], _Bool);
void main()
{
//int k = 4; //elements to win
char board[n][m];
char currentPlayer = human;
setUp(board);
}
//===============================================================/
Bool equals5(char a, char b, char c , char d , char e)
{
//printf("\ninequals5");
return a == b && b == c && c == d && d == e && a != '’;
}
//================================================================///
char checkWinner(char board[5][5])
{ int openSpots = 0,row, column;
printf("\ncheckWinner\n");
//initialize nobody is the winner
char winner = ‘N’;
//horizontal
for(row=0 ; row<n ; row++)
{
if( equals5(board[row][0] ,board[row][1] ,board[row][2], board[row][3], board[row][4] ))
{
winner = board[row][0];
}
}
//vertical
for(column=0 ; column<n ; column++)
{
if( equals5( board[0][column], board[1][column], board[2][column], board[3][column], board[4][column] ))
{
winner = board[0][column];
}
}
// Diagonals
if (equals5(board[0][0], board[1][1], board[2][2], board[3][3], board[4][4]))
{
winner = board[0][0];
}
if (equals5(board[4][0], board[1][3], board[2][2], board[3][1], board[4][0]))
{
winner = board[4][0];
}
//check if board has space left for next move
for (row = 0; row < n; row++)
{
for (column = 0; column < m; column++)
{
if (board[row][column] == ‘_’)
{
openSpots++;
}
}
}
if (winner == 'N' && openSpots == 0)
{
return 'T';
}
else
{
return winner;
}
}
//======================================================================//
void showBoard(char board[m][n])
{
int row, column;
printf("\ninshowBoard\n");
for(row=0 ; row<m ; row++)
{ for(column=0 ; column<n ; column++)
{
printf("%c | “,board[row][column]);
}
printf(”\n");
}
}
///////////////////////////////////////////////////////////////////////////
int minimum(int a, int b)
{
// printf("\ninMin\n");
int min;
min = (a <= b) ? a : b;
return min;
}
int maximum(int a, int b)
{
//printf("\ninMax\n");
int max;
max = (a >= b) ? a : b;
return max;
}
//======================================================================//
void bestMove(char board[m][n])
{
printf("\ninbestMove\n");
//computer’s move
int bestScore = -1000;
int move[2];
int score;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
// Is the spot available?
if (board[i][j] == ‘’)
{
board[i][j] = ai;
score = minimax(board, false);
board[i][j] = '’;
printf("\n back in bestMove");
if (score > bestScore)
{
printf("\n in if of bestMove");
bestScore = score;
move[0] = i;
move[1] = j;
}
}
}
}
printf("\nThe best Value is : %d",bestScore);
board[move[0]][move[1]] = ai;
showBoard(board);
char result = checkWinner(board);
if (result == 'R')
{ printf("\n\nI(computer) is the winner now!!");
exit(1);
}
else if (result == 'B')
{ printf("\n\nYou are the winner of the game!!");
exit(1);
}
else if (result == 'T')
{ printf("\n\nIt's a tie!!");
exit(1);
}
currentPlayer = human ;
humanChance(board);
}
///////////////////////////////////////////////////////////////////////////////////////
int minimax(char board[m][n], _Bool isMaximizing)
{
printf("\ninMinimax\n");
int best;
char result = checkWinner(board);
if(result == ‘R’)
{ return 10; }
if(result == 'B')
{ return -10; }
if(result == 'T')
{ return 0; }
//for computer
if (isMaximizing)
{
printf("\nIn true of minimax");
int best = -1000;
// Traverse all cells
for (i = 0; i<m; i++)
{
for (j = 0; j<n; j++)
{
// Check if cell is empty
if (board[i][j]=='_')
{
// Make the move
board[i][j] = ai;
printf("\ntrue beforei = %d j = %d", i ,j);
showBoard(board);
// Call minimax recursively and choose
// the maximum value
best = maximum( best, minimax(board, !isMaximizing) );
printf("\ntrue after best = %d i = %d j = %d", best,i ,j);
printf("\nIn true of minimax: when i return back\n");
// Undo the move
board[i][j] = '_';
showBoard(board);
}
}
}
return best;
}
// If this minimizer's move
else
{
int best = 1000;
printf("\nIn false of minimax");
// Traverse all cells
for (i = 0; i<m; i++)
{
for (j = 0; j<n; j++)
{
// Check if cell is empty
if (board[i][j]=='_')
{
printf("\nfalse before i = %d j = %d", i ,j);
// Make the move
board[i][j] = human;
showBoard(board);
// Call minimax recursively and choose
// the minimum value
best = minimum(best, minimax(board,!isMaximizing));
printf("\nfalse after best = %d i = %d j = %d",best, i ,j);
printf("\nIn false of minimax: when i return back\n");
// Undo the move
board[i][j] = '_';
showBoard(board);
}
}
}
return best;
}
}
//======================================================================//
void humanChance(char board[5][5])
{
printf("\ninhumanChance\n");
int row , column;
printf(“Enter row: “);
scanf(”%d”,&row);
printf(“Enter column: “);
scanf(”%d”,&column);
if(board[row-1][column-1] == '_')
{
board[row-1][column-1] = human;
showBoard(board);
char result = checkWinner(board);
if (result == 'R')
{ printf("\n\nI(computer) is the winner now!!");
exit(1);
}
else if (result == 'B')
{ printf("\n\nYou are the winner of the game!!");
exit(1);
}
else if (result == 'T')
{ printf("\n\nIt's a tie!!");
exit(1);
}
currentPlayer = ai ;
bestMove(board);
}
else
{
printf("\nEnter appropriate options: ");
humanChance(board);
}
}
//===============================================================/////
void setUp(char mat[5][5])
{
printf("\ninsetUp\n");
char board[5][5] = {{’’, '’, ‘’, '’ ,’’},
{’’, ‘’, '’, ‘’ ,’’},
{’’, '’, ‘’, '’ ,’’},
{’’, ‘’, '’, ‘’ ,’’},
{’’, '’, ‘’, '’ ,’_’},
};
for(i=0 ; i<n ; i++)
{
for(j=0 ; j<m ; j++)
{
printf("%c",board[i][j]);
}
printf("\n");
}
humanChance(board);
}
/////////============================================================////////