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;
int choice;
char full[9] ={0};
char X,O;
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]= 'X';
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);
switch(choice-1){
case 8:if(board[8]=='X');
printf("That space is taken,choose anohter");
break;
case 7: if(board[7]=='X');
printf("That space is taken,choose another");
break;
case 6:if(board[6]=='X');
printf("That space is taken,choose another");
break;
case 5:if(board[6]=='X');
printf("That space is taken,choose another");
break;
case 4:if(board[6]=='X');
printf("That space is taken,choose another");
break;
case 3:if(board[6]=='X');
printf("That space is taken,choose another");
break;
case 2:if(board[6]=='X');
printf("That space is taken,choose another");
break;
case 1:if(board[6]=='X');
printf("That space is taken,choose another");
break;
case 0:if(board[6]=='X');
printf("That space is taken,choose another");
break;
}
}
return 0;
}