From c4e80b8b0a50c2834a1f8868a8e640cde8312b37 Mon Sep 17 00:00:00 2001 From: Chathura Nimesh Date: Wed, 10 Feb 2021 19:32:01 +0530 Subject: [PATCH] fix: several bugs on games/tic_tac_toe.c (#771) * fixed several bugs on tic_tac_toe * final changes bug fixed * added requested changes * fixed changes * fixed stuff * fix clang-tidy warnings * removed unnecessary headers * resolved some warnings * fixed warning c4244 * Tiggering CI checks --- games/tic_tac_toe.c | 65 ++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/games/tic_tac_toe.c b/games/tic_tac_toe.c index c37db5f2..d8b2f421 100644 --- a/games/tic_tac_toe.c +++ b/games/tic_tac_toe.c @@ -13,6 +13,7 @@ #include #include #include +#include // Functions Declarations static void singlemode(); @@ -34,7 +35,8 @@ static char game_table[9]; * @note No checks are included for program execution failures! */ int main() -{ srand(time(NULL)); +{ + srand( (unsigned int)time(NULL)); int l = 0; do { @@ -218,6 +220,31 @@ void doublemode() } } +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 ((game_table[n1-1] == 'x') || (game_table[n1-1]) == 'o' || (n1== 0)){ + fprintf(stderr,"Already allocated, Enter number: "); + continue; + } + return n1; + } +} + + + + + /** * @brief Update table by placing an `X` * @@ -227,7 +254,7 @@ void doublemode() */ void placex(int m) { - int n1; + int n1 = 0; if (m >= 1 && m <= 9) { if (game_table[m - 1] != 'x' && game_table[m - 1] != 'o') @@ -236,22 +263,14 @@ void placex(int m) } else { - printf("Invalid move\n"); - - printf("Enter new position : "); - scanf("%d", &n1); - - placex(n1); + int n = check_placex(); + placex(n); } } else { - printf("Invalid move \n"); - - printf("Enter new position : "); - scanf("%d", &n1); - - placex(n1); + int n = check_placex(); + placex(n); } } /** @@ -286,7 +305,7 @@ void place() */ void placey(int e1) { - int n1; + int n1 = 0; if (e1 >= 1 && e1 <= 9) { if (game_table[e1 - 1] != 'x' && game_table[e1 - 1] != 'o') @@ -295,22 +314,14 @@ void placey(int e1) } else { - printf("Invalid move \n"); - - printf("Enter new position : "); - scanf("%d", &n1); - - placey(n1); + int n = check_placex(); + placex(n); } } else { - printf("Invalid move \n"); - - printf("Enter new position : "); - scanf("%d", &n1); - - placey(n1); + int n = check_placex(); + placex(n); } } /**