Question
Please help me modify my program...here is the questions..I need to adjust what I have to have the second player be computer automated... Here is
Please help me modify my program...here is the questions..I need to adjust what I have to have the second player be computer automated...
Here is what I have so far...
#include
#include
int check_board(int x[][3]) { //this function checks board for winner or a tie
int winningLine;
//following line checks diagonal for winner
if ((x[0][0] == x[1][1] && x[0][0] == x[2][2]) || (x[0][2] == x[1][1] && x[0][2] == x[2][0])) {
return 1;
}
// following for loop checks all other scenarios for player to win
for (winningLine = 0; winningLine
// following if statement checks horizontal & vertical lines for winner
if ((x[winningLine][0] == x[winningLine][1] && x[winningLine][0] == x[winningLine][2]) || (x[0][winningLine] == x[1][winningLine] && x[0][winningLine] == x[2][winningLine])) {
return 1;
}
}
return 0;
}
void print_board(int x[][3]) { //Function that makes the board
/* following creates spacing for board */
printf(" ");
printf(" %c | %c | %c ", x[0][0], x[0][1], x[0][2]); //top row
printf("---|---|--- ");
printf(" %c | %c | %c ", x[1][0], x[1][1], x[1][2]); //middle row
printf("---|---|--- ");
printf(" %c | %c | %c ", x[2][0], x[2][1], x[2][2]); //bottom row
}
//int check_board(int x[][3]); //Function that tells which player won or if it is a tie
//void print_board(int x[][3]); //Function makes the board
int main(void) {
int userInput = 0;
int winner = 0;
int nextInput = 0;
int across = 0;
int down = 0;
int count;
char playerName[20];
/* following makes table easier for user to select coordinate */
int x[3][3] = { {' ',' ',' '}, {' ',' ',' '}, {' ',' ',' '} };
printf("Player 1 enter your name: ");
scanf("%s", playerName);
printf("%s,Let's play tic tac toe: ", playerName);
/* following for loop starts game..9 moves in a game... continues until winner is selected */
for (count = 0; count
print_board(x); // funtion call... print the board
userInput = count % 2 + 1; //Alternates between players 1 & 2
do {
printf(" Player %d enter an 'X': ", userInput);// (userInput == 1) ? 'X' : 'O'); //Prompts user to enter a location to move and alternates the players
scanf("%d", &nextInput);
across = --nextInput / 3; //
down = nextInput % 3; //
}
while (nextInput 9 || x[across][down] > '9'); //Conditon for do loop, when each user enters a move
x[across][down] = (userInput == 1) ? 'X' : 'O'; //Prints X's and O's at desired numerical spot
if (check_board(x)) { //Checks the board the find a winner
winner = userInput; //Winner selected
}
}
print_board(x); //Prints the board with results
if (winner == 0) { //No winner
printf("It's a tie! ");
}
else {
printf("Congratulations Player %d you have won! ", winner);
}
return 0;
}
1 Interactive Tic Tac Toe (25 points) Implement an interactive tic tac toe game where player 2 is automated by the computer. The game starts by printing an empty board. Each player takes turns and adds an 'X' or an 'O' to the appropriate location. The game ends when the users or the computer wins, or the board is full. Your code should employ at least the following two functions. int check-board(int x 3); returns 0 if player 1 wins, 1 if player 2 wins, or 2 if tie void print-board (int xMI31 prints the board on screen l); Sample execution. Player 1 enter your name: Nicholas Nicholas, Let's play tic tac toe: Player 1, enter an 'X' 1 1 The computer entered an 'O' at position: 2 2
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started