Magic-number elimination.

This commit is contained in:
Eric S. Raymond 2017-07-03 07:14:15 -04:00
parent f48ec1eb36
commit eba8015059
4 changed files with 11 additions and 10 deletions

View file

@ -171,7 +171,7 @@ static int bigwords(token_t foo)
* Look up foo in special section of vocab to determine which word we've got. * Look up foo in special section of vocab to determine which word we've got.
* Last word zips the eggs back to the giant room (unless already there). */ * Last word zips the eggs back to the giant room (unless already there). */
{ {
char word[6]; char word[TOKLEN+1];
packed_to_token(foo, word); packed_to_token(foo, word);
int k = (int) get_special_vocab_id(word); int k = (int) get_special_vocab_id(word);
int spk = NOTHING_HAPPENS; int spk = NOTHING_HAPPENS;
@ -1043,7 +1043,7 @@ static int say(struct command_t *command)
b = command->wd2x; b = command->wd2x;
command->wd1 = command->wd2; command->wd1 = command->wd2;
} }
char word1[6]; char word1[TOKLEN+1];
packed_to_token(command->wd1, word1); packed_to_token(command->wd1, word1);
int wd = (int) get_vocab_id(word1); int wd = (int) get_vocab_id(word1);
/* FIXME: magic numbers */ /* FIXME: magic numbers */

View file

@ -6,6 +6,7 @@
#include "dungeon.h" #include "dungeon.h"
#define LINESIZE 1024 #define LINESIZE 1024
#define TOKLEN 5 // # sigificant character sin a token */
#define NDWARVES 6 // number of dwarves #define NDWARVES 6 // number of dwarves
#define PIRATE NDWARVES // must be NDWARVES-1 when zero-origin #define PIRATE NDWARVES // must be NDWARVES-1 when zero-origin
#define DALTLC LOC_NUGGET // alternate dwarf location #define DALTLC LOC_NUGGET // alternate dwarf location
@ -151,7 +152,7 @@ struct game_t {
long trnluz; // # points lost so far due to number of turns used long trnluz; // # points lost so far due to number of turns used
long turns; // how many commands he's given (ignores yes/no) long turns; // how many commands he's given (ignores yes/no)
bool wzdark; // whether the loc he's leaving was dark bool wzdark; // whether the loc he's leaving was dark
char zzword[6]; // randomly generated magic word from bird char zzword[TOKLEN+1]; // randomly generated magic word from bird
bool blooded; // has player drunk of dragon's blood? bool blooded; // has player drunk of dragon's blood?
long abbrev[NLOCATIONS + 1]; long abbrev[NLOCATIONS + 1];
long atloc[NLOCATIONS + 1]; long atloc[NLOCATIONS + 1];
@ -190,7 +191,7 @@ extern struct settings_t settings;
extern char* xstrdup(const char* s); extern char* xstrdup(const char* s);
extern void* xmalloc(size_t size); extern void* xmalloc(size_t size);
extern void packed_to_token(long, char token[]); extern void packed_to_token(long, char token[]);
extern long token_to_packed(const char token[6]); extern long token_to_packed(const char token[TOKLEN+1]);
extern void tokenize(char*, long tokens[4]); extern void tokenize(char*, long tokens[4]);
extern void vspeak(const char*, bool, va_list); extern void vspeak(const char*, bool, va_list);
extern bool wordeq(token_t, token_t); extern bool wordeq(token_t, token_t);

4
main.c
View file

@ -1089,8 +1089,8 @@ L2607:
} else } else
lampcheck(); lampcheck();
char word1[6]; char word1[TOKLEN+1];
char word2[6]; char word2[TOKLEN+1];
packed_to_token(command.wd1, word1); packed_to_token(command.wd1, word1);
packed_to_token(command.wd2, word2); packed_to_token(command.wd2, word2);
V1 = get_vocab_id(word1); V1 = get_vocab_id(word1);

8
misc.c
View file

@ -35,7 +35,7 @@ void* xmalloc(size_t size)
return (ptr); return (ptr);
} }
void packed_to_token(long packed, char token[6]) void packed_to_token(long packed, char token[TOKLEN+1])
{ {
// The advent->ascii mapping. // The advent->ascii mapping.
const char advent_to_ascii[] = { const char advent_to_ascii[] = {
@ -68,7 +68,7 @@ void packed_to_token(long packed, char token[6])
} }
} }
long token_to_packed(const char token[6]) long token_to_packed(const char token[TOKLEN+1])
{ {
const char ascii_to_advent[] = { const char ascii_to_advent[] = {
63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
@ -113,7 +113,7 @@ void tokenize(char* raw, long tokens[4])
int word_count = sscanf(raw, "%s%s", words[0], words[1]); int word_count = sscanf(raw, "%s%s", words[0], words[1]);
// make space for substrings and zero it out // make space for substrings and zero it out
char chunk_data[][6] = { char chunk_data[][TOKLEN+1] = {
{"\0\0\0\0\0"}, {"\0\0\0\0\0"},
{"\0\0\0\0\0"}, {"\0\0\0\0\0"},
{"\0\0\0\0\0"}, {"\0\0\0\0\0"},
@ -679,7 +679,7 @@ long randrange(long range)
return range * get_next_lcg_value() / game.lcg_m; return range * get_next_lcg_value() / game.lcg_m;
} }
void make_zzword(char zzword[6]) void make_zzword(char zzword[TOKLEN+1])
{ {
for (int i = 0; i < 5; ++i) { for (int i = 0; i < 5; ++i) {
zzword[i] = 'A' + randrange(26); zzword[i] = 'A' + randrange(26);