home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / games / volume15 / reversi2 / part02 < prev    next >
Encoding:
Internet Message Format  |  1993-01-26  |  21.8 KB

  1. Path: uunet!ogicse!news.u.washington.edu!uw-beaver!zephyr.ens.tek.com!master!saab!billr
  2. From: billr@saab.CNA.TEK.COM (Bill Randle)
  3. Newsgroups: comp.sources.games
  4. Subject: v15i019:  reversi2 - another reversi game, Part02/02
  5. Message-ID: <3834@master.CNA.TEK.COM>
  6. Date: 29 Oct 92 16:28:19 GMT
  7. Article-I.D.: master.3834
  8. Sender: news@master.CNA.TEK.COM
  9. Lines: 1056
  10. Approved: billr@saab.CNA.TEK.COM
  11. Xref: uunet comp.sources.games:1518
  12.  
  13. Submitted-by: esafern@shearson.COM (Eric Safern)
  14. Posting-number: Volume 15, Issue 19
  15. Archive-name: reversi2/Part02
  16. Environment: curses
  17.  
  18.  
  19. #! /bin/sh
  20. # This is a shell archive.  Remove anything before this line, then unpack
  21. # it by saving it into a file and typing "sh file".  To overwrite existing
  22. # files, type "sh file -c".  You can also feed this as standard input via
  23. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  24. # will see the following message at the end:
  25. #        "End of archive 2 (of 2)."
  26. # Contents:  corners.c count.c display.c edges.c fini.c genedge.c
  27. #   hasmove.c makefile minmax.c move.c reversi.h score.c
  28. # Wrapped by billr@saab on Thu Oct 29 08:20:57 1992
  29. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  30. if test -f 'corners.c' -a "${1}" != "-c" ; then 
  31.   echo shar: Will not clobber existing file \"'corners.c'\"
  32. else
  33. echo shar: Extracting \"'corners.c'\" \(1758 characters\)
  34. sed "s/^X//" >'corners.c' <<'END_OF_FILE'
  35. X/*
  36. X *    cornerscores.c
  37. X */
  38. X
  39. Xshort cornerscores[81] = {
  40. X        -20,    /* O O O O */
  41. X          0,    /* O O O - */
  42. X         10,    /* O O O * */
  43. X        -20,    /* O O - O */
  44. X          0,    /* O O - - */
  45. X        -10,    /* O O - * */
  46. X        -20,    /* O O * O */
  47. X          0,    /* O O * - */
  48. X         20,    /* O O * * */
  49. X        -20,    /* O - O O */
  50. X          0,    /* O - O - */
  51. X        -10,    /* O - O * */
  52. X        -20,    /* O - - O */
  53. X          0,    /* O - - - */
  54. X        -10,    /* O - - * */
  55. X        -20,    /* O - * O */
  56. X          0,    /* O - * - */
  57. X         20,    /* O - * * */
  58. X        -20,    /* O * O O */
  59. X          0,    /* O * O - */
  60. X         20,    /* O * O * */
  61. X        -20,    /* O * - O */
  62. X          0,    /* O * - - */
  63. X         10,    /* O * - * */
  64. X        -20,    /* O * * O */
  65. X          0,    /* O * * - */
  66. X         10,    /* O * * * */
  67. X         40,    /* - O O O */
  68. X          0,    /* - O O - */
  69. X        -40,    /* - O O * */
  70. X         40,    /* - O - O */
  71. X          0,    /* - O - - */
  72. X        -40,    /* - O - * */
  73. X         40,    /* - O * O */
  74. X          0,    /* - O * - */
  75. X        -40,    /* - O * * */
  76. X         40,    /* - - O O */
  77. X          0,    /* - - O - */
  78. X        -40,    /* - - O * */
  79. X         40,    /* - - - O */
  80. X          0,    /* - - - - */
  81. X        -40,    /* - - - * */
  82. X         40,    /* - - * O */
  83. X          0,    /* - - * - */
  84. X        -40,    /* - - * * */
  85. X         40,    /* - * O O */
  86. X          0,    /* - * O - */
  87. X        -40,    /* - * O * */
  88. X         40,    /* - * - O */
  89. X          0,    /* - * - - */
  90. X        -40,    /* - * - * */
  91. X         40,    /* - * * O */
  92. X          0,    /* - * * - */
  93. X        -40,    /* - * * * */
  94. X        -20,    /* * O O O */
  95. X          0,    /* * O O - */
  96. X         20,    /* * O O * */
  97. X        -10,    /* * O - O */
  98. X          0,    /* * O - - */
  99. X         20,    /* * O - * */
  100. X        -10,    /* * O * O */
  101. X          0,    /* * O * - */
  102. X         20,    /* * O * * */
  103. X        -10,    /* * - O O */
  104. X          0,    /* * - O - */
  105. X         20,    /* * - O * */
  106. X        -10,    /* * - - O */
  107. X          0,    /* * - - - */
  108. X         20,    /* * - - * */
  109. X        -10,    /* * - * O */
  110. X          0,    /* * - * - */
  111. X         20,    /* * - * * */
  112. X        -10,    /* * * O O */
  113. X          0,    /* * * O - */
  114. X         20,    /* * * O * */
  115. X        -10,    /* * * - O */
  116. X          0,    /* * * - - */
  117. X         20,    /* * * - * */
  118. X        -10,    /* * * * O */
  119. X          0,    /* * * * - */
  120. X         20,    /* * * * * */
  121. X    };
  122. END_OF_FILE
  123. if test 1758 -ne `wc -c <'corners.c'`; then
  124.     echo shar: \"'corners.c'\" unpacked with wrong size!
  125. fi
  126. # end of 'corners.c'
  127. fi
  128. if test -f 'count.c' -a "${1}" != "-c" ; then 
  129.   echo shar: Will not clobber existing file \"'count.c'\"
  130. else
  131. echo shar: Extracting \"'count.c'\" \(255 characters\)
  132. sed "s/^X//" >'count.c' <<'END_OF_FILE'
  133. X/*
  134. X *    count.c
  135. X *
  136. X *    count up the board
  137. X */
  138. X
  139. X# include    "reversi.h"
  140. X
  141. Xcount (player, board)
  142. XboardT    board;
  143. X{
  144. X    register int    x, y, count;
  145. X
  146. X    count = 0;
  147. X    for (x = 1; x <= SIZE; x++)
  148. X        for (y = 1; y <= SIZE; y++)
  149. X            count += board[x][y];
  150. X    return count * player;
  151. X}
  152. END_OF_FILE
  153. if test 255 -ne `wc -c <'count.c'`; then
  154.     echo shar: \"'count.c'\" unpacked with wrong size!
  155. fi
  156. # end of 'count.c'
  157. fi
  158. if test -f 'display.c' -a "${1}" != "-c" ; then 
  159.   echo shar: Will not clobber existing file \"'display.c'\"
  160. else
  161. echo shar: Extracting \"'display.c'\" \(4938 characters\)
  162. sed "s/^X//" >'display.c' <<'END_OF_FILE'
  163. X/*
  164. X *    display.c
  165. X */
  166. X
  167. X# include    "reversi.h"
  168. X# include    <curses.h>
  169. X# include    <ctype.h>
  170. X
  171. X# define    toscrx(x)    ((x) * 5 - 1)
  172. X# define    toscry(y)    ((y) * 3 - 2)
  173. X
  174. X# define    LINEX    (toscrx(10)-2)
  175. X# define    LINEY    (toscry(5)+1)
  176. X
  177. Xstatic    helpShown;
  178. X
  179. XdispInit ()
  180. X{
  181. X    register int    i, j;
  182. X    int                dispEnd();
  183. X
  184. X    initscr ();
  185. X    savetty();
  186. X    noecho ();
  187. X    crmode ();
  188. X    helpShown = 0;
  189. X    for (i = 1; i <= SIZE; i++) {
  190. X        move (toscry(i), 0);
  191. X        printw ("%1d", i);
  192. X        move (0, toscrx(i));
  193. X        printw ("%1d", i);
  194. X    }
  195. X    refresh ();
  196. X}
  197. X
  198. XdispGrid ()
  199. X{
  200. X    register int    i, j;
  201. X
  202. X    for (i = 1; i <= SIZE; i++) {
  203. X        for (j = 1; j <= SIZE + 1; j++) {
  204. X            if (i <= SIZE) {
  205. X                move (toscry(i)+1, toscrx(j)-2);
  206. X                addch ('|');
  207. X                move (toscry(i), toscrx(j)-2);
  208. X                addch ('|');
  209. X                move (toscry(i)-1, toscrx(j)-2);
  210. X            }
  211. X            if (j <= SIZE) {
  212. X                if (i == 1)
  213. X                    printw ("+-%1d--", j);
  214. X                else
  215. X                    addstr ("+----");
  216. X            } else
  217. X                addstr ("+");
  218. X        }
  219. X    }
  220. X    refresh ();
  221. X}
  222. X
  223. XdispNoGrid ()
  224. X{
  225. X    register int    i, j;
  226. X
  227. X    for (i = 1; i <= SIZE; i++) {
  228. X        for (j = 1; j <= SIZE + 1; j++) {
  229. X            move (toscry(i)+1, toscrx(j)-2);
  230. X            addch (' ');
  231. X            move (toscry(i), toscrx(j)-2);
  232. X            addch (' ');
  233. X            move (toscry(i)-1, toscrx(j)-2);
  234. X            if (j <= SIZE) {
  235. X                if (i == 1)
  236. X                    printw ("  %1d  ", j);
  237. X                else
  238. X                    addstr ("     ");
  239. X            } else
  240. X                addstr (" ");
  241. X        }
  242. X    }
  243. X    refresh ();
  244. X}
  245. X
  246. XdispEnd ()
  247. X{
  248. X    clearok(stdscr, 1);
  249. X    erase ();
  250. X    refresh ();
  251. X    resetty();
  252. X    endwin ();
  253. X    exit (0);
  254. X}
  255. X
  256. XboardT    old;
  257. X
  258. Xdisplay (board)
  259. XboardT    board;
  260. X{
  261. X    register int    i,j;
  262. X    extern int    showScore;
  263. X
  264. X    for (i = 1; i <= SIZE; i++)
  265. X        for (j = 1; j <= SIZE; j++)
  266. X            if (board[i][j] != old[i][j]) {
  267. X                dispOne (i, j, board[i][j]);
  268. X                old[i][j] = board[i][j];
  269. X            }
  270. X    refresh ();
  271. X    if (showScore)
  272. X        dispScore (board);
  273. X}
  274. X
  275. XdispOne (y, x, who)
  276. X{
  277. X    move (toscry (y), toscrx (x));
  278. X    switch (who) {
  279. X    case BLACK:
  280. X        addstr ("/\\");
  281. X        break;
  282. X    case WHITE:
  283. X        addstr ("**");
  284. X        break;
  285. X    case EMPTY:
  286. X        addstr ("  ");
  287. X        break;
  288. X    }
  289. X    move (toscry(y) + 1, toscrx (x));
  290. X    switch (who) {
  291. X    case BLACK:
  292. X        addstr ("\\/");
  293. X        break;
  294. X    case WHITE:
  295. X        addstr ("**");
  296. X        break;
  297. X    case EMPTY:
  298. X        addstr ("  ");
  299. X        break;
  300. X    }
  301. X}
  302. X
  303. XdispScore (board)
  304. Xregister boardT    board;
  305. X{
  306. X    register int    i,j;
  307. X    register int    ws, bs;
  308. X
  309. X    ws = bs = 0;
  310. X    for (i = 1; i <= SIZE; i++)
  311. X        for (j = 1; j <= SIZE; j++)
  312. X            switch (board[i][j]) {
  313. X            case WHITE:
  314. X                ws++; break;
  315. X            case BLACK:
  316. X                bs++; break;
  317. X            }
  318. X    move (LINEY - 3, LINEX);
  319. X    printw ("white: %-2d  black: %-2d", ws, bs);
  320. X    refresh ();
  321. X}
  322. X
  323. XdispNoScore ()
  324. X{
  325. X    move (LINEY - 3, LINEX);
  326. X    clrtoeol ();
  327. X    refresh ();
  328. X}
  329. X
  330. Xstatic char *helpText[] = {
  331. X    "y, x              [no] grid",
  332. X    "[no] help         hint",
  333. X    "play              quit",
  334. X    "restart           record",
  335. X    "replay            save",
  336. X    "[no] score        undo",
  337. X    "level",
  338. X    "white|black first",
  339. X    "white|black second",
  340. X    0,
  341. X};
  342. X
  343. XdispTurn (player)
  344. X{
  345. X    static displayed = EMPTY;
  346. X
  347. X    if (displayed == player)
  348. X        return;
  349. X    move (LINEY-1, LINEX);
  350. X    switch (player) {
  351. X    case WHITE:
  352. X        addstr ("white's turn");
  353. X        break;
  354. X    case BLACK:
  355. X        addstr ("black's turn");
  356. X        break;
  357. X    case EMPTY:
  358. X        clrtoeol ();
  359. X    }
  360. X    displayed = player;
  361. X    refresh ();
  362. X}
  363. X
  364. XdispHelp ()
  365. X{
  366. X    register int    i;
  367. X    register char    **h;
  368. X
  369. X    if (helpShown)
  370. X        return;
  371. X    i = 0;
  372. X    for (h = helpText; *h; ++h) {
  373. X        move (i, LINEX);
  374. X        addstr (*h);
  375. X        ++i;
  376. X    }
  377. X    move (LINEY+4, LINEX);
  378. X    printw ("white pieces are  **");
  379. X    move (LINEY+5, LINEX+18);
  380. X    printw ("**");
  381. X    move (LINEY+7, LINEX);
  382. X    printw ("black pieces are  /\\");
  383. X    move (LINEY+8, LINEX+18);
  384. X    printw ("\\/");
  385. X    refresh ();
  386. X    ++helpShown;
  387. X}
  388. X
  389. XdispNoHelp ()
  390. X{
  391. X    register int    i;
  392. X    register char    **h;
  393. X
  394. X    if (!helpShown)
  395. X        return;
  396. X    i = 0;
  397. X    for (h = helpText; *h; ++h) {
  398. X        move (i, LINEX);
  399. X        clrtoeol ();
  400. X        ++i;
  401. X    }
  402. X    move (LINEY+4, LINEX);
  403. X    clrtoeol ();
  404. X    move (LINEY+5, LINEX+18);
  405. X    clrtoeol ();
  406. X    move (LINEY+7, LINEX);
  407. X    clrtoeol ();
  408. X    move (LINEY+8, LINEX+18);
  409. X    clrtoeol ();
  410. X    refresh ();
  411. X    helpShown = 0;
  412. X}
  413. X
  414. Xstatic char    lexbuf[256];
  415. Xstatic char    *lexpnt;
  416. X
  417. XreadLine ()
  418. X{
  419. X    int    ch, x, y;
  420. X
  421. X    move (LINEY, LINEX);
  422. X    addstr ("-> ");
  423. Xloop:
  424. X    x = LINEX+3;
  425. X    y = LINEY;
  426. X    move (y, x);
  427. X    clrtoeol ();
  428. X    refresh ();
  429. X    lexpnt = lexbuf;
  430. X    for (;;) {
  431. X        ch = getch ();
  432. X        if (ch == -1)
  433. X            ch = '\004';
  434. X        *lexpnt++ = ch;
  435. X        if (isprint (ch)) {
  436. X            addch (ch);
  437. X            ++x;
  438. X            refresh ();
  439. X        } else
  440. X            switch (ch) {
  441. X            case '\f':
  442. X                clearok (stdscr, 1);
  443. X            case '\030':
  444. X            case '\025':
  445. X                goto loop;
  446. X            case '\004':
  447. X                *lexpnt++ = -1;
  448. X            case '\r':
  449. X            case '\n':
  450. X                move (LINEY+1, LINEX);
  451. X                refresh ();
  452. X                *lexpnt++ = '\0';
  453. X                goto done;
  454. X            case '\b':
  455. X                if (lexpnt >= lexbuf + 2) {
  456. X                    lexpnt -= 2;
  457. X                    --x;
  458. X                    move (y,x);
  459. X                    delch ();
  460. X                    refresh ();
  461. X                } else
  462. X                    --lexpnt;
  463. X                break;
  464. X            default:
  465. X                --lexpnt;
  466. X                write (1, "\007", 1);
  467. X                break;
  468. X            }
  469. X    }
  470. Xdone:    lexpnt = lexbuf;
  471. X    dispError ("");
  472. X}
  473. X
  474. Xlexgetc ()
  475. X{
  476. X    int    c;
  477. X    extern int yylineno;
  478. X
  479. X    c = *lexpnt++;
  480. X    if (c == -1)
  481. X        c = 4;
  482. X    c &= 0177;
  483. X    if (c == '\r')
  484. X        c = '\n';
  485. X    if (c == '\n')
  486. X        ++yylineno;
  487. X    return c;
  488. X}
  489. X
  490. Xlexungetc (c)
  491. X{
  492. X    --lexpnt;
  493. X}
  494. X
  495. XdispError (s)
  496. Xchar *s;
  497. X{
  498. X    move (LINEY+1, LINEX);
  499. X    clrtoeol ();
  500. X    addstr (s);
  501. X    refresh ();
  502. X}
  503. END_OF_FILE
  504. if test 4938 -ne `wc -c <'display.c'`; then
  505.     echo shar: \"'display.c'\" unpacked with wrong size!
  506. fi
  507. # end of 'display.c'
  508. fi
  509. if test -f 'edges.c' -a "${1}" != "-c" ; then 
  510.   echo shar: Will not clobber existing file \"'edges.c'\"
  511. else
  512. echo shar: Extracting \"'edges.c'\" \(77 characters\)
  513. sed "s/^X//" >'edges.c' <<'END_OF_FILE'
  514. X/*
  515. X *    edgescores.c
  516. X */
  517. X
  518. Xshort edgescores [6561] = {
  519. X# include "edges.out"
  520. X};
  521. END_OF_FILE
  522. if test 77 -ne `wc -c <'edges.c'`; then
  523.     echo shar: \"'edges.c'\" unpacked with wrong size!
  524. fi
  525. # end of 'edges.c'
  526. fi
  527. if test -f 'fini.c' -a "${1}" != "-c" ; then 
  528.   echo shar: Will not clobber existing file \"'fini.c'\"
  529. else
  530. echo shar: Extracting \"'fini.c'\" \(599 characters\)
  531. sed "s/^X//" >'fini.c' <<'END_OF_FILE'
  532. X/*
  533. X *    fini.c
  534. X *
  535. X *    count up score and display winner
  536. X */
  537. X
  538. X# include    "reversi.h"
  539. X
  540. Xfini (board)
  541. XboardT    board;
  542. X{
  543. X    register int    x,y;
  544. X    register int    wscore, bscore;
  545. X    char            sbuf[80];
  546. X
  547. X    wscore = bscore = 0;
  548. X
  549. X    for (x = 1; x <= SIZE; x++)
  550. X        for (y = 1; y <= SIZE; y++)
  551. X            if (board[x][y] == WHITE)
  552. X                ++wscore;
  553. X            else if (board[x][y] == BLACK)
  554. X                ++bscore;
  555. X    if (wscore > bscore)
  556. X        sprintf (sbuf, "white wins %d to %d.", wscore, bscore);
  557. X    else if (bscore > wscore)
  558. X        sprintf (sbuf, "black wins %d to %d.", bscore, wscore);
  559. X    else
  560. X        sprintf (sbuf, "tie game %d to %d.", wscore, bscore);
  561. X    dispError (sbuf);
  562. X}
  563. END_OF_FILE
  564. if test 599 -ne `wc -c <'fini.c'`; then
  565.     echo shar: \"'fini.c'\" unpacked with wrong size!
  566. fi
  567. # end of 'fini.c'
  568. fi
  569. if test -f 'genedge.c' -a "${1}" != "-c" ; then 
  570.   echo shar: Will not clobber existing file \"'genedge.c'\"
  571. else
  572. echo shar: Extracting \"'genedge.c'\" \(679 characters\)
  573. sed "s/^X//" >'genedge.c' <<'END_OF_FILE'
  574. X/*
  575. X *    generate preliminary edge score array
  576. X */
  577. X
  578. Xchar    board[9];
  579. X
  580. Xmain ()
  581. X{
  582. X    register int    i;
  583. X    for (board[1] = -1; board[1] <= 1; board[1]++)
  584. X    for (board[2] = -1; board[2] <= 1; board[2]++)
  585. X    for (board[3] = -1; board[3] <= 1; board[3]++)
  586. X    for (board[4] = -1; board[4] <= 1; board[4]++)
  587. X    for (board[5] = -1; board[5] <= 1; board[5]++)
  588. X    for (board[6] = -1; board[6] <= 1; board[6]++)
  589. X    for (board[7] = -1; board[7] <= 1; board[7]++)
  590. X    for (board[8] = -1; board[8] <= 1; board[8]++) {
  591. X        for (i = 1; i <= 8; i++)
  592. X            switch (board[i]) {
  593. X            case 0:
  594. X                printf (" -");
  595. X                break;
  596. X            case -1:
  597. X                printf (" O");
  598. X                break;
  599. X            case 1:
  600. X                printf (" *");
  601. X                break;
  602. X            }
  603. X        printf ("\n");
  604. X    }
  605. X}
  606. END_OF_FILE
  607. if test 679 -ne `wc -c <'genedge.c'`; then
  608.     echo shar: \"'genedge.c'\" unpacked with wrong size!
  609. fi
  610. # end of 'genedge.c'
  611. fi
  612. if test -f 'hasmove.c' -a "${1}" != "-c" ; then 
  613.   echo shar: Will not clobber existing file \"'hasmove.c'\"
  614. else
  615. echo shar: Extracting \"'hasmove.c'\" \(271 characters\)
  616. sed "s/^X//" >'hasmove.c' <<'END_OF_FILE'
  617. X/*
  618. X *    hasmove.c
  619. X *
  620. X *    figure out if player has move in board
  621. X */
  622. X
  623. X# include    "reversi.h"
  624. X
  625. Xhasmove (player, board)
  626. XboardT    board;
  627. X{
  628. X    register int    x,y;
  629. X
  630. X    for (x = 1; x <= SIZE; x++)
  631. X        for (y = 1; y <= SIZE; y++)
  632. X            if (legal (player, x, y, board))
  633. X                return 1;
  634. X    return 0;
  635. X}
  636. END_OF_FILE
  637. if test 271 -ne `wc -c <'hasmove.c'`; then
  638.     echo shar: \"'hasmove.c'\" unpacked with wrong size!
  639. fi
  640. # end of 'hasmove.c'
  641. fi
  642. if test -f 'makefile' -a "${1}" != "-c" ; then 
  643.   echo shar: Will not clobber existing file \"'makefile'\"
  644. else
  645. echo shar: Extracting \"'makefile'\" \(934 characters\)
  646. sed "s/^X//" >'makefile' <<'END_OF_FILE'
  647. XCFLAGS=-g
  648. XOFILES=user.o ulex.o move.o display.o hasmove.o fini.o \
  649. X    minmax.o score.o edges.o corners.o count.o
  650. XSOURCE= makefile corners.c count.c display.c edges.c fini.c genedge.c \
  651. X    hasmove.c makeedge.y minmax.c move.c reversi.h score.c ulex.l user.y
  652. X
  653. Xreversi: $(OFILES)
  654. X    cc $(CFLAGS) -o reversi $(OFILES) -lcurses -ltermlib
  655. X
  656. Xreversi.shar: $(SOURCE)
  657. X    shar -c $(SOURCE) > reversi.shar
  658. X
  659. X$(OFILES): reversi.h
  660. Xuser.o: user.c
  661. Xuser.c: user.y
  662. X    yacc -dv user.y
  663. X    mv y.tab.c user.c
  664. Xulex.o: ulex.c
  665. Xulex.c: ulex.l
  666. X    lex ulex.l
  667. X    mv lex.yy.c ulex.c
  668. Xcorners.o: corners.c
  669. X    $(CC) $(CFLAGS) -R -c corners.c
  670. X
  671. Xedges.o: edges.c edges.out
  672. X    $(CC) $(CFLAGS) -R -c edges.c
  673. Xedges.out: makeedge genedge
  674. X    genedge | makeedge > edges.out
  675. Xmakeedge: makeedge.o
  676. X    $(CC) $(CFLAGS) -o makeedge makeedge.o
  677. Xmakeedge.o: makeedge.c
  678. Xmakeedge.c: makeedge.y
  679. X    yacc makeedge.y
  680. X    mv y.tab.c makeedge.c
  681. Xgenedge: genedge.o
  682. X    $(CC) $(CFLAGS) -o genedge genedge.o
  683. Xgenedge.o: genedge.c
  684. END_OF_FILE
  685. if test 934 -ne `wc -c <'makefile'`; then
  686.     echo shar: \"'makefile'\" unpacked with wrong size!
  687. fi
  688. # end of 'makefile'
  689. fi
  690. if test -f 'minmax.c' -a "${1}" != "-c" ; then 
  691.   echo shar: Will not clobber existing file \"'minmax.c'\"
  692. else
  693. echo shar: Extracting \"'minmax.c'\" \(2658 characters\)
  694. sed "s/^X//" >'minmax.c' <<'END_OF_FILE'
  695. X/*
  696. X *    minmax.c
  697. X */
  698. X
  699. X# include    "reversi.h"
  700. X
  701. Xint    maxlev, movex, movey;
  702. X
  703. X/*
  704. X *    this defines the order in which the board
  705. X *    is searched for the best move.  It is
  706. X *    here to shorten the time to best move,
  707. X *    this increasing the chance of hitting
  708. X *    a good trimming point as well as
  709. X *    increasing the possibility of making
  710. X *    a reasonable move when an interrupt is
  711. X *    caught.
  712. X */
  713. Xshort    morder[64][2] = {
  714. X    1,1, 1,8, 8,1, 8,8,
  715. X    1,3, 1,6, 3,1, 3,8, 6,1, 6,8, 8,3, 8,6,
  716. X    3,3, 3,6, 6,3, 6,6,
  717. X    1,4, 1,5, 4,1, 4,8, 5,1, 5,8, 8,4, 8,5,
  718. X    3,4, 3,5, 4,3, 4,6, 5,3, 5,6, 6,4, 6,5,
  719. X    2,3, 2,6, 3,2, 3,7, 6,2, 6,7, 7,3, 7,6,
  720. X    2,4, 2,5, 4,2, 4,7, 5,2, 5,7, 7,4, 7,5,
  721. X    1,2, 1,7, 2,1, 2,8, 7,1, 7,8, 8,2, 8,7,
  722. X    2,2, 2,7, 7,2, 7,7, 4,4, 4,5, 5,4, 5,5,
  723. X};
  724. X
  725. X# define    NOMOVE    (-32760)
  726. X
  727. X
  728. X# define    USECOPY
  729. X# ifdef    USECOPY
  730. Xstruct copyB {
  731. X    boardT    data;
  732. X};
  733. X# define    copyb(next,board)    (*((struct copyB *)next) = *((struct copyB *) board))
  734. X# else
  735. X# define    copyb(next,board)    copy(next,board)
  736. X# endif
  737. X
  738. Xcopy(next, board)
  739. Xregister int    *next, *board;
  740. X{
  741. X    register int    count;
  742. X
  743. X    count = sizeof (boardT) / sizeof (int);
  744. X    do {
  745. X        *next++ = *board++;
  746. X    } while (--count);
  747. X}
  748. X
  749. Xcomputer (player, board, level)
  750. XboardT    board;
  751. X{
  752. X    int    i;
  753. X    extern int    com, defcom;
  754. X
  755. X    maxlev = level;
  756. X    movex = movey = -1;
  757. X    i = seek (player, board, 0, 1, -NOMOVE);
  758. X    if (movex == -1 || movey == -1)
  759. X        return 0;
  760. X    move (player, movex, movey, board);
  761. X    return 1;
  762. X}
  763. X
  764. Xhint (player, board, level)
  765. XboardT board;
  766. X{
  767. X    int    i;
  768. X
  769. X    maxlev = level;
  770. X    i = seek (player, board, 0, 1, -NOMOVE);
  771. X    if (movex == -1 || movey == -1)
  772. X        return 0;
  773. X    return 1;
  774. X}
  775. X
  776. Xseek (player, board, level, moved, best)
  777. Xregister player;
  778. Xregister boardT    board;
  779. X{
  780. X    boardT        next;
  781. X    register int    x, y;
  782. X    register int    s;
  783. X    int        max, i;
  784. X    int        bestx, besty;
  785. X    int        m, j;
  786. X    extern int    gotsignal;
  787. X
  788. X    max = NOMOVE;
  789. X    m = 0;
  790. X    for (j = 0; j < 60; j++) {
  791. X        x = morder[j][0];
  792. X        y = morder[j][1];
  793. X        if (gotsignal)
  794. X            return 0;
  795. X        if (legal (player, x, y, board)) {
  796. X            copyb (next, board);
  797. X            if (level == 0 && movex == -1) {
  798. X                movex = x;
  799. X                movey = y;
  800. X            }
  801. X            move (player, x, y, next);
  802. X            ++m;
  803. X            if (level >= maxlev)
  804. X                s = score (next, player);
  805. X            else
  806. X                s = seek (-player, next, level+1, 1, -max);
  807. X            if (s >= max) {
  808. X                /*
  809. X                 *    try to make the game appear random
  810. X                 *    by choosing among equal moves
  811. X                 *    randomly
  812. X                 */
  813. X                if (s == max && rand()&01)
  814. X                    goto skip;
  815. X                if (s > best)
  816. X                    return -s;
  817. X                bestx = x;
  818. X                besty = y;
  819. X                if (level == 0) {
  820. X                    movex = bestx;
  821. X                    movey = besty;
  822. X                }
  823. X                max = s;
  824. X            }
  825. Xskip:            ;
  826. X        }
  827. X    }
  828. X    if (m == 0)
  829. X        if (moved && level)
  830. X            return seek (-player, board, level + 1, 0, -best);
  831. X        else
  832. X            return - count (player, board) * 500;
  833. X    return -max;
  834. X}
  835. END_OF_FILE
  836. if test 2658 -ne `wc -c <'minmax.c'`; then
  837.     echo shar: \"'minmax.c'\" unpacked with wrong size!
  838. fi
  839. # end of 'minmax.c'
  840. fi
  841. if test -f 'move.c' -a "${1}" != "-c" ; then 
  842.   echo shar: Will not clobber existing file \"'move.c'\"
  843. else
  844. echo shar: Extracting \"'move.c'\" \(867 characters\)
  845. sed "s/^X//" >'move.c' <<'END_OF_FILE'
  846. X/*
  847. X *    move.c
  848. X *
  849. X *    move player to x,y in board
  850. X */
  851. X
  852. X# include    "reversi.h"
  853. X
  854. Xint        offsets[] = { -11, -10, -9, -1, 1, 9, 10, 11, 0 };
  855. X
  856. Xmove (player, x, y, board)
  857. Xregister int    player;
  858. Xint                x, y;
  859. XboardT            board;
  860. X{
  861. X    register char    *b, *m;
  862. X    register int    *o, i;
  863. X
  864. X    b = & board[x][y];
  865. X    *b = player;
  866. X    player = -player;
  867. X    for (o = offsets; i = *o++;) {
  868. X        if (b[i] == player) {
  869. X            m = b+i;
  870. X            while (*m == player)
  871. X                m += i;
  872. X            if (*m == -player) {
  873. X                while (m != b) {
  874. X                    *m = -player;
  875. X                    m -= i;
  876. X                }
  877. X            }
  878. X        }
  879. X    }
  880. X}
  881. X
  882. Xlegal (player, x, y, board)
  883. Xregister int    player;
  884. Xint                x, y;
  885. XboardT            board;
  886. X{
  887. X    register char    *b, *m;
  888. X    register int    *o, i;
  889. X
  890. X    b = & board[x][y];
  891. X    player = -player;
  892. X    if (*b == EMPTY) {
  893. X        for (o = offsets; i = *o++;) {
  894. X            if (b[i] == player) {
  895. X                m = b+i;
  896. X                while (*m == player)
  897. X                    m += i;
  898. X                if (*m == -player)
  899. X                    return 1;
  900. X            }
  901. X        }
  902. X    }
  903. X    return 0;
  904. X}
  905. END_OF_FILE
  906. if test 867 -ne `wc -c <'move.c'`; then
  907.     echo shar: \"'move.c'\" unpacked with wrong size!
  908. fi
  909. # end of 'move.c'
  910. fi
  911. if test -f 'reversi.h' -a "${1}" != "-c" ; then 
  912.   echo shar: Will not clobber existing file \"'reversi.h'\"
  913. else
  914. echo shar: Extracting \"'reversi.h'\" \(189 characters\)
  915. sed "s/^X//" >'reversi.h' <<'END_OF_FILE'
  916. X/*
  917. X *    reversi.h
  918. X *
  919. X *    include file for game program
  920. X */
  921. X
  922. X# define    SIZE    8
  923. X
  924. Xtypedef char    boardT[SIZE+2][SIZE+2];
  925. X
  926. Xtypedef boardT    *boardP;
  927. X
  928. X# define    EMPTY    0
  929. X# define    WHITE    1
  930. X# define    BLACK    -1
  931. END_OF_FILE
  932. if test 189 -ne `wc -c <'reversi.h'`; then
  933.     echo shar: \"'reversi.h'\" unpacked with wrong size!
  934. fi
  935. # end of 'reversi.h'
  936. fi
  937. if test -f 'score.c' -a "${1}" != "-c" ; then 
  938.   echo shar: Will not clobber existing file \"'score.c'\"
  939. else
  940. echo shar: Extracting \"'score.c'\" \(2547 characters\)
  941. sed "s/^X//" >'score.c' <<'END_OF_FILE'
  942. X/*
  943. X *    score.c
  944. X *
  945. X *    score a board position
  946. X */
  947. X
  948. X# include    "reversi.h"
  949. X#ifdef SDEBUG
  950. Xextern int sdebug;
  951. X#endif
  952. X
  953. XboardT    base = {
  954. X      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  955. X      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  956. X      0,   0, -40, -10,  -5,  -5, -10, -40,   0,   0,
  957. X      0,   0, -10,   3,   1,   1,   3, -10,   0,   0,
  958. X      0,   0,  -5,   1,   0,   0,   1,  -5,   0,   0,
  959. X      0,   0,  -5,   1,   0,   0,   1,  -5,   0,   0,
  960. X      0,   0, -10,   3,   1,   1,   3, -10,   0,   0,
  961. X      0,   0, -40, -10,  -5,  -5, -10, -40,   0,   0,
  962. X      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  963. X      0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  964. X};
  965. X
  966. Xshort edgemod[10][3] = {
  967. X    0,    0,    0,
  968. X    0,    0,    0,
  969. X    0,    0,    0,
  970. X    5,    -10,    5,
  971. X    5,    -5,    5,
  972. X    5,    -5,    5,
  973. X    5,    -10,    5,
  974. X    0,    0,    0,
  975. X    0,    0,    0,
  976. X    0,    0,    0,
  977. X};
  978. X
  979. Xextern short    cornerscores[3][3][3][3];
  980. X
  981. Xextern short    edgescores[6561];
  982. X
  983. X# define edgesc(a,b,c,d,e,f,g,h)    edgescores[a*2187 + b*729 + c*243 + \
  984. X                    d*81 + e*27 + f*9 + g*3 + h + 3280]
  985. X
  986. Xscore (board, player)
  987. Xregister boardT    board;
  988. Xint    player;
  989. X{
  990. X    register char    *j, *b;
  991. X    char    *l;
  992. X    register int    score;
  993. X    int    i;
  994. X    int    n, m;
  995. X
  996. X#ifdef SDEBUG
  997. X    if (sdebug) {
  998. X        display (board);
  999. X    }
  1000. X#endif
  1001. X    score = 0;
  1002. X    for (i = 3; i < SIZE - 1; i++) {
  1003. X        j = & board[i][3];
  1004. X        b = & base [i][3];
  1005. X        l = & board[i][SIZE-1];
  1006. X        while (j != l) {
  1007. X            n = *j++;
  1008. X            score += n * *b++;
  1009. X        }
  1010. X        score += board[2][i]      * edgemod[i][board[1][i]+1];
  1011. X        score += board[SIZE-1][i] * edgemod[i][board[SIZE][i]+1];
  1012. X        score += board[i][2]      * edgemod[i][board[i][1]+1];
  1013. X        score += board[i][SIZE-1] * edgemod[i][board[i][SIZE]+1];
  1014. X    }
  1015. X    score +=
  1016. X        cornerscores[board[1][1] + 1]
  1017. X            [board[1][2] + 1]
  1018. X            [board[2][1] + 1]
  1019. X            [board[2][2] + 1] +
  1020. X        cornerscores[board[1][8] + 1]
  1021. X            [board[1][7] + 1]
  1022. X            [board[2][8] + 1]
  1023. X            [board[2][7] + 1] +
  1024. X        cornerscores[board[8][1] + 1]
  1025. X            [board[8][2] + 1]
  1026. X            [board[7][1] + 1]
  1027. X            [board[7][2] + 1] +
  1028. X        cornerscores[board[8][8] + 1]
  1029. X            [board[8][7] + 1]
  1030. X            [board[7][8] + 1]
  1031. X            [board[7][7] + 1];
  1032. X    score += edgesc (board[1][1], board[1][2], board[1][3], board[1][4],
  1033. X             board[1][5], board[1][6], board[1][7], board[1][8]) +
  1034. X         edgesc (board[8][1], board[8][2], board[8][3], board[8][4],
  1035. X             board[8][5], board[8][6], board[8][7], board[8][8]);
  1036. X    score += edgesc (board[1][1], board[2][1], board[3][1], board[4][1],
  1037. X             board[5][1], board[6][1], board[7][1], board[8][1]) +
  1038. X         edgesc (board[1][8], board[2][8], board[3][8], board[4][8],
  1039. X             board[5][8], board[6][8], board[7][8], board[8][8]);
  1040. X#ifdef SDEBUG
  1041. X    if (sdebug)
  1042. X        printf ("score: %d\n", score);
  1043. X#endif
  1044. X    return score * player;
  1045. X}
  1046. END_OF_FILE
  1047. if test 2547 -ne `wc -c <'score.c'`; then
  1048.     echo shar: \"'score.c'\" unpacked with wrong size!
  1049. fi
  1050. # end of 'score.c'
  1051. fi
  1052. echo shar: End of archive 2 \(of 2\).
  1053. cp /dev/null ark2isdone
  1054. MISSING=""
  1055. for I in 1 2 ; do
  1056.     if test ! -f ark${I}isdone ; then
  1057.     MISSING="${MISSING} ${I}"
  1058.     fi
  1059. done
  1060. if test "${MISSING}" = "" ; then
  1061.     echo You have unpacked both archives.
  1062.     rm -f ark[1-9]isdone
  1063. else
  1064.     echo You still need to unpack the following archives:
  1065.     echo "        " ${MISSING}
  1066. fi
  1067. ##  End of shell archive.
  1068. exit 0
  1069.