New Tic Tac Toe program. Only need help with int main function...
ran once but does not display gameboard and I'm 100% sure it's something to do with my int main. I really struggle with calling functions together..... (it's at very bottom)
Code:
#include <stdio.h>
void info()
{
printf("TIC-TAC-TOE\nWritten by MEEEE\n");
printf("Your Symbol 'x'");
printf("Computer Symbol 'o'");
}
/*initialize board*/
void init(char cell[])
{
int i;
for (i=0; i<9; i++) cell[i] = ' ';
}
/*print gameboard */
void gameBoard(char cell[])
{
printf("\n %c | %c | %c \n", cell[0], cell[1], cell[2]);
printf("---|---|---\n");
printf(" %c | %c | %c \n", cell[3], cell[4], cell[5]);
printf("---|---|---\n");
printf(" %c | %c | %c \n\n", cell[6], cell[7], cell[8]);
}
/*player moves*/
void userMove(char *cell)
{
int i = 0;
do {
printf("Your Move (0-8): ");
scanf("%d", &i);
}
// Here should also check if cell already filled
while ( i < 0 || i > 8 );
cell[i]='x';
}
void computerMove(char *cell)
{
// Here should play smart, for now just choose first empty cell
int i;
for (i=0;i!=9;i++)
{
if (cell[i] == ' ')
{
cell[i] = 'o';
return;
}
}
}
char checkWinner(char* cell)
{
//diagonals
if (cell[0] != ' ')
if ((cell[0] == cell[4]) && (cell[0] == cell[8]))
return cell[0];
if (cell[2] != ' ')
if ((cell[2] == cell[4]) && (cell[2] == cell[6]))
return cell[2];
// horizontals
if (cell[0] != ' ')
if ((cell[0] == cell[1]) && (cell[0] == cell[2]))
return cell[3];
if (cell[3] != ' ')
if ((cell[3] == cell[4]) && (cell[3] == cell[5]))
return cell[0];
if (cell[6] != ' ')
if ((cell[6] == cell[7]) && (cell[6] == cell[8]))
return cell[6];
// verticals
if (cell[0] != ' ')
if ((cell[0] == cell[3]) && (cell[0] == cell[6]))
return cell[0];
if (cell[1] != ' ')
if ((cell[1] == cell[4]) && (cell[1] == cell[7]))
return cell[1];
if (cell[2] != ' ')
if ((cell[2] == cell[5]) && (cell[2] == cell[8]))
return cell[2];
return ' ';
}
bool_gameIsOver(char *cell)
{
char winner;
winner = checkWinner(cell);
if (winner=='o')
printf("Computer win !\n");
else
if (winner=='x')
printf("You win !\n");
return winner != ' ';
}
int main()
{
int false;
int bool_continue;
int _continue;
char cell[9];
bool_continue;
info();
init(cell);
_continue = true;
while(_continue) {
computerMove(cell);
gameBoard(cell);
if (gameIsOver(cell))
_continue = false;
else
{
userMove(cell);
gameBoard(cell);
if (gameIsOver(cell))
_continue = false;
}
}
return 0;
}
well that is what I was originally going to put but you have totally disregarded....
the end game function....now how exactly will I implement it into the int main. I really need help because I am so stummped... suggetions really can't help me here... :(I have all the functions...ok.....but now I need help constructing the int main...which I really have no clue to begin with...someone help me.
ok so now I have included checkWinner, but I am calling the function wrong... how do
I call the function so it performs checkwinner?
Code:
#include <stdio.h>
#include <stdlib.h>
void info()
{
printf("TIC-TAC-TOE\nWritten by MEEEE\n");
printf("Your Symbol 'x'");
printf("Computer Symbol 'o'");
}
/*initialize board*/
void init(char cell[])
{
int i;
for (i=0; i<9; i++)
cell[i] = ' ';
}
/*print gameboard */
void gameBoard(char cell[])
{
printf("\n %c | %c | %c \n", cell[0], cell[1], cell[2]);
printf("---|---|---\n");
printf(" %c | %c | %c \n", cell[3], cell[4], cell[5]);
printf("---|---|---\n");
printf(" %c | %c | %c \n\n", cell[6], cell[7], cell[8]);
}
/*player moves*/
void userMove(char cell[])
{
int i = 0;
int cont = 1;
do{
printf("Your Move (0-8): ");
scanf("%d", &i);
if((cell[i] != ' ') || (i < 0 || i > 8))
{
printf("Invalid Input\n"); //invalid input
}
else
{
cell[i] = 'x';
cont--; //set while loop to false, break
}
}
while(cont);
}
/*computer moves*/
void computerMove(char cell[])
{
int i = 0;
int cont = 1;
do
{
if(cell[i] == ' ')
{
cell[i] = 'o';
cont--;
}
else
{
i++;
}
}while(cont);
}
char checkWinner(char cell[])
{
//diagonals
if (cell[0] != ' ')
if ((cell[0] == cell[4]) && (cell[0] == cell[8]))
return cell[0];
if (cell[2] != ' ')
if ((cell[2] == cell[4]) && (cell[2] == cell[6]))
return cell[2];
// horizontals
if (cell[0] != ' ')
if ((cell[0] == cell[1]) && (cell[0] == cell[2]))
return cell[3];
if (cell[3] != ' ')
if ((cell[3] == cell[4]) && (cell[3] == cell[5]))
return cell[0];
if (cell[6] != ' ')
if ((cell[6] == cell[7]) && (cell[6] == cell[8]))
return cell[6];
// verticals
if (cell[0] != ' ')
if ((cell[0] == cell[3]) && (cell[0] == cell[6]))
return cell[0];
if (cell[1] != ' ')
if ((cell[1] == cell[4]) && (cell[1] == cell[7]))
return cell[1];
if (cell[2] != ' ')
if ((cell[2] == cell[5]) && (cell[2] == cell[8]))
return cell[2];
return 'o';
return 'x';
char winner;
winner = checkWinner(cell);
if (winner=='o')
printf("Computer win !\n");
else
if (winner=='x')
printf("You win !\n");
return winner != ' ';
}
int main()
{
char cell[9];
int cont = 1;
int ccode = 0;
info();
init(cell);
gameBoard(cell);
while(cont)
{
userMove(cell);
gameBoard(cell);
//check if user wins
//if user wins, print user won
//and cont-- to exit loop
computerMove(cell);
gameBoard(cell);
checkWinner(cell);
//check if computer wins, print computer won
//and cont-- to exit loop
//if nobody won get another user input
}
return 0;
}
you mean something like this?
Code:
if (cell[0] != ' ')
{
if ((cell[0] == cell[1]) && (cell[0] == cell[2]))
return cell[3];
}
if (cell[3] != ' ')
{if ((cell[3] == cell[4]) && (cell[3] == cell[5]))
return cell[0];
}
if (cell[6] != ' ')
{
if ((cell[6] == cell[7]) && (cell[6] == cell[8]))
return cell[6];
}
Ok ok, It compiles the same exact way even without the curly brackets...try
Code:
#include <stdio.h>
#include <stdlib.h>
void info()
{
printf("TIC-TAC-TOE\nWritten by MEEEE\n");
printf("Your Symbol 'x'");
printf("Computer Symbol 'o'");
}
/*initialize board*/
void init(char cell[])
{
int i;
for (i=0; i<9; i++)
cell[i] = ' ';
}
/*print gameboard */
void gameBoard(char cell[])
{
printf("\n %c | %c | %c \n", cell[0], cell[1], cell[2]);
printf("---|---|---\n");
printf(" %c | %c | %c \n", cell[3], cell[4], cell[5]);
printf("---|---|---\n");
printf(" %c | %c | %c \n\n", cell[6], cell[7], cell[8]);
}
/*player moves*/
void userMove(char cell[])
{
int i = 0;
int cont = 1;
do{
printf("Your Move (0-8): ");
scanf("%d", &i);
if((cell[i] != ' ') || (i < 0 || i > 8))
{
printf("Invalid Input\n"); //invalid input
}
else
{
cell[i] = 'x';
cont--; //set while loop to false, break
}
}
while(cont);
}
/*computer moves*/
void computerMove(char cell[])
{
int i = 0;
int cont = 1;
do
{
if(cell[i] == ' ')
{
cell[i] = 'o';
cont--;
}
else
{
i++;
}
}while(cont);
}
char checkWinner(char cell[])
{
//diagonals
if (cell[0] != ' ')
{
if ((cell[0] == cell[4]) && (cell[0] == cell[8]))
return cell[0];
}
if (cell[2] != ' ')
{
if ((cell[2] == cell[4]) && (cell[2] == cell[6]))
return cell[2];
}
// horizontals
if (cell[0] != ' ')
{
if ((cell[0] == cell[1]) && (cell[0] == cell[2]))
return cell[3];
}
if (cell[3] != ' ')
{
if ((cell[3] == cell[4]) && (cell[3] == cell[5]))
return cell[0];
}
if (cell[6] != ' ')
{
if ((cell[6] == cell[7]) && (cell[6] == cell[8]))
return cell[6];
}
// verticals
if (cell[0] != ' ')
{
if ((cell[0] == cell[3]) && (cell[0] == cell[6]))
return cell[0];
}
if (cell[1] != ' ')
{
if ((cell[1] == cell[4]) && (cell[1] == cell[7]))
return cell[1];
}
if (cell[2] != ' ')
{
if ((cell[2] == cell[5]) && (cell[2] == cell[8]))
return cell[2];
}
return 'o';
return 'x';
char winner;
winner = checkWinner(cell);
if (winner=='o')
printf("Computer win !\n");
else
if (winner=='x')
printf("You win !\n");
return winner != ' ';
}
int main()
{
char cell[9];
int cont = 1;
int ccode = 0;
info();
init(cell);
gameBoard(cell);
while(cont)
{
userMove(cell);
gameBoard(cell);
//check if user wins
//if user wins, print user won
//and cont-- to exit loop
computerMove(cell);
gameBoard(cell);
checkWinner(cell);
//check if computer wins, print computer won
//and cont-- to exit loop
//if nobody won get another user input
}
return 0;
}