home *** CD-ROM | disk | FTP | other *** search
- Path: uunet!zephyr.ens.tek.com!master!saab!billr
- From: billr@saab.CNA.TEK.COM (Bill Randle)
- Newsgroups: comp.sources.games
- Subject: v15i007: reversi - play a game of reversi against the computer or a human, Part01/02
- Message-ID: <3803@master.CNA.TEK.COM>
- Date: 23 Oct 92 15:38:10 GMT
- Sender: news@master.CNA.TEK.COM
- Lines: 1804
- Approved: billr@saab.CNA.TEK.COM
- Xref: uunet comp.sources.games:1506
-
- Submitted-by: elias@proxxi.se (Elias M}rtensson )
- Posting-number: Volume 15, Issue 7
- Archive-name: reversi/Part01
- Environment: curses
-
-
-
- #! /bin/sh
- # This is a shell archive. Remove anything before this line, then unpack
- # it by saving it into a file and typing "sh file". To overwrite existing
- # files, type "sh file -c". You can also feed this as standard input via
- # unshar, or by typing "sh <file", e.g.. If this archive is complete, you
- # will see the following message at the end:
- # "End of archive 1 (of 2)."
- # Contents: README MANIFEST COPYING comp.c graphics.c menu.h move.c
- # reversi.c
- # Wrapped by billr@saab on Fri Oct 23 08:35:19 1992
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f 'README' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'README'\"
- else
- echo shar: Extracting \"'README'\" \(2083 characters\)
- sed "s/^X//" >'README' <<'END_OF_FILE'
- X reversi - play a game of reversi against the computer or a human
- X Copyright (C) 1992 Elias Martenson
- X
- X This program is free software; you can redistribute it and/or modify
- X it under the terms of the GNU General Public License as published by
- X the Free Software Foundation; either version 2 of the License, or
- X (at your option) any later version.
- X
- X This program is distributed in the hope that it will be useful,
- X but WITHOUT ANY WARRANTY; without even the implied warranty of
- X MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- X GNU General Public License for more details.
- X
- X You should have received a copy of the GNU General Public License
- X along with this program; if not, write to the Free Software
- X Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- X
- X Contact me by email at elias@proxxi.se
- X
- X
- X
- X
- X COMPILING AND INSTALLING
- X
- XEdit the Makefile to suit Your needs and enter "make" or "make
- Xinstall". The program should compile without problems on any
- XANSI-C compiler. If You don't have an ANSI-C compiler at hand You
- Xmight have to change the struct initializations for the menu
- Xroutines by hand.
- X
- XThe game has been tested on several machines (VAX 11/750 running
- XBDS4.3, DECstation 3100 running Ultrix 4.2, i486 running Linux,
- Xi486 running Interactive Unix. The game was compiled with GCC on
- Xall systems).
- X
- XIf You have problems compiling the game, please send me a mail
- Xdescribing You problems and eventually what You did to fix it. I
- Xwould like to see that this game compiles and runs sucessfully on
- Xas many machines as possible.
- X
- X
- X PLAYING THE GAME
- X
- XWhen the game starts up You will be presented with a menu. Move
- Xthe pointer using "j" and "k", select with RETURN. In the game
- XYou move the cursor using "h", "j", "k" and "l", and You place a
- Xtile on the board using SPACE. A menu with variuos selections is
- Xavailable by pressing ESC.
- X
- XIn-game documentation is available by pressing "?" at any time in
- Xthe game.
- X
- X
- XPlease send bug reports and suggestions to: elias@proxxi.se. Send
- Xflames anywhere except to me. :-)
- END_OF_FILE
- if test 2083 -ne `wc -c <'README'`; then
- echo shar: \"'README'\" unpacked with wrong size!
- fi
- # end of 'README'
- fi
- if test -f 'MANIFEST' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'MANIFEST'\"
- else
- echo shar: Extracting \"'MANIFEST'\" \(455 characters\)
- sed "s/^X//" >'MANIFEST' <<'END_OF_FILE'
- X File Name Archive # Description
- X-----------------------------------------------------------
- X COPYING 1
- X MANIFEST 1 This shipping list
- X Makefile 2
- X README 1
- X comp.c 1
- X graphics.c 1
- X menu.c 2
- X menu.h 1
- X move.c 1
- X reversi.c 1
- X reversi.h 2
- END_OF_FILE
- if test 455 -ne `wc -c <'MANIFEST'`; then
- echo shar: \"'MANIFEST'\" unpacked with wrong size!
- fi
- # end of 'MANIFEST'
- fi
- if test -f 'COPYING' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'COPYING'\"
- else
- echo shar: Extracting \"'COPYING'\" \(17976 characters\)
- sed "s/^X//" >'COPYING' <<'END_OF_FILE'
- X GNU GENERAL PUBLIC LICENSE
- X Version 2, June 1991
- X
- X Copyright (C) 1989, 1991 Free Software Foundation, Inc.
- X 675 Mass Ave, Cambridge, MA 02139, USA
- X Everyone is permitted to copy and distribute verbatim copies
- X of this license document, but changing it is not allowed.
- X
- X Preamble
- X
- X The licenses for most software are designed to take away your
- Xfreedom to share and change it. By contrast, the GNU General Public
- XLicense is intended to guarantee your freedom to share and change free
- Xsoftware--to make sure the software is free for all its users. This
- XGeneral Public License applies to most of the Free Software
- XFoundation's software and to any other program whose authors commit to
- Xusing it. (Some other Free Software Foundation software is covered by
- Xthe GNU Library General Public License instead.) You can apply it to
- Xyour programs, too.
- X
- X When we speak of free software, we are referring to freedom, not
- Xprice. Our General Public Licenses are designed to make sure that you
- Xhave the freedom to distribute copies of free software (and charge for
- Xthis service if you wish), that you receive source code or can get it
- Xif you want it, that you can change the software or use pieces of it
- Xin new free programs; and that you know you can do these things.
- X
- X To protect your rights, we need to make restrictions that forbid
- Xanyone to deny you these rights or to ask you to surrender the rights.
- XThese restrictions translate to certain responsibilities for you if you
- Xdistribute copies of the software, or if you modify it.
- X
- X For example, if you distribute copies of such a program, whether
- Xgratis or for a fee, you must give the recipients all the rights that
- Xyou have. You must make sure that they, too, receive or can get the
- Xsource code. And you must show them these terms so they know their
- Xrights.
- X
- X We protect your rights with two steps: (1) copyright the software, and
- X(2) offer you this license which gives you legal permission to copy,
- Xdistribute and/or modify the software.
- X
- X Also, for each author's protection and ours, we want to make certain
- Xthat everyone understands that there is no warranty for this free
- Xsoftware. If the software is modified by someone else and passed on, we
- Xwant its recipients to know that what they have is not the original, so
- Xthat any problems introduced by others will not reflect on the original
- Xauthors' reputations.
- X
- X Finally, any free program is threatened constantly by software
- Xpatents. We wish to avoid the danger that redistributors of a free
- Xprogram will individually obtain patent licenses, in effect making the
- Xprogram proprietary. To prevent this, we have made it clear that any
- Xpatent must be licensed for everyone's free use or not licensed at all.
- X
- X The precise terms and conditions for copying, distribution and
- Xmodification follow.
- X
- X GNU GENERAL PUBLIC LICENSE
- X TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
- X
- X 0. This License applies to any program or other work which contains
- Xa notice placed by the copyright holder saying it may be distributed
- Xunder the terms of this General Public License. The "Program", below,
- Xrefers to any such program or work, and a "work based on the Program"
- Xmeans either the Program or any derivative work under copyright law:
- Xthat is to say, a work containing the Program or a portion of it,
- Xeither verbatim or with modifications and/or translated into another
- Xlanguage. (Hereinafter, translation is included without limitation in
- Xthe term "modification".) Each licensee is addressed as "you".
- X
- XActivities other than copying, distribution and modification are not
- Xcovered by this License; they are outside its scope. The act of
- Xrunning the Program is not restricted, and the output from the Program
- Xis covered only if its contents constitute a work based on the
- XProgram (independent of having been made by running the Program).
- XWhether that is true depends on what the Program does.
- X
- X 1. You may copy and distribute verbatim copies of the Program's
- Xsource code as you receive it, in any medium, provided that you
- Xconspicuously and appropriately publish on each copy an appropriate
- Xcopyright notice and disclaimer of warranty; keep intact all the
- Xnotices that refer to this License and to the absence of any warranty;
- Xand give any other recipients of the Program a copy of this License
- Xalong with the Program.
- X
- XYou may charge a fee for the physical act of transferring a copy, and
- Xyou may at your option offer warranty protection in exchange for a fee.
- X
- X 2. You may modify your copy or copies of the Program or any portion
- Xof it, thus forming a work based on the Program, and copy and
- Xdistribute such modifications or work under the terms of Section 1
- Xabove, provided that you also meet all of these conditions:
- X
- X a) You must cause the modified files to carry prominent notices
- X stating that you changed the files and the date of any change.
- X
- X b) You must cause any work that you distribute or publish, that in
- X whole or in part contains or is derived from the Program or any
- X part thereof, to be licensed as a whole at no charge to all third
- X parties under the terms of this License.
- X
- X c) If the modified program normally reads commands interactively
- X when run, you must cause it, when started running for such
- X interactive use in the most ordinary way, to print or display an
- X announcement including an appropriate copyright notice and a
- X notice that there is no warranty (or else, saying that you provide
- X a warranty) and that users may redistribute the program under
- X these conditions, and telling the user how to view a copy of this
- X License. (Exception: if the Program itself is interactive but
- X does not normally print such an announcement, your work based on
- X the Program is not required to print an announcement.)
- X
- XThese requirements apply to the modified work as a whole. If
- Xidentifiable sections of that work are not derived from the Program,
- Xand can be reasonably considered independent and separate works in
- Xthemselves, then this License, and its terms, do not apply to those
- Xsections when you distribute them as separate works. But when you
- Xdistribute the same sections as part of a whole which is a work based
- Xon the Program, the distribution of the whole must be on the terms of
- Xthis License, whose permissions for other licensees extend to the
- Xentire whole, and thus to each and every part regardless of who wrote it.
- X
- XThus, it is not the intent of this section to claim rights or contest
- Xyour rights to work written entirely by you; rather, the intent is to
- Xexercise the right to control the distribution of derivative or
- Xcollective works based on the Program.
- X
- XIn addition, mere aggregation of another work not based on the Program
- Xwith the Program (or with a work based on the Program) on a volume of
- Xa storage or distribution medium does not bring the other work under
- Xthe scope of this License.
- X
- X 3. You may copy and distribute the Program (or a work based on it,
- Xunder Section 2) in object code or executable form under the terms of
- XSections 1 and 2 above provided that you also do one of the following:
- X
- X a) Accompany it with the complete corresponding machine-readable
- X source code, which must be distributed under the terms of Sections
- X 1 and 2 above on a medium customarily used for software interchange; or,
- X
- X b) Accompany it with a written offer, valid for at least three
- X years, to give any third party, for a charge no more than your
- X cost of physically performing source distribution, a complete
- X machine-readable copy of the corresponding source code, to be
- X distributed under the terms of Sections 1 and 2 above on a medium
- X customarily used for software interchange; or,
- X
- X c) Accompany it with the information you received as to the offer
- X to distribute corresponding source code. (This alternative is
- X allowed only for noncommercial distribution and only if you
- X received the program in object code or executable form with such
- X an offer, in accord with Subsection b above.)
- X
- XThe source code for a work means the preferred form of the work for
- Xmaking modifications to it. For an executable work, complete source
- Xcode means all the source code for all modules it contains, plus any
- Xassociated interface definition files, plus the scripts used to
- Xcontrol compilation and installation of the executable. However, as a
- Xspecial exception, the source code distributed need not include
- Xanything that is normally distributed (in either source or binary
- Xform) with the major components (compiler, kernel, and so on) of the
- Xoperating system on which the executable runs, unless that component
- Xitself accompanies the executable.
- X
- XIf distribution of executable or object code is made by offering
- Xaccess to copy from a designated place, then offering equivalent
- Xaccess to copy the source code from the same place counts as
- Xdistribution of the source code, even though third parties are not
- Xcompelled to copy the source along with the object code.
- X
- X 4. You may not copy, modify, sublicense, or distribute the Program
- Xexcept as expressly provided under this License. Any attempt
- Xotherwise to copy, modify, sublicense or distribute the Program is
- Xvoid, and will automatically terminate your rights under this License.
- XHowever, parties who have received copies, or rights, from you under
- Xthis License will not have their licenses terminated so long as such
- Xparties remain in full compliance.
- X
- X 5. You are not required to accept this License, since you have not
- Xsigned it. However, nothing else grants you permission to modify or
- Xdistribute the Program or its derivative works. These actions are
- Xprohibited by law if you do not accept this License. Therefore, by
- Xmodifying or distributing the Program (or any work based on the
- XProgram), you indicate your acceptance of this License to do so, and
- Xall its terms and conditions for copying, distributing or modifying
- Xthe Program or works based on it.
- X
- X 6. Each time you redistribute the Program (or any work based on the
- XProgram), the recipient automatically receives a license from the
- Xoriginal licensor to copy, distribute or modify the Program subject to
- Xthese terms and conditions. You may not impose any further
- Xrestrictions on the recipients' exercise of the rights granted herein.
- XYou are not responsible for enforcing compliance by third parties to
- Xthis License.
- X
- X 7. If, as a consequence of a court judgment or allegation of patent
- Xinfringement or for any other reason (not limited to patent issues),
- Xconditions are imposed on you (whether by court order, agreement or
- Xotherwise) that contradict the conditions of this License, they do not
- Xexcuse you from the conditions of this License. If you cannot
- Xdistribute so as to satisfy simultaneously your obligations under this
- XLicense and any other pertinent obligations, then as a consequence you
- Xmay not distribute the Program at all. For example, if a patent
- Xlicense would not permit royalty-free redistribution of the Program by
- Xall those who receive copies directly or indirectly through you, then
- Xthe only way you could satisfy both it and this License would be to
- Xrefrain entirely from distribution of the Program.
- X
- XIf any portion of this section is held invalid or unenforceable under
- Xany particular circumstance, the balance of the section is intended to
- Xapply and the section as a whole is intended to apply in other
- Xcircumstances.
- X
- XIt is not the purpose of this section to induce you to infringe any
- Xpatents or other property right claims or to contest validity of any
- Xsuch claims; this section has the sole purpose of protecting the
- Xintegrity of the free software distribution system, which is
- Ximplemented by public license practices. Many people have made
- Xgenerous contributions to the wide range of software distributed
- Xthrough that system in reliance on consistent application of that
- Xsystem; it is up to the author/donor to decide if he or she is willing
- Xto distribute software through any other system and a licensee cannot
- Ximpose that choice.
- X
- XThis section is intended to make thoroughly clear what is believed to
- Xbe a consequence of the rest of this License.
- X
- X 8. If the distribution and/or use of the Program is restricted in
- Xcertain countries either by patents or by copyrighted interfaces, the
- Xoriginal copyright holder who places the Program under this License
- Xmay add an explicit geographical distribution limitation excluding
- Xthose countries, so that distribution is permitted only in or among
- Xcountries not thus excluded. In such case, this License incorporates
- Xthe limitation as if written in the body of this License.
- X
- X 9. The Free Software Foundation may publish revised and/or new versions
- Xof the General Public License from time to time. Such new versions will
- Xbe similar in spirit to the present version, but may differ in detail to
- Xaddress new problems or concerns.
- X
- XEach version is given a distinguishing version number. If the Program
- Xspecifies a version number of this License which applies to it and "any
- Xlater version", you have the option of following the terms and conditions
- Xeither of that version or of any later version published by the Free
- XSoftware Foundation. If the Program does not specify a version number of
- Xthis License, you may choose any version ever published by the Free Software
- XFoundation.
- X
- X 10. If you wish to incorporate parts of the Program into other free
- Xprograms whose distribution conditions are different, write to the author
- Xto ask for permission. For software which is copyrighted by the Free
- XSoftware Foundation, write to the Free Software Foundation; we sometimes
- Xmake exceptions for this. Our decision will be guided by the two goals
- Xof preserving the free status of all derivatives of our free software and
- Xof promoting the sharing and reuse of software generally.
- X
- X NO WARRANTY
- X
- X 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
- XFOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
- XOTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
- XPROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
- XOR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- XMERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
- XTO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
- XPROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
- XREPAIR OR CORRECTION.
- X
- X 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
- XWILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
- XREDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
- XINCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
- XOUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
- XTO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
- XYOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
- XPROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
- XPOSSIBILITY OF SUCH DAMAGES.
- X
- X END OF TERMS AND CONDITIONS
- X
- X How to Apply These Terms to Your New Programs
- X
- X If you develop a new program, and you want it to be of the greatest
- Xpossible use to the public, the best way to achieve this is to make it
- Xfree software which everyone can redistribute and change under these terms.
- X
- X To do so, attach the following notices to the program. It is safest
- Xto attach them to the start of each source file to most effectively
- Xconvey the exclusion of warranty; and each file should have at least
- Xthe "copyright" line and a pointer to where the full notice is found.
- X
- X <one line to give the program's name and a brief idea of what it does.>
- X Copyright (C) 19yy <name of author>
- X
- X This program is free software; you can redistribute it and/or modify
- X it under the terms of the GNU General Public License as published by
- X the Free Software Foundation; either version 2 of the License, or
- X (at your option) any later version.
- X
- X This program is distributed in the hope that it will be useful,
- X but WITHOUT ANY WARRANTY; without even the implied warranty of
- X MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- X GNU General Public License for more details.
- X
- X You should have received a copy of the GNU General Public License
- X along with this program; if not, write to the Free Software
- X Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- X
- XAlso add information on how to contact you by electronic and paper mail.
- X
- XIf the program is interactive, make it output a short notice like this
- Xwhen it starts in an interactive mode:
- X
- X Gnomovision version 69, Copyright (C) 19yy name of author
- X Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
- X This is free software, and you are welcome to redistribute it
- X under certain conditions; type `show c' for details.
- X
- XThe hypothetical commands `show w' and `show c' should show the appropriate
- Xparts of the General Public License. Of course, the commands you use may
- Xbe called something other than `show w' and `show c'; they could even be
- Xmouse-clicks or menu items--whatever suits your program.
- X
- XYou should also get your employer (if you work as a programmer) or your
- Xschool, if any, to sign a "copyright disclaimer" for the program, if
- Xnecessary. Here is a sample; alter the names:
- X
- X Yoyodyne, Inc., hereby disclaims all copyright interest in the program
- X `Gnomovision' (which makes passes at compilers) written by James Hacker.
- X
- X <signature of Ty Coon>, 1 April 1989
- X Ty Coon, President of Vice
- X
- XThis General Public License does not permit incorporating your program into
- Xproprietary programs. If your program is a subroutine library, you may
- Xconsider it more useful to permit linking proprietary applications with the
- Xlibrary. If this is what you want to do, use the GNU Library General
- XPublic License instead of this License.
- END_OF_FILE
- if test 17976 -ne `wc -c <'COPYING'`; then
- echo shar: \"'COPYING'\" unpacked with wrong size!
- fi
- # end of 'COPYING'
- fi
- if test -f 'comp.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'comp.c'\"
- else
- echo shar: Extracting \"'comp.c'\" \(7143 characters\)
- sed "s/^X//" >'comp.c' <<'END_OF_FILE'
- X/*
- X reversi - play a game of reversi against the computer or a human
- X Copyright (C) 1992 Elias Martenson
- X
- X This program is free software; you can redistribute it and/or modify
- X it under the terms of the GNU General Public License as published by
- X the Free Software Foundation; either version 2 of the License, or
- X (at your option) any later version.
- X
- X This program is distributed in the hope that it will be useful,
- X but WITHOUT ANY WARRANTY; without even the implied warranty of
- X MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- X GNU General Public License for more details.
- X
- X You should have received a copy of the GNU General Public License
- X along with this program; if not, write to the Free Software
- X Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- X
- X Contact me by email at elias@proxxi.se
- X*/
- X
- X#include <stdio.h>
- X#include <curses.h>
- X#include "reversi.h"
- X
- Xextern int *game_board;
- X
- X/*
- X * get_comp_move, calculates the best move for player
- X *
- X */
- X
- Xget_comp_move( player, board, x, y, level, f )
- Xint player;
- Xint *board;
- Xint *x, *y;
- Xint level, f;
- X{
- X int score[8][8];
- X int max_score = -10000;
- X int x2, y2, c = 0;
- X int t = FALSE;
- X char str[20];
- X
- X for( x2 = 0 ; x2 < 8 ; x2++ ){
- X for( y2 = 0 ; y2 < 8 ; y2++ ){
- X score[x2][y2] = -10000;
- X }
- X }
- X
- X if( f ){
- X for( x2 = 0 ; x2 < 8 ; x2++ ){
- X for( y2 = 0 ; y2 < 8 ; y2++ ){
- X if( get_board( board, x2, y2 ) == 0 ){
- X c++;
- X }
- X }
- X }
- X if( c <= 15 ){
- X level++;
- X }
- X
- X mvaddstr( 7, 52, " " );
- X move( 7, 52 );
- X refresh();
- X }
- X
- X /* Go though all possible squares */
- X for( x2 = 0 ; x2 < 8 ; x2++ ){
- X for( y2 = 0 ; y2 < 8 ; y2++ ){
- X if( possible_move_square( board, x2, y2, player ) ){
- X score[x2][y2] = calc_score_move( board, x2, y2, player, level );
- X if( f ) addch( '.' );
- X refresh();
- X }
- X }
- X }
- X
- X#ifndef DEBUG
- X /* Return best score for move */
- X for( x2 = 0 ; x2 < 8 ; x2++ ){
- X for( y2 = 0 ; y2 < 8 ; y2++ ){
- X if( possible_move_square( board, x2, y2, player ) ){
- X if( score[x2][y2] > max_score ){
- X max_score = score[x2][y2];
- X *x = x2;
- X *y = y2;
- X }
- X t = TRUE;
- X }
- X }
- X }
- X#else /* DEBUG */
- X /* Return best score for move */
- X if( !f ){
- X for( x2 = 0 ; x2 < 8 ; x2++ ){
- X for( y2 = 0 ; y2 < 8 ; y2++ ){
- X if( possible_move_square( board, x2, y2, player ) ){
- X if( score[x2][y2] > max_score ){
- X max_score = score[x2][y2];
- X *x = x2;
- X *y = y2;
- X }
- X t = TRUE;
- X }
- X }
- X }
- X }
- X else{
- X for( x2 = 0 ; x2 < 8 ; x2++ ){
- X for( y2 = 0 ; y2 < 8 ; y2++ ){
- X if( possible_move_square( board, x2, y2, player ) ){
- X sprintf( str, "%d", score[x2][y2] );
- X mvaddstr( y2 * 3, x2 * 6, str );
- X if( score[x2][y2] > max_score ){
- X max_score = score[x2][y2];
- X *x = x2;
- X *y = y2;
- X }
- X t = TRUE;
- X }
- X }
- X }
- X refresh();
- X getch();
- X draw_board( game_board );
- X }
- X#endif /* DEBUG */
- X
- X if( !t ){
- X printf( "Internal panic! No possible move found!\n" );
- X printf( "x = %d, y = %d, score = %d\n", *x, *y, max_score );
- X exit( 1 );
- X }
- X return( max_score );
- X}
- X
- X/*
- X * calc_score_move, returns the score for move
- X *
- X */
- X
- Xcalc_score_move( board, x, y, player, level )
- Xint *board;
- Xint x, y, player, level;
- X{
- X int temp_board[64];
- X int dir, score;
- X int x2, y2, x3, y3, tmp;
- X
- X score = 0;
- X
- X for( x2 = 0 ; x2 < 8 ; x2++ ){
- X for( y2 = 0 ; y2 < 8 ; y2++ ){
- X get_board( temp_board, x2, y2 ) = get_board( board, x2, y2 );
- X }
- X }
- X
- X for( dir = 0 ; dir < 8 ; dir++ ){
- X if( (tmp = possible_move_dir( board, x, y, dir, player, &x2, &y2 )) != 0 ){
- X tmp *= SQUARE_SCORE;
- X score += tmp;
- X }
- X }
- X tmp = add_score_move( board, x, y, player );
- X score += tmp;
- X
- X if( level != 0 ){
- X put_square( player, temp_board, x, y );
- X if( possible_move( temp_board, !player ) ){
- X tmp = get_comp_move( !player, temp_board, &x3, &y3, level - 1, 0 );
- X score += 4000 - (int)((float)tmp / DIV_POINTS);
- X }
- X else{
- X score += NO_MOVE_BONUS;
- X }
- X }
- X
- X return( score );
- X}
- X
- X/*
- X * add_score_move, calulate the bonus for this position
- X *
- X */
- X
- Xadd_score_move( board, x, y, player )
- Xint *board;
- Xint x, y, player;
- X{
- X int tmp;
- X int c;
- X
- X if( (x == 0 && y == 0 ) ||
- X (x == 0 && y == 7 ) ||
- X (x == 7 && y == 0 ) ||
- X (x == 7 && y == 7 )){
- X return( CORNER_SCORE );
- X }
- X
- X if( (x == 0) || (x == 7) ){
- X if( y <= 3 ){
- X tmp = BORDER_SCORE + BORDER_POS_BONUS * (y + 1);
- X }
- X else{
- X tmp = BORDER_SCORE + (BORDER_POS_BONUS * ((3 - (y - 4)) + 1));
- X }
- X
- X if( get_board( board, x, y - 1 ) == (!player + 1) && get_board( board, x, y + 1 ) != (!player + 1) ){
- X c = y - 1;
- X while( (get_board( board, x, c ) == (!player + 1)) && (c > 0) ) c--;
- X if( get_board( board, x, c ) != (player + 1) ){
- X return( 0 );
- X }
- X }
- X else if( get_board( board, x, y + 1 ) == (!player + 1) && get_board( board, x, y - 1 ) != (!player + 1) ){
- X c = y + 1;
- X while( (get_board( board, x, c ) == (!player + 1)) && (c < 7) ) c++;
- X if( get_board( board, x, c ) != (player + 1) ){
- X return( 0 );
- X }
- X }
- X
- X/* Check if this is a double score position */
- X if( get_board( board, x, y - 1 ) == (!player + 1) && get_board( board, x, y + 1 ) == (!player + 1) ){
- X return( tmp * 2 );
- X }
- X else{
- X return( tmp );
- X }
- X }
- X
- X if( (y == 0) || (y == 7) ){
- X if( x <= 3 ){
- X tmp = BORDER_SCORE + BORDER_POS_BONUS * (x + 1);
- X }
- X else{
- X tmp = BORDER_SCORE + (BORDER_POS_BONUS * ((3 - (x - 4)) + 1));
- X }
- X
- X if( get_board( board, x - 1, y ) == (!player + 1) && get_board( board, x + 1, y ) != (!player + 1) ){
- X c = x - 1;
- X while( (get_board( board, c, y ) == (!player + 1)) && (c > 0) ) c--;
- X if( get_board( board, c, x ) != (player + 1) ){
- X return( 0 );
- X }
- X }
- X else if( get_board( board, x + 1, y ) == (!player + 1) && get_board( board, x - 1, y ) != (!player + 1) ){
- X c = y - 1;
- X while( (get_board( board, c, y ) == (!player + 1)) && (c < 7) ) c++;
- X if( get_board( board, c, x ) != (player + 1) ){
- X return( 0 );
- X }
- X }
- X
- X/* Check if this is a double score position */
- X if( get_board( board, x - 1, y ) == (!player + 1) && get_board( board, x + 1, y ) == (!player + 1) ){
- X return( tmp * 2 );
- X }
- X else{
- X return( tmp );
- X }
- X }
- X
- X if( (x == 1 && y == 1) ||
- X (x == 1 && y == 6) ||
- X (x == 6 && y == 1) ||
- X (x == 6 && y == 6)){
- X return( BORDER2_CORNER_SCORE );
- X }
- X
- X if( (x == 1) || (x == 6) ){
- X if( y <= 3 ) return( BORDER2_SCORE + BORDER2_POS_BONUS * y );
- X else return( BORDER2_SCORE + (BORDER2_POS_BONUS * (4 - (y - 3))) );
- X }
- X
- X if( (y == 1) || (y == 6) ){
- X if( x <= 3 ) return( BORDER2_SCORE + BORDER2_POS_BONUS * x );
- X else return( BORDER2_SCORE + (BORDER2_POS_BONUS * (4 - (x - 3))) );
- X }
- X
- X if( (x == 2 && y == 2) ||
- X (x == 2 && y == 5) ||
- X (x == 5 && y == 2) ||
- X (x == 5 && y == 5)){
- X return( BORDER3_CORNER_SCORE );
- X }
- X
- X if( (x == 2) || (y == 5) ){
- X return( BORDER3_SCORE );
- X }
- X
- X if( (y == 2) || (y == 5) ){
- X return( BORDER3_SCORE );
- X }
- X
- X return( 0 );
- X}
- END_OF_FILE
- if test 7143 -ne `wc -c <'comp.c'`; then
- echo shar: \"'comp.c'\" unpacked with wrong size!
- fi
- # end of 'comp.c'
- fi
- if test -f 'graphics.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'graphics.c'\"
- else
- echo shar: Extracting \"'graphics.c'\" \(6179 characters\)
- sed "s/^X//" >'graphics.c' <<'END_OF_FILE'
- X/*
- X reversi - play a game of reversi against the computer or human
- X Copyright (C) 1992 Elias Martenson
- X
- X This program is free software; you can redistribute it and/or modify
- X it under the terms of the GNU General Public License as published by
- X the Free Software Foundation; either version 2 of the License, or
- X (at your option) any later version.
- X
- X This program is distributed in the hope that it will be useful,
- X but WITHOUT ANY WARRANTY; without even the implied warranty of
- X MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- X GNU General Public License for more details.
- X
- X You should have received a copy of the GNU General Public License
- X along with this program; if not, write to the Free Software
- X Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- X
- X Contact me by email at elias@proxxi.se
- X*/
- X
- X
- X#include <curses.h>
- X#include "reversi.h"
- X#include "menu.h"
- X
- Xextern int *game_board;
- X
- X/*
- X * Do all curses screen init
- X *
- X */
- X
- Xinit_screen()
- X{
- X initscr();
- X cbreak();
- X nonl();
- X noecho();
- X
- X clear();
- X refresh();
- X}
- X
- Xexit_screen()
- X{
- X endwin();
- X}
- X
- Xdraw_board( board )
- Xint *board;
- X{
- X int x, y;
- X int num_w = 0, num_b = 0;
- X char str[80];
- X
- X for( x = 0 ; x < 8 ; x++ ){
- X for( y = 0 ; y < 8 ; y++ ){
- X if( get_board( board, x, y ) == 1 ) num_w++;
- X else if( get_board( board, x, y ) == 2 ) num_b++;
- X }
- X }
- X
- X mvaddstr( 17, 52, " " );
- X mvaddstr( 18, 52, " " );
- X sprintf( str, "White: %d", num_w );
- X mvaddstr( 17, 52, str );
- X sprintf( str, "Black: %d", num_b );
- X mvaddstr( 18, 52, str );
- X
- X mvaddstr( 20, 52, "ESC for menu" );
- X mvaddstr( 21, 52, "By Elias Martenson" );
- X
- X for( y = 0 ; y < 8 ; y++ ){
- X for( x = 0 ; x < 8 ; x++ ){
- X switch( get_board( board, x, y ) ){
- X case 0:
- X mvaddstr( y * 3, x * 6, " .. " );
- X mvaddstr( y * 3 + 1, x * 6, " .. " );
- X break;
- X case 1:
- X mvaddstr( y * 3, x * 6, "< >" );
- X mvaddstr( y * 3 + 1, x * 6, "< >" );
- X break;
- X case 2:
- X mvaddstr( y * 3, x * 6, "####" );
- X mvaddstr( y * 3 + 1, x * 6, "####" );
- X break;
- X default:
- X printf( "Error: I see there is an illegal square on the board!\n" );
- X break;
- X }
- X }
- X }
- X
- X refresh();
- X}
- X
- Xget_move( player, x, y )
- Xint player;
- Xint *x, *y;
- X{
- X int x2, y2, end_getmove = FALSE;
- X
- X x2 = 3;
- X y2 = 3;
- X
- X do{
- X move( y2 * 3, x2 * 6 + 1 );
- X refresh();
- X
- X switch( getch() ){
- X case 'h':
- X if( x2 >= 1 ) x2--;
- X break;
- X case 'j':
- X if( y2 <= 6 ) y2++;
- X break;
- X case 'k':
- X if( y2 >= 1 ) y2--;
- X break;
- X case 'l':
- X if( x2 <= 6 ) x2++;
- X break;
- X case ' ':
- X if( possible_move_square( game_board, x2, y2, player ) ){
- X end_getmove = TRUE;
- X }
- X break;
- X case '?':
- X do_help();
- X draw_board( game_board );
- X break;
- X case 12:
- X clearok( stdscr, TRUE );
- X refresh();
- X break;
- X case 27:
- X do_game_menu( player );
- X break;
- X }
- X }while( !end_getmove );
- X
- X *x = x2;
- X *y = y2;
- X}
- X
- Xdo_help( player )
- Xint player;
- X{
- X WINDOW *win;
- X
- X win = newwin( 24, 80, 0, 0 );
- X
- X wclear( win );
- X touchwin( win );
- X
- X mvwaddstr( win, 0, 0, "reversi, V1.24" );
- X mvwaddstr( win, 1, 0, "Copyright 1992 Elias Martenson" );
- X mvwaddstr( win, 3, 0, "Move the cursor by using the keys h, j, k, l" );
- X mvwaddstr( win, 4, 0, "And place your tile on a square with space." );
- X mvwaddstr( win, 6, 0, "The object of the game is to have the most number" );
- X mvwaddstr( win, 7, 0, "of tiles with your colour when none of the players" );
- X mvwaddstr( win, 8, 0, "has a legal move." );
- X mvwaddstr( win, 10, 0, "Each tile is white on one side and black on the" );
- X mvwaddstr( win, 11, 0, "other. When a tile is placed, you flip the enemys" );
- X mvwaddstr( win, 12, 0, "tiles that has become \"trapped\" between the one" );
- X mvwaddstr( win, 13, 0, "you just put and another tile of your colour." );
- X mvwaddstr( win, 23, 0, "Press a key to resume play." );
- X
- X wrefresh( win );
- X
- X getch();
- X
- X delwin( win );
- X clearok( stdscr, TRUE );
- X refresh();
- X}
- X
- Xdo_game_menu( player )
- Xint player;
- X{
- X int selected_menu;
- X MenuEntry game_menu[] = {
- X "About", NULL, SELECTABLE,
- X "Setup", NULL, SELECTABLE,
- X "Undo last move", NULL, SELECTABLE,
- X "Show possible moves", NULL, SELECTABLE,
- X "Save game", NULL, SELECTABLE,
- X "Quit game", NULL, SELECTABLE,
- X "Resume game", NULL, SELECTABLE,
- X NULL, NULL, 0
- X };
- X
- X do{
- X selected_menu = menu( 52, 9, 0, 0, 0, WRAP | ENABLE_QUIT | CLEAR_ON_EXIT, game_menu );
- X
- X switch( selected_menu ){
- X case 0:
- X show_about();
- X break;
- X case 1:
- X /* do_setup();*/
- X break;
- X case 2:
- X do_undo_move();
- X break;
- X case 3:
- X do_show_possible( player );
- X break;
- X case 4:
- X save_game( player );
- X break;
- X case 5:
- X exit_rout( 0 );
- X break;
- X }
- X
- X }while( selected_menu != -1 && selected_menu != 5 );
- X}
- X
- Xshow_about()
- X{
- X WINDOW *win;
- X
- X win = newwin( 24, 80, 0, 0 );
- X
- X wclear( win );
- X touchwin( win );
- X
- X mvwaddstr( win, 0, 0, "reversi, V1.24" );
- X mvwaddstr( win, 1, 0, "Copyright 1992 Elias Martenson (elias@proxxi.se)" );
- X mvwaddstr( win, 3, 0, "Menu routines by Stefan Rapp (rappen@proxxi.se)" );
- X mvwaddstr( win, 5, 0, "Beta testers:" );
- X mvwaddstr( win, 6, 0, "Gustav Berggren (gustav@proxxi.se)" );
- X mvwaddstr( win, 7, 0, "Anders Bollmark (bollen@proxxi.se)" );
- X mvwaddstr( win, 8, 0, "Martin Green (bullen@proxxi.se)" );
- X mvwaddstr( win, 9, 0, "Magnus Krafft (krafft@proxxi.se)" );
- X mvwaddstr( win, 10, 0, "Stefan Rapp (rappen@proxxi.se)" );
- X mvwaddstr( win, 11, 0, "Carl Serrander (calle@proxxi.se)" );
- X mvwaddstr( win, 23, 0, "Press any key to resume play." );
- X
- X wrefresh( win );
- X
- X getch();
- X
- X delwin( win );
- X clearok( stdscr, TRUE );
- X refresh();
- X}
- X
- Xdo_show_possible( player )
- Xint player;
- X{
- X int count_x, count_y;
- X
- X for( count_x = 0 ; count_x < 8 ; count_x++ ){
- X for( count_y = 0 ; count_y < 8 ; count_y++ ){
- X if( possible_move_square( game_board, count_x, count_y, player ) ){
- X standout();
- X mvaddstr( count_y * 3, count_x * 6, " " );
- X mvaddstr( count_y * 3 + 1, count_x * 6, " " );
- X standend();
- X }
- X }
- X }
- X
- X refresh();
- X getch();
- X
- X draw_board( game_board );
- X}
- END_OF_FILE
- if test 6179 -ne `wc -c <'graphics.c'`; then
- echo shar: \"'graphics.c'\" unpacked with wrong size!
- fi
- # end of 'graphics.c'
- fi
- if test -f 'menu.h' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'menu.h'\"
- else
- echo shar: Extracting \"'menu.h'\" \(1011 characters\)
- sed "s/^X//" >'menu.h' <<'END_OF_FILE'
- X/*
- X * If TRUE and FALSE isn't defined, we define our own
- X *
- X */
- X
- X#ifndef TRUE
- X#define TRUE (1)
- X#define FALSE (0)
- X#endif
- X
- X/*
- X * Definitions for the keys to control the menus.
- X *
- X */
- X
- X#define MV_UP 'k'
- X#define MV_DOWN 'j'
- X#define PG_UP 'K'
- X#define PG_DOWN 'J'
- X#define QUIT 27
- X#define SELECT 13
- X#define HELP '?'
- X
- X/*
- X * Definitions for menu flags
- X *
- X */
- X
- X#define WRAP 0x01 /* If the menu should wrap */
- X#define DRAW_BOX 0x02 /* If the menu should have a box */
- X#define ENABLE_QUIT 0x04 /* If the menu should accept QUIT key */
- X /* and return -1 */
- X#define ENABLE_HELP 0x08 /* If the menu should accept help key */
- X#define CLEAR_ON_EXIT 0x10 /* Clear the menuwindow on exit */
- X
- X#define DRAW_BOX_SYMBOL_HORIZ '-'
- X#define DRAW_BOX_SYMBOL_VERT '?'
- X
- X#define SELECTABLE 0x01
- X
- X/*
- X * Types for the menu
- X *
- X */
- X
- Xtypedef struct {
- X char *name;
- X char *help;
- X int flags;
- X} MenuEntry;
- X
- Xint menu( int x, int y, int width, int height, int start, int flags, MenuEntry *names );
- END_OF_FILE
- if test 1011 -ne `wc -c <'menu.h'`; then
- echo shar: \"'menu.h'\" unpacked with wrong size!
- fi
- # end of 'menu.h'
- fi
- if test -f 'move.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'move.c'\"
- else
- echo shar: Extracting \"'move.c'\" \(6182 characters\)
- sed "s/^X//" >'move.c' <<'END_OF_FILE'
- X/*
- X reversi - play a game of reversi against the computer or a human
- X Copyright (C) 1992 Elias Martenson
- X
- X This program is free software; you can redistribute it and/or modify
- X it under the terms of the GNU General Public License as published by
- X the Free Software Foundation; either version 2 of the License, or
- X (at your option) any later version.
- X
- X This program is distributed in the hope that it will be useful,
- X but WITHOUT ANY WARRANTY; without even the implied warranty of
- X MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- X GNU General Public License for more details.
- X
- X You should have received a copy of the GNU General Public License
- X along with this program; if not, write to the Free Software
- X Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- X
- X Contact me by email at elias@proxxi.se
- X*/
- X
- X#include "reversi.h"
- X
- Xextern int *game_board;
- X
- X/*
- X * possible_move, returns FALSE if there are no possible moves for
- X * the player, and TRUE if there is.
- X *
- X */
- X
- Xpossible_move( board, player )
- Xint *board;
- Xint player;
- X{
- X int x, y;
- X
- X for( x = 0 ; x < 8 ; x++ ){
- X for( y = 0 ; y < 8 ; y++ ){
- X if( get_board( board, x, y ) == 0 ){
- X if( possible_move_square( board, x, y, player ) ){
- X return( TRUE );
- X }
- X }
- X }
- X }
- X return( FALSE );
- X}
- X
- X/*
- X * possible_move_square, returns FALSE if x, y is an illegal move
- X * for player.
- X *
- X */
- X
- Xpossible_move_square( board, x, y, player )
- Xint *board;
- Xint x, y, player;
- X{
- X int dir, x2, y2;
- X
- X if( get_board( board, x, y ) != 0 ) return( FALSE );
- X for( dir = 0 ; dir < 8 ; dir++ ){
- X if( possible_move_dir( board, x, y, dir, player, &x2, &y2 ) != 0 ){
- X return( TRUE );
- X }
- X }
- X return( FALSE );
- X}
- X
- X/*
- X * possible_move_dir, returns 0 if x, y, dir is an illegal move
- X * for player, otherwise the number of turned squares
- X *
- X */
- X
- Xpossible_move_dir( board, x, y, dir, player, x2, y2 )
- Xint *board;
- Xint x, y, dir, player;
- Xint *x2, *y2;
- X{
- X *x2 = x;
- X *y2 = y;
- X
- X
- X switch( dir ){
- X case 0: /* Up */
- X if( --*y2 < 1 ) return( 0 );
- X if( get_board( board, x, *y2 ) == 0 || get_board( board, x, *y2 ) == (player + 1)) return( 0 );
- X while( *y2 > 0 ){
- X (*y2)--;
- X if( get_board( board, x, *y2 ) == 0 ) return( 0 );
- X if( get_board( board, x, *y2 ) == (player + 1) ) return( y - *y2 );
- X }
- X break;
- X case 1: /* Up Right */
- X if( --*y2 < 1 || ++*x2 > 6 ) return( 0 );
- X if( get_board( board, *x2, *y2 ) == 0 || get_board( board, *x2, *y2 ) == (player + 1)) return( 0 );
- X while( *y2 > 0 && *x2 < 7 ){
- X (*y2)--;
- X (*x2)++;
- X if( get_board( board, *x2, *y2 ) == 0 ) return( 0 );
- X if( get_board( board, *x2, *y2 ) == (player + 1) ) return( *x2 - x );
- X }
- X break;
- X case 2: /* Right */
- X if( ++*x2 > 6 ) return( 0 );
- X if( get_board( board, *x2, y ) == 0 || get_board( board, *x2, y ) == (player + 1)) return( 0 );
- X while( *x2 < 7 ){
- X (*x2)++;
- X if( get_board( board, *x2, y ) == 0 ) return( 0 );
- X if( get_board( board, *x2, y ) == (player + 1) ) return( *x2 - x );
- X }
- X break;
- X case 3: /* Down Right */
- X if( ++*y2 > 6 || ++*x2 > 6 ) return( 0 );
- X if( get_board( board, *x2, *y2 ) == 0 || get_board( board, *x2, *y2 ) == ( player + 1)) return( 0 );
- X while( *y2 < 7 && *x2 < 7 ){
- X (*y2)++;
- X (*x2)++;
- X if( get_board( board, *x2, *y2 ) == 0 ) return( 0 );
- X if( get_board( board, *x2, *y2 ) == (player + 1) ) return( *x2 - x );
- X }
- X break;
- X case 4: /* Down */
- X if( ++*y2 > 6 ) return( 0 );
- X if( get_board( board, x, *y2 ) == 0 || get_board( board, x, *y2 ) == (player + 1)) return( 0 );
- X while( *y2 < 7 ){
- X (*y2)++;
- X if( get_board( board, x, *y2 ) == 0 ) return( 0 );
- X if( get_board( board, x, *y2 ) == (player + 1) ) return( *y2 - y );
- X }
- X break;
- X case 5: /* Down Left */
- X if( ++*y2 > 6 || --*x2 < 1 ) return( 0 );
- X if( get_board( board, *x2, *y2 ) == 0 || get_board( board, *x2, *y2 ) == (player + 1)) return( 0 );
- X while( *y2 < 7 && *x2 > 0 ){
- X (*y2)++;
- X (*x2)--;
- X if( get_board( board, *x2, *y2 ) == 0 ) return( 0 );
- X if( get_board( board, *x2, *y2 ) == (player + 1) ) return( x - *x2 );
- X }
- X break;
- X case 6: /* Left */
- X if( --*x2 < 1 ) return( 0 );
- X if( get_board( board, *x2, y ) == 0 || get_board( board, *x2, y ) == (player + 1)) return( 0 );
- X while( *x2 > 0 ){
- X (*x2)--;
- X if( get_board( board, *x2, y ) == 0 ) return( 0 );
- X if( get_board( board, *x2, y ) == (player + 1) ) return( x - *x2 );
- X }
- X break;
- X case 7: /* Up Left */
- X if( --*y2 < 1 || --*x2 < 1 ) return( 0 );
- X if( get_board( board, *x2, *y2 ) == 0 || get_board( board, *x2, *y2 ) == (player + 1)) return( 0 );
- X while( *y2 > 0 && *x2 > 0 ){
- X (*y2)--;
- X (*x2)--;
- X if( get_board( board, *x2, *y2 ) == 0 ) return( 0 );
- X if( get_board( board, *x2, *y2 ) == (player + 1) ) return( x - *x2 );
- X }
- X break;
- X }
- X return( 0 );
- X}
- X
- X/*
- X * put_square, puts a square of mark player on the board, turning
- X * other squares
- X *
- X */
- X
- Xput_square( player, board, x, y )
- Xint player;
- Xint *board;
- Xint x, y;
- X{
- X int dir, x2, y2;
- X
- X for( dir = 0 ; dir < 8 ; dir++ ){
- X if( possible_move_dir( board, x, y, dir, player, &x2, &y2 ) != 0 ){
- X get_board( board, x, y ) = player + 1;
- X flip( board, x, y, x2, y2, dir, player );
- X }
- X }
- X}
- X
- X/*
- X * flip, flips all squares in a line, direction dir
- X *
- X */
- X
- Xflip( board, x, y, x2, y2, dir, player )
- Xint *board;
- Xint x, y, x2, y2, dir, player;
- X{
- X int a_x, a_y;
- X
- X switch( dir ){
- X case 0: /* Up */
- X a_x = 0;
- X a_y = -1;
- X break;
- X case 1: /* Up Right */
- X a_x = 1;
- X a_y = -1;
- X break;
- X case 2: /* Right */
- X a_x = 1;
- X a_y = 0;
- X break;
- X case 3: /* Down Right */
- X a_x = 1;
- X a_y = 1;
- X break;
- X case 4: /* Down */
- X a_x = 0;
- X a_y = 1;
- X break;
- X case 5: /* Down Left */
- X a_x = -1;
- X a_y = 1;
- X break;
- X case 6: /* Left */
- X a_x = -1;
- X a_y = 0;
- X break;
- X case 7: /* Up Left */
- X a_x = -1;
- X a_y = -1;
- X break;
- X }
- X
- X x += a_x;
- X y += a_y;
- X
- X do{
- X get_board( board, x, y ) = player + 1;
- X x += a_x;
- X y += a_y;
- X }while( get_board( board, x, y ) == ( !player + 1 ) );
- X}
- END_OF_FILE
- if test 6182 -ne `wc -c <'move.c'`; then
- echo shar: \"'move.c'\" unpacked with wrong size!
- fi
- # end of 'move.c'
- fi
- if test -f 'reversi.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'reversi.c'\"
- else
- echo shar: Extracting \"'reversi.c'\" \(9443 characters\)
- sed "s/^X//" >'reversi.c' <<'END_OF_FILE'
- X/*
- X reversi - play a game of reversi against the computer or a human
- X Copyright (C) 1992 Elias Martenson
- X
- X This program is free software; you can redistribute it and/or modify
- X it under the terms of the GNU General Public License as published by
- X the Free Software Foundation; either version 2 of the License, or
- X (at your option) any later version.
- X
- X This program is distributed in the hope that it will be useful,
- X but WITHOUT ANY WARRANTY; without even the implied warranty of
- X MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- X GNU General Public License for more details.
- X
- X You should have received a copy of the GNU General Public License
- X along with this program; if not, write to the Free Software
- X Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- X
- X Contact me by email at elias@proxxi.se
- X*/
- X
- X#include <stdio.h>
- X#include <signal.h>
- X#include <curses.h>
- X#include <string.h>
- X#include "reversi.h"
- X#include "menu.h"
- X
- Xchar *getenv( char * );
- X
- Xint game_board2[64];
- Xint *game_board;
- Xint *old_gameboard;
- Xint game_lev1, game_lev2;
- Xint old_pointer;
- Xint to_load = 0;
- Xint p1, p2;
- X
- X#ifdef HAVE_VOID_SIGNALS
- Xvoid exit_rout();
- X#else
- Xint exit_rout();
- X#endif
- X
- Xmain( argc, argv )
- Xint argc;
- Xchar **argv;
- X{
- X int w_b;
- X char str[20];
- X char c;
- X int starting_player;
- X char tmp_str[80];
- X FILE *fp;
- X int curr_scr;
- X int x, y;
- X int t;
- X MenuEntry game_setup[] = {
- X "Standard", NULL, SELECTABLE,
- X "Inverse ", NULL, SELECTABLE,
- X NULL, NULL, 0
- X };
- X MenuEntry select_player[] = {
- X "Human ", NULL, SELECTABLE,
- X "Computer", NULL, SELECTABLE,
- X NULL, NULL, 0
- X };
- X MenuEntry level_menu[] = {
- X "Apprentice", NULL, SELECTABLE,
- X "Amateur ", NULL, SELECTABLE,
- X "Regular ", NULL, SELECTABLE,
- X "Advanced ", NULL, SELECTABLE,
- X "Hard ", NULL, SELECTABLE,
- X "Very Hard ", NULL, SELECTABLE,
- X NULL, NULL, 0
- X };
- X
- X old_pointer = 0;
- X starting_player = 0;
- X
- X if( argc == 2 ){
- X if( strcmp( argv[1], "-r" ) == 0 ){
- X to_load = TRUE;
- X }
- X else{
- X usage();
- X }
- X }
- X else if( argc > 2 ){
- X usage();
- X }
- X
- X init_screen();
- X
- X signal( SIGINT, exit_rout );
- X
- X game_board = (int *)game_board2;
- X if( (old_gameboard = (int *)malloc( sizeof(int) * 64 * 60 )) == NULL ){
- X fprintf( stderr, "Could not allocate memory.\n" );
- X endwin();
- X exit( 1 );
- X }
- X
- X addstr( "reversi version 1.24, Copyright (C) 1992 Elias Martenson\n" );
- X addstr( "reversi comes with ABSOLUTELY NO WARRANTY.\n" );
- X addstr( "This is free software, and you are welcome to redistribute it\n" );
- X addstr( "under certain conditions; see the file COPYING for details.\n\n" );
- X
- X refresh();
- X
- X if( !to_load ){
- X
- X mvaddstr( 8, 0, "Select game setup" );
- X refresh();
- X w_b = menu( 0, 10, 18, 2, 0, WRAP, game_setup );
- X
- X mvaddstr( 8, 20, "White played by?" );
- X refresh();
- X p1 = menu( 20, 10, 18, 2, 0, WRAP, select_player );
- X if( p1 == 1 ){
- X mvaddstr( 14, 20, "Enter level:" );
- X refresh();
- X game_lev1 = menu( 20, 16, 18, 6, 0, WRAP, level_menu );
- X }
- X
- X mvaddstr( 8, 40, "Black played by?" );
- X refresh();
- X p2 = menu( 40, 10, 18, 2, 0, WRAP, select_player );
- X if( p2 == 1 ){
- X mvaddstr( 14, 40, "Enter level:" );
- X refresh();
- X game_lev2 = menu( 40, 16, 18, 6, 0, WRAP, level_menu );
- X }
- X
- X clear();
- X refresh();
- X init_board( w_b );
- X
- X }
- X else{
- X strcpy( tmp_str, getenv( "HOME" ) );
- X strcat( tmp_str, "/.reversi_save" );
- X if( (fp = fopen( tmp_str, "r" )) == NULL ){
- X fprintf( stderr, "Could not open save file\n" );
- X exit_rout( 0 );
- X }
- X fread( &starting_player, sizeof( int ), 1, fp );
- X fread( &p1, sizeof( int ), 1, fp );
- X fread( &p2, sizeof( int ), 1, fp );
- X fread( &game_lev1, sizeof( int ), 1, fp );
- X fread( &game_lev2, sizeof( int ), 1, fp );
- X
- X curr_scr = 0;
- X do{
- X for( x = 0 ; x < 8 ; x++ ){
- X for( y = 0 ; y < 8 ; y++ ){
- X fread( &t, sizeof( int ), 1, fp );
- X get_board( old_gameboard + curr_scr, x, y ) = t;
- X }
- X }
- X curr_scr += 64;
- X }while( !feof( fp ) );
- X fclose( fp );
- X curr_scr -= 64;
- X old_pointer = curr_scr;
- X for( x = 0 ; x < 8 ; x++ ){
- X for( y = 0 ; y < 8 ; y++ ){
- X get_board( game_board, x, y ) = get_board( old_gameboard + curr_scr - 64, x, y );
- X }
- X }
- X }
- X clear();
- X
- X draw_board( game_board );
- X play( p1, p2, starting_player );
- X exit_rout( 0 );
- X}
- X
- Xusage()
- X{
- X fprintf( stderr, "Usage: reversi [-r]\n" );
- X exit( 1 );
- X}
- X
- X/*
- X * Init game board
- X *
- X */
- X
- Xinit_board( w_b )
- Xint w_b;
- X{
- X int x, y;
- X
- X for( x = 0 ; x < 8 ; x++ ){
- X for( y = 0 ; y < 8 ; y++ ){
- X get_board( game_board, x, y ) = 0;
- X }
- X }
- X
- X if( w_b == 0 ){
- X get_board( game_board, 3, 3 ) = 1;
- X get_board( game_board, 4, 3 ) = 2;
- X get_board( game_board, 3, 4 ) = 2;
- X get_board( game_board, 4, 4 ) = 1;
- X }
- X else{
- X get_board( game_board, 3, 3 ) = 2;
- X get_board( game_board, 4, 3 ) = 1;
- X get_board( game_board, 3, 4 ) = 1;
- X get_board( game_board, 4, 4 ) = 2;
- X }
- X}
- X
- X#ifdef HAVE_VOID_SIGNALS
- Xvoid exit_rout( sig )
- X#else
- Xint exit_rout( sig )
- X#endif
- Xint sig;
- X{
- X move( 23, 0 );
- X refresh();
- X exit_screen();
- X exit(0);
- X}
- X
- Xplay( p1, p2, starting_player )
- Xint p1, p2, starting_player;
- X{
- X int x, y, x2, y2;
- X int c_w, c_w2, cur_player, end_w = FALSE;
- X int p[2], game_l[2];
- X int a;
- X char str[200];
- X char player_str[2][40];
- X int count_x, count_y;
- X
- X c_w = 0;
- X c_w2 = 0;
- X
- X p[0] = p1;
- X p[1] = p2;
- X game_l[0] = game_lev1;
- X game_l[1] = game_lev2;
- X
- X if( p[0] == 0 ){
- X strcpy( player_str[0], "Human plays white" );
- X }
- X else{
- X sprintf( player_str[0], "Computer level %d plays white", game_l[0] );
- X }
- X
- X if( p[1] == 0 ){
- X strcpy( player_str[1], "Human plays black" );
- X }
- X else{
- X sprintf( player_str[1], "Computer level %d plays black", game_l[1] );
- X }
- X
- X mvaddstr( 2, 52, player_str[0] );
- X mvaddstr( 3, 52, player_str[1] );
- X refresh();
- X
- X if( !to_load ){
- X for( count_x = 0 ; count_x < 8 ; count_x++ ){
- X for( count_y = 0 ; count_y < 8 ; count_y++ ){
- X get_board( old_gameboard, count_x, count_y ) = get_board( game_board, count_x, count_y );
- X }
- X }
- X
- X old_pointer += 64;
- X
- X for( count_x = 0 ; count_x < 8 ; count_x++ ){
- X for( count_y = 0 ; count_y < 8 ; count_y++ ){
- X get_board( old_gameboard + old_pointer, count_x, count_y ) = get_board( game_board, count_x, count_y );
- X }
- X }
- X
- X old_pointer += 64;
- X }
- X
- X do{
- X for( cur_player = starting_player ; cur_player < 2 ; cur_player++ ){
- X starting_player = 0;
- X if( possible_move( game_board, cur_player ) ){
- X if( p[cur_player] == 0 ){
- X get_move( cur_player, &x, &y );
- X }
- X else{
- X#ifndef DEBUG
- X get_comp_move( cur_player, game_board, &x, &y, game_l[cur_player], 1 );
- X#else /* DEBUG */
- X a = get_comp_move( cur_player, game_board, &x, &y, game_l[cur_player], 1 );
- X mvaddstr( 6, 52, " " );
- X sprintf( str, "Score for move: %d", a );
- X mvaddstr( 6, 52, str );
- X#endif /* DEBUG */
- X if( !possible_move_square( game_board, x, y, cur_player ) ){
- X printf( "Internal panic! Tried to put square at: %d, %d\n", x, y );
- X exit( 1 );
- X }
- X }
- X put_square( cur_player, game_board, x, y );
- X draw_board( game_board );
- X
- X }
- X else{
- X if( possible_move( game_board, !cur_player ) ){
- X mvaddstr( 7, 52, "Cannot move, press space." );
- X refresh();
- X getch();
- X mvaddstr( 7, 52, " " );
- X refresh();
- X }
- X else{
- X end_w = TRUE;
- X }
- X }
- X
- X for( count_x = 0 ; count_x < 8 ; count_x++ ){
- X for( count_y = 0 ; count_y < 8 ; count_y++ ){
- X get_board( old_gameboard + old_pointer, count_x, count_y ) = get_board( game_board, count_x, count_y );
- X }
- X }
- X old_pointer += 64;
- X
- X }
- X }while( !end_w );
- X
- X for( x2 = 0 ; x2 < 8 ; x2++ ){
- X for( y2 = 0 ; y2 < 8 ; y2++ ){
- X if( get_board( game_board, x2, y2 ) == 1 ){
- X c_w++;
- X }
- X else if( get_board( game_board, x2, y2 ) == 2 ){
- X c_w2++;
- X }
- X }
- X }
- X
- X if( c_w == c_w2 ){
- X mvaddstr( 9, 52, "This game was a draw." );
- X refresh();
- X }
- X else if( c_w > c_w2 ){
- X mvaddstr( 9, 52, "White is the winner." );
- X refresh();
- X }
- X else{
- X mvaddstr( 9, 52, "Black is the winner." );
- X refresh();
- X }
- X sprintf( str, "White: %d, Black: %d", c_w, c_w2 );
- X mvaddstr( 10, 52, str );
- X move( 23, 0 );
- X refresh();
- X}
- X
- Xdo_undo_move()
- X{
- X int count_x, count_y;
- X
- X if( old_pointer > 192 ){
- X old_pointer -= 192;
- X for( count_x = 0 ; count_x < 8 ; count_x++ ){
- X for( count_y = 0 ; count_y < 8 ; count_y++ ){
- X get_board( game_board, count_x, count_y ) = get_board( old_gameboard + old_pointer, count_x, count_y );
- X }
- X }
- X
- X draw_board( game_board );
- X old_pointer += 64;
- X }
- X}
- X
- Xsave_game( player )
- Xint player;
- X{
- X FILE *fp;
- X char home_path[80];
- X int c;
- X int x, y;
- X int t;
- X
- X strcpy( home_path, getenv( "HOME" ) );
- X strcat( home_path, "/.reversi_save" );
- X
- X if( (fp = fopen( home_path, "w" )) == NULL ){
- X mvaddstr( 8, 52, "Could not open save file" );
- X refresh();
- X return;
- X }
- X
- X fwrite( &player, sizeof( int ), 1, fp );
- X fwrite( &p1, sizeof( int ), 1, fp );
- X fwrite( &p2, sizeof( int ), 1, fp );
- X fwrite( &game_lev1, sizeof( int ), 1, fp );
- X fwrite( &game_lev2, sizeof( int ), 1, fp );
- X
- X for( c = 0 ; c < old_pointer ; c += 64 ){
- X for( x = 0 ; x < 8 ; x++ ){
- X for( y = 0 ; y < 8 ; y++ ){
- X t = get_board( old_gameboard + c, x, y );
- X fwrite( &t, sizeof( int ), 1, fp );
- X }
- X }
- X }
- X fclose( fp );
- X}
- END_OF_FILE
- if test 9443 -ne `wc -c <'reversi.c'`; then
- echo shar: \"'reversi.c'\" unpacked with wrong size!
- fi
- # end of 'reversi.c'
- fi
- echo shar: End of archive 1 \(of 2\).
- cp /dev/null ark1isdone
- MISSING=""
- for I in 1 2 ; do
- if test ! -f ark${I}isdone ; then
- MISSING="${MISSING} ${I}"
- fi
- done
- if test "${MISSING}" = "" ; then
- echo You have unpacked both archives.
- rm -f ark[1-9]isdone
- else
- echo You still need to unpack the following archives:
- echo " " ${MISSING}
- fi
- ## End of shell archive.
- exit 0
-