can someone pls help me add comments to my program ?? thank you so much !! #incl
ID: 3842951 • Letter: C
Question
can someone pls help me add comments to my program ?? thank you so much !!
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
struct ticTacToe
{
char board[3][3];
char human;
char PC;
};
void initalize_board (char board[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = '.';
}
}
}
void print_board(char board[3][3]) {
for (int i = 0; i < 3; i++) {
cout << endl;
for (int j = 0; j < 3; j++) {
cout << " ";
cout << board[i][j];
}
}
cout << endl << endl;
}
char check_win(char board[3][3]) {
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];
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];
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 user, char computer);
int pick_best_move(char board[3][3], char user, char computer) {
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] = user;
score_for_this_move = -(negamax(board, computer, user));
board[r][c] = '.';
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);
}
int negamax(char board[3][3], char user, char computer) {
int best_move_score = -9999;
int score_for_this_move = 0;
if (check_win(board) == user)
return 1000;
else if (check_win(board) == computer)
return -1000;
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
if (board[r][c] == '.') {
board[r][c] = user;
score_for_this_move = -(negamax(board, computer, user));
board[r][c] = '.';
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;
}
void player_placement(char board[3][3], char user) {
while (1) {
string string_row, string_col;
int row = 0, col = 0;
while (1) {
cout << "Enter 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 only ! ." << endl;
}
while (1) {
cout << "Enter 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 only !!!." << endl;
}
if (board[row-1][col-1] == '.') {
board[row-1][col-1] = user;
break;
}
else
cout << " Already played there not available." << endl << endl;
}
}
char determine_computer_choice (char user) {
char computer_char;
cout << "Tic Tac Toe PC vs Human " << endl;
if (user == 'x' || user == 'X')
computer_char = 'O';
else
computer_char = 'X';
return computer_char;
}
void play_game_1_player (char board[3][3], char player1, char computer) {
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, computer, player1);
int row = where_to_move / 10;
int col = where_to_move % 10;
board[row][col] = computer;
moves++;
print_board(board);
if (check_win(board)) {
cout << "Computer WON!" <<endl;
}
}
cout << "We dont have a winner" << endl;
}
int main()
{
ticTacToe game;
initalize_board(game.board);
game.human = 'X';
game.PC = determine_computer_choice(game.human);
print_board(game.board);
play_game_1_player(game.board, game.human, game.PC);
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
struct ticTacToe
{
char board[3][3]; /* here we take 2-D char array or string for board elements "
char human;
char PC;
};
void initalize_board (char board[3][3]) { /* this method is used to intialize the board */
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = '.'; /* here we intialize the board elements */
}
}
}
void print_board(char board[3][3]) { /* this method to print the board */
for (int i = 0; i < 3; i++) {
cout << endl;
for (int j = 0; j < 3; j++) {
cout << " ";
cout << board[i][j]; /* by this line we print the board elemets */
}
}
cout << endl << endl;
}
char check_win(char board[3][3]) { /* this method is used to check win */
if (board[0][0] != '.' && (board[0][0] == board[0][1] && board[0][0] == board[0][2] || /* here we applied condition to comparsion elements of board */
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];
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];
if (board[2][2] != '.' && (board[2][2] == board[0][2] && board[2][2] == board[1][2] || /* here we applied condition to comparsion elements of board */
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 user, char computer);
int pick_best_move(char board[3][3], char user, char computer) { /* here we write method to pick best move */
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] = user;
score_for_this_move = -(negamax(board, computer, user));
board[r][c] = '.';
if (score_for_this_move >= best_move_score) { /*comparison for score of this move */
best_move_score = score_for_this_move;
best_move_row = r;
best_move_col = c;
}
}
}
}
return (10*best_move_row + best_move_col);
}
int negamax(char board[3][3], char user, char computer) {
int best_move_score = -9999;
int score_for_this_move = 0;
if (check_win(board) == user)
return 1000;
else if (check_win(board) == computer) /* here we call method to check win */
return -1000;
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
if (board[r][c] == '.') {
board[r][c] = user;
score_for_this_move = -(negamax(board, computer, user));
board[r][c] = '.';
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;
}
void player_placement(char board[3][3], char user) {
while (1) {
string string_row, string_col;
int row = 0, col = 0;
while (1) {
cout << "Enter row # : ";
cin >> string_row; /* here we take value of row #*/
row = atoi(string_row.c_str());
if (row >= 1 && row <= 3) /* to give message to the user if condition is true */
break;
cout << "You need to enter a row on the board between 1 and 3 only ! ." << endl;
}
while (1) {
cout << "Enter column # : ";
/* here we take value of column #*/
cin >> string_col;
col = atoi(string_col.c_str());
if (col >= 1 && col <= 3) /* to give message to the user if condition is true */
break;
cout << "You need to enter a column on the board between 1 and 3 only !!!." << endl;
}
if (board[row-1][col-1] == '.') { /* here we check condition */
board[row-1][col-1] = user;
break;
}
else
cout << " Already played there not available." << endl << endl;
}
}
char determine_computer_choice (char user) { /* this methos to determine computer choice */
char computer_char;
cout << "Tic Tac Toe PC vs Human " << endl;
if (user == 'x' || user == 'X')
computer_char = 'O';
else
computer_char = 'X';
return computer_char;
}
void play_game_1_player (char board[3][3], char player1, char computer) {
int moves = 0;
while (moves < 9) {
player_placement(board, player1);
moves++;
print_board(board); /* to display board */
if (check_win(board)) { /* to check win */
cout << player1 << " won!" <<endl;
}
if (moves == 9)
break;
int where_to_move = pick_best_move(board, computer, player1);
int row = where_to_move / 10;
int col = where_to_move % 10;
board[row][col] = computer;
moves++;
print_board(board);
if (check_win(board)) {
cout << "Computer WON!" <<endl;
}
}
cout << "We dont have a winner" << endl;
}
int main()
{
ticTacToe game;
initalize_board(game.board); /*here we call to intialize_board method to intilaize game borad */
game.human = 'X';
game.PC = determine_computer_choice(game.human); /*this method call to determine the compute chocie or means it will give use computer choice */
print_board(game.board); /* this method is called to display board elements */
play_game_1_player(game.board, game.human, game.PC);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.