]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - dab/main.cc
Tidy up curses state when we quit, instead of just doing exit().
[bsdgames-darwin.git] / dab / main.cc
1 /* $NetBSD: main.cc,v 1.3 2005/07/02 15:48:03 jdc 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("$NetBSD: main.cc,v 1.3 2005/07/02 15:48:03 jdc 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 GAMESCREEN *sc;
56
57 // Print the command line usage
58 static void usage(char* pname)
59 {
60 char* p = strrchr(pname, '/');
61 if (p)
62 p++;
63 else
64 p = pname;
65 std::cerr << "Usage: " << p
66 << " [-w] [-p <c|h><c|h>] [-n <ngames>] [<ydim> [<xdim>]]" << std::endl;
67 }
68
69 // Play a single game
70 static void play(BOARD& b, PLAYER* p[2])
71 {
72 // Initialize
73 b.init();
74 p[0]->init();
75 p[1]->init();
76 b.paint();
77
78 // Alternate turns between players, scoring each turn
79 for (size_t i = 0;; i = (i + 1) & 1) {
80 b.score(i, *p[i]);
81 if (!p[i]->domove(b)) {
82 // No more moves, game over
83 break;
84 }
85 b.score(i, *p[i]);
86 }
87
88 // Find who won
89 p[0]->wl(p[1]->getScore());
90 p[1]->wl(p[0]->getScore());
91
92 // Post scores
93 b.score(0, *p[0]);
94 b.score(1, *p[1]);
95
96 // Post totals
97 b.total(0, *p[0]);
98 b.total(1, *p[1]);
99
100 // Post games
101 b.games(0, *p[0]);
102 b.games(1, *p[1]);
103
104 // Post ties
105 b.ties(*p[0]);
106 }
107
108 int main(int argc, char** argv)
109 {
110 size_t ny, nx, nn = 1, wt = 0;
111 const char* nc = "ch";
112 int c;
113 int acs = 1;
114
115 while ((c = getopt(argc, argv, "awp:n:")) != -1)
116 switch (c) {
117 case 'a':
118 acs = 0;
119 break;
120 case 'w':
121 wt++;
122 break;
123
124 case 'p':
125 nc = optarg;
126 break;
127
128 case 'n':
129 nn = atoi(optarg);
130 break;
131
132 default:
133 usage(argv[0]);
134 return 1;
135 }
136
137 // Get the size of the board if specified
138 switch (argc - optind) {
139 case 0:
140 ny = nx = 3;
141 break;
142
143 case 1:
144 ny = nx = atoi(argv[optind]);
145 break;
146
147 case 2:
148 nx = atoi(argv[optind]);
149 ny = atoi(argv[optind+1]);
150 break;
151
152 default:
153 usage(argv[0]);
154 return 1;
155 }
156
157
158 PLAYER* p[2];
159
160 // Allocate players
161 for (size_t i = 0; i < 2; i++) {
162 char n = nc[1] == nc[0] ? i + '0' : nc[i];
163 switch (nc[i]) {
164 case 'c':
165 p[i] = new ALGOR(n);
166 break;
167
168 case 'h':
169 p[i] = new HUMAN(n);
170 break;
171
172 default:
173 usage(argv[0]);
174 return 1;
175 }
176 }
177
178 sc = TTYSCRN::create(acs, ny, nx);
179 if (sc == NULL)
180 ::errx(1, "Dimensions too large for current screen.");
181
182 BOARD b(ny, nx, sc);
183
184 // Play games
185 while (nn--) {
186 play(b, p);
187 if (wt)
188 b.getmove();
189 }
190
191 if (wt == 0)
192 b.getmove();
193 // Cleanup
194 delete sc;
195 delete p[0];
196 delete p[1];
197 return 0;
198 }