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