home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / go / comp / iku.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-05  |  11.7 KB  |  375 lines

  1. // copying
  2.     // Copyright (C) 1991 David Stoutamire (daves@alpha.ces.cwru.edu)
  3.     //
  4.     // This program is free software; you can redistribute it and/or modify
  5.     // it under the terms of the GNU General Public License as published by
  6.     // the Free Software Foundation, version 2.
  7.     // 
  8.     // This program is distributed in the hope that it will be useful,
  9.     // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.     // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.     // GNU General Public License for more details.
  12.     // 
  13.     // You should have received a copy of the GNU General Public License
  14.     // along with this program (file "COPYING"); if not, write to the Free
  15.     // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  
  17. // includes
  18.     #include <stream.h>
  19.     #include <ctype.h>
  20.     #include <std.h>
  21.     #include <String.h>
  22.     #include <File.h>
  23.     #include <sys/time.h>
  24.     #include <sys/resource.h>    
  25.     #ifndef IRIX
  26.         #include <CursesW.h>
  27.     #else /* IRIX */
  28.     #ifdef IRIX
  29.         #define USG 1
  30.     #endif
  31.     #endif /* IRIX */
  32.         #include <curses.h>
  33.     #ifdef IRIX
  34.         #include <CursesW.h>
  35.         #undef USG
  36.     #endif /* IRIX */
  37.  
  38. // enums and consts
  39.     enum bool          { false, true };
  40.     enum boardenum     { black, white, empty };
  41.     enum stoneenum     { blackstone, whitestone };
  42.     enum moveenum      { play, pass, resign, invalid };
  43.     enum territoryenum { seki, dame, alivewhite, aliveblack,
  44.               deadwhite, deadblack,
  45.               territorywhite, territoryblack };
  46.     enum resultenum    { blacklost, whitelost, blackresigned, whiteresigned,
  47.               unfinished, tied, unknown };
  48.     enum playerenum    { randomopponent, cursesopponent, mapopponent };
  49.     enum patenum       { patnxn, patdiamond, patgroup };
  50.  
  51.     const float reallylowfloat = -999999.0;
  52.  
  53. // class protos
  54.     class Pos;
  55.     class Board;
  56.     class Game;
  57.     class Move;
  58.     class Opponent;
  59.     class PatternusingOpponent;
  60.     class Tactician;
  61.     class HashOpponent;
  62.     class SDCOpponent;
  63.     class RandomOpponent;
  64.     class CursesOpponent;
  65.     class MapOpponent;
  66.     class GreedyOpponent;
  67.     class MetaOpponent;
  68.     class String;
  69.     class MLCG;
  70.     class Pattern;
  71.     class PatRep;
  72.     class Patnxn;
  73.     class Patdiamond;
  74.     class Patgroup;
  75.  
  76. // typedefs, defines, globals, misc.
  77.     typedef void (*movefuncptr)(Move);    // typedefs for functions with function args
  78.     typedef void (*Stringfuncptr)(String&);
  79.  
  80.     #ifdef MAINFILE
  81.         #define INIT(x) =x
  82.         #define GLOBAL
  83.     #else
  84.         #define INIT(x)
  85.         #define GLOBAL extern
  86.     #endif
  87.     
  88.     GLOBAL bool errortest    INIT(true);   // Do error checking?
  89.     GLOBAL bool shouldabort  INIT(false);  // Blow up on a fatal?
  90.     GLOBAL bool allowsuicide INIT(true);   // Allow suicide moves as legal?
  91.     GLOBAL bool considerresignation INIT(false);  // Even consider resigning?
  92.     GLOBAL bool dontprint    INIT(false);  // print out score when learning?
  93.     GLOBAL bool dolearn         INIT(true);   // learn during study?
  94.  
  95.     GLOBAL String evalfile   INIT("nofile");
  96.     GLOBAL String dribble    INIT("nofile");
  97.     GLOBAL String postscriptdir INIT("nodir");
  98.  
  99.     GLOBAL int global_argc;           // allows constructors to read arg list
  100.     GLOBAL char **global_argv;           // (set in main)
  101.  
  102.     // This may have to change at your site.
  103.     // This is set up to work with the supplied games if run in the
  104.     // compiling directory.
  105.     GLOBAL String allgames     INIT("games/*/*");
  106.  
  107.     #define chkpt(x)      cout << form("chkpt at %c.\n",x)
  108.     #define loop          for(;;)
  109.     #define doboard(i,j)  for(i=0;i<19;i++)for(j=0;j<19;j++)
  110.     #define onboard(i,j)  ((i<19)&&(j<19)&&(i>=0)&&(j>=0))    
  111.     #define PatternHASH(x) (x.hash())
  112.     #define foreach(m,s)  for(m=s.first();m!=0;s.next(m))
  113.  
  114. // function prototypes
  115.     volatile void fatal(String errmsg);             
  116.     Opponent *makeopponent();    
  117.     const int syma(int a,int b,int i);
  118.     const int symb(int a,int b,int i);
  119.     void Patgroup_groupify();
  120.     String Patgroup_gex(int iter);
  121.     String nextarg();
  122.     void playgame();
  123.     void study();
  124.     void getnum(int& what);
  125.     void takescore(int i, float f, int compress=1);
  126.  
  127. class Pos {
  128.     public:
  129.         int row, col;        // 0--18, 0--18
  130.         Pos();            // constructor for invalid position
  131.         Pos(String s);        // construct from Korshelt notation
  132.         Pos(int r, int c);    // construct from numbers (row, col)
  133.         bool valid();           // is it a valid position?
  134.         bool operator== (Pos vs);
  135.         bool operator!= (Pos vs);
  136.         Pos up();            // these may return 'invalid'.
  137.         Pos down();
  138.         Pos left();
  139.         Pos right();
  140.         String str();        // move in Korschelt notation
  141.     };
  142. #include "Pos.SLSet.h"
  143.  
  144. class Move {
  145.     public:
  146.         moveenum kind;
  147.         Pos where;                      // only matters for type 'play'
  148.         Move();
  149.         // Move(Move& m);
  150.         Move(moveenum k, Pos at=Pos());
  151.         Move(String s);                // contruct from a string
  152.         bool valid();            // is it not 'invalid'?
  153.         // void operator= (Move m);
  154.         bool operator== (Move vs);
  155.         const String str();                    // make a string out of move
  156.     };
  157.  
  158. class Block {
  159.     // an adjacent bunch of stones of the same color
  160.     public:
  161.         PosSLSet stones;
  162.         PosSLSet liberties;
  163.         stoneenum color;
  164.         Block();
  165.         Block(Block& b);
  166.         Block(Board& b, Pos p);
  167.         String str();
  168.     };
  169. #include "Block.DLList.h"
  170.  
  171. class Board {
  172.         void removeblock(Pix bl);        // utility for deleting blocks
  173.         void transfer(Pix from, Pix to); // merge and delete from into to
  174.     public:
  175.         boardenum board[19][19];    // what the board looks like
  176.         Pix blocks[19][19];         // fast access by position
  177.         BlockDLList allBlocks;      // lists of blocks
  178.         int movenum;                // move number (after handicap)
  179.         Pos korestriction;        // if valid, play forbidden here
  180.         bool gameover;            // has the game terminated?
  181.         String comment;             // any old comment
  182.         stoneenum turntoplay;       // whose move it is
  183.         int whitetaken, blacktaken; // captured stones
  184.  
  185.         Board(String hand="");      // make Board for beginning of game
  186.         Board(Board& b);            // copier
  187.         void operator=(Board& b);   // assignment
  188.  
  189.         void applymove(Move m);     // destructively apply a move
  190.         bool islegalmove(Move m);   // is a move legal for this board?
  191.  
  192.         String str();               // string representation of board
  193.         int sizeat(Pos p);          // size of group at p
  194.         int libsat(Pos p);          // # libs of group at p
  195.         };
  196.  
  197. class Game {
  198.     private:
  199.         float komi;
  200.         String handicap;
  201.         resultenum result;        // result of game: who won, etc.
  202.         float finalscore;        // score() returns finalscore - komi
  203.         const int Game_maxmoves=400;
  204.     Board current;                  // cached way board looks
  205.         int currentidx;                 // idx of current;
  206.     public:
  207.         int nummoves;
  208.         Board& snapshot(int idx);       // how board is before move idx
  209.         Move moves[Game_maxmoves];
  210.         String comment;
  211.         Game(String filename);
  212.         float score();                // w/ respect to black, includes komi
  213.         void save(String filename);
  214.         String str();
  215.     };
  216.  
  217. class PatRep {
  218.     public:
  219.         virtual PatRep* duplicate();
  220.         virtual patenum identification();
  221.         virtual void extract(PatternusingOpponent& op,
  222.                                    Board& b, Move m);
  223.         virtual bool operator== (PatRep& vs);
  224.         virtual unsigned int hash();
  225.         virtual String str();
  226.         virtual ~PatRep() {};
  227.     };
  228.  
  229. class Pattern {
  230.     // Pattern is really a handle for PatRep to allow virtual calls
  231.     private:
  232.         PatRep* rep;
  233.     public:
  234.         Pattern(patenum patterntouse=patnxn, String s="");    // provided for loading
  235.         Pattern(Pattern& p);
  236.         ~Pattern();
  237.         void extract(PatternusingOpponent& op,
  238.                             Board& b, Move m);
  239.         bool operator== (Pattern& vs);
  240.         void operator= (Pattern& p);
  241.         unsigned int hash(int hashsize=0);
  242.         String str();            // for saving
  243.     };
  244. #include "Pattern.float.CHMap.h"
  245.  
  246. class Patnxn: public PatRep {
  247.     private:
  248.         String rep;
  249.     public:
  250.         Patnxn(String s="");    
  251.         Patnxn(PatRep& p);
  252.         PatRep* duplicate();
  253.         patenum identification();
  254.         void extract(PatternusingOpponent& op, 
  255.                            Board& b, Move m);
  256.         bool operator== (PatRep& vs);
  257.         unsigned int hash();
  258.         String str();            
  259.     };
  260.  
  261. class Patdiamond: public PatRep {
  262.     private:
  263.         String rep;
  264.     public:
  265.         Patdiamond(String s="");    
  266.         Patdiamond(PatRep& p);
  267.         PatRep* duplicate();
  268.         patenum identification();
  269.         void extract(PatternusingOpponent& op, 
  270.                            Board& b, Move m);
  271.         bool operator== (PatRep& vs);
  272.         unsigned int hash();
  273.         String str();            
  274.     };
  275.  
  276. class Patgroup: public PatRep {
  277.     private:
  278.         String rep;
  279.     public:
  280.         Patgroup(String s="");
  281.         Patgroup(PatRep& p);
  282.         PatRep* duplicate();
  283.         patenum identification();
  284.         void extract(PatternusingOpponent& op, 
  285.                            Board& b, Move m);
  286.         bool operator== (PatRep& vs);
  287.         unsigned int hash();
  288.         String str();
  289.     };
  290.  
  291. class Opponent {
  292.     public:
  293.         String filename;               // disk image - "nofile" if none
  294.         float playevals[19][19];
  295.         bool playable[19][19];
  296.         Move getmove(Board& b);
  297.         virtual float evalpos(Board& b, Pos p);
  298.         virtual void evaluate(Board& b);
  299.         virtual void docheckpt();           // sync disk and memory
  300.         virtual void study(Board& b,
  301.                                 Move m);       // study a particular move
  302.         // the way to handle studying, by default
  303.         virtual void slide(Board& b, Pos p, float deriv);
  304.         float score(Move m);                   // print normalized error
  305.         virtual ~Opponent();
  306.     };
  307.  
  308. class PatternusingOpponent: public Opponent {
  309.     protected:
  310.         Pattern extract(Board& b,Move m);   // extract pattern with my specs
  311.     public:
  312.         patenum patterntouse;            // which type of pattern
  313.         int nxnsize;                // used by Patnxn
  314.         int mindiamondsize, maxdiamondsize, // used by PatDiamond
  315.             diamondmustsee, libscutoff;        // used by PatGroup
  316.         int groupradius;
  317.         PatternusingOpponent();
  318.     };
  319.  
  320. class HashOpponent: public PatternusingOpponent {
  321.     private:
  322.         float *table;
  323.         int hashsize;
  324.     public:
  325.         HashOpponent();
  326.         ~HashOpponent();
  327.     void docheckpt();
  328.         float evalpos(Board& b, Pos p);
  329.         void slide(Board& b, Pos p, float deriv);
  330.     };
  331.  
  332. class MapOpponent: public PatternusingOpponent {
  333.     private:
  334.         PatternfloatCHMap vals;
  335.     public:
  336.         MapOpponent();
  337.         String description();
  338.         float evalpos(Board& b, Pos p);
  339.         void evaluate(Board& b, Move m);
  340.         void slide(Board& b, Pos p, float deriv);
  341.         void docheckpt();
  342.         friend void learn(Board& b, Move m);
  343.     };
  344.  
  345. class MetaOpponent: public Opponent {
  346.     private:
  347.         Opponent **agents;
  348.         int numagents;
  349.     public:
  350.         MetaOpponent();
  351.         ~MetaOpponent();
  352.         void evaluate(Board& b);
  353.         void study(Board& b, Move m);
  354.     };
  355.  
  356. class RandomOpponent: public Opponent {
  357.     public:
  358.         float evalpos(Board& b, Pos p);
  359.     };
  360.  
  361. class GreedyOpponent: public Opponent {
  362.     public:
  363.         float evalpos(Board& b, Pos p);
  364.     };
  365.  
  366. class CursesOpponent: public Opponent {
  367.     private:
  368.         int row=9, col=9;
  369.         CursesWindow *w;
  370.     public:
  371.         CursesOpponent();
  372.         ~CursesOpponent();
  373.         void evaluate(Board& b);
  374.     };
  375.