-
Notifications
You must be signed in to change notification settings - Fork 0
/
wheel.c
204 lines (178 loc) · 5.65 KB
/
wheel.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*
* P1: Wheel of Fortune
* COSC 301, Operating Systems, Fall 2016
* Team: TODO
*/
// Constants for word list
#define WORDS_FILE "/usr/share/dict/words"
#define MAX_WORD_LENGTH 50
// Constants and variables for wheel
#define WHEEL_SPACES 24
int wheel_amounts[WHEEL_SPACES];
int MAX_AMOUNT = 900;
int MIN_AMOUNT = 100;
#define INCREMENT_AMOUNT 50
// Constants and variables for wheel spin GUI
#define DISPLAY_ROWS 5
#define DISPLAY_COLS (WHEEL_SPACES * 2 + 1)
char* wheel_row[DISPLAY_ROWS];
#define SPIN_VARIANCE 6
#define NSEC_PER_MS 1000000
#define SPIN_TIME 500
#define STOP_SPEED 200
int ENABLE_ANIMATION = 1;
// Constants for user input
#define MAX_INPUT_LENGTH 50
// Constants for game play
int VOWEL_COST = 250;
int NUM_PLAYERS = 2;
int RANDOM_SEED = 1;
// Structure for linked list
struct item {
char *word;
struct item *next;
};
typedef struct item item_t;
/*
* Initializes the amounts and GUI for the wheel.
* DO NOT MODIFY
*/
void initialize_wheel() {
// Populate the spaces on the wheel with dollar amounts
int choices = ((MAX_AMOUNT - MIN_AMOUNT) / INCREMENT_AMOUNT) + 1;
for (int s = 0; s < WHEEL_SPACES; s++) {
wheel_amounts[s] = random() % choices * INCREMENT_AMOUNT + MIN_AMOUNT;
}
// Determine the characters to display for the wheel
for (int r = 0; r < DISPLAY_ROWS-1; r++) {
wheel_row[r] = (char*)malloc(DISPLAY_COLS);
for (int s = 0; s < WHEEL_SPACES; s++) {
char text[DISPLAY_ROWS];
snprintf(text, DISPLAY_ROWS, "%4d", wheel_amounts[s]);
wheel_row[r][s*2] = ' ';
wheel_row[r][s*2+1] = text[r];
}
}
wheel_row[DISPLAY_ROWS-1] = (char*)malloc(DISPLAY_COLS);
memset(wheel_row[DISPLAY_ROWS-1], ' ', DISPLAY_COLS);
wheel_row[DISPLAY_ROWS-1][DISPLAY_COLS-1] = '\0';
}
/*
* Displays a low-tech ASCII animation of a wheel spin.
* @param force how much force to use in spinning the wheel
* @return the amount on the slot where the wheel stops spinning
* DO NOT MODIFY
*/
int spin_wheel(int force) {
static int curr_space = 0;
// Print spaces on wheel
for (int r = 0; r < DISPLAY_ROWS-1; r++) {
printf("%s\n",wheel_row[r]);
}
// Select how many spaces to advance
int num_ticks = WHEEL_SPACES + force * SPIN_VARIANCE;
num_ticks += (random() % SPIN_VARIANCE) - (SPIN_VARIANCE/2);
// Animate wheel spin
if (ENABLE_ANIMATION) {
long start_speed = SPIN_TIME / num_ticks * NSEC_PER_MS;
long slow_down = (STOP_SPEED * NSEC_PER_MS - start_speed) / num_ticks;
struct timespec sleep_time = {0, start_speed };
for (int t = 0; t < num_ticks; t++) {
curr_space = (curr_space + 1) % WHEEL_SPACES;
int col = (curr_space % WHEEL_SPACES) * 2 + 1;
wheel_row[DISPLAY_ROWS-1][col] = '+';
printf("%s\r",wheel_row[DISPLAY_ROWS-1]);
fflush(stdout);
wheel_row[DISPLAY_ROWS-1][col] = ' ';
sleep_time.tv_nsec += slow_down;
nanosleep(&sleep_time, NULL);
}
printf("\n\n");
} else {
curr_space = (curr_space + num_ticks) % WHEEL_SPACES;
int col = (curr_space % WHEEL_SPACES) * 2 + 1;
wheel_row[DISPLAY_ROWS-1][col] = '+';
printf("%s\n\n",wheel_row[DISPLAY_ROWS-1]);
wheel_row[DISPLAY_ROWS-1][col] = ' ';
}
// Return amount on space
return wheel_amounts[curr_space];
}
/*
* Adds an item to the front of the linked list of words.
* @param list_head the first item in the list; NULL if the list is empty
* @param word the word to add
* @return the added item (i.e., the new first item in the list)
* TODO: Write this method
*/
item_t *prepend_item(item_t *list_head, char *word) {
return NULL;
}
/*
* Loads a list of words from a file into a linked list. Words containing
* non-alpha characters are ignored. All words are stored in upper-case.
* @param filepath path to the file of words
* @param words_loaded populated with the number of words loaded from the file
* @return the linked list of words; NULL if an error occurred
* TODO: Write this method
*/
item_t *load_words(const char *filepath, int *words_loaded) {
return NULL;
}
/*
* Destroys a linked list and frees all memory it was using.
* @param list the first item in the list; NULL if the list is empty
* TODO: Write this method
*/
void free_words(item_t *list_head) {
}
/*
* Chooses a random word from a linked list of words.
* @param list_head the first item in the list; NULL if the list is empty
* @param length the number of words in the list
* @return the chosen word; NULL if an error occurred
* TODO: Write this method
*/
char *choose_random_word(item_t *list_head, int length) {
return "COLGATE";
}
/*
* Play a single round of wheel of fortune.
* @param word the puzzle
* @return the number of the player who won
* TODO: Write this method
*/
int play_round(const char *word) {
return 0;
}
/*
* Play wheel of fortune using words loaded from a file.
*/
int main() {
// Initialize wheel
initialize_wheel();
// Select random seed
srandom(RANDOM_SEED);
// Load words
int numwords = 0;
item_t *list_head = load_words(WORDS_FILE, &numwords);
if (NULL == list_head) {
printf("Failed to load words from %s\n", WORDS_FILE);
return 1;
}
// Select a word
char *word = choose_random_word(list_head, numwords);
if (NULL == word) {
printf("Failed to choose a word\n");
return 1;
}
// Play game
int winner = play_round(word);
printf("Player %d solved the puzzle!\n", winner);
// Clean up
free_words(list_head);
}