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;
}
}
}
}