aboutsummaryrefslogtreecommitdiff
path: root/nmm.c
blob: 117a65d334d4e7693b2355d5f56bf2dd3de50d0d (plain) (blame)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
/*
 * Copyright (C) 2013 Ryan Kavanagh <rak@debian.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <ctype.h>
#include <curses.h>
#include <err.h>
#include <errno.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/cdefs.h>

/*
 * __dead isn't defined everywhere; although it's typically installed
 * in sys/cdefs.h. The following is based off of OpenBSD's
 * sys/cdefs.h
 */
#if !defined(__dead) && defined(__dead2)
#define __dead	__dead2
#elif !defined(__dead) && defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define __dead	__volatile
#elif !defined(__dead) && !defined(__STRICT_ANSI__)
#define __dead	__atribute__((__noreturn__))
#elif !defined(__dead)
#define __dead	/* NORETURN */
#endif

#define VERSION "1.0"

#define EMPTY 'E'
#define WHITEC 'W'
#define BLACKC 'B'
#define WHITE 0
#define BLACK 1
#define NORTH 0
#define SOUTH 1
#define WEST 2
#define EAST 3
#define NE 4
#define SW 5
#define NW 6
#define SE 7
#define MINDIR NORTH
#define MAXDIR SE
#define NORTHC "n"
#define EASTC "e"
#define SOUTHC "s"
#define WESTC "w"
#define NEC "ne"
#define NWC "nw"
#define SEC "se"
#define SWC "sw"

#define ROWS 3
#define COLS 9

#define NMM 4
#define TMM 3
#define TWMM 12

#define sbrow 3         /* score box */
#define sbcol 37
#define brdrow 2        /* board     */
#define brdcol 4
#define legendsep 3     /* distance board<->legend */
#define msgrow 20       /* message box */
#define msgcol 7
#define promptcol 16
#define promptrow 7
#define versrow 17      /* version col calculated based on helpcol */
#define helpcol 65
#define helprow 0

/* Board & game construction */
typedef struct point {
  char v; /* Value: what's currently here */
  struct point* n[8]; /* Our neighbours, indexed by NORTH, SOUTH, WEST, EAST,
			 NE, SW, NW, SE, see dirc below */
} point;

typedef point* nboard[ROWS][COLS];

typedef struct game {
  nboard board;
  int state;
  int pieces[2];
  int totalpieces;
  int phase;
  int type;
} game;

typedef struct scrgame {
  struct game *game;
  WINDOW *score_w;
  WINDOW *board_w;
  WINDOW *msg_w;
} scrgame;

/* Let's make looking up directions -> indices easier */
struct strkey { char *k; int v; };

static const struct strkey dirc[] = {
  { NORTHC , NORTH }, { SOUTHC, SOUTH }, { EASTC, EAST }, { WESTC, WEST },
  { NEC, NE }, { NWC, NW }, { SEC, SE }, { SWC, SW },
  { 0 } /* GCC will incorrectly complain about this, GCC BUG#53119 */
};

__BEGIN_DECLS
int	 joinpts(const int, point *, point *);
void	 joinns(point *, point *);
void	 joinwe(point *, point *);
void	 joinnesw(point *, point *);
void	 joinnwse(point *, point *);
void	 freepoint(point *);
int	 dirtoindex(const char *);
point	 makepoint(const char, point *, point *,
		   point *, point *);
void	 updatepoint(point *, const char, point *, point *,
		     point *, point *);
void	 init3board(nboard);
void	 init9board(nboard);
void	 init12board(nboard);
void	 initgame(game *, const int);
void	 initall(scrgame *, const int);
/*	 Rendering functions */
WINDOW	*create_scorebox(const game *);
void	 update_scorebox(WINDOW *, const game *);
WINDOW	*create_board(const game *);
WINDOW	*create_3board(const game *);
WINDOW	*create_9board(const game *);
WINDOW	*create_12board(const game *);
void	 update_board(WINDOW *, const game *);
void	 update_3board(WINDOW *, const game *);
void	 update_9board(WINDOW *, const game *);
WINDOW	*create_msgbox(void);
void	 update_msgbox(WINDOW *, const char *);
char	*getgname(const game *);
void	 full_redraw(scrgame *);
void	 printinstrs(scrgame *);
int	 gameend(const scrgame *);
__dead void	 quit(void);
/*	 During game */
int	 surrounded(const game *);
int	 inmill(const point *);
point	*mill_handler(scrgame *, char *, int);
char	 statechar(const game *);
int	 validcoords(const game *, const char *);
int	 valid3coords(const char *);
int	 valid9coords(const char *);
int      checkdir(const char *);
char    *getinput(scrgame *, char *, const int);
char	*getmove(scrgame *, char *, int);
point	*getpoint(const game *, const char *);
point	*get3point(const game *, const char *);
point	*get9point(const game *, const char *);
point	*placepiece(const scrgame *, const char *);
point	*movepiece(point *, const char *);
point   *jumppiece(point *, game *, const char *);
point   *removepiece(game *, point *);
point	*phaseone(scrgame *);
point	*phasetwothree(scrgame *);
char	*lower(char *);
int	 main(int, char **);
__END_DECLS

const char *const instructions[] = {
  "The board consists of positions connected with dashed lines, initially\n",
  "marked empty (E), but then filled with black (B) and white (W) pieces.\n",
  "Players place, move, and remove pieces, and the winner is the first to\n",
  "reduce the opponent to 2 pieces. Gameplay is split into three phases.\n",
  "\n",
  "Phase 1. Users alternatingly place a piece on the board, until each\n",
  "  player has placed 3/9/12 pieces. The location is selected by entering\n",
  "  its coordinates, e.g. `a1' or `g7'. A mill can be formed by aligning\n",
  "  three pieces along a dashed line, at which point an opponent's piece\n",
  "  is removed. This piece is selected by entering its coordinates.\n",
  "Phase 2. Users alternatingly slide one of their pieces along dashed line\n",
  "  to an adjacent empty position. The piece is selected by entering its\n",
  "  coordinates and its direction is chosen from n s e w ne nw se sw, for\n",
  "  North, South, East, West, etc. For example, `a1n' moves the piece at\n",
  "  a1 north, `g7sw' moves the piece at g7 south west. Mills are formed\n",
  "  and pieces removed as in phase 1. Phase 2 continues until a player\n",
  "  only has 3 pieces remaining or can make no move.\n",
  "Phase 3. The player with three pieces may move a piece to any empty\n",
  "  location, e.g. `a1g7' moves the piece at a1 to location g7. The player\n",
  "  with more than three pieces moves as in phase 2. The first player to\n",
  "  reduce the opponent to two pieces wins.\n",
  "\n",
  "Press `?' to display these instructions, and `q' to quit.\n",
  "\n",
  "Press any key to continue...",
  NULL
};

/* **************************
 * Board and game creation
 * ************************** */

/*
 * Connect two points, with p1 in direction L1 of p2
 */
int
joinpts(const int L1, /*@null@*/ point *p1, /*@null@*/ point *p2)
{
  /* Check L1 % 2 == 0 to make sure L1 == NORTH,WEST,NE,NW */
  if (MINDIR <= L1 && L1 <= MAXDIR && L1 % 2 == 0) {
    if (p1) {
      p1->n[L1+1] = p2;
      freepoint(p1);
    }
    if (p2) {
      p2->n[L1] = p1;
      freepoint(p2);
    }
    return 1;
  }
  return 0;
}

/*
 * Join the north point n to the south point s
 */
void
joinns(/*@null@*/ point *n, /*@null@*/ point *s)
{
  joinpts(NORTH, n, s);
}

/*
 * Join the west point w to the east point e
 */
void
joinwe(/*@null@*/ point *w, /*@null@*/ point *e)
{
  joinpts(WEST, w, e);
}

/*
 * Join the north east point ne to the south west point sw
 */
void
joinnesw(/*@null@*/ point *ne, /*@null@*/ point *sw)
{
  joinpts(NE, ne, sw);
}

/*
 * Join the north west point nw to the south east point se
 */
void
joinnwse(/*@null@*/ point *nw, /*@null@*/ point *se)
{
  joinpts(NW, nw, se);
}

/*
 * Free the memory allocated to a point if it has no neighbours
 */
void
freepoint(point *p)
{
  int i, joined = 0;
  for (i = 0; i <= MAXDIR; i++) {
    if (p->n[i]) {
      joined = 1;
      break;
    }
  }
  if (!joined) {
    /* p has no neighbours */
    /* FIXME: Why does this crash with modified chunk-pointer? */
    /* free(p); */
  }
}

/*
 * Converts a direction to a neighbour index
 */
int
dirtoindex(const char *dir)
{
  const struct strkey *ptr;
  /* dirc is bound by a sentinal with null key */
  for (ptr = dirc; ptr->k; ptr++) {
    if (strncmp(ptr->k, dir, 2) == 0) {
      return ptr->v;
    }
  }
  return -1;
}

/*
 * Given a label and four main neighbours, create a point
 */
point
makepoint(const char v, /*@null@*/ point *n, /*@null@*/ point *s,
	  /*@null@*/ point *e, /*@null@*/ point *w)
{
  point temp;
  updatepoint(&temp, v, n, s, e, w);
  return temp;
}

/*
 * Update the value and four main neighbours of a point p
 */
void
updatepoint(point *p, const char v, point *n, point *s,
	    point *w, point *e)
{
  p->v = v;
  joinns(p, s);
  joinns(n, p);
  joinwe(w, p);
  joinwe(p, e);
}

/*
 * Create the board for Three Man Morris
 */
void
init3board(nboard b)
{
  int r = 0;
  /* Initialize everything to an empty grid */
  for (r = 0; r < 3; r++) {
    updatepoint(b[r][0], EMPTY, NULL, NULL, NULL, NULL);
    updatepoint(b[r][1], EMPTY, NULL, NULL, b[r][0], NULL);
    updatepoint(b[r][2], EMPTY, NULL, NULL, b[r][1], NULL);
    b[r][3] = NULL;
  }
  for (r = 0; r < 2; r++) {
    joinns(b[r][0], b[r+1][0]);
    joinns(b[r][1], b[r+1][1]);
    joinns(b[r][2], b[r+1][2]);
  }
}

/*
 * Create the board for Nine Man Morris
 */
void
init9board(nboard b)
{
  int r = 0;
  /* Initialize everything to an empty grid */
  for (r = 0; r < 3; r++) {
    updatepoint(b[r][0], EMPTY, NULL, NULL, b[r][7], NULL); /* Top mid   */
    updatepoint(b[r][1], EMPTY, NULL, NULL, b[r][0], NULL); /* Top right */
    updatepoint(b[r][2], EMPTY, b[r][1], NULL, NULL, NULL); /* Mid right */
    updatepoint(b[r][3], EMPTY, b[r][2], NULL, NULL, NULL); /* Bot right */
    updatepoint(b[r][4], EMPTY, NULL, NULL, NULL, b[r][3]); /* Bot mid   */
    updatepoint(b[r][5], EMPTY, NULL, NULL, NULL, b[r][4]); /* Bot left  */
    updatepoint(b[r][6], EMPTY, NULL, b[r][5], NULL, NULL); /* Mid left  */
    updatepoint(b[r][7], EMPTY, NULL, b[r][6], NULL, NULL); /* Top left  */
    b[r][8] = NULL;
  }
  for (r = 0; r < 2; r++) {
    printw("\n\n%d\n\n\n", r);
    joinns(b[r][0], b[r+1][0]); /* Top vertical line */
    joinns(b[r+1][4], b[r][4]); /* Bot vertical line */
    joinwe(b[r][6], b[r+1][6]); /* Left horiz line   */
    joinwe(b[r+1][2], b[r][2]); /* Right horiz line  */
  }
}

/*
 * Create the board for Twelve Man Morris
 */
void
init12board(nboard b)
{
  int r = 0;
  init9board(b);
  for (r = 0; r < 2; r++) {
    /* Top right */
    joinnesw(b[r][1], b[r+1][1]);
    /* Bottom right */
    joinnwse(b[r+1][3], b[r][3]);
    /* Bottom left */
    joinnesw(b[r+1][5], b[r][5]);
    /* Top left */
    joinnwse(b[r][7], b[r+1][7]);
  }
}

/*
 * Initialize a game with the appropriate boards, reset scores, etc.
 */
void
initgame(game *g, const int type)
{
  switch (type)
  {
  case TMM:
    init3board(g->board);
    g->type = TMM;
    break;

  case TWMM:
    init12board(g->board);
    g->type = TWMM;
    break;

  default:
    init9board(g->board);
    g->type = NMM;
  }
  /* We'll assume that, as in chess, black moves first */
  g->state = BLACK;
  g->phase = 1;
  g->totalpieces = 0;
  g->pieces[WHITE] = 0;
  g->pieces[BLACK] = 0;
}

/*
 * Initialize the board and all of the windors in a scrgame
 */
void
initall(scrgame *sg, const int type)
{
  initgame(sg->game, type);
  full_redraw(sg);
}

/* ********************
 * Drawing functions
 * ******************** */

/*
 * Create the scorebox / prompt window
 */
WINDOW *
create_scorebox(const game *g)
{
  WINDOW *local_win;

  /* WINDOW *newwin(int nlines, int ncols, int begin_y,
     int begin_x); */
  local_win = newwin(10, 27, sbrow, sbcol);
  wborder(local_win, '|', '|', '-', '-', '+', '+', '+', '+');
  /* Uncomment the following for the curses box, but I prefer the
     "rustic" boxes drawn with pipes, hyphens and plusses */
  /* box(local_win, 0, 0); */
  switch (g->type)
  {
  case TMM:
    mvwprintw(local_win, 1, 2, "Three Man Morris");
    break;
  case TWMM:
    mvwprintw(local_win, 1, 2, "Twelve Man Morris");
    break;
  default:
    mvwprintw(local_win, 1, 2, "Nine Man Morris");
    break;
  }
  /* update will call a refresh for us */
  update_scorebox(local_win, g);
  return local_win;
}

/*
 * Update the score, phase, and current player
 */
void
update_scorebox(WINDOW *w, const game *g)
{
  mvwprintw(w, 3, 2, "Phase %d", g->phase);
  mvwprintw(w, 4, 2, "White pieces: %d", g->pieces[WHITE]);
  mvwprintw(w, 5, 2, "Black pieces: %d", g->pieces[BLACK]);
  if (g->state == BLACK) {
    mvwprintw(w, 7, 2, "Black's move: ");
  } else {
    mvwprintw(w, 7, 2, "White's move: ");
  }
  mvwaddstr(w, promptrow, promptcol, "          |");
  wrefresh(w);
}

/*
 * Determine the type of board to draw and draw it
 */
WINDOW *
create_board(const game *g)
{
  switch (g->type)
  {
  case TMM:
    return create_3board(g);

  case TWMM:
    return create_12board(g);

  default:
    return create_9board(g);
  }
}

/*
 * Draw board for Three Man Morris
 */
WINDOW *
create_3board(const game *g)
{
  WINDOW *local_win;
  int r = 0;
  char c = 'a';

  local_win = newwin(13 + legendsep, 21 + legendsep, brdrow, brdcol);

  /* Draw the legend */
  for (r = 0; r < 3; r++) {
    mvwprintw(local_win, 6*r, 0, "%d", 3-r);
  }
  c = 'a';
  for (r = 0; r < 21; r += 9) {
    mvwaddch(local_win, 14, legendsep + r, c++);
  }
  /* Draw the board lines */
  for (r = 0; r < 3; r++) {
    mvwhline(local_win, 6*r, legendsep, '-', 18);
    mvwvline(local_win, 1, legendsep + 9*r, '|', 11);
  }

  update_3board(local_win, g);
  return local_win;
}

/*
 * Draw board for Nine Man Morris
 */
WINDOW *
create_9board(const game *g)
{
  WINDOW *local_win;
  int r = 0;
  char c = 'a';

  local_win = newwin(13 + legendsep, 21 + legendsep, brdrow, brdcol);

  /* Draw the legend */
  for (r = 0; r < 7; r++) {
    mvwprintw(local_win, 2*r, 0, "%d", 7-r);
  }
  c = 'a';
  for (r = 0; r < 21; r += 3) {
    mvwaddch(local_win, 14, legendsep + r, c++);
  }

  /* Draw the board lines */
  for (r = 0; r < 3; r++) {
    /* Top line */
    mvwhline(local_win, 2*r, legendsep + 3*r, '-', 18 - 6*r);
    /* Left line */
    mvwvline(local_win, 2*r, legendsep + 3*r, '|', 13 - 4*r);
    /* Bottom line */
    mvwhline(local_win, 12 - 2*r, legendsep + 3*r, '-', 18 - 6*r);
    /* Right line */
    mvwvline(local_win, 2*r, 18 + legendsep - 3*r, '|', 13 - 4*r);
  }
  /* TV crossline */
  mvwvline(local_win, 1, legendsep + 9, '|', 3);
  /* LH crossline */
  mvwhline(local_win, 6, legendsep + 1, '-', 5);
  /* BV crossline */
  mvwvline(local_win, 9, legendsep + 9, '|', 3);
  /* RH crossline */
  mvwhline(local_win, 6, legendsep + 13, '-', 5);

  update_9board(local_win, g);
  return local_win;
}

/*
 * Draw board for Twelve Man Morris
 */
WINDOW *
create_12board(const game *g)
{
  WINDOW *local_win;
  int r = 0;
  local_win = create_9board(g);
  /* Draw the diagonal lines */
  for (r = 0; r < 2; r++) {
    /* Top left */
    mvwaddch(local_win, 2*r + 1, legendsep + 3*r + 2, '\\');
    /* Top right */
    mvwaddch(local_win, 2*r + 1, legendsep + 16 - 3*r, '/');
    /* Bottom left */
    mvwaddch(local_win, 11 - 2*r, legendsep + 3*r + 2, '/');
    /* Bottom right */
    mvwaddch(local_win, 11 - 2*r, legendsep + 16 - 3*r, '\\');
  }
  wrefresh(local_win);
  return local_win;
}

/*
 * Select the correct function to redraw points
 */
void
update_board(WINDOW *w, const game *g)
{
  switch (g->type)
  {
  case TMM:
    update_3board(w, g);
    break;

  case TWMM:
  case NMM:
  default:
    update_9board(w, g);
  }
}

/*
 * Redraw points for Three Man Morris
 */
void
update_3board(WINDOW *w, const game *g)
{
  int r = 0;
  for (r = 0; r < 3; r++) {
    mvwaddch(w, 6*r, legendsep, g->board[r][0]->v);
    mvwaddch(w, 6*r, legendsep + 9, g->board[r][1]->v);
    mvwaddch(w, 6*r, legendsep + 18, g->board[r][2]->v);
  }
  wrefresh(w);
}

/*
 * Redraw points for Nine and Twelve Man Morris
 */
void
update_9board(WINDOW *w, const game *g)
{
  int r = 0;
  for (r = 0; r < 3; r++) {
    mvwaddch(w, 2*r, legendsep + 9, g->board[r][0]->v);
    mvwaddch(w, 2*r, legendsep + 18 - 3*r, g->board[r][1]->v);
    mvwaddch(w, 6, legendsep + 18 - 3*r, g->board[r][2]->v);
    mvwaddch(w, 12-2*r, legendsep + 18 - 3*r, g->board[r][3]->v);
    mvwaddch(w, 12-2*r, legendsep + 9, g->board[r][4]->v);
    mvwaddch(w, 12-2*r, legendsep + 3*r, g->board[r][5]->v);
    mvwaddch(w, 6, legendsep + 3*r, g->board[r][6]->v);
    mvwaddch(w, 2*r, legendsep + 3*r, g->board[r][7]->v);
  }
  wrefresh(w);
}

/*
 * Create window for displaying messages
 */
WINDOW *
create_msgbox(void)
{
  return newwin(2, 80-msgcol, msgrow, msgcol);
}

/*
 * Change message displayed in window
 */
void
update_msgbox(WINDOW *w, const char *msg)
{
  werase(w);
  mvwaddstr(w, 0, 0, msg);
  wrefresh(w);
}

char *
getgname(const game *g)
{
  switch (g->type)
  {
  case TMM:
    return "tmm";
    
  case TWMM:
    return "twmm";
    
  case NMM:
  default:
    return "nmm";
  }
}

/*
 * Completely redraw *everything*
 */
void
full_redraw(scrgame *sg)
{
  static char *name = NULL;
  static size_t vers_len;
  static const char *helpstr = "? : help";
  static const size_t helplen = 8;
  if (!name) {
    name = getgname(sg->game);
  }
  /* 10 = strlen(" version ") */
  vers_len = strlen(name) + strlen(VERSION) + 10;
  delwin(sg->board_w);
  delwin(sg->score_w);
  delwin(sg->msg_w);
  clear();
  /* We want the ends of the version string and of
   * helpstr to line up. */
  mvprintw(versrow, helpcol + helplen - vers_len,
	   "%s version %s", name, VERSION);
  mvprintw(helprow, helpcol, helpstr);
  refresh();
  sg->board_w = create_board(sg->game);
  sg->score_w = create_scorebox(sg->game);
  sg->msg_w = create_msgbox();
}

/*
 * Print instructions to screen and prompt to continue
 */
void
printinstrs(scrgame *sg)
{
  /* TODO: Factor out the refresh-everything part */
  const char *const *instr;
  clear();
  for (instr = instructions; *instr; instr++) {
    printw("%s", *instr);
  }
  refresh();
  getch();
  full_redraw(sg);
}

/*
 * Announce winner, prompt user to play again, non-zero if user wants
 * to.
 */
int
gameend(const scrgame *sg)
{
  char c;
  if (sg->game->pieces[WHITE] < sg->game->pieces[BLACK]) {
    update_msgbox(sg->msg_w,
		  "Black wins! Play again?");
  } else {
    update_msgbox(sg->msg_w,
		  "White wins! Play again?");
  }
  mvwprintw(sg->score_w, promptrow, 2, "Play again?:     ");
  wrefresh(sg->score_w);
  c = mvwgetch(sg->score_w, promptrow, promptcol);
  c = tolower(c);
  return (c == 'y');
}

/*
 * Properly end curses and exit
 */
__dead void
quit(void)
{
  refresh();
  endwin();
  exit(0);
}

/* ********************************
 * Functions concerning game logic
 * ******************************** */

/*
 * Checks if the current player can slide to any adjacent position.
 */
int
surrounded(const game *g)
{
  int r, c, n;
  char currplayerch = statechar(g);
  c = 0;
  for (r = 0; g->board[r][c]; r++) {
    for (c = 0; g->board[r][c]; c++) {
      if (g->board[r][c]->v == currplayerch) {
	for (n = MINDIR; n <= MAXDIR; n++) {
	  if (g->board[r][c]->n[n] &&
	      g->board[r][c]->n[n]->v == EMPTY) {
	    return 0;
	  }
	}
      }
    }
  }
  return 1;
}

/*
 * Is the point p in a mill?
 */
int
inmill(const point *p)
{
  if (p->v == BLACKC || p->v == WHITEC) {
    point *neighbours[8];
    int counts[4] = {1, 1, 1, 1};
    int i, dir = MINDIR;
    for (dir = MINDIR; dir <= MAXDIR; dir++) {
      neighbours[dir] = p->n[dir];
    }

    /* We should walk at most 2 steps in any direction */
    for (i = 0; i < 2; i++) {
      for (dir = MINDIR; dir <= MAXDIR; dir++) {
	if (neighbours[dir] && neighbours[dir]->v == p->v) {
	  /* Makes use of the fact that directions along the same axis
	   * are 1 apart (e.g. SOUTH = NORTH + 1 = 1), and that
	   * division truncates toward zero */
	  counts[dir / 2]++;
	  neighbours[dir] = neighbours[dir]->n[dir];
	}
      }
    }
    /* If we have three in a row along any axis, we're in a mill */
    for (i = 0; i < 4; i++) {
      if (counts[i] == 3) {
	return 1;
      }
    }
  }
  /* If we're EMPTY, we're clearly not in a mill */
  return 0;
}

/*
 * Handle a mill by dealing with the removal of an opponent's piece.
 * Prompts the user for a piece to remove, checks its legality, and
 * removes it. Return the removed piece, recursing until a piece is
 * removed.
 */
point *
mill_handler(scrgame *sg, char *move, int count)
{
  point *p = NULL;

  if (count == 0) {
    update_msgbox(sg->msg_w,
		  "You've formed a mill, enter opponent piece to remove.");
  }
  getmove(sg, move, 3);
  if ((p = getpoint(sg->game, move))) {
    if (p->v != statechar(sg->game) && p->v != EMPTY) {
      if (inmill(p)) {
	/* We can only break an opponent's mill if there are no other
	   pieces to remove. We could keep a list of each player's
	   pieces, but this implies the headache of always updating
	   it. Alternatively, we can just iterate through at most 21
	   points, until we find an opponent piece not in a mill, or
	   nothing. */
	int r = 0;
	int c = 0;
	int opp = 0;
	if (sg->game->state == WHITE) {
	  opp = BLACKC;
	} else {
	  opp = WHITEC;
	}
	for (r = 0; r < 3; r++) {
	  for (c = 0; c < 7; c++) {
	    if (sg->game->board[r][c]->v == opp && !inmill(sg->game->board[r][c])) {
	      update_msgbox(sg->msg_w,
			    "It is possible to remove a piece not in a mill; do so.");
	      return mill_handler(sg, move, 1);
	    }
	  }
	}
	/* By this point, we've iterated through all of the opponent's
	   pieces without encountering one not in a mill. Allow the
	   desired piece to be removed. */
      }
      p->v = EMPTY;
      /* Decrement the opponent's pieces */
      sg->game->pieces[sg->game->state ^ BLACK]--;
      return p;
    } else if (p->v == EMPTY) {
      update_msgbox(sg->msg_w,
		    "You tried clearing an empty position. Please try again.");
      return mill_handler(sg, move, 1);
    } else {
      update_msgbox(sg->msg_w,
		    "You tried removing your own piece. Please try again.");
      return mill_handler(sg, move, 1);
    }
  } else {
    update_msgbox(sg->msg_w, "Invalid coordinates. Please try again.");
    return mill_handler(sg, move, 1);
  }
}

/*
 * Return the char corresponding to the current state
 */
char
statechar(const game *g)
{
  return g->state == WHITE ? WHITEC : BLACKC;
}

/*
 * Checks the validity of coordinates for a game
 * Assumes coords are lower case and of length 2
 */
int
validcoords(const game *g, const char *coords)
{
  switch (g->type)
  {
  case TMM:
    return valid3coords(coords);

  case NMM:
  case TWMM:
  default:
    return valid9coords(coords);
  }
}

/*
 * Checks the validity of coordinates for Three Man Morris
 * Assumes coords are lower case and of length 2
 */
int
valid3coords(const char *coords)
{
  return ('a' <= coords[0] && coords[0] <= 'c' &&
	  '1' <= coords[1] && coords[1] <= '3');
}

/*
 * Checks the validity of coordinates for Nine, Twelve Man Morris
 * Assumes coords are lower case and of length 2
 */
int
valid9coords(const char *coords)
{
  if (coords[0] < 'a' || 'g' < coords[0] || coords[1] < '0' || '7' < coords[1]) {
    return 0;
  }
  switch (coords[0])
  {
  case 'a': case 'g':
    return (coords[1] == '1' || coords[1] == '4' || coords[1] == '7');

  case 'b': case 'f':
    return (coords[1] == '2' || coords[1] == '4' || coords[1] == '6');

  case 'c': case 'e':
    return ('3' <= coords[1] && coords[1] <= '5');

  case 'd':
    return (coords[1] != '4');

  default:
    /* UNREACHABLE */
    return 0;
  }
}

/*
 * Checks the validity of a direction
 */
int
checkdir(const char *dir)
{
  if (isalpha(dir[0])) {
    char d[3];
    int i;
    for (i = 0; i < 2; i++) {
      d[i] = tolower(dir[0]);
    }
    d[2] = '\0';
    i = dirtoindex(d);
    return (MINDIR <= i && i <= MAXDIR);
  }
  return 0;
}

/*
 * Prompt user for input, handling backspace, return.
 * q twice at start of line: quit()
 * ? at start of line: printinstrs()
 * Otherwise, read up to length characters from sg->scorer_w into inp
 * Assume than inp always has room for at least '\0'
 * Recurse until no errors, return pointer to inp.
 */
char *
getinput(scrgame *sg, char *inp, const int length)
{
  int l, ch, i, quitc;
  quitc = 0;
  /* Clear the prompt area */
  mvwaddstr(sg->score_w, promptrow, promptcol, "          |");
  wrefresh(sg->score_w);
  for (l = 0; l < length - 1; l++) {
    ch = mvwgetch(sg->score_w, promptrow, promptcol + l);
    if (ch == 12) {
      /* We were given a ^L */
      full_redraw(sg);
      update_msgbox(sg->msg_w, "");
      for (i = 0; i < l; i++) {
	mvwaddch(sg->score_w, promptrow, promptcol + i, inp[i]);
      }
      wrefresh(sg->score_w);
      l--;
      continue;
    }
    if (ch == '\b' || ch == KEY_BACKSPACE ||
	ch == KEY_DC || ch == 127) {
      /* We need to retake the current character; l++ will increase
       * it, so counteract that with an l-- to remove the BACKSPACE. */
      l--;
      if (l >= 0) {
	/* Erase the input char from the screen */
	mvwaddch(sg->score_w, promptrow, promptcol + l, ' ');
	/* And another l-- so that we overwrite the input char from
	 * inp on the next iteration of this loop. */
	l--;
      } else {
	/* We're at the start of the line, so there was no input char
	 * to erase */
      }
      wrefresh(sg->score_w);
      update_msgbox(sg->msg_w, "");
      continue;
    } else if (isalnum(ch)) {
      if (ch == 'q' && l == 0) {
	if (quitc) {
	  quit();
	} else {
	  quitc = 1;
	  update_msgbox(sg->msg_w, "Enter 'q' again to quit");
	  mvwaddch(sg->score_w, promptrow, promptcol, ' ');
	  l--;
	}
      } else {
	inp[l] = (char) ch;
	mvwaddch(sg->score_w, promptrow, promptcol + l, ch);
      }
      wrefresh(sg->score_w);
    } else if (ch == '?' && l == 0) {
      printinstrs(sg);
      mvwaddch(sg->score_w, promptrow, promptcol, ' ');
      l--;
    } else if (ch == '\n') {
      inp[l] = '\0';
      break;
    } else {
      update_msgbox(sg->msg_w, "Unexpected non-ASCII input");
      return getinput(sg, inp, length);
    }
  }
  /* l <= length-1 */
  inp[l] = '\0';
  return inp;
}

/*
 * Prompts the user for a move with getinput, sanitizes it, and makes
 * the move. It stores the move in *move, you can specify the maximum
 * length l of the move to take, this should be at most the length of
 * move. If mill is less than three or more than five, move should
 * hold at least 5 characters. Returns pointer to the move.
 */
char *
getmove(scrgame *sg, char *move, int l)
{
  int length, pieces, index, twmm = 0;
  /* Do we only have 3 pieces, i.e. are we in skipping mode? */
  pieces = sg->game->pieces[(int) sg->game->state];
  if (l < 3 || 5 < l) {
    if (sg->game->type == TWMM) {
      /* We need to add one to our length to make up for the
       * possibility of ne/nw/se/sw over n/e/s/w */
      twmm = 1;
    }
    switch (sg->game->phase)
    {
    case 1:
      /* In piece placing mode */
      length = 3;
      break;

    case 2:
      /* Of form d3s or d3sw */
      length = 4 + twmm;
      break;

    case 3:
      if (pieces == 3) {
	/* Of form d3a1 */
	length = 5;
      } else {
	/* Of form d3s or d3sw */
	length = 4 + twmm;
      }
      break;

    default:
      length = 0;
      /* NOTREACHED */
    }
  } else {
    length = l;
  }
  move = getinput(sg, move, length);
  move = lower(move);
  if (!validcoords(sg->game, move) ||
      /* Check the length to make sure we're not in mill mode */
      (pieces == 3 && length != 3 && !validcoords(sg->game, &move[2]))) {
    update_msgbox(sg->msg_w, "Invalid coordinates");
    return getmove(sg, move, length);
  }
  /* If we'e in piece sliding stage. The length check makes sure we're
     not in mill mode. */
  if (sg->game->phase != 1 && pieces != 3 && length != 3) {
    index = dirtoindex(&move[2]);
    if (index == -1) {
      update_msgbox(sg->msg_w, "Invalid direction");
      return getmove(sg, move, length);
    } else if (!(getpoint(sg->game, /* We already checked that move[2]
				       is safe */
			  move)->n[index])) {
      update_msgbox(sg->msg_w, "Impossible to move in that direction");
      return getmove(sg, move, length);
    }
  }
  move[length - 1] = '\0';
  return move;
}

/*
 * Given a coordinate array, retrieve the corresponding point by
 * calling the appropriate function.
 */
point *
getpoint(const game *g, const char *coords)
{
  switch (g->type)
  {
  case TMM:
    return get3point(g, coords);

  case NMM:
  case TWMM:
  default:
    return get9point(g, coords);
  }
}

/*
 * Given a coordinate array, retrieve the corresponding point
 */
point *
get3point(const game *g, const char *coords)
{
  int r;
  if (validcoords(g, coords)) {
    r = 2 + '1' - coords[1];
    /* = 2 - (coords[0] - '1'); */
    return g->board[r][coords[0] - 'a'];
  }
  return NULL;
}


/*
 * Given a coordinate array, retrieve the corresponding point
 */
point *
get9point(const game *g, const char *coords)
{
  int r, c = 0;
  if (validcoords(g, coords)) {
    switch (coords[1])
    {
    case '4':
      if (coords[0] < 'd') {
	r = coords[0] - 'a';
	c = 6;
      } else {
	r = 'g' - coords[0];
	c = 2;
      }
      break;

    case '1': case '7':
      r = 0;
      break;

    case '2': case '6':
      r = 1;
      break;

    case '3': case '5':
      r = 2;
      break;

    default:
      /* NOTREACHED */
      endwin();
      errx(1, "Reached an unreachable branch in getpoint");
      r = 0; /* Shut up gcc complaining that r may be
		used unitialized */
    }
    if (coords[1] != '4') {
      if (coords[0] < 'd') {
	c = coords[1] < '4' ? 5 : 7;
      } else if (coords[0] == 'd') {
	c = coords[1] < '4' ? 4 : 0;
      } else {
	c = coords[1] < '4' ? 3 : 1;
      }
    }
    return g->board[r][c];
  }
  return NULL;
}

/*
 * Place a piece corresponding to the current player at the coordinates coords.
 */
point *
placepiece(const scrgame *sg, const char *coords)
{
  point *p = NULL;
  if ((p = getpoint(sg->game, coords))) {
    if (p->v == EMPTY) {
      p->v = statechar(sg->game);
      return p;
    } else {
      update_msgbox(sg->msg_w, "That location is already occupied, please try again.");
      return NULL;
    }
  } else {
    update_msgbox(sg->msg_w, "Invalid coordinates. Please try again.");
    return NULL;
  }
}


/*
 * Move a piece from p in direction dir
 */
point *
movepiece(point *p, const char *dir)
{
  int index;
  index = dirtoindex(dir);
  if (p->n[index] && p->n[index]->v == EMPTY) {
    p->n[index]->v = p->v;
    p->v = EMPTY;
    return p->n[index];
  }
  return NULL;
}

/*
 * Move point p to the given position
 */
point *
jumppiece(point *p, game *g, const char *position)
{
  point *ptr = getpoint(g, position);
  if (ptr && ptr->v == EMPTY) {
    ptr->v = p->v;
    p->v = EMPTY;
    return ptr;
  }
  return NULL;
}

/*
 * Remove a point by setting it to empty and decrementing the piece
 * counts
 */
point *
removepiece(game *g, point *p)
{
  if (p && g) {
    /* We let stupid people remove their own pieces */
    if (p->v == BLACKC) {
      g->pieces[BLACK]--;
    } else if (p->v == WHITE) {
      g->pieces[WHITE]--;
    } else {
      errno = ENOTSUP;
      endwin();
      errx(ENOTSUP, "Tried removing EMPTY. This should NEVER happen.");
    }
    p->v = EMPTY;
    return p;
  }
  return NULL;
}

/*
 * Phase one of the game
 * Returns NULL if one player is guaranteed to have less than 3 pieces
 * at the end of the phase, thus loosing.
 */
point *
phaseone(scrgame *sg)
{
  point *p = NULL;
  char coords[4];
  int maxrem = 0;
  for (; sg->game->totalpieces < 2 * sg->game->type; sg->game->totalpieces++) {
    getmove(sg, coords, 0);
    if (!(p = placepiece(sg, coords))) {
      sg->game->totalpieces--;
      continue;
    }
    sg->game->pieces[sg->game->state]++;
    if (inmill(p)) {
      /* We should redraw the board now so that the player can see the
	 piece he just played */
      update_scorebox(sg->score_w, sg->game);
      update_board(sg->board_w, sg->game);
      update_msgbox(sg->msg_w, "");
      mill_handler(sg, coords, 0);
    }
    sg->game->state ^= BLACK;
    update_scorebox(sg->score_w, sg->game);
    update_board(sg->board_w, sg->game);
    update_msgbox(sg->msg_w, "");
    maxrem = (2 * sg->game->type - sg->game->totalpieces) / 2;
    if (sg->game->pieces[0] + maxrem < 3 ||
	sg->game->pieces[1] + maxrem < 3) {
      /* It's impossible for a player to place more than 3 pieces, abort */
      return NULL;
    }
  }
  if (sg->game->type == TMM) {
    /* Three Man Morris doesn't have a phase 2 */
    sg->game->phase = 3;
  } else {
    sg->game->phase = 2;
  }
  update_scorebox(sg->score_w, sg->game);
  return p;
}

/*
 * Phases two and three of the game
 */
point *
phasetwothree(scrgame *sg)
{
  point *p = NULL;
  char coords[6];
  while (sg->game->pieces[WHITE] >= 3 && sg->game->pieces[BLACK] >= 3) {
    if (sg->game->pieces[sg->game->state] > 3 && surrounded(sg->game)) {
      update_scorebox(sg->score_w, sg->game);
      update_board(sg->board_w, sg->game);
      return NULL;
    }
    getmove(sg, coords, 0);
    if (!(p = getpoint(sg->game, coords))) {
      update_msgbox(sg->msg_w, "Something went wrong...");
      continue;
    }
    if (p->v != statechar(sg->game)) {
      update_msgbox(sg->msg_w, "Please move your own piece.");
      continue;
    }
    if (sg->game->pieces[sg->game->state] == 3) {
      if (!(p = jumppiece(p, sg->game, &coords[2]))) {
	update_msgbox(sg->msg_w,
		      "That location is already occupied. Please try again");
	continue;
      }
    } else if (!(p = movepiece(p, &coords[2]))) {
      update_msgbox(sg->msg_w,
		    "That location is already occupied. Please try again");
      continue;
    }
    if (inmill(p)) {
      /* We should redraw the board now so that the player can see the
	 piece he just played */
      update_scorebox(sg->score_w, sg->game);
      update_board(sg->board_w, sg->game);
      update_msgbox(sg->msg_w, "");
      mill_handler(sg, coords, 0);
    }
    sg->game->state ^= BLACK;
    if (sg->game->pieces[BLACK] == 3 || sg->game->pieces[WHITE] == 3) {
      sg->game->phase = 3;
    }
    update_scorebox(sg->score_w, sg->game);
    update_board(sg->board_w, sg->game);
    update_msgbox(sg->msg_w, "");
  }
  return p;
}

/*
 * Applies tolower() to every element of a null-terminated string
 * Will do bad things(TM) if the string is not null-terminated
 */
char*
lower(char *s)
{
  char *c = s;
  for (c = s; *c; c++) {
    *c = tolower(*c);
  }
  return s;
}

/*
 * The Big Cheese
 */
int
main(int argc, char *argv[])
{
  scrgame *sg;
  int c;
  char *bn = basename(argv[0]);
  if (!bn || errno) {
    /* basename can return a NULL pointer, causing a segfault on
       strncmp below */
    errx(errno, "Something went wrong in determining the %s",
	 "filename by which nmm was called.");
  }
  if (argc != 1) {
    errx(EINVAL, "%s doesn't take any arguments.", bn);
  }
  if ((sg = malloc(sizeof(*sg)))) {
    if ((sg->game = malloc(sizeof(*sg->game)))) {
      point *board;
      if ((board = calloc(ROWS * COLS, sizeof(*board)))) {
	int r;
	for (r = 0; r < ROWS; r++) {
	  for (c = 0; c < COLS; c++) {
	    sg->game->board[r][c] = board++;
	  }
	}
      } else {
	errx(errno, "Unable to allocate memory for the board");
      }
    } else {
      errx(errno, "Unable to allocate memory for the game");
    }
  } else {
    errx(errno, "Unable to allocate memory");
  }
  initscr();
  cbreak();
  keypad(stdscr, TRUE);
  clear();
  printw("Display instructions? (y/n) ");
  refresh();
  c = getch();
  c = tolower(c);
  if (c == 'y') {
    printinstrs(sg);
  }
  clear();
  noecho();
  refresh();
  if (strncmp("tmm", bn, 3) == 0) {
    c = TMM;
  } else if (strncmp("twmm", bn, 4) == 0) {
    c = TWMM;
  } else {
    c = NMM;
  }
  for (;;) {
    initall(sg, c);
    refresh();
    phaseone(sg);
    phasetwothree(sg);
    if (!gameend(sg)) {
      break;
    }
  }
  refresh();
  endwin();
  return 0;
}