-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
131 lines (119 loc) · 4.79 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
// Program 2 -- Boggle-ish
// An approximation of the Boggle game
// This game ONLY works with a 4x4 Boggle tray
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <ctype.h>
unsigned int lfsrNext(); // get next LFSR value
void seedRandom(unsigned int seed); // initialize random number generator
unsigned int getRandom(unsigned int limit); // return random number between 0 and limit-1
void permute(unsigned int values[], size_t size);
void shakeTray(); // "shake" Boggle dice and set up tray
void printTray(); // print tray in 4x4 grid
void startTimer(unsigned int seconds); // count seconds
unsigned int scoreWord(const char word[]); // check if word in grid and return score
bool findWord(const char word[]); // check if word is in grid
// global variables
// justification
// (1) Values are needed by multiple functions.
// (2) Passing as arguments makes the function interfaces awkward and hard to use.
// (3) There's no need for keeping multiple local copies of these data structures.
unsigned int lfsr = 1;
unsigned int trayDice[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
char trayLetters[16] = {0};
const char dice[16][6] = {{'A', 'A', 'C', 'I', 'O', 'T'}, // die 0
{'A', 'H', 'M', 'O', 'R', 'S'}, // die 1
{'E', 'G', 'K', 'L', 'U', 'Y'}, // die 2
{'A', 'B', 'I', 'L', 'T', 'Y'}, // die 3
{'A', 'C', 'D', 'E', 'M', 'P'}, // die 4
{'E', 'G', 'I', 'N', 'T', 'V'}, // die 5
{'G', 'I', 'L', 'R', 'U', 'W'}, // die 6
{'E', 'L', 'P', 'S', 'T', 'U'}, // die 7
{'D', 'E', 'N', 'O', 'S', 'W'}, // die 8
{'A', 'C', 'E', 'L', 'R', 'S'}, // die 9
{'A', 'B', 'J', 'M', 'O', 'Q'}, // die 10
{'E', 'E', 'F', 'H', 'I', 'Y'}, // die 11
{'E', 'H', 'I', 'N', 'P', 'S'}, // die 12
{'D', 'K', 'N', 'O', 'T', 'U'}, // die 13
{'A', 'D', 'E', 'N', 'V', 'Z'}, // die 14
{'B', 'I', 'F', 'O', 'R', 'X'} // die 15
};
// Boggle-ish main function
// (1) Gets random seed and timer duration from user
// (2) Set up letter tray
// (3) Run timer -- user writes down words
// (4) User provides list of words. Program checks and scores -- enter "q" to quit.
int main() {
unsigned int seed;
unsigned int seconds;
printf("Enter a random seed: ");
fflush(stdout);
scanf("%i", &seed);
seedRandom(seed); // if zero, seed is ignored
printf("Timer (seconds): ");
fflush(stdout);
scanf("%d", &seconds);
shakeTray();
printTray();
startTimer(seconds);
char word[18] = "";
unsigned int total = 0;
unsigned int score = 0;
while (1) {
// get word from user, at most 16 characters
printf("Word: ");
fflush(stdout);
scanf("%17s", word);
if (strcasecmp(word, "q") == 0) break; // "q" means quit
// convert to uppercase, to make comparisons easier
// also, check for any non-letter characters, and report a score of zero
bool justLetters = true;
for (int i = 0; word[i] && (i < 17); i++) {
if (isalpha(word[i])) word[i] = toupper(word[i]);
else {
justLetters = false;
break; // no need to look any further
}
}
if (justLetters) score = scoreWord(word);
else score = 0;
total += score;
printf("Word score = %d, Total score = %d\n", score, total);
}
printf("TOTAL: %d\n", total);
return 0;
}
// lfsrNext -- function to advance an LFSR for pseudorandom number generation
// uses global variable (lfsr)
// code from: https://en.wikipedia.org/wiki/Linear-feedback_shift_register
unsigned int lfsrNext(void) {
lfsr ^= lfsr >> 7;
lfsr ^= lfsr << 9;
lfsr ^= lfsr >> 13;
return lfsr;
}
// startTimer -- counts from 0 to (seconds-1)
// prints dots and numbers to help user keep track of time
// prints a message when time expires
void startTimer(unsigned int seconds) {
unsigned int t = 0;
unsigned int display = 1;
while (t++ < seconds) {
sleep(1); // do nothing for one second
// when wakes up, print either a dot or a number
if ((display % 5) == 0) printf(" %d ", display);
else printf(".");
fflush(stdout);
// at the end of each minute, go to a new line
if (display == 60) { printf("\n"); display = 0; }
display++;
}
if (display != 1) printf("\n");
printf("\nTIME'S UP!\n");
}