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

We need to adjust a code and implement a structure for an assignment and I\'m no

ID: 3540505 • Letter: W

Question

We need to adjust a code and implement a structure for an assignment and I'm not understanding it.  The directions are included in the code with //


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// Create a structure that stores a player's name (up to 20 characters) and score (# of wins)
// Call the struct Player

void info(void) {
  printf(" insertname assignmentnumber insertdate ");
}

void display(char pos[3][3]) {
  char c;
  int k;

  fflush(stdin);
  printf("   COL 0   1   2   ");

  for(k = 0; k < 3; k++) {
    printf(" ROW ------------- ");
    printf("     |   |   |   | ");
    printf("  %d  | %c | %c | %c | ", k, pos[k][0], pos[k][1], pos[k][2]);
    printf("     |   |   |   | ");
  }
  printf("     ------------- ");

  printf("Press Enter to Continue...");
  scanf("%c", &c);
  fflush(stdin);
}

void move(int m, char pos[3][3], Player * p1, Player * p2, FILE *f) {
  char piece;
  int row, col;

  do {
    if(m % 2 == 0) {
      piece = 'X';
      // Print the Player 1's name
    }
    else {
      piece = 'O';
      // Print the Player 2's name
    }

    printf(", enter your move (row 0 - 2) (col 0 - 2):  ");
    fscanf(f, "%d %d", &row, &col);
    printf("%d %d ", row, col);

  } while(!isValid(row, col, pos));

  pos[row][col] = piece;
}

int isValid(int r, int c, char pos[3][3]) {
  if((r < 0) || (r > 2) || (c < 0) || (c > 2) ) {
    printf("Illegal Move ");
    return 0;
  }
  else if(pos[r][c] != ' ') {
    printf("Illegal Move ");
    return 0;
  }
  return 1;
}

int winner(char pos[3][3], Player * p1, Player * p2) {
  int k, p;
  char win = ' ', str[4];

  for(k = 0; k < 3; k++) {
    strncpy(str, pos[k], 3);
    str[3] = '';

    if(!strcmp(str, "XXX") || !strcmp(str, "OOO")) {
      win = pos[k][0];
      break;
    }

    if((pos[0][k] != ' ') && (pos[0][k] == pos[1][k]) && (pos[0][k] == pos[2][k])) {
      win = pos[0][k];
      break;
    }
  }

  if((win == ' ') && (pos[0][0] != ' ') && (pos[0][0] == pos[1][1]) && (pos[0][0] == pos[2][2])) {
    win = pos[0][0];
  }
  if((win == ' ') && (pos[0][2] != ' ') && (pos[0][2] == pos[1][1]) && (pos[0][2] == pos[2][0])) {
    win = pos[0][2];
  }
  if(win == 'X') {
    // Print the Player 1's name
    // Increment Player 1's score
    return 1;
  }
  else if(win == 'O') {
    // Print the Player 2's name
    // Increment Player 2's score
    return 1;
  }

  return 0;
}

int main(int argc, char ** argv) {
  int i, k, loop = 0, won = 0;
  char c, ch, pos[3][3], datafile[20], player1Name[20], player2Name[20];
  FILE * ifp;

  printf("Enter Player 1's name:  ");
  gets(player1Name);
  printf("Enter Player 2's name:  ");
  gets(player2Name);

  // Declare two structs of type Player, p1 and p2.
  // Initialize the score value of each Player
  // Copy the name of each Player in the player's structure name,
  //   the first player's name is in player1Name and
  //   the second player's name is in player2Name

  for(i = 0; i < 3; i++) {
    for(k  = 0; k < 3; k++) {
      pos[i][k] = ' ';
    }
  }

  info();

  printf("What is the name of the data file?  ");

  gets(datafile);
  ifp = fopen(datafile, "r");
  fflush(stdin);

  display(pos);

  while(loop < 9) {
    // call the function move, pass in the:
    //   loop number,
    //   positions of the board,
    //   two players, and
    //   file pointer ifp
    // move(loop, pos, ___, ___, ifp);

    display(pos);
    loop++;

    // call the function winner, pass in the:
    //   positions of the board,
    //   two players
    // place the result into won
    // won = winner(pos, ___, ___);

    if(won || loop == 9) {
      if(!won) {
    printf(" There is no winner, you have tied! ");
      }
      printf("Would you like to play again (y or n)?  ");
      fscanf(ifp, "%c", &ch);
      while(ch == ' ') {
    fscanf(ifp, "%c", &ch);
      }
      printf("%c ", ch);

      if(ch == 'y') {
    loop = 0;
    for(i = 0; i < 3; i++) {
      for(k  = 0; k < 3; k++) {
        pos[i][k] = ' ';
      }
    }
    display(pos);
      }
      else {
    fclose(ifp);

        // Print the name of the both Players and their total scores

    loop = 9;
      }
    }
  }
}

Explanation / Answer

#include <stdio.h>

#include <string.h>

#include <stdlib.h>


// Create a structure that stores a player's name (up to 20 characters) and score (# of wins)

// Call the struct Player

typedef struct {

char name[20];

int score;

}Player;


void info(void) {

printf(" insertname assignmentnumber insertdate ");

}


void display(char pos[3][3]) {

char c;

int k;


fflush(stdin);

printf(" COL 0 1 2 ");


for(k = 0; k < 3; k++) {

printf(" ROW ------------- ");

printf(" | | | | ");

printf(" %d | %c | %c | %c | ", k, pos[k][0], pos[k][1], pos[k][2]);

printf(" | | | | ");

}

printf(" ------------- ");


printf("Press Enter to Continue...");

scanf("%c", &c);

fflush(stdin);

}


void move(int m, char pos[3][3], Player * p1, Player * p2, FILE *f) {

char piece;

int row, col;


do {

if(m % 2 == 0) {

piece = 'X';

// Print the Player 1's name

printf("%s",p1->name);

}

else {

piece = 'O';

// Print the Player 2's name

printf("%s",p2->name);

}


printf(", enter your move (row 0 - 2) (col 0 - 2): ");

fscanf(f, "%d %d", &row, &col);

printf("%d %d ", row, col);


} while(!isValid(row, col, pos));


pos[row][col] = piece;

}


int isValid(int r, int c, char pos[3][3]) {

if((r < 0) || (r > 2) || (c < 0) || (c > 2) ) {

printf("Illegal Move ");

return 0;

}

else if(pos[r][c] != ' ') {

printf("Illegal Move ");

return 0;

}

return 1;

}


int winner(char pos[3][3], Player * p1, Player * p2) {

int k, p;

char win = ' ', str[4];


for(k = 0; k < 3; k++) {

strncpy(str, pos[k], 3);

str[3] = '';


if(!strcmp(str, "XXX") || !strcmp(str, "OOO")) {

win = pos[k][0];

break;

}


if((pos[0][k] != ' ') && (pos[0][k] == pos[1][k]) && (pos[0][k] == pos[2][k])) {

win = pos[0][k];

break;

}

}


if((win == ' ') && (pos[0][0] != ' ') && (pos[0][0] == pos[1][1]) && (pos[0][0] == pos[2][2])) {

win = pos[0][0];

}

if((win == ' ') && (pos[0][2] != ' ') && (pos[0][2] == pos[1][1]) && (pos[0][2] == pos[2][0])) {

win = pos[0][2];

}

if(win == 'X') {

// Print the Player 1's name

printf("%s",p1->name);

// Increment Player 1's score

(p1->score)++;

return 1;

}

else if(win == 'O') {

// Print the Player 2's name

printf("%s",p2->name);

// Increment Player 2's score

(p2->score)++;

return 1;

}


return 0;

}


int main(int argc, char ** argv) {

int i, k, loop = 0, won = 0;

char c, ch, pos[3][3], datafile[20], player1Name[20], player2Name[20];

FILE * ifp;


printf("Enter Player 1's name: ");

gets(player1Name);

printf("Enter Player 2's name: ");

gets(player2Name);


// Declare two structs of type Player, p1 and p2.

Player p1,p2;

// Initialize the score value of each Player

p1.score=p2.score=0;

// Copy the name of each Player in the player's structure name,

// the first player's name is in player1Name and

// the second player's name is in player2Name

strcpy(p1.name,player1Name);

strcpy(p2.name,player2Name);


for(i = 0; i < 3; i++) {

for(k = 0; k < 3; k++) {

pos[i][k] = ' ';

}

}


info();


printf("What is the name of the data file? ");


gets(datafile);

ifp = fopen(datafile, "r");

fflush(stdin);


display(pos);


while(loop < 9) {

// call the function move, pass in the:

// loop number,

// positions of the board,

// two players, and

// file pointer ifp

// move(loop, pos, ___, ___, ifp);

move(loop,pos,&p1,&p2,ifp);


display(pos);

loop++;


// call the function winner, pass in the:

// positions of the board,

// two players

// place the result into won

// won = winner(pos, ___, ___);

won = winner(pos,&p1,&p2);


if(won || loop == 9) {

if(!won) {

printf(" There is no winner, you have tied! ");

}

printf("Would you like to play again (y or n)? ");

fscanf(ifp, "%c", &ch);

while(ch == ' ') {

fscanf(ifp, "%c", &ch);

}

printf("%c ", ch);


if(ch == 'y') {

loop = 0;

for(i = 0; i < 3; i++) {

for(k = 0; k < 3; k++) {

pos[i][k] = ' ';

}

}

display(pos);

}

else {

fclose(ifp);


// Print the name of the both Players and their total scores

printf("Player 1: %s, Total Score: %d ",p1.name,p1.score);

printf("Player 2: %s, Total Score: %d ",p2.name,p2.score);


loop = 9;

}

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote