Hi, This an odd problem, I am making a connect 5 game with a 15x15 array but after a player enters a row and column the board doesn't print [13][14] or [14][0-6] for some reason. Run this code and you will see. The initial printBoard() works fine but after 1 turn this happens, I can't figure out the problem. Thanks
Code:
#include <stdio.h>
void printBoard();
void initBoard();
void addStone();
char board[14][14];
int row, col;
int turn = 1; //1 = player 1 turn, 2 = player 2 turn
int main()
{
initBoard();
while(1)
{
printBoard();
addStone();
}
return(0);
}
void addStone()
{
if(turn == 1)
printf("%s", "\nPlayer 1: ");
else
printf("%s", "\nPlayer 2: ");
scanf("%d %d", &row, &col);
if(row == 99 && col == 99) //To end game early enter 99 99
exit(0);
if(board[row][col] == '-' && row < 15 && row >= 0 && col < 15 && col >= 0)
if(turn == 1)
{
board[row][col] = 'O';
turn = 2;
}
else
{
board[row][col] = 'X';
turn = 1;
}
else
printf("%s", "Invalid space, go again.\n");
}
void initBoard()
{
int i,j;
for(i=0; i<15; i++)
for(j=0; j<15; j++)
board[i][j] = '-';
}
void printBoard()
{
int i,j;
int rowColNumbers[15] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4};
printf("%s", " "); //Apply one space to align numbers correctly
for(i=0; i<15; i++)
printf("%d", rowColNumbers[i]);
for(i=0; i<15; i++)
{
printf("%s%d", "\n", rowColNumbers[i]);
for(j =0; j<15; j++)
printf("%c", board[i][j]);
}
}