[PR #771] fix: several bugs on games/tic_tac_toe.c #1286

Open
opened 2026-01-29 15:18:56 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/TheAlgorithms/C/pull/771

State: closed
Merged: Yes


Description of Change

Issue: #729
As mentioned in the issue , The piece code listed below throws the program into an loop (from line 247) if an invalid character is entered.

        else
        {
+            int n = check_placex();
-             printf("Invalid move\n");

-            printf("Enter new position : ");
-            scanf("%d", &n1);
+           placex(n);
-            placex(n1);
        }
    }
    else
    {
-        printf("Invalid move \n");
+       int n = check_placex();
-        printf("Enter new position : ");
-        scanf("%d", &n1);
+       placex(n);
-        placex(n1);
    }
}

Fixes / Changes

Added an array [dynamic] at line36 :
+ int *stored_pos = NULL;

line 47:

+  stored_pos = (int*)calloc(9,sizeof(int))
Assignment of the stored_pos array:

line 331, line 308 & line 278

example:

+ stored_pos[e] = 1;
Freed the stored_pos memory

line 75

+ free(stored_pos);
Added a new function called check_places at line237 to line256
int check_placex(){
	char input[50];
	int n1;
	while (1){
		fgets(input,49,stdin);
		if ( strlen(input) > 2 || strlen(input)  == 0){
			fprintf(stderr,"Invalid move, Enter number 1 - 9: ");
			continue;
		}
		if(sscanf(input,"%d",&n1) != 1){
			fprintf(stderr,"Invalid move, Enter number 1 - 9: ");
			continue;
		} 
		if ((stored_pos[n1-1] == 1) || (n1== 0)){
			fprintf(stderr,"Already allocated, Enter number: ");
			continue;
		}
		return n1;
	}
}	

The while-loop in the above above function will ask for an input until it gets a correct output.

  1. The first if condition checks whether the input is within the range. (strlen() is used so string.h lib is need)
  2. Second if condition checks if the value is integer
  3. Third if condition checks if the value (posistion) is already occupied.

References

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes:

**Original Pull Request:** https://github.com/TheAlgorithms/C/pull/771 **State:** closed **Merged:** Yes --- #### Description of Change Issue: #729 As **mentioned** in the issue , The piece code listed below throws the program into an loop (from `line 247`) if an **invalid** character is entered. ```diff else { + int n = check_placex(); - printf("Invalid move\n"); - printf("Enter new position : "); - scanf("%d", &n1); + placex(n); - placex(n1); } } else { - printf("Invalid move \n"); + int n = check_placex(); - printf("Enter new position : "); - scanf("%d", &n1); + placex(n); - placex(n1); } } ``` #### Fixes / Changes ##### Added an array [dynamic] at `line36` : ```diff + int *stored_pos = NULL; ``` `line 47`: ```diff + stored_pos = (int*)calloc(9,sizeof(int)) ``` ##### _Assignment_ of the **stored_pos** array: `line 331`, `line 308` & `line 278` example: ```diff + stored_pos[e] = 1; ``` ##### Freed the _stored_pos_ memory `line 75` ```diff + free(stored_pos); ``` ##### Added a new function called `check_places` at `line237` to `line256` ```C int check_placex(){ char input[50]; int n1; while (1){ fgets(input,49,stdin); if ( strlen(input) > 2 || strlen(input) == 0){ fprintf(stderr,"Invalid move, Enter number 1 - 9: "); continue; } if(sscanf(input,"%d",&n1) != 1){ fprintf(stderr,"Invalid move, Enter number 1 - 9: "); continue; } if ((stored_pos[n1-1] == 1) || (n1== 0)){ fprintf(stderr,"Already allocated, Enter number: "); continue; } return n1; } } ``` The `while-loop` in the above above function will ask for an input until it gets a correct output. 1. The first **if condition** checks whether the **input** is within the range. _(strlen() is used so `string.h` lib is need)_ 1. Second **if condition** checks if the value is integer 1. Third **if condition** checks if the value (_posistion_) is already occupied. #### References #### Checklist - [x] Added description of change - [x] Added file name matches [File name guidelines](https://github.com/TheAlgorithms/C/blob/master/CONTRIBUTING.md#File-Name-guidelines) - [x] Added tests and example, test must pass - [x] Relevant documentation/comments is changed or added - [x] PR title follows semantic [commit guidelines](https://github.com/TheAlgorithms/C/blob/master/CONTRIBUTING.md#Commit-Guidelines) - [x] Search previous suggestions before making a new one, as yours may be a duplicate. - [x] I acknowledge that all my contributions will be made under the project's license. Notes: <!-- Please add a one-line description for developers or pull request viewers --> <a href="https://gitpod.io/#https://github.com/TheAlgorithms/C/pull/771"><img src="https://gitpod.io/api/apps/github/pbs/github.com/kana800/C.git/2d27be85528772fb60e09e8ac40a3fff7bf01e40.svg" /></a> <a href="https://gitpod.io/#https://github.com/TheAlgorithms/C/pull/771"><img src="https://gitpod.io/api/apps/github/pbs/github.com/kana800/C.git/e0245894b8f3c3a5fad4bba53a8db9a84ef93d5f.svg" /></a>
claunia added the pull-request label 2026-01-29 15:18:56 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/C#1286