Thread: Structures

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    4

    Structures

    For a class assignment, we are told to modify this code following the instructions given within the code. It is a tictactoe program.

    Code:
    #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("\nName\nAssignment\nDate\n\n");
    }
    
    void display(char pos[3][3]) {
      char c;
      int k;
    
      fflush(stdin);
      printf("   COL 0   1   2  \n");
    
      for(k = 0; k < 3; k++) {
        printf(" ROW -------------\n");
        printf("     |   |   |   |\n");
        printf("  %d  | %c | %c | %c |\n", k, pos[k][0], pos[k][1], pos[k][2]);
        printf("     |   |   |   |\n");
      }
      printf("     -------------\n");
    
      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\n\n", 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\n");
        return 0;
      }
      else if(pos[r][c] != ' ') {
        printf("Illegal Move\n");
        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] = '\0';
    
        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("\nThere is no winner, you have tied!\n\n");
          }
          printf("Would you like to play again (y or n)?  ");
          fscanf(ifp, "%c", &ch);
          while(ch == '\n') {
        fscanf(ifp, "%c", &ch);
          }
          printf("%c\n\n", 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;
          }
        }
      }
    }
    I've ended up with the following code, but the part about the name of the datafile is giving errors upon running the program, and I have no idea about this part of the code. Could anyone help out?

    Code:
    #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("\nName\nAssignment\nDate\n\n");
    
    }
    
    
    void display(char pos[3][3]) {
    
      char c;
      int k;
    
    
      fflush(stdin);
      printf("   COL 0   1   2  \n");
    
      for(k = 0; k < 3; k++) {
    
        printf(" ROW -------------\n");
        printf("     |   |   |   |\n");
        printf("  %d  | %c | %c | %c |\n", k, pos[k][0], pos[k][1], pos[k][2]);
        printf("     |   |   |   |\n");
         }
      printf("     -------------\n");
      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\n",p1->name);
        }
        else {
          piece = 'O';
          // Print the Player 2's name
          printf("%s\n",p2->name);
        }
    
        printf(", enter your move (row 0 - 2) (col 0 - 2):  ");
        fscanf(f, "%d %d", &row, &col);
        printf("%d %d\n\n", 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\n");
        return 0;
      }
      else if(pos[r][c] != ' ') {
        printf("Illegal Move\n");
        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] = '\0';
    
        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\n",p1->name);
        // Increment Player 1's score
          (p1->score)++;
        return 1;
      }
      else if(win == 'O') {
        // Print the Player 2's name
          printf("%s\n",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("\nThere is no winner, you have tied!\n\n");
          }
          printf("Would you like to play again (y or n)?  ");
          fscanf(ifp, "%c", &ch);
          while(ch == '\n') {
        fscanf(ifp, "%c", &ch);
          }
          printf("%c\n\n", 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\n",p1.name,p1.score);
        printf("Player 2: %s, Total Score: %d\n",p2.name,p2.score);
    
        loop = 9;
          }
        }
      }
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    char datafile[20];
    gets(datafile);
      ifp = fopen(datafile, "r");
      fflush(stdin);
    Never used gets. But I assume, this was not your choice.

    What are the problems you are talking about?
    datafile can support 19 letters + 1 for the null terminator.
    I guess you are inputting something with less letters than 19, but the file can not be found.
    Check if ifp is null, which means, that the file was not open, as you expected.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    4
    Gets was not my choice, it is part of the given code and I have to use it.

    By errors, I mean that when the program prompts me to what the name of the data file is, I input a name and then I reach the part of the program where it says to press Enter to continue. This is where I get a loop of errors when running the program, sorry I didn't clarify. I actually do not know if the error is with datafile or if it is elsewhere in the code, but I cannot figure it out.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Did you try what I said?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    4
    I understand where the problem was now, I appreciate the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring structures inside structures ?
    By jamaican1231 in forum C Programming
    Replies: 6
    Last Post: 04-13-2010, 03:40 PM
  2. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Accessing structures contained in structures
    By enigmaatj in forum C Programming
    Replies: 1
    Last Post: 04-18-2002, 08:53 AM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM