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