main.cc 4.5 KB

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