main.cc revision 1.6 1 1.6 christos /* $NetBSD: main.cc,v 1.6 2012/10/06 19:39:51 christos 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.1 christos * main.C: Main dots program
34 1.1 christos */
35 1.1 christos #include "defs.h"
36 1.6 christos RCSID("$NetBSD: main.cc,v 1.6 2012/10/06 19:39:51 christos Exp $")
37 1.1 christos
38 1.4 christos #include <stdio.h>
39 1.1 christos #include <unistd.h>
40 1.1 christos #include <stdlib.h>
41 1.1 christos #include <string.h>
42 1.1 christos #include <err.h>
43 1.1 christos #include "algor.h"
44 1.1 christos #include "board.h"
45 1.1 christos #include "human.h"
46 1.1 christos #include "ttyscrn.h"
47 1.1 christos
48 1.3 jdc GAMESCREEN *sc;
49 1.3 jdc
50 1.1 christos // Print the command line usage
51 1.1 christos static void usage(char* pname)
52 1.1 christos {
53 1.1 christos char* p = strrchr(pname, '/');
54 1.1 christos if (p)
55 1.1 christos p++;
56 1.1 christos else
57 1.1 christos p = pname;
58 1.4 christos (void)::fprintf(stderr,
59 1.4 christos "Usage: %s [-w] [-p <c|h><c|h>] [-n <ngames>] [<ydim> [<xdim>]]\n", p);
60 1.1 christos }
61 1.1 christos
62 1.1 christos // Play a single game
63 1.1 christos static void play(BOARD& b, PLAYER* p[2])
64 1.1 christos {
65 1.1 christos // Initialize
66 1.1 christos b.init();
67 1.1 christos p[0]->init();
68 1.1 christos p[1]->init();
69 1.1 christos b.paint();
70 1.1 christos
71 1.1 christos // Alternate turns between players, scoring each turn
72 1.1 christos for (size_t i = 0;; i = (i + 1) & 1) {
73 1.1 christos b.score(i, *p[i]);
74 1.1 christos if (!p[i]->domove(b)) {
75 1.1 christos // No more moves, game over
76 1.1 christos break;
77 1.1 christos }
78 1.1 christos b.score(i, *p[i]);
79 1.1 christos }
80 1.1 christos
81 1.1 christos // Find who won
82 1.1 christos p[0]->wl(p[1]->getScore());
83 1.1 christos p[1]->wl(p[0]->getScore());
84 1.1 christos
85 1.1 christos // Post scores
86 1.1 christos b.score(0, *p[0]);
87 1.1 christos b.score(1, *p[1]);
88 1.1 christos
89 1.1 christos // Post totals
90 1.1 christos b.total(0, *p[0]);
91 1.1 christos b.total(1, *p[1]);
92 1.1 christos
93 1.1 christos // Post games
94 1.1 christos b.games(0, *p[0]);
95 1.1 christos b.games(1, *p[1]);
96 1.1 christos
97 1.1 christos // Post ties
98 1.1 christos b.ties(*p[0]);
99 1.1 christos }
100 1.1 christos
101 1.1 christos int main(int argc, char** argv)
102 1.1 christos {
103 1.1 christos size_t ny, nx, nn = 1, wt = 0;
104 1.2 thorpej const char* nc = "ch";
105 1.1 christos int c;
106 1.1 christos int acs = 1;
107 1.1 christos
108 1.1 christos while ((c = getopt(argc, argv, "awp:n:")) != -1)
109 1.1 christos switch (c) {
110 1.1 christos case 'a':
111 1.1 christos acs = 0;
112 1.1 christos break;
113 1.1 christos case 'w':
114 1.1 christos wt++;
115 1.1 christos break;
116 1.1 christos
117 1.1 christos case 'p':
118 1.1 christos nc = optarg;
119 1.1 christos break;
120 1.1 christos
121 1.1 christos case 'n':
122 1.1 christos nn = atoi(optarg);
123 1.1 christos break;
124 1.1 christos
125 1.1 christos default:
126 1.1 christos usage(argv[0]);
127 1.1 christos return 1;
128 1.1 christos }
129 1.1 christos
130 1.1 christos // Get the size of the board if specified
131 1.1 christos switch (argc - optind) {
132 1.1 christos case 0:
133 1.1 christos ny = nx = 3;
134 1.1 christos break;
135 1.1 christos
136 1.1 christos case 1:
137 1.1 christos ny = nx = atoi(argv[optind]);
138 1.1 christos break;
139 1.1 christos
140 1.1 christos case 2:
141 1.1 christos nx = atoi(argv[optind]);
142 1.1 christos ny = atoi(argv[optind+1]);
143 1.1 christos break;
144 1.1 christos
145 1.1 christos default:
146 1.1 christos usage(argv[0]);
147 1.1 christos return 1;
148 1.1 christos }
149 1.1 christos
150 1.1 christos
151 1.1 christos PLAYER* p[2];
152 1.1 christos
153 1.1 christos // Allocate players
154 1.1 christos for (size_t i = 0; i < 2; i++) {
155 1.1 christos char n = nc[1] == nc[0] ? i + '0' : nc[i];
156 1.1 christos switch (nc[i]) {
157 1.1 christos case 'c':
158 1.1 christos p[i] = new ALGOR(n);
159 1.1 christos break;
160 1.1 christos
161 1.1 christos case 'h':
162 1.1 christos p[i] = new HUMAN(n);
163 1.1 christos break;
164 1.1 christos
165 1.1 christos default:
166 1.1 christos usage(argv[0]);
167 1.1 christos return 1;
168 1.1 christos }
169 1.1 christos }
170 1.1 christos
171 1.6 christos sc = TTYSCRN::create(acs, &ny, &nx);
172 1.1 christos if (sc == NULL)
173 1.1 christos ::errx(1, "Dimensions too large for current screen.");
174 1.1 christos
175 1.1 christos BOARD b(ny, nx, sc);
176 1.1 christos
177 1.1 christos // Play games
178 1.1 christos while (nn--) {
179 1.1 christos play(b, p);
180 1.1 christos if (wt)
181 1.1 christos b.getmove();
182 1.1 christos }
183 1.1 christos
184 1.1 christos if (wt == 0)
185 1.1 christos b.getmove();
186 1.1 christos // Cleanup
187 1.1 christos delete sc;
188 1.1 christos delete p[0];
189 1.1 christos delete p[1];
190 1.1 christos return 0;
191 1.1 christos }
192