Home | History | Annotate | Line # | Download | only in dab
      1 /*	$NetBSD: main.cc,v 1.7 2021/12/05 09:22:45 rillig Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Main dots program
     34  */
     35 
     36 #include "defs.h"
     37 RCSID("$NetBSD: main.cc,v 1.7 2021/12/05 09:22:45 rillig Exp $")
     38 
     39 #include <stdio.h>
     40 #include <unistd.h>
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <err.h>
     44 #include "algor.h"
     45 #include "board.h"
     46 #include "human.h"
     47 #include "ttyscrn.h"
     48 
     49 GAMESCREEN *sc;
     50 
     51 // Print the command line usage
     52 static void usage(char* pname)
     53 {
     54     char* p = strrchr(pname, '/');
     55     if (p)
     56 	p++;
     57     else
     58 	p = pname;
     59     (void)::fprintf(stderr,
     60 	"Usage: %s [-w] [-p <c|h><c|h>] [-n <ngames>] [<ydim> [<xdim>]]\n", p);
     61 }
     62 
     63 // Play a single game
     64 static void play(BOARD& b, PLAYER* p[2])
     65 {
     66     // Initialize
     67     b.init();
     68     p[0]->init();
     69     p[1]->init();
     70     b.paint();
     71 
     72     // Alternate turns between players, scoring each turn
     73     for (size_t i = 0;; i = (i + 1) & 1) {
     74 	b.score(i, *p[i]);
     75 	if (!p[i]->domove(b)) {
     76 	    // No more moves, game over
     77 	    break;
     78 	}
     79 	b.score(i, *p[i]);
     80     }
     81 
     82     // Find who won
     83     p[0]->wl(p[1]->getScore());
     84     p[1]->wl(p[0]->getScore());
     85 
     86     // Post scores
     87     b.score(0, *p[0]);
     88     b.score(1, *p[1]);
     89 
     90     // Post totals
     91     b.total(0, *p[0]);
     92     b.total(1, *p[1]);
     93 
     94     // Post games
     95     b.games(0, *p[0]);
     96     b.games(1, *p[1]);
     97 
     98     // Post ties
     99     b.ties(*p[0]);
    100 }
    101 
    102 int main(int argc, char** argv)
    103 {
    104     size_t ny, nx, nn = 1, wt = 0;
    105     const char* nc = "ch";
    106     int c;
    107     int acs = 1;
    108 
    109     while ((c = getopt(argc, argv, "awp:n:")) != -1)
    110 	switch (c) {
    111 	case 'a':
    112 	    acs = 0;
    113 	    break;
    114 	case 'w':
    115 	    wt++;
    116 	    break;
    117 
    118 	case 'p':
    119 	    nc = optarg;
    120 	    break;
    121 
    122 	case 'n':
    123 	    nn = atoi(optarg);
    124 	    break;
    125 
    126 	default:
    127 	    usage(argv[0]);
    128 	    return 1;
    129 	}
    130 
    131     // Get the size of the board if specified
    132     switch (argc - optind) {
    133     case 0:
    134 	ny = nx = 3;
    135 	break;
    136 
    137     case 1:
    138 	ny = nx = atoi(argv[optind]);
    139 	break;
    140 
    141     case 2:
    142 	nx = atoi(argv[optind]);
    143 	ny = atoi(argv[optind+1]);
    144 	break;
    145 
    146     default:
    147 	usage(argv[0]);
    148 	return 1;
    149     }
    150 
    151 
    152     PLAYER* p[2];
    153 
    154     // Allocate players
    155     for (size_t i = 0; i < 2; i++) {
    156 	char n = nc[1] == nc[0] ? i + '0' : nc[i];
    157 	switch (nc[i]) {
    158 	case 'c':
    159 	    p[i] = new ALGOR(n);
    160 	    break;
    161 
    162 	case 'h':
    163 	    p[i] = new HUMAN(n);
    164 	    break;
    165 
    166 	default:
    167 	    usage(argv[0]);
    168 	    return 1;
    169 	}
    170     }
    171 
    172     sc = TTYSCRN::create(acs, &ny, &nx);
    173     if (sc == NULL)
    174 	::errx(1, "Dimensions too large for current screen.");
    175 
    176     BOARD b(ny, nx, sc);
    177 
    178     // Play games
    179     while (nn--) {
    180 	play(b, p);
    181 	if (wt)
    182 	    b.getmove();
    183     }
    184 
    185     if (wt == 0)
    186 	b.getmove();
    187     // Cleanup
    188     delete sc;
    189     delete p[0];
    190     delete p[1];
    191     return 0;
    192 }
    193