]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - dab/main.C
import dots+boxes games.
[bsdgames-darwin.git] / dab / main.C
1 /* $NetBSD: main.C,v 1.1.1.1 2003/12/26 17:57:03 christos 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * main.C: Main dots program
41 */
42 #include "defs.h"
43 RCSID("$Id: main.C,v 1.1.1.1 2003/12/26 17:57:03 christos Exp $")
44
45 #include <iostream>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <err.h>
50 #include "algor.h"
51 #include "board.h"
52 #include "human.h"
53 #include "ttyscrn.h"
54
55 // Print the command line usage
56 static void usage(char* pname)
57 {
58 char* p = strrchr(pname, '/');
59 if (p)
60 p++;
61 else
62 p = pname;
63 std::cerr << "Usage: " << p
64 << " [-w] [-p <c|h><c|h>] [-n <ngames>] [<ydim> [<xdim>]]" << std::endl;
65 }
66
67 // Play a single game
68 static void play(BOARD& b, PLAYER* p[2])
69 {
70 // Initialize
71 b.init();
72 p[0]->init();
73 p[1]->init();
74 b.paint();
75
76 // Alternate turns between players, scoring each turn
77 for (size_t i = 0;; i = (i + 1) & 1) {
78 b.score(i, *p[i]);
79 if (!p[i]->domove(b)) {
80 // No more moves, game over
81 break;
82 }
83 b.score(i, *p[i]);
84 }
85
86 // Find who won
87 p[0]->wl(p[1]->getScore());
88 p[1]->wl(p[0]->getScore());
89
90 // Post scores
91 b.score(0, *p[0]);
92 b.score(1, *p[1]);
93
94 // Post totals
95 b.total(0, *p[0]);
96 b.total(1, *p[1]);
97
98 // Post games
99 b.games(0, *p[0]);
100 b.games(1, *p[1]);
101
102 // Post ties
103 b.ties(*p[0]);
104 }
105
106 int main(int argc, char** argv)
107 {
108 size_t ny, nx, nn = 1, wt = 0;
109 char* nc = "ch";
110 int c;
111 int acs = 1;
112
113 while ((c = getopt(argc, argv, "awp:n:")) != -1)
114 switch (c) {
115 case 'a':
116 acs = 0;
117 break;
118 case 'w':
119 wt++;
120 break;
121
122 case 'p':
123 nc = optarg;
124 break;
125
126 case 'n':
127 nn = atoi(optarg);
128 break;
129
130 default:
131 usage(argv[0]);
132 return 1;
133 }
134
135 // Get the size of the board if specified
136 switch (argc - optind) {
137 case 0:
138 ny = nx = 3;
139 break;
140
141 case 1:
142 ny = nx = atoi(argv[optind]);
143 break;
144
145 case 2:
146 nx = atoi(argv[optind]);
147 ny = atoi(argv[optind+1]);
148 break;
149
150 default:
151 usage(argv[0]);
152 return 1;
153 }
154
155
156 PLAYER* p[2];
157
158 // Allocate players
159 for (size_t i = 0; i < 2; i++) {
160 char n = nc[1] == nc[0] ? i + '0' : nc[i];
161 switch (nc[i]) {
162 case 'c':
163 p[i] = new ALGOR(n);
164 break;
165
166 case 'h':
167 p[i] = new HUMAN(n);
168 break;
169
170 default:
171 usage(argv[0]);
172 return 1;
173 }
174 }
175
176 GAMESCREEN *sc = TTYSCRN::create(acs, ny, nx);
177 if (sc == NULL)
178 ::errx(1, "Dimensions too large for current screen.");
179
180 BOARD b(ny, nx, sc);
181
182 // Play games
183 while (nn--) {
184 play(b, p);
185 if (wt)
186 b.getmove();
187 }
188
189 if (wt == 0)
190 b.getmove();
191 // Cleanup
192 delete sc;
193 delete p[0];
194 delete p[1];
195 return 0;
196 }