Can you make the following code much much smaller? Please use C only. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can you make the following code much much smaller? Please use C only.

https://github.com/J-Mark-Mascarenhas/Tic-Tac-Toe/blob/main/tictactoe.c

25th Oct 2021, 2:18 PM
Jolen Mascarenhas
2 Answers
+ 1
Have you learned about arrays? Using an array or two you can generalize the logic instead of repeating specific if statements. The concept could look like this code reduction:     char board[10] = {0, '1', '2', '3', '4', '5', '6', '7', '8', '9'};     int map[10] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0};     int x,y,z; . . .     for(z=100;z>0;z--)     {         printf("\n\nPlayer 1: ");         scanf("%d",&x);         if(x>=1 && x<=9 && map[x]==0)         {             map[x]=x;             board[x]='X';             printf("You played\n %c | %c | %c \n __________\n %c | %c | %c \n __________\n %c | %c | %c \n", board[1], board[2], board[3], board[4], board[5], board[6], board[7], board[8], board[9]);         }         else         {             printf("You entered wrong value or the number is already taken\nYour turn is terminated\n\n");         } . . .
25th Oct 2021, 3:33 PM
Brian
Brian - avatar
+ 1
Oh... Thanks bro
25th Oct 2021, 3:37 PM
Jolen Mascarenhas