I have a problem with the error "cannot convert from 'int' to char[][] ". I check it for many times but I do not know how to fix. I just start with C. Could you help me, please?
The error in the lines 23, 25, 28, 32, 64. I would like to design a Connect 4 game with 7 columns and 6 rows. Thanks!!
# include <stdio.h>
# define MAX 10
void welcome();
void initGrid(char[6][7]);
void drawGrid(char[6][7]);
int promptPlayer(int);
int checkSetValue(char[6][7], int);
int setGrid(char [6][7], int, int);
int checkThreeInRow(char[6][7], int, int);
int checkFull(char[6][7]);
void win(int);
void draw();
int main(int argc, char *argv[])
{
int gameOver = 0;
int currentPlayer = 1;
int gridWidth=7;
int gridHeight=6;
//int MAX1=6;
//int MAX=100;
char connect4grid[MAX][MAX]=connect4grid[gridHeight][gridWidth];
welcome();
initGrid(connect4grid);
while(gameOver !=1)
{
drawGrid(connect4grid);
int enteredValue;
int heightValue;
enteredValue = promptPlayer(currentPlayer);
if(checkSetValue(connect4grid,enteredValue)==0)
{
printf("that space is already taken! try again, please! \n");
continue;
}
else
{
heightValue=setGrid(connect4grid, currentPlayer, enteredValue);
}
if(checkThreeInRow(connect4grid, enteredValue, heightValue) !=0)
{
gameOver =1;
drawGrid(connect4grid);
win(currentPlayer);
}
else if(checkFull(connect4grid)==1)
{
gameOver=1;
drawGrid(connect4grid);
draw();
}
if(currentPlayer ==1)
currentPlayer =2;
else
currentPlayer =1;
}
printf("\n Goodbye!\n");
return 0;
}
void welcome()
{
printf("\n\n Welcome to Connect 4 \n");
}
void initGrid(char connectgridarray[6][7])
{
for(int i=0; i<=6; i++)
{
for( int j = 0; j <= 7;j++)
{
connectgridarray[j][i]= ' ';
}
}
}
void drawGrid(char connectgridarray[6][7])
{
printf("\n\t");
int i=0;
for(i=1;i<=8;i++)
{
printf("| %d |", i);
}
printf("\n\t");
for(i=0;i<=6;i++)
{
for(int j=0;j<=7;j++)
{
printf(" | %c |", connectgridarray[j][i]);
}
printf("\n\t");
}
printf("\n");
}
int promptPlayer(int player)
{
int scannedNumber = 0;
int wrong = 0;
while( scannedNumber < 1|| scannedNumber > 8)
{
if(0 == wrong)
printf("Player %i, enter a grid number still available [1-7]: ", player);
else
printf("Invalid number, player %i, enter a grid number [1-7]", player);
scanf("%d", &scannedNumber);
wrong =1;
}
return scannedNumber;
}
int checkSetValue(char connectgridarray[6][7], int column)
{
column -= 1;
if(connectgridarray[column][0] ==' ')
return 1;
return 0;
}
int setGrid(char connectgridarray[6][7], int currentPlayer, int column)
{
column -= 1;
for(int i =5; i>=0; i--)
if(connectgridarray[column][i]== ' ')
{
if(currentPlayer ==1)
{ connectgridarray[column][i] = 'X';
}
else
{
connectgridarray[column][i] = 'O';
}
return i;
}
return 0;
}
int checkThreeInRow(char connectgridarray[6][7], int column, int height)
{
column -= 1;
char characterToCheck = connectgridarray[column][height];
int connect4 = 1;
connect4 =1;
int i=0;
for(i=1; i<=3 && height+i<=6; i++)
{
if(connectgridarray[column][height+i]==characterToCheck)
connect4++;
}
if(3==connect4)
return 1;
//check right and left
connect4 =1;
for(i=1; i<=3 && column+i <=7; i++)
{
if(connectgridarray[column+i][height] == characterToCheck)
connect4++;
}
for(i=1; i<=3 && column-i >=0; i++)
{ if(connectgridarray[column-i][height] == characterToCheck)
connect4++;
}
if(3 == connect4)
return 1;
//check up/left diagonal and down/right diagonal
connect4 =1;
for(i =1; i<=3 && height+i <=5 && column+i >=0; i++)
{
if(connectgridarray[column-i][height-i]==characterToCheck)
connect4++;
}
if(connect4==4)
return 1;
//check up/right diagonal and down/left diagonal
connect4 =1;
for(i=1; i<=2 && height-i>=0 && column+i <=6; i++)
{
if(connectgridarray[column+i][height-i]==characterToCheck)
connect4++;
}
for(i=1;i<=2 && height+i<=6 && column-i>=0;i++)
{
if(connectgridarray[column-i][height+i]== characterToCheck)
connect4++;
}
if(connect4==3)
return 1;
return 0;
}
int checkFull(char connectgridarray[6][7])
{
int full =0;
int counter = 0;
for(int i=0; i<=6; i++)
{
for(int j =0; j<=7; j++)
{
if(connectgridarray[j][i] != ' ')
{
counter++;
}
}
}
if(7*8 == counter)
full =1;
return full;
}
void win(int player)
{
printf("\n\tCongratulations Player %i, you WIN !\n", player);
}
void draw()
{
printf("\n\tA stalemate.....you have DRAWN!\n");
}