here's the codes of my flawed program..still there are errors..can you help me out??ive got sntax errors..i think somehow i got the idea how to do things but,syntactically,im lacking...T_T
#include
#include
char createTable(char x[3][3]) //function to fill the array with dashes and print the inital table.
{
int i=0, j=0;
while (i<3)
{
while (j<3)
{
x[i][j]='-';
printf("Welcome to Tic-Tac-Toe Extreme!\n");
printf("%c\n", x[i][j]);
}
}
return x[][];
}
void printTable(char x[3][3]) //function to print the new board after a player's turn
{
int i=0, j=0;
while (i<3)
{
while (j<3)
{
printf("%c\n", x[i][j]);
}
}
}
char player1(char x[3][3]) //function for player 1's turn
{
int i, j;
char position;
printf("Player 1, input position:\n");
scanf("%c\n", &position);
switch(position)
{ case 'A':
case 'a': i=1, j=1; break;
case 'B':
case 'b': i=1, j=2; break;
case 'C':
case 'c': i=1, j=3; break;
case 'D':
case 'd': i=2, j=1; break;
case 'E':
case 'e': i=2, j=2; break;
case 'F':
case 'f': i=2, j=3; break;
case 'G':
case 'g': i=3, j=1; break;
case 'H':
case 'h': i=3, j=2; break;
case 'I':
case 'i': i=3, j=3; break;
default : printf("error\n");
x[][] = player1(x[][]);
break;
}
if(x[i][j]=='-')
{
x[i][j]='O';
printTable(x[3][3]);
}
else printf("Please choose another cell.\n");
return x[][];
}
char player2(char x[3][3]) //function for player 2's turn
{
int i, j;
char position;
printf("Player 2, input position:\n");
scanf("%c",&position);
switch(position)
{ case 'A':
case 'a': i=1, j=1; break;
case 'B':
case 'b': i=1, j=2; break;
case 'C':
case 'c': i=1, j=3; break;
case 'D':
case 'd': i=2, j=1; break;
case 'E':
case 'e': i=2, j=2; break;
case 'F':
case 'f': i=2, j=3; break;
case 'G':
case 'g': i=3, j=1; break;
case 'H':
case 'h': i=3, j=2; break;
case 'I':
case 'i': i=3, j=3; break;
default : printf("error");
x[][] = player2(x[][]);
break;
}
if(x[i][j]=='-') //checks if there's still a cell to occupy
{
x[i][j]='X';
printTable(x[][]);
}
else printf("Please choose another cell.\n");
return x[][];
}
int checkWinner(char x[3][3], char symbol)
{
int win=0; //boolean is initially 0 or false
if(x[1][1]==symbol && x[1][2]==symbol && x[1][3]==symbol || /*check ABC*/
x[2][1]==symbol && x[2][2]==symbol && x[2][3]==symbol || /*check DEF*/
x[3][1]==symbol && x[3][2]==symbol && x[3][3]==symbol || /*check GHI*/
x[1][1]==symbol && x[2][1]==symbol && x[3][1]==symbol || /*check ADG*/
x[1][2]==symbol && x[2][2]==symbol && x[3][2]==symbol || /*check BEH*/
x[1][3]==symbol && x[2][3]==symbol && x[3][3]==symbol || /*check CFI*/
x[1][1]==symbol && x[2][2]==symbol && x[3][3]==symbol || /*check AEI*/
x[1][3]==symbol && x[2][2]==symbol && x[3][1]==symbol) /*check CEG*/
win=1; //boolean will become 1 or true if there is a winner
else win=0;
return win;
}
main()
{
char board[3][3]; //create variable for the board, an array with 3 rows and 2 columns
int countTurn=0; //counts the number of turns so far
int hasWinner=0; //tells if there is already a winner, 0 for no winner and 1 for has winner
char symbol;
board[][]=createTable(board[][]);
while(hasWinner!=1)
{
board[][]=player1(board[][]);
countTurn++;
if(countTurn>=4) //checking if there is a winner after every turn starting on the fourth turn
{
hasWinner=checkWinner(board[][], 'O');
}
else
{
board[][]=player2(board[][]);
countTurn++;
if(countTurn>=4)
{
hasWinner=checkWinner(board[][], 'X');
}
}
}
}
///////////*notes*/////////////
/*
a=1 1 b=1 2 c=1 3
d=2 1 e=2 2 f=2 3
g=3 1 h=3 2 i=3 3
possible positions of wins: abc, def, ghi, adg, beh, cfi, aei, ceg
*/