|
1 | 1 |
|
2 | 2 | #include "./utilities.h"
|
3 | 3 |
|
| 4 | +#include <ctype.h> |
4 | 5 | #include <stdio.h>
|
5 | 6 | #include <string.h>
|
6 | 7 |
|
7 | 8 | #include "../../libs/main.h"
|
8 | 9 | #include "../macros.h"
|
9 | 10 | #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 | +} |
10 | 25 |
|
11 |
| -unsigned char requestPlayerNames(SList* players) { |
12 |
| - char* lineBreak; |
| 26 | +unsigned char checkPlayerName(char *_name) { |
| 27 | + char *lineBreak; |
13 | 28 |
|
14 |
| - Player player = {.points = 0, .gamesWons = 0, .lostGames = 0, .tiedGames = 0}; |
| 29 | + lineBreak = strrchr(_name, '\n'); |
15 | 30 |
|
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'; |
20 | 33 |
|
21 |
| - lineBreak = strrchr(player.name, '\n'); |
22 |
| - if (lineBreak != NULL) *lineBreak = '\0'; |
| 34 | + if (*lineBreak == '0') return 0; |
23 | 35 |
|
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 | +} |
25 | 42 |
|
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); |
28 | 48 | fflush(stdin);
|
29 |
| - fgets(player.name, PLAYER_NAME_LENGTH, stdin); |
30 | 49 | puts("");
|
31 | 50 |
|
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); |
34 | 64 |
|
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); |
36 | 73 |
|
37 | 74 | if (!pushSListElement(players, &player, sizeof(player))) return 0;
|
38 | 75 | };
|
39 |
| - |
40 | 76 | return 1;
|
41 | 77 | }
|
42 | 78 |
|
|
0 commit comments