Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can anyone help me make this tic tac toe default x to the user and O to the comp

ID: 3581504 • Letter: C

Question

can anyone help me make this tic tac toe default x to the user and O to the computer ?

the way its made right now, it asks the user which character they want..

the first output should be :

Hello, I am the computer you will be playing.

. . .

. . .

. . .

Where would you like to play?

Enter the row:

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
struct tic_tac_toe
{
char board[3][3];
char player1;
char cpu;
};
/*********************************************************************
** Function: initalize_board
** Description: Fills the 9 spots on the tic-tac-toe board with periods.
** Parameters: char[3][3]
** Pre-Conditions: Board was created in main.
** Post-Conditions: Board is initalized with periods.
*********************************************************************/
// parameters char[3][3]
void initalize_board (char board[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
//fill 9 spots with dots
board[i][j] = '.';
}
}
}
/*********************************************************************
** Function: print_board
** Description: Prints out the tic-tac-toe board.
** Parameters: char[3][3]
** Pre-Conditions: None.
** Post-Conditions: None.
*********************************************************************/
//print_board function
void print_board(char board[3][3]) {
for (int i = 0; i < 3; i++) {
cout << endl;
for (int j = 0; j < 3; j++) {
cout << " ";
//prints out the tic tac toe board
cout << board[i][j];
}
}
cout << endl << endl;
}
/*********************************************************************
** Function: check_win
** Description: Checks for three in a row in all directions.
** Parameters: char[3][3]
** Pre-Conditions: None.
** Post-Conditions: Returns the character that that the three in a row consists of.
*********************************************************************/
char check_win(char board[3][3]) {
  
// Check horizontal, vertical & diagonal through [0][0]
if (board[0][0] != '.' && (board[0][0] == board[0][1] && board[0][0] == board[0][2] ||
board[0][0] == board[1][0] && board[0][0] == board[2][0] ||
board[0][0] == board[1][1] && board[0][0] == board[2][2]))
  
return board[0][0];
  
// Check horizontal, vertical & diagonal through [1][1]
if (board[1][1] != '.' && (board[1][1] == board[1][0] && board[1][1] == board[1][2] ||
board[1][1] == board[0][1] && board[1][1] == board[2][1] ||
board[1][1] == board[2][0] && board[1][1] == board[0][2]))
  
return board[1][1];
  
// Check horizontal, vertical & diagonal through [2][2]
if (board[2][2] != '.' && (board[2][2] == board[0][2] && board[2][2] == board[1][2] ||
board[2][2] == board[2][0] && board[2][2] == board[2][1]))
  
return board[2][2];
  
return 0;
}
int negamax(char board[3][3], char player1, char player2); //player 2

//the pick_best_move is to find the best possible move
//char[3][3]
int pick_best_move(char board[3][3], char player1, char player2) {
int best_move_score = -9999;
int best_move_row = -9999;
int best_move_col = -9999;
int score_for_this_move = 0;
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
if (board[r][c] == '.') {
board[r][c] = player1; //Try test move.
score_for_this_move = -(negamax(board, player2, player1));
board[r][c] = '.'; //Put back test move.
if (score_for_this_move >= best_move_score) {
best_move_score = score_for_this_move;
best_move_row = r;
best_move_col = c;
}
}
}
}
return (10*best_move_row + best_move_col); //best move ??
}

int negamax(char board[3][3], char player1, char player2) {
int best_move_score = -9999;
int score_for_this_move = 0;
  
//If player 1 wins, then the score is high (good for player1)
if (check_win(board) == player1)
return 1000;
  
//If player 2 loses, then the score is low (bad for player1)
else if (check_win(board) == player2)
return -1000;
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
if (board[r][c] == '.') {
board[r][c] = player1; //Try test move.
score_for_this_move = -(negamax(board, player2, player1));
board[r][c] = '.'; //Put back test move.
if (score_for_this_move >= best_move_score) {
best_move_score = score_for_this_move;
}
}
}
}
  
if (best_move_score == -9999 || best_move_score == 0)
return 0;
  
else if (best_move_score < 0)
return best_move_score + 1;
else if (best_move_score > 0)
return best_move_score - 1; //As the game goes longer, and the recursion goes deeper, the moves near the end are less favorable than in the beginning.
  
}

//player_placement asks the user for a row and column and places their character in that spot
void player_placement(char board[3][3], char player) {
  
while (1) {
string string_row, string_col;
int row = 0, col = 0;
while (1) {
cout << "Where would you like to play? " << endl << "Enter the row: ";
cin >> string_row;
row = atoi(string_row.c_str());
if (row >= 1 && row <= 3)
break;
cout << "You need to enter a row on the board (between 1 and 3, inclusive)." << endl;
}
while (1) {
cout << "Enter the column: ";
cin >> string_col;
col = atoi(string_col.c_str());
if (col >= 1 && col <= 3)
break;
cout << "You need to enter a column on the board (between 1 and 3, inclusive)." << endl;
}
if (board[row-1][col-1] == '.') {
board[row-1][col-1] = player;
break;
}
else
cout << "Someone already played there." << endl << endl;
}
}

//this funtion determins the computer's choice
//it also decides whether the computer is playing x's or o's
char determine_cpu_choice (char player1) {
char cpu_char;
cout << "Hello, I am the computer you will be playing." << endl;
if (player1 == 'x' || player1 == 'X')
cpu_char = 'o';
else
cpu_char = 'x';
return cpu_char;
}
/*********************************************************************
** Function: determine_player_choice
** Description: Asks the player to select a character.
** Parameters: string
** Pre-Conditions: Variable in main to store user's choice.
** Post-Conditions: Player's character will be selected.
*********************************************************************/

char determine_player_choice (string s) {
  
while (1) {
string choice;
cout << s << ": What would you like your character to be? ";
cin >> choice;
if (choice.size() > 1) {
cout << "You inputted more than one character. Please try again." << endl;
continue;
}
cout << endl;
return choice[0];
}
}
// this program only allowes player 1 so only against the computer
void play_game_1_player (char board[3][3], char player1, char cpu) {
  
int moves = 0;
while (moves < 9) {
player_placement(board, player1);
moves++;
print_board(board);
if (check_win(board)) {
cout << player1 << " won!" <<endl;

}
if (moves == 9)
break;
int where_to_move = pick_best_move(board, cpu, player1);
int row = where_to_move / 10;
int col = where_to_move % 10;
board[row][col] = cpu;
moves++;
print_board(board);
if (check_win(board)) {
cout << cpu << " won!" <<endl;
cout << "If I were you, I would've played at: " << "(row " << where_to_move/10 + 1 << ", col " << where_to_move % 10 + 1 << ")" << endl;
}
}
cout << "you guys are even!" << endl;
}

int main()
{
tic_tac_toe game;
initalize_board(game.board);
game.player1 = determine_player_choice("Player 1");
game.cpu = determine_cpu_choice(game.player1);
print_board(game.board);
play_game_1_player(game.board, game.player1, game.cpu);
return 0;
}

Explanation / Answer

just replace third line in main() function from

game.player1 = determine_player_choice("Player 1");

to
game.player1 = 'x';

This will make player's character as 'x' always, and give you the required first output.

Also, you can remove function definition of determine_player_choice(), as it is no longer required.

Following is the final code after implementing suggestions I gave above:

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
struct tic_tac_toe
{
char board[3][3];
char player1;
char cpu;
};
/*********************************************************************
** Function: initalize_board
** Description: Fills the 9 spots on the tic-tac-toe board with periods.
** Parameters: char[3][3]
** Pre-Conditions: Board was created in main.
** Post-Conditions: Board is initalized with periods.
*********************************************************************/
// parameters char[3][3]
void initalize_board (char board[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
//fill 9 spots with dots
board[i][j] = '.';
}
}
}
/*********************************************************************
** Function: print_board
** Description: Prints out the tic-tac-toe board.
** Parameters: char[3][3]
** Pre-Conditions: None.
** Post-Conditions: None.
*********************************************************************/
//print_board function
void print_board(char board[3][3]) {
for (int i = 0; i < 3; i++) {
cout << endl;
for (int j = 0; j < 3; j++) {
cout << " ";
//prints out the tic tac toe board
cout << board[i][j];
}
}
cout << endl << endl;
}
/*********************************************************************
** Function: check_win
** Description: Checks for three in a row in all directions.
** Parameters: char[3][3]
** Pre-Conditions: None.
** Post-Conditions: Returns the character that that the three in a row consists of.
*********************************************************************/
char check_win(char board[3][3]) {

// Check horizontal, vertical & diagonal through [0][0]
if (board[0][0] != '.' && (board[0][0] == board[0][1] && board[0][0] == board[0][2] ||
board[0][0] == board[1][0] && board[0][0] == board[2][0] ||
board[0][0] == board[1][1] && board[0][0] == board[2][2]))

return board[0][0];

// Check horizontal, vertical & diagonal through [1][1]
if (board[1][1] != '.' && (board[1][1] == board[1][0] && board[1][1] == board[1][2] ||
board[1][1] == board[0][1] && board[1][1] == board[2][1] ||
board[1][1] == board[2][0] && board[1][1] == board[0][2]))

return board[1][1];

// Check horizontal, vertical & diagonal through [2][2]
if (board[2][2] != '.' && (board[2][2] == board[0][2] && board[2][2] == board[1][2] ||
board[2][2] == board[2][0] && board[2][2] == board[2][1]))

return board[2][2];

return 0;
}
int negamax(char board[3][3], char player1, char player2); //player 2

//the pick_best_move is to find the best possible move
//char[3][3]
int pick_best_move(char board[3][3], char player1, char player2) {
int best_move_score = -9999;
int best_move_row = -9999;
int best_move_col = -9999;
int score_for_this_move = 0;
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
if (board[r][c] == '.') {
board[r][c] = player1; //Try test move.
score_for_this_move = -(negamax(board, player2, player1));
board[r][c] = '.'; //Put back test move.
if (score_for_this_move >= best_move_score) {
best_move_score = score_for_this_move;
best_move_row = r;
best_move_col = c;
}
}
}
}
return (10*best_move_row + best_move_col); //best move ??
}

int negamax(char board[3][3], char player1, char player2) {
int best_move_score = -9999;
int score_for_this_move = 0;

//If player 1 wins, then the score is high (good for player1)
if (check_win(board) == player1)
return 1000;

//If player 2 loses, then the score is low (bad for player1)
else if (check_win(board) == player2)
return -1000;
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
if (board[r][c] == '.') {
board[r][c] = player1; //Try test move.
score_for_this_move = -(negamax(board, player2, player1));
board[r][c] = '.'; //Put back test move.
if (score_for_this_move >= best_move_score) {
best_move_score = score_for_this_move;
}
}
}
}

if (best_move_score == -9999 || best_move_score == 0)
return 0;

else if (best_move_score < 0)
return best_move_score + 1;
else if (best_move_score > 0)
return best_move_score - 1; //As the game goes longer, and the recursion goes deeper, the moves near the end are less favorable than in the beginning.

}

//player_placement asks the user for a row and column and places their character in that spot
void player_placement(char board[3][3], char player) {

while (1) {
string string_row, string_col;
int row = 0, col = 0;
while (1) {
cout << "Where would you like to play? " << endl << "Enter the row: ";
cin >> string_row;
row = atoi(string_row.c_str());
if (row >= 1 && row <= 3)
break;
cout << "You need to enter a row on the board (between 1 and 3, inclusive)." << endl;
}
while (1) {
cout << "Enter the column: ";
cin >> string_col;
col = atoi(string_col.c_str());
if (col >= 1 && col <= 3)
break;
cout << "You need to enter a column on the board (between 1 and 3, inclusive)." << endl;
}
if (board[row-1][col-1] == '.') {
board[row-1][col-1] = player;
break;
}
else
cout << "Someone already played there." << endl << endl;
}
}

//this funtion determins the computer's choice
//it also decides whether the computer is playing x's or o's
char determine_cpu_choice (char player1) {
char cpu_char;
cout << "Hello, I am the computer you will be playing." << endl;
if (player1 == 'x' || player1 == 'X')
cpu_char = 'o';
else
cpu_char = 'x';
return cpu_char;
}
// this program only allowes player 1 so only against the computer
void play_game_1_player (char board[3][3], char player1, char cpu) {

int moves = 0;
while (moves < 9) {
player_placement(board, player1);
moves++;
print_board(board);
if (check_win(board)) {
cout << player1 << " won!" <<endl;

}
if (moves == 9)
break;
int where_to_move = pick_best_move(board, cpu, player1);
int row = where_to_move / 10;
int col = where_to_move % 10;
board[row][col] = cpu;
moves++;
print_board(board);
if (check_win(board)) {
cout << cpu << " won!" <<endl;
cout << "If I were you, I would've played at: " << "(row " << where_to_move/10 + 1 << ", col " << where_to_move % 10 + 1 << ")" << endl;
}
}
cout << "you guys are even!" << endl;
}

int main()
{
tic_tac_toe game;
initalize_board(game.board);
game.player1 = 'x';
game.cpu = determine_cpu_choice(game.player1);
print_board(game.board);
play_game_1_player(game.board, game.player1, game.cpu);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote