Skip to content

Commit 92eed3b

Browse files
fix: unexpected behavior in requestPlayerNames
Co-authored-by: Lucas Hoz <[email protected]>
1 parent ade6aac commit 92eed3b

File tree

2 files changed

+60
-18
lines changed

2 files changed

+60
-18
lines changed

src/play/utilities.c

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,78 @@
11

22
#include "./utilities.h"
33

4+
#include <ctype.h>
45
#include <stdio.h>
56
#include <string.h>
67

78
#include "../../libs/main.h"
89
#include "../macros.h"
910
#include "../structs.h"
11+
#include "../utilities.h"
12+
13+
unsigned char formatPlayerName(char *_name) {
14+
char *linepointer;
15+
16+
linepointer = strrchr(_name, '\0');
17+
if (linepointer == NULL) return 0;
18+
linepointer--;
19+
while (!isalpha(*linepointer) && linepointer > _name) {
20+
linepointer--;
21+
}
22+
*(linepointer + 1) = '\0';
23+
return 1;
24+
}
1025

11-
unsigned char requestPlayerNames(SList* players) {
12-
char* lineBreak;
26+
unsigned char checkPlayerName(char *_name) {
27+
char *lineBreak;
1328

14-
Player player = {.points = 0, .gamesWons = 0, .lostGames = 0, .tiedGames = 0};
29+
lineBreak = strrchr(_name, '\n');
1530

16-
printf("> Enter a player name (0 to exit): ");
17-
fflush(stdin);
18-
fgets(player.name, PLAYER_NAME_LENGTH, stdin);
19-
puts("");
31+
if (lineBreak == NULL) return 0;
32+
*lineBreak = '\0';
2033

21-
lineBreak = strrchr(player.name, '\n');
22-
if (lineBreak != NULL) *lineBreak = '\0';
34+
if (*lineBreak == '0') return 0;
2335

24-
if (*(player.name) == '0' || !pushSListElement(players, &player, sizeof(player))) return 0;
36+
if (isspace(*_name) || *_name == '\0') {
37+
printf("> Name is required.\n\n");
38+
return 0;
39+
}
40+
return 1;
41+
}
2542

26-
while (*(player.name) != '0') {
27-
printf("> Enter a player name (0 to exit): ");
43+
void requestPlayerName(char *name) {
44+
printf("> Enter a player name (0 to exit): ");
45+
do {
46+
fflush(stdin);
47+
fgets(name, PLAYER_NAME_LENGTH, stdin);
2848
fflush(stdin);
29-
fgets(player.name, PLAYER_NAME_LENGTH, stdin);
3049
puts("");
3150

32-
lineBreak = strrchr(player.name, '\n');
33-
if (lineBreak != NULL) *lineBreak = '\0';
51+
} while (!checkPlayerName(name));
52+
return;
53+
}
54+
55+
unsigned char requestPlayerNames(SList *players) {
56+
Player player;
57+
char playerName[PLAYER_NAME_LENGTH];
58+
59+
requestPlayerName(playerName);
60+
formatPlayerName(playerName);
61+
if (*playerName == '0') return 0;
62+
63+
newPlayer(&player, playerName);
3464

35-
if (*(player.name) == '0') break;
65+
if (!pushSListElement(players, &player, sizeof(player))) return 0;
66+
67+
while (*playerName != '0') {
68+
requestPlayerName(playerName);
69+
formatPlayerName(playerName);
70+
71+
if (*playerName == '0') break;
72+
newPlayer(&player, playerName);
3673

3774
if (!pushSListElement(players, &player, sizeof(player))) return 0;
3875
};
39-
4076
return 1;
4177
}
4278

src/play/utilities.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
#include "../structs.h"
77
#include "./tateti/main.h"
88

9-
unsigned char requestPlayerNames(SList* players);
9+
unsigned char checkPlayerName(char *_name);
10+
11+
unsigned char formatPlayerName(char *_name);
12+
13+
unsigned char requestPlayerNames(SList *players);
1014

1115
void isPlayerReady();
1216

17+
void requestPlayerName(char *name);
18+
1319
#endif // SRC_PLAY_UTILITIES_H_INCLUDED

0 commit comments

Comments
 (0)