[PR #778] [CLOSED] Update tic_tac_toe.c #1293

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

📋 Pull Request Information

Original PR: https://github.com/TheAlgorithms/C/pull/778
Author: @driti924
Created: 12/16/2020
Status: Closed

Base: masterHead: driti924_Update_tic_tac_toe


📝 Commits (2)

📊 Changes

1 file changed (+46 additions, -14 deletions)

View changed files

📝 games/tic_tac_toe.c (+46 -14)

📄 Description

Successful exceptional handling

Description of Change

Issue: #729

Fixes / Changes

#line no 16
[+] #include <string.h>

First, I changed type of 'n' from int to char, so that if player enters any character other than a number it still gets consumed and does not remain in input buffer. As this input buffer effects the scanning of next number(i.e 'l' here) and doesn't scan player's choice to play again.
#line no 41
[-] int n = 0;
[+] char n = '0';

Second, to avoid infinite loop on getting any character on line no.247 , I have added a new function 'TakeInput'. Thus a player can enter any character/s or number(not in range of 1-9) without entering infinite loop. This time a message will prompt them to give valid input.
Also note, my code is always using TakeInput function to input players choice to avoid the same problem of unused input buffer.
#line no 21
[+] static int TakeInput(); // used to take input from player and handle exceptions accordingly

#line no 85
[+] int TakeInput(){

// Input player's choice of placing 'x' or 'o' in variable x 
char x[100];
fgets(x, 100, stdin);

// to check the length of input!
int i;
for (i = 0; x[i] != '\0'; i++);   

// if length of input=1 (i.e i=2) and input value is a digit then its a valid input.
if(i==2 && x[0]-'0'>=1 && x[0]-'0'<=9  ){   
    int m=x[0]-'0';
    return m;
}
else{
    printf("(Enter a valid number between [1 - 9]) ");
    return TakeInput();
}

}

Other changer are made accordingly. Like
#line no 54
[-] scanf("%d", &n);
[+] scanf(" %c", &n);

#line no 59
[-] case 1:
[+] case '1':

#line no 62
[-] case 2:
[+] case '2':

For better visibility
#line no 66
[-] printf("THANK YOU and EXIT!");
[+] printf("THANK YOU and EXIT!\n");

lines 105 and 176

[-] scanf("%d",&m)
[+] m=TakeInput()

#line no 182
[-] scanf("%d", &e1);
[+] e1=TakeInput();

#line no 242 and 252
[-] scanf("%d", &n1);
[+] n1=TakeInput();

For better visibility
line no 272
[-] printf("\n Computer placed at %d position\n", e + 1);
[+] printf("\nComputer placed at %d position\n", e + 1);

#line no 301 and 311
[-] scanf("%d", &n1);
[+] n1=TakeInput();


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/TheAlgorithms/C/pull/778 **Author:** [@driti924](https://github.com/driti924) **Created:** 12/16/2020 **Status:** ❌ Closed **Base:** `master` ← **Head:** `driti924_Update_tic_tac_toe` --- ### 📝 Commits (2) - [`f72f7c9`](https://github.com/TheAlgorithms/C/commit/f72f7c91bb42f3fffbae7b749901d8102e9dcb13) Update tic_tac_toe.c - [`9cfbdb9`](https://github.com/TheAlgorithms/C/commit/9cfbdb98463dbb07306e6ccb42c652e2fdfdc5e6) Update tic_tac_toe.c ### 📊 Changes **1 file changed** (+46 additions, -14 deletions) <details> <summary>View changed files</summary> 📝 `games/tic_tac_toe.c` (+46 -14) </details> ### 📄 Description Successful exceptional handling #### Description of Change Issue: #729 Fixes / Changes #line no 16 [+] #include <string.h> First, I changed type of 'n' from int to char, so that if player enters any character other than a number it still gets consumed and does not remain in input buffer. As this input buffer effects the scanning of next number(i.e 'l' here) and doesn't scan player's choice to play again. #line no 41 [-] int n = 0; [+] char n = '0'; Second, to avoid infinite loop on getting any character on line no.247 , I have added a new function 'TakeInput'. Thus a player can enter any character/s or number(not in range of 1-9) without entering infinite loop. This time a message will prompt them to give valid input. Also note, my code is always using TakeInput function to input players choice to avoid the same problem of unused input buffer. #line no 21 [+] static int TakeInput(); // used to take input from player and handle exceptions accordingly #line no 85 [+] int TakeInput(){ // Input player's choice of placing 'x' or 'o' in variable x char x[100]; fgets(x, 100, stdin); // to check the length of input! int i; for (i = 0; x[i] != '\0'; i++); // if length of input=1 (i.e i=2) and input value is a digit then its a valid input. if(i==2 && x[0]-'0'>=1 && x[0]-'0'<=9 ){ int m=x[0]-'0'; return m; } else{ printf("(Enter a valid number between [1 - 9]) "); return TakeInput(); } } Other changer are made accordingly. Like #line no 54 [-] scanf("%d", &n); [+] scanf(" %c", &n); #line no 59 [-] case 1: [+] case '1': #line no 62 [-] case 2: [+] case '2': For better visibility #line no 66 [-] printf("THANK YOU and EXIT!"); [+] printf("THANK YOU and EXIT!\n"); # lines 105 and 176 [-] scanf("%d",&m) [+] m=TakeInput() #line no 182 [-] scanf("%d", &e1); [+] e1=TakeInput(); #line no 242 and 252 [-] scanf("%d", &n1); [+] n1=TakeInput(); For better visibility line no 272 [-] printf("\n Computer placed at %d position\n", e + 1); [+] printf("\nComputer placed at %d position\n", e + 1); #line no 301 and 311 [-] scanf("%d", &n1); [+] n1=TakeInput(); <a href="https://gitpod.io/#https://github.com/TheAlgorithms/C/pull/778"><img src="https://gitpod.io/api/apps/github/pbs/github.com/driti924/C.git/f72f7c91bb42f3fffbae7b749901d8102e9dcb13.svg" /></a> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-29 15:19:00 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/C#1293