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

Hello, I have a program in C that is a board game. There are 2 manual players, t

ID: 3868245 • Letter: H

Question

Hello, I have a program in C that is a board game. There are 2 manual players, turnNum == 0 is the first, turnNum ==2 is the second.

The player is to input 2 numbers that change a positioin on the board, with (0 0) being the top left corner.

Currenty my code can not pick up if someone writes something like this:

Player 0: hell0 h8

This will place the tile on the board in position (0,8).

How can I error check to see if the player input is only 2 digits. Thanks!!

CODE:

int player_input(char* result, struct Game* newgame, int** board) {

  

int position, next, i, count = 0;

int entries = 0, countFirst = 0;

  

if (newgame->turnNum == 0) {

do {printf("Player O] ");

position = 0;

next = 0;

entries = 0;

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

result[i] = '!';

}

countFirst =0;

count = 0;

while ((next = fgetc(stdin)) != ' ') {

if (next == (char)EOF){

error_output(6);

}

count++;

if ((char)next == ' ') {

entries++;

countFirst = count - 1,

count = 0;

}

result[position] = next;

printf("%c ", result[position]);

position++;

}

} while (input_check(position, result, newgame->rowNumber,

newgame->columnNumber, entries, board, newgame, countFirst, count) == 1);

  

update_board_human(board, result, newgame);

}

for (i = 0; i < 71 ; i++)

result[i] = '!';

  

if (newgame->turnNum == 1) {

do {printf("Player X] ");

position = 0;

next = 0;

entries = 0;

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

result[i] = '!';

}

countFirst = 0;

count = 0;

while ((next = fgetc(stdin)) != ' ') {

if (next == (char)(EOF)){

error_output(6);

}

count++;

if ((char)next == ' ') {

entries++;

countFirst = count - 1,

count = 0;

}

result[position] = (char)next;

  

position++;

}

} while (input_check(position, result, newgame->rowNumber,

   newgame->columnNumber, entries, board, newgame, countFirst, count) == 1);

update_board_human(board, result, newgame);

}

  

for (i = 0; i < 71 ; i++)

result[i] = '!';

  

return 0;

}

int input_check(int position, char* result, int row, int col, int entries,

int** board, struct Game* newgame, int firstCount, int secondCount) {

  

char* token;

int i = 0, j = firstCount + 1;

long rowNumber, colNumber;

char* compare = &result[0];

char firstCharacter = *compare;

char* compare2 = &result[1];

char secondCharacter = *compare2;

  

if (firstCharacter == 's') {

save_game(newgame, board, result);

free(board);

free(result);

exit(0);

} else if (firstCharacter == '^' && secondCharacter == 'C') {

free(result);

free(board);

exit(0);

}

  

rowNumber = strtol(result, &token, 10);

colNumber = strtol(&result[2], &token, 10);

  

if (entries > 1 || entries == 0){

return 1;

} else if ((rowNumber >= row) || (colNumber >= col)

   || (board[rowNumber][colNumber] == 1) ||

   (board[rowNumber][colNumber] == 0 )) {

printf("size probs ");

return 1;

} else {

return 0;

}

  

return 1;

}

Explanation / Answer

Added statements made as bold one for easy identification. Here solution is , check char by char with 'isDigit(char)' method.


int input_check(int position, char* result, int row, int col, int entries,
int** board, struct Game* newgame, int firstCount, int secondCount) {
  
char* token;
int i = 0, j = firstCount + 1;
long rowNumber, colNumber;
char* compare = &result[0];
char firstCharacter = *compare;
char* compare2 = &result[1];
char secondCharacter = *compare2;
  
if (firstCharacter == 's') {
save_game(newgame, board, result);
free(board);
free(result);
exit(0);
} else if (firstCharacter == '^' && secondCharacter == 'C') {
free(result);
free(board);
exit(0);
}
/* Checking numbers */
char[] input = result[2];
int _len = strlen (result[2]);
for (i=0;i<_len; i++) {
if (!isdigit(input[i]))
{
printf ("Problem in input number ");
return 1;
}
}
input = result[2];
_len = strlen (result[0]);
for (i=0;i<_len; i++) {
if (!isdigit(input[i]))
{
printf ("Problem in input number ");
return 1;
}
}
  

rowNumber = strtol(result, &token, 10);
colNumber = strtol(&result[2], &token, 10);
  
if (entries > 1 || entries == 0){
return 1;
} else if ((rowNumber >= row) || (colNumber >= col)
|| (board[rowNumber][colNumber] == 1) ||
(board[rowNumber][colNumber] == 0 )) {
printf("size probs ");
return 1;
} else {
return 0;
}
  
return 1;
}

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