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