aboutsummaryrefslogtreecommitdiff
path: root/nmm.c
blob: 104e2fa2d0903512e332f9c4140d9e8a967323ae (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
/*
 * 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#define VERSION "0.1"

#define EMPTY 'E'
#define WHITEC 'W'
#define BLACKC 'B'
#define WHITE 0
#define BLACK 1
#define NORTH 0
#define EAST 1
#define SOUTH 2
#define WEST 3
#define NORTHC 'n'
#define EASTC 'e'
#define SOUTHC 's'
#define WESTC 'w'
#define PIECECOUNT 8

#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

/* Board & game construction */
struct point {
  char v; /* Value: what's currently here */
  struct point* n[4]; /* Our neighbours, indexed by NORTH, SOUTH, EAST, WEST */
};

typedef struct point nboard[3][8];

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

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

void joinns(struct point *, struct point *);
void joinwe(struct point *, struct point *);
void freepoint(struct point *);
struct point makepoint(const char, struct point *, struct point *,
		       struct point *, struct point *);
void updatepoint(struct point *, const char, struct point *, struct point *,
		 struct point *, struct point *);
void initboard(nboard);
void initgame(struct game *);
int gameend(struct scrgame *);
/* Rendering functions */
WINDOW *create_scorebox(const struct game *);
void update_scorebox(WINDOW *, const struct game *);
WINDOW *create_board(const struct game *);
void update_board(WINDOW *, const struct game *);
WINDOW *create_msgbox(void);
void update_msgbox(WINDOW *, const char *);
void initall(struct scrgame *);
/* During game */
void printgame(const struct game *);
int inmill(const struct point *);
struct point *mill_handler(struct scrgame *, char *, int);
int validcoords(const char *);
int dirtoindex(const char);
char statechar(const struct game *);
char *getmove(struct scrgame *, char *, int);
struct point *getpoint(struct game *, const char *);
struct point *placepiece(struct scrgame *, const char *);
int checkadjacent(const struct point *, const struct point *);
struct point *removepiece(struct game *, struct point *);
struct point *jumppiece(struct point *, struct game *, const char *);
struct point *movepiece(struct point *, char);
struct point *phaseone(struct scrgame *);
struct point *phasetwo(struct scrgame *);
int checkdir(const char *);
char* prompt(const char *, char *, const int);
char* promptcheck(const char *, char *, const int, const int,
		  const char *, int (*function)(const char *));
int main(void);

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 7 pieces. The location is selected by entering its\n",
  "  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, for North, South,\n",
  "  East, West. For example, `a1n' moves the piece at a1 north, `g7s' moves\n",
  "  the piece at g7 south. Mills are formed and pieces removed as in\n",
  "  phase 1. Phase 2 continues until a player only has 3 pieces remaining.\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 any key to continue...",
  NULL
};

/*
 * Board and game creation
 */

void
joinns(/*@null@*/ struct point *n, /*@null@*/ struct point *s)
{
  if (n) {
    n->n[SOUTH] = s;
    freepoint(n);
  }
  if (s) {
    s->n[NORTH] = n;
    freepoint(s);
  }
}

void
joinwe(/*@null@*/ struct point *w, /*@null@*/ struct point *e)
{
  if (e) {
    e->n[WEST] = w;
    freepoint(e);
  }
  if (w) {
    w->n[EAST] = e;
    freepoint(w);
  }
}

void freepoint(struct point *p)
{
  if (!p->n[NORTH] && !p->n[SOUTH] && !p->n[EAST] && !p->n[WEST]) {
    /* free(p); */
  }
}

struct point
makepoint(const char v, /*@null@*/ struct point *n, /*@null@*/ struct point *s,
	  /*@null@*/ struct point *e, /*@null@*/ struct point *w)
{
  struct point temp;
  updatepoint(&temp, v, n, s, e, w);
  return temp;
}

void
updatepoint(struct point *p, const char v, struct point *n, struct point *s,
	    struct point *e, struct point *w)
{
  p->v = v;
  joinns(p, s);
  joinns(n, p);
  joinwe(w, p);
  joinwe(p, e);
}

void
initboard(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);        /* 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, &b[r][3], NULL);    /* Bot mid   */
    updatepoint(&b[r][5], EMPTY, NULL, NULL, &b[r][4], NULL);    /* Bot left  */
    updatepoint(&b[r][6], EMPTY, NULL, &b[r][5], NULL, NULL);    /* Mid left  */
    updatepoint(&b[r][7], EMPTY, NULL, &b[r][6], &b[r][0], NULL); /* Top left */
  }
  for (r = 0; r < 2; 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  */
  }
}

void
initgame(struct game *g)
{
  initboard(g->board);
  /* 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;
}

void
initall(struct scrgame *sg)
{
  initgame(sg->game);
  sg->score_w = create_scorebox(sg->game);
  sg->board_w = create_board(sg->game);
  sg->msg_w = create_msgbox();
}

/*
 * Drawing functions
 */

WINDOW
*create_scorebox(const struct 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); */
  mvwprintw(local_win, 1, 2, "Nine Man Morris");
  /* update will call a refresh for us */
  update_scorebox(local_win, g);
  return local_win;
}

void
update_scorebox(WINDOW *w, const struct 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: ");
  }
  wrefresh(w);
}

WINDOW
*create_board(const struct 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);
    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_board(local_win, g);
  return local_win;
}

void
update_board(WINDOW *w, const struct 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);
}

WINDOW
*create_msgbox(void)
{
  return newwin(2, 80-msgcol, msgrow, msgcol);
}

void
update_msgbox(WINDOW *w, const char *msg)
{
  werase(w);
  mvwaddstr(w, 0, 0, msg);
  wrefresh(w);
}

int
gameend(struct 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');
}

/*
 * Functions concerning game logic
 */

int
inmill(const struct point *p)
{
  if (p->v == BLACKC || p->v == WHITEC) {
    int steps = 0;
    int hc = 1;
    int vc = 1; /* horizontal count, vertical count */
    struct point *np = p->n[NORTH];
    struct point *sp = p->n[SOUTH];
    struct point *ep = p->n[EAST];
    struct point *wp = p->n[WEST];

    /* We make up to three steps in each direction to account for when
       we're on the edge */
    for (steps = 0; steps < 3; steps++) {
      if (np && np->v == p->v) {
	vc++;
	np = np->n[NORTH];
      }
      if (sp && sp->v == p->v) {
	vc++;
	sp = sp->n[SOUTH];
      }
      if (ep && ep->v == p->v) {
	hc++;
	ep = ep->n[EAST];
      }
      if (wp && wp->v == p->v) {
	hc++;
	wp = wp->n[WEST];
      }
    }
    return (hc == 3 || vc == 3);
  }
  /* If we're EMPTY, we're clearly not in a mill */
  return 0;
}

int
adjacent(const struct point *p1, const struct point *p2)
{
  return (p2 == p1->n[EAST] || p2 == p1->n[WEST] ||
	  p2 == p1->n[NORTH] || p2 == p1->n[SOUTH]);
}

char
statechar(const struct game *g)
{
  return g->state == WHITE ? WHITEC : BLACKC;
}

struct point*
mill_handler(struct scrgame *sg, char *move, int count)
{
  struct 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);
  }
}

struct point*
placepiece(struct scrgame *sg, const char *coords)
{
  struct 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;
  }
}

int
checkdir(const char *dir)
{
  if (isalpha(dir[0])) {
    char d;
    d = tolower(dir[0]);
    return (d == NORTHC || d == SOUTHC || d == EASTC || d == WESTC);
  }
  return 0;
}

int
validcoords(const char *coords)
{
  /* TODO: Why doesn't this work? */
  if ((sizeof(coords) / sizeof(*coords)) < 2) {
    return 0;
  }
  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;
    }
}

char
*getmove(struct scrgame *sg, char *move, int l)
{
  int length = 0;
  int ch = 0;
  int c = 0;
  if (l < 3 || 5 < l) {
    if (sg->game->phase == 1) {
      /* Of form d3 */
      length = 3;
    } else if (sg->game->phase == 2) {
      /* Of form d3s */
      length = 4;
    } else if (sg->game->phase == 3) {
      if (sg->game->pieces[(int) sg->game->state] == 3) {
	/* Of form d3a1 */
	length = 5;
      } else {
	/* Of form d3s */
	length = 4;
      }
    }
  } else {
    length = l;
  }
  /* Clear the prompt area */
  mvwaddstr(sg->score_w, promptrow, promptcol, "          |");
  for (c = 0; c < length - 1; c++) {
    /* Store into int to make sure we don't miss out on special
       ncurses keys, such as KEY_BACKSPACE */
    ch = mvwgetch(sg->score_w, promptrow, promptcol + c);
    if (ch == '\b' || ch == KEY_BACKSPACE) {
      /* We don't want users erasing the whole screen */
      if (c > 0) {
	mvwaddch(sg->score_w, promptrow, promptcol + c, ' ');
	c--;
      } else {
	mvwaddch(sg->score_w, promptrow, promptcol, ' ');
      }
      continue;
    } else if (isascii(ch)) {
      if (ch == '\n') {
	move[c] = ch;
	c++;
	break;
      } else {
	move[c] = ch;
	mvwaddch(sg->score_w, promptrow, promptcol + c, ch);
      }
    } else {
      update_msgbox(sg->msg_w, "Unexpected non-ASCII input");
      return getmove(sg, move, length);
    }
  }
  move[0] = tolower(move[0]);
  move[2] = tolower(move[2]);
  if (!validcoords(move) ||
      (length == 5 && !validcoords(&move[2]))) {
    update_msgbox(sg->msg_w, "Invalid coordinates");
    return getmove(sg, move, length);
  }
  if (length == 4) {
    if (!(move[2] == NORTHC || move[2] == SOUTHC ||
	  move[2] == EASTC || move[2] == WESTC)) {
      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[dirtoindex(move[2])])) {
      update_msgbox(sg->msg_w, "Impossible to move in that direction");
      return getmove(sg, move, length);
    }
  }
  move[length - 1] = '\0';
  return move;
}

struct point
*getpoint(struct game *g, const char *coords)
{
  int r, c = 0;
  if (validcoords(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:
	errx(1, "Reached an unreachable branch in getpoint");
      }
    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;
}


struct point*
phaseone(struct scrgame *sg)
{
  struct point *p = NULL;
  char coords[4];
  for (; sg->game->totalpieces < PIECECOUNT; 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, "");
  }
  sg->game->phase = 2;
  update_scorebox(sg->score_w, sg->game);
  return p;
}

struct point*
phasetwo(struct scrgame *sg)
{
  struct point *p = NULL;
  char coords[6];
  while (sg->game->pieces[WHITE] >= 3 && sg->game->pieces[BLACK] >= 3) {
    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;
}

int
dirtoindex(const char dir)
{
  switch (dir)
    {
    case NORTHC:
      return NORTH;

    case SOUTHC:
      return SOUTH;

    case EASTC:
      return EAST;

    case WESTC:
      return WEST;

    default:
      return -1;
    }
}

struct point*
movepiece(struct 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;
}

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

struct point*
removepiece(struct game *g, struct 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;
      errx(ENOTSUP, "Tried removing EMPTY. This should NEVER happen.");
    }
    p->v = EMPTY;
    return p;
  }
  return NULL;
}

int
main(void)
{
  struct scrgame *sg;
  const char *const *instr;
  int c;
  initscr();
  keypad(stdscr, TRUE);
  clear();
  refresh();
  printw("Display instructions? (y/n) ");
  c = getch();
  c = tolower(c);
  if (c == 'y') {
    clear(); 
    for (instr = instructions; *instr; instr++) {
      printw("%s", *instr);
    }
    refresh();
    getch();
  }
  clear();
  refresh();
  noecho();
  sg = malloc(sizeof(struct scrgame));
  if (sg) {
    sg->game = malloc(sizeof(struct game));
    if (sg->game) {
      mvprintw(25, 57, "nmm version %s", VERSION);
      for (;;) {
	initall(sg);
	refresh();
	phaseone(sg);
	phasetwo(sg);
	if (!gameend(sg)) {
	  break;
	}
      }
    }
  } else {
    refresh();
    endwin();
    errx(ENOMEM, "Unable to allocate enough memory.");
  }
  refresh();
  endwin();
  return 0;
}