1: #include "stdio.h"
2: #include "lrnref"
3:
4: int nsave = 0;
5:
6: selunit()
7: {
8: char fnam[20], s[50];
9: static char dobuff[50];
10: char posslev[20][20];
11: int diff[20], i, k, m, n, best, alts;
12: FILE *f;
13: char zb[200];
14: static char saved[20];
15:
16: while (ask) {
17: printf("What lesson? ");
18: fflush(stdout);
19: gets(dobuff);
20: if (strcmp(dobuff, "bye") == 0)
21: wrapup(0);
22: level = todo = dobuff;
23: sprintf(s, "../../%s/L%s", sname, dobuff);
24: if (access(s, 04) == 0)
25: return;
26: printf("no such lesson\n");
27: }
28: alts = 0;
29: retry:
30: f=scrin;
31: if (f==NULL) {
32: sprintf(fnam, "../../%s/L%s", sname, level);
33: f = fopen(fnam, "r");
34: if (f==NULL) {
35: fprintf(stderr, "No script for lesson %s.\n", level);
36: wrapup(1);
37: }
38: while (fgets(zb, 200, f)) {
39: trim(zb);
40: if (strcmp(zb, "#next")==0)
41: break;
42: }
43: }
44: if (feof(f)) {
45: printf("Congratulations; you have finished this sequence.\n");
46: fflush(stdout);
47: todo = 0;
48: return;
49: }
50: for(i=0; fgets(s, 50, f); i++) {
51: sscanf(s, "%s %d", posslev[i], &diff[i]);
52: }
53: best = -1;
54: /* cycle through lessons from random start */
55: /* first try the current place, failing that back up to
56: last place there are untried alternatives (but only one backup) */
57: n = grand()%i;
58: for(k=0; k<i; k++) {
59: m = (n+k)%i;
60: if (already(posslev[m],0)) continue;
61: if (best<0) best=m;
62: /* real alternatives */
63: alts++;
64: if (abs(diff[m]-speed) < abs(diff[best]-speed))
65: best=m;
66: }
67: if (best < 0 && nsave) {
68: nsave--;
69: strcpy(level, saved);
70: goto retry;
71: }
72: if (best <0) {
73: /* lessons exhausted or missing */
74: printf("Sorry, there are no alternative lessons at this stage.\n");
75: printf("See someone for help.\n");
76: fflush(stdout);
77: todo = 0;
78: return;
79: }
80: strcpy (dobuff, posslev[best]);
81: if (alts>1) {
82: nsave=1;
83: strcpy (saved, level);
84: }
85: todo = dobuff;
86: fclose(f);
87: }
88:
89: abs(x)
90: {
91: return(x>=0? x: -x);
92: }
93:
94: grand()
95: {
96: static int garbage;
97: int a[2], b;
98:
99: time(a);
100: b = a[1]+10*garbage++;
101: return(b&077777);
102: }
Defined functions
abs
defined in line
89; used 2 times
grand
defined in line
94; used 1 times
Defined variables
nsave
defined in line
4; used 3 times