Skip to content

Commit 787b48b

Browse files
[FIX]: Fixing input player names (#35)
2 parents d332371 + 92eed3b commit 787b48b

File tree

3 files changed

+70
-31
lines changed

3 files changed

+70
-31
lines changed

src/play/tateti/main.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -225,31 +225,28 @@ void getValidatedInput(inputValuePlayer *datainput) {
225225
char _input[4];
226226

227227
do {
228-
puts("\n> Enter a coordinate to play. (ex: 0,0) \n WARNING:\n\t(maximum is (2,2)\n");
228+
puts(
229+
"\n> Enter a coordinate to play. (ex: 1,1) \n WARNING:\n\t(maximum is (3,3) / minimum)"
230+
"is (1,1)\n");
229231
fflush(stdin);
230-
fgets(_input, sizeof(_input), stdin); // TODO: prevent negative or out range
232+
fgets(_input, sizeof(_input), stdin);
231233
fflush(stdin);
232234
} while (!checkInputFormat(datainput, _input));
233235
return;
234236
}
235237
unsigned char checkInputFormat(inputValuePlayer *datainput, char *_input) {
236-
char *pointer = _input;
237-
int raw, column;
238+
int raw, column, finalvalue = 2;
238239

239-
raw = atoi(pointer);
240-
if (raw < 0 || raw > 3) {
240+
if (finalvalue != sscanf(_input, "%d,%d", &raw, &column)) {
241241
puts("\n> No valid Format. Try Again \n");
242242
return 0;
243243
}
244-
pointer++;
245-
pointer++;
246-
column = atoi(pointer);
247-
if (column < 0 || column > 3) {
248-
puts("\n> No valid Format. Try Again \n");
244+
if ((raw < 1 || raw > 3) || (column < 1 || column > 3)) {
245+
puts("\n> No valid number range. Try Again \n");
249246
return 0;
250247
}
251-
datainput->column = column;
252-
datainput->raw = raw;
248+
datainput->column = column - 1;
249+
datainput->raw = raw - 1;
253250
return 1;
254251
}
255252

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)