summaryrefslogtreecommitdiff
path: root/usr.bin/learn/lib/editor/L44.1g
blob: 0ea3d7894c700d1fa8015939deab27be037f9cf3 (plain)
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
#print
Of course, you can also search for lines that
contain backslashes; again, it's simply a matter
of doubling the backslash in the search pattern.
That is,
   /\\/
finds the next line with a backslash.

Find the second occurrence of "\n\n" in the file
"prog.c", and type "answer N", where N is its
line number.
#create prog.c

int  nsave  = 0;

selunit()
{
  char fnam[20], s[50];
  static char dobuff[50];
  char posslev[20][20];
  int diff[20], i, k, m, n, best, alts, statb[20];
  FILE *f;
  char zb[200];
  static char saved[20];

  while (ask) {
    printf("What lesson? ");
    fflush(stdout);
    gets(dobuff);
    if (strcmp(dobuff, "bye") == 0)
      wrapup(0);
    level = todo = dobuff;
    snprintf(s, sizeof s, "../L%s", dobuff);
    if (access(s, 04) == 0)
      return;
    printf("no such lesson\n");
  }
  alts = 0;
retry:
  f=scrin;
  if (f==NULL) {
    snprintf(fnam, sizeof fnam, "../L%s", level);
    f = fopen(fnam, "r");
    if (f==NULL) {
      fprintf(stderr, "No script for lesson %s.\n", level);
      wrapup(1);
    }
    while (fgets(zb, 200, f)) {
      trim(zb);
      if (strcmp(zb, "#next")==0)
        break;
    }
  }
  if (feof(f)) {
    printf("Congratulations\n\n.You have finished this sequence.\n");
    fflush(stdout);
    todo = 0;
    return;
  }
  for(i=0; fgets(s, 50, f); i++) {
    sscanf(s, "%s %d", &posslev[i], &diff[i]);
  }
  best = -1;
  /* cycle through lessons from random start */
  /* first try the current place, failing that back up to
       last place there are untried alternatives (but only one backup) */
  n = grand()%i;
  for(k=0; k<i; k++) {
    m = (n+k)%i;
    if (already(posslev[m])) continue;
    if (best<0) best=m;
    /* real alternatives */
    alts++;
    if (abs(diff[m]-speed) < abs(diff[best]-speed))
      best=m;
  }
  if (best < 0 && nsave) {
    nsave--;
    strlcpy(level, saved, sizeof level);
    goto retry;
  }
  if (best <0) {
    /* lessons exhausted or missing */
    printf("Sorry, there are no alternative lessons at this stage.\n");
    printf("See someone for help.\n\n");
    fflush(stdout);
    todo = 0;
    return;
  }
  strlcpy(dobuff, posslev[best], sizeof dobuff);
  if (alts>1) {
    nsave=1;
    strcpy(saved, level, sizeof saved);
  }
  todo = dobuff;
  fclose(f);
}

abs(x)
{
  return(x>=0? x: -x);
}

grand()
{
  static int garbage;
  int a[2], b;

  time(a);
  b = a[1]+10*garbage++;
  return(b&077777);
}
#copyin
#user
#uncopyin
#match 73
#bad 43
The second______ one.
#log
#next
44.1h 10