Tic-tac-toe implementation problems
Here is my problem I'm supposed to write a tic-tac-toe game.By the way this is the biggest program I've ever attempted to write. When I started writing the game I thought that it wouldn't be too difficult to do-boy was I wrong. The specs for the game are that a human plays the computer (random number generator). The human player chooses to be X or O,X moves first. Right now all the game does is prompt the user to choose X or O. After that is done I can play as X or O only. I can fill the game board with X's or O's (run the code thus far to see what I'm talking about). This is what I need to add to the game.
- A way to store a previous move choice and not allow a move to be over written by a subsequent move
- A call to the random number generator to get the computer's move
As for Item one I think that I need to use two arrays to solve this problem:
One is the board[] array that is the actual game board and I think that the other array I need is a copy of the board[] array to store move choices. Lets say I name this array- full[]. The fulll[] array holds the actual move choices. I think that I need to continually search the full array (using a while loop) to find move choices and to prompt the user that they can't pick that spot. For example if the human player chooses number 5 and puts an X in there the computer can not put an O in the same place and the human player cant put another X in space 5. As for problem two I just need to write a function to call a random number generator. Someone point me in the right direction. I don't want anyone to do it for me,but I do need some help. Here is my code to this point:
Code:
#include<stdio.h>
int getXorO(void);
void drawBoard(void);
int human(int x,int board[]);
int computer(int o,int board[]);
int play(int x,char board[]);
int main(void){
int initialChoice;
char board[9]= {'1','2','3','4','5','6','7','8','9'};
int humanMove;
int computerMove;
initialChoice = getXorO();
drawBoard();
play(initialChoice,board);
return 0;
}
int getXorO(void){//player decides to be X or O here and returns the initialChoice to main
int initialChoice;
printf("Let's play TIC-TAC-TOE!\n\n");
printf("Do you want to be X or O (X moves first)? ");
printf("\nEnter 1 for X and 2 for O: ");
// scanf("%d",&initialChoice);
if(scanf("%i",&initialChoice)!=1){
printf("\nInvalid input,exiting game \n");
exit(1);
}
while(initialChoice!=1 && initialChoice!=2){
printf("Do you want to be X or O (X moves first)? \n");
printf("Enter 1 for X and 2 for O:");
if(scanf("%i",&initialChoice)!=1){
printf("\nInvalid input,exiting game\n");
exit(1);
}
}
return initialChoice;
}//getMove returns initialChoice to be X or O
void drawBoard(void){
char board[9]= {'1','2','3','4','5','6','7','8','9'};
printf("\n");
printf("\t %c | %c | %c \n",board[0],board[1],board[2]);
printf("\t---|---|--- \n");
printf("\t %c | %c | %c \n",board[3],board[4],board[5]);
printf("\t---|---|--- \n");
printf("\t %c | %c | %c \n\n",board[6],board[7],board[8]);
return;
}
int play(int initialChoice,char board[]){
int i;
int humanMove;
int computerMove;
char X;
int choice;
char full[9] = {'1','2','3','4','5','6','7','8','9'};
if(initialChoice==1)
initialChoice = 88;//ascii for X/set player as X
else
initialChoice = 79;//ascii for O/set player as O
printf("Enter the number of an available space you are,%c: ",initialChoice);
scanf("%d",&choice);
for(i=0;i<9;i++){
board[choice-1]= 88;
while(board[choice]==88)
printf("\nError");
printf("\n");
printf("\t %c | %c | %c \n",board[0],board[1],board[2]);
printf("\t---|---|--- \n");
printf("\t %c | %c | %c \n",board[3],board[4],board[5]);
printf("\t---|---|--- \n");
printf("\t %c | %c | %c \n\n",board[6],board[7],board[8]);
printf("Enter the number of an available space you are,%c: "
"",initialChoice);
//should call the computer here
scanf("%d",&choice);
}
return 0;
}