I made a 10×10 tic-tac-toe program and the winner is the player who has 5 marked in a row,column and diagonals. When I put ‘X’ or ‘O’ in the first column, which is 0, or the last column, which is 9, for example: 8 0, 9 0, 0 9. There will be a second mark on the other column which are 8 0->7 9, 9 0->8 9, 0 9->1 0.
0 1 2 3 4 5 6 7 8 9
|---|---|---|---|---|---|---|---|---|---|
0| | | | | | | | | | X |
|---|---|---|---|---|---|---|---|---|---|
1| X | | | | | | | | | |
|---|---|---|---|---|---|---|---|---|---|
2| | | | | | | | | | |
|---|---|---|---|---|---|---|---|---|---|
3| | | | | | | | | | |
|---|---|---|---|---|---|---|---|---|---|
4| | | | | | | | | | |
|---|---|---|---|---|---|---|---|---|---|
5| | | | | | | | | | |
|---|---|---|---|---|---|---|---|---|---|
6| | | | | | | | | | |
|---|---|---|---|---|---|---|---|---|---|
7| | | | | | | | | | X |
|---|---|---|---|---|---|---|---|---|---|
8| X | | | | | | | | | O |
|---|---|---|---|---|---|---|---|---|---|
9| O | | | | | | | | | |
|---|---|---|---|---|---|---|---|---|---|
Here is the code:
#include <stdio.h>
#include <time.h>
char board[9][9];
const char PLAYER ='X';
const char PLAYER2 = 'O';
const char BOT ='O';
void resetBoard(){
int i,j;
for(i=0;i<10;i++){
for(j=0;j<10;j++){
board[i][j]=' ';
}
}
}
printBoard(){
printf(" 0 1 2 3 4 5 6 7 8 9");
printf("\n |---|---|---|---|---|---|---|---|---|---|");
printf("\n0| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |",board[0][0],board[0][1],board[0][2],board[0][3],board[0][4],board[0][5],board[0][6],board[0][7],board[0][8],board[0][9]);
printf("\n |---|---|---|---|---|---|---|---|---|---|");
printf("\n1| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |",board[1][0],board[1][1],board[1][2],board[1][3],board[1][4],board[1][5],board[1][6],board[1][7],board[1][8],board[1][9]);
printf("\n |---|---|---|---|---|---|---|---|---|---|");
printf("\n2| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |",board[2][0],board[2][1],board[2][2],board[2][3],board[2][4],board[2][5],board[2][6],board[2][7],board[2][8],board[2][9]);
printf("\n |---|---|---|---|---|---|---|---|---|---|");
printf("\n3| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |",board[3][0],board[3][1],board[3][2],board[3][3],board[3][4],board[3][5],board[3][6],board[3][7],board[3][8],board[3][9]);
printf("\n |---|---|---|---|---|---|---|---|---|---|");
printf("\n4| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |",board[4][0],board[4][1],board[4][2],board[4][3],board[4][4],board[4][5],board[4][6],board[4][7],board[4][8],board[4][9]);
printf("\n |---|---|---|---|---|---|---|---|---|---|");
printf("\n5| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |",board[5][0],board[5][1],board[5][2],board[5][3],board[5][4],board[5][5],board[5][6],board[5][7],board[5][8],board[5][9]);
printf("\n |---|---|---|---|---|---|---|---|---|---|");
printf("\n6| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |",board[6][0],board[6][1],board[6][2],board[6][3],board[6][4],board[6][5],board[6][6],board[6][7],board[6][8],board[6][9]);
printf("\n |---|---|---|---|---|---|---|---|---|---|");
printf("\n7| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |",board[7][0],board[7][1],board[7][2],board[7][3],board[7][4],board[7][5],board[7][6],board[7][7],board[7][8],board[7][9]);
printf("\n |---|---|---|---|---|---|---|---|---|---|");
printf("\n8| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |",board[8][0],board[8][1],board[8][2],board[8][3],board[8][4],board[8][5],board[8][6],board[8][7],board[8][8],board[8][9]);
printf("\n |---|---|---|---|---|---|---|---|---|---|");
printf("\n9| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |",board[9][0],board[9][1],board[9][2],board[9][3],board[9][4],board[9][5],board[9][6],board[9][7],board[9][8],board[9][9]);
printf("\n |---|---|---|---|---|---|---|---|---|---|");
}
void printWinner(char winner){
if (winner==PLAYER){
printf("\nYou win!");
}
else if(winner==BOT){
printf("\nYou lose!");
}
else printf("\nTie!");
}
void printWinnerHuman(char winner){
if (winner==PLAYER){
printf("\nPlayer 1 wins!");
}
else if(winner==PLAYER2){
printf("\nPlayer 2 wins!");
}
else printf("\nTie!");
}
void playerMove(){
int x,y;
do{
printf("\nYou are X!");
printf("\nEnter move (Ex: 0 2): ");
scanf("%d %d",&x,&y);
if (board[x][y]!=' '){
printf("\n\n\n\n\n\n\nSpace already taken\n");
printf("\nTry another move!\n");
printBoard();
}
else {
board[x][y]=PLAYER;
break;
}
}
while(board[x][y]!=' ');
}
void player2Move(){
int x,y;
do{
printf("\n\nYou are O!");
printf("\nEnter your move (Ex: 1 2): ");
scanf("%d %d",&x,&y);
if(board[x][y]!=' '){
printf("\nSpace already taken!\n");
printBoard();
}
else{
board[x][y]=PLAYER2;
break;
}
}
while(board[x][y]!=' ');
}
void botMove(){
//create random moves for bot
srand(time(0));
int x,y;
if(checkFreeSpaces()>0){
do{
x=rand()%9;
y=rand()%9;
}
while(board[x][y]!=' ');
board[x][y]=BOT;
}
else printWinner(' ');
}
char checkWinner(){
int i,j;
int found=0;
int count=0;
//check rows winner
for(i=0;i<10;i++){
if(count<4){
count=0;
for(j=0;j<10;j++){
if (board[i][j]==board[i][j+1]&&board[i][j]!=' '){
count++;
if(count==4){
return board[i][j];
}
}
}
}
}
//check columns winner
for(i=0;i<10;i++){
if(count<4){
count=0;
for(j=0;j<10;j++){
if (board[j][i]==board[j+1][i]&&board[j][i]!=' '){
count++;
if(count==4){
return board[j][i];
}
}
}
}
}
//check all diagonals above MAIN diagonals(\*)
int row;
int column;
for(i=5;i>=0;i--){
column=i;
row=0;
count=0;
for(j=0;j<10;j++){
if(board[row][column]==board[row+1][column+1]&&board[row][column]!=' '){
row++;
column++;
count++;
if(count==4){
return board[row][column];
}
else if(board[row][column]==board[row+1][column+1]&&board[row][column]==' '){
row++;
column++;
}
}
}
}
//check all diagonals above SECOND diagonal(*/)
for(i=4;i<10;i++){
column=i;
row=0;
count=0;
for(j=0;j<10;j++){
if(board[row][column]==board[row+1][column-1]&&board[row][column]!=' '){
row++;
column--;
count++;
if(count==4){
return board[row][column];
}
else if(board[row][column]==board[row+1][column-1]&&board[row][column]==' '){
row++;
column--;
}
}
}
}
//check all diagonals below MAIN diagonal(*\)
for(i=1;i<10;i++){
row=i;
column=0;
count=0;
for(j=0;j<10;j++){
if(board[row][column]==board[row+1][column+1]&&board[row][column]!=' '){
row++;
column++;
count++;
if(count==4){
return board[row][column];
}
else if(board[row][column]==board[row+1][column+1]&&board[row][column]==' '){
row++;
column++;
}
}
}
}
//check all diagonals below SECOND diagonal(\*)
for(i=1;i<10;i++){
row=i;
column=9;
count=0;
for(j=0;j<10;j++){
if(board[row][column]==board[row+1][column-1]&&board[row][column]!=' '){
row++;
column--;
count++;
if(count==4){
return board[row][column];
}
else if(board[row][column]==board[row+1][column-1]&&board[row][column]==' '){
row++;
column--;
}
}
}
}
return ' '; //no winners
}
int checkFreeSpaces(){
int i,j;
int freeSpaces=100;
for(i=0;i<10;++i){
for(j=0;j<10;++j){
if (board[i][j]!=' '){
freeSpaces--;
}
}
}
return freeSpaces;
}
void humanPlay(){
resetBoard(); //reset default declaration of computer
char winner =' ';
while(winner == ' ' &&checkFreeSpaces()!=0){ //while there is no winner and there are still spaces
printBoard();
playerMove();
winner=checkWinner(); //after every move of player, program checks if there is a winner and if there are spaces left.
if(winner!=' ' || checkFreeSpaces()==0){
break;
}
printBoard();
player2Move();
winner=checkWinner();
if(winner!=' '||checkFreeSpaces()==0){ //if there is no spaces left and no winner, break loop.
break;
}
}
printBoard();
printWinnerHuman(winner);
}
void botPlay(){
resetBoard(); //reset default declarartion of computer
char winner =' ';
while(winner == ' ' &&checkFreeSpaces()!=0){ //while there is no winner and there are still spaces
printBoard();
playerMove();
winner=checkWinner(); //after every move of player, program checks if there is a winner and if there are spaces left.
if(winner!=' ' || checkFreeSpaces()==0){
break;
}
botMove();
winner=checkWinner();
if(winner!=' ' || checkFreeSpaces()==0){
break;
}
}
printBoard();
printWinner(winner);
}
//main
int main(){
humanPlay();
return 0;
}
I tried looking again at the printBoard() function to check if i misstyped any matrix position but i didn’t see any
>Solution :
You have to specify not the maximum index but the number of elements when you declare arrays in C.
The available indice for the array char board[9][9];
is 0 to 8 for each dimension.
Allocate enough elements like char board[10][10];
to fix the error.