home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / ARASAN_S.ZIP / TESTSRC.CPP < prev    next >
C/C++ Source or Header  |  1994-07-31  |  4KB  |  203 lines

  1. // Copyright 1994 by Jon Dart.  All Rights Reserved.
  2.  
  3. // Stand-alone DOS executable to test the search engine
  4.  
  5. #include "board.h"
  6. #include "movegen.h"
  7. #include "search.h"
  8. #include "options.h"
  9. #include "movearr.h"
  10. #include "notation.h"
  11. #include "timectrl.h"
  12. extern "C"
  13. {
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <assert.h>
  17. };
  18. #include <fstream.h>
  19. #include <iostream.h>
  20. #include <strstrea.h>
  21. #include <ctype.h>
  22. #include <time.h>
  23.  
  24. Options *global_options = NULL;
  25. Move_Array *game_moves = NULL;
  26. int count = 0;
  27.  
  28. static Move search(Board &board, Search_Limit limit,
  29.    Search_Type type, Search::Statistics &stats)
  30. {
  31.     Search searcher;
  32.     global_options = new Options();
  33.     game_moves = new Move_Array();
  34.  
  35.     Time_Info ti(type,limit);
  36.     Move move = searcher.find_best_move(board, ti,
  37.       stats, Move::NullMove(), False );
  38.  
  39.     ExtendedMove emove(board,move);
  40.     char move_text[20];
  41.     Notation::Image(board, emove, move_text);
  42.     cout << "search:";
  43.     cout << '\t' << move_text << "\t" << stats.elapsed_time <<
  44.      " seconds.\t" << stats.num_moves << " moves.\t" <<
  45.      stats.num_pos << " positions.";
  46.  
  47.     delete game_moves;
  48.     delete global_options;
  49.     return (Move)emove;
  50. }
  51.  
  52. int main(int argc, char **argv)
  53. {
  54.    Board board;
  55.    Search_Limit limit;
  56.    Search_Type type = Fixed_Ply;
  57.    limit.max_ply = 2;
  58.    int arg = 1;
  59.  
  60.    if (argc ==1)
  61.    {
  62.      cerr << "Usage: testsrc [-t seconds] [-p plies] position" << endl;
  63.      return -1;
  64.    }
  65.    else
  66.    {
  67.     if (*(argv[arg]) == '-')
  68.     {
  69.        char c = tolower(*(argv[arg]+1));
  70.        switch (c)
  71.        {
  72.           case 'p':
  73.          type = Fixed_Ply; break;
  74.           case 't':
  75.          type = Time_Limit; break;
  76.           default:
  77.         cerr << "Usage: testsrc [-t seconds] [-p plies] position" << endl;
  78.         return -1;
  79.        }
  80.        ++arg;
  81.        if (argc <= 2)
  82.        {
  83.         cerr << "Usage: testsrc [-t seconds] [-p plies] position" << endl;
  84.         return -1;
  85.        }
  86.        if (type == Fixed_Ply)
  87.           limit.max_ply = atoi(argv[arg]);
  88.        else
  89.        {
  90.           limit.seconds = atol(argv[arg]);
  91.        }
  92.        ++arg;
  93.     }
  94.  
  95.     ifstream pos_file( argv[arg], ios::in);
  96.     char buf[256];
  97.     if (pos_file.good())
  98.     {
  99.        while (!pos_file.eof())
  100.        {
  101.           pos_file.getline(buf,256);
  102.           if (!pos_file)
  103.           {
  104.          cout << "Bad format!" << endl;
  105.          return -1;
  106.           }
  107.           if (strncmp(buf,"svfe",4)==0)
  108.           {
  109.           istrstream s(buf+5);
  110.           s >> board;
  111.           }
  112.           else if (strncmp(buf,"echo",4)==0)
  113.           {
  114.           cout << buf+4 << endl;
  115.           continue;
  116.           }
  117.           else if (strncmp(buf,"noop",4)==0)
  118.           continue;
  119.           else if (strncmp(buf,"srch",4)==0)
  120.           {
  121.           Move moves[10];
  122.           int move_count = 0;
  123.           for (char *p = buf+5;*p;)
  124.           {
  125.               while (isspace(*p) && *p != '\0') ++p;
  126.               if (*p == '\0')
  127.              break;
  128.               char tmp[10];
  129.               int i = 0;
  130.               char *q = tmp;
  131.               while (!isspace(*p) && *p != '+' && *p != '\0' && i < 10)
  132.               {
  133.                *q++ = *p++;
  134.                ++i;
  135.               }
  136.               *q = '\0'; 
  137.               Move m = Notation::Value(board,board.Side(),tmp);
  138.               if (!m.IsNull())
  139.               {
  140.              assert(move_count < 10);
  141.              moves[move_count++] = m;
  142.               }
  143.               if (*p == '+') ++p;
  144.           }
  145.           ++count;
  146.           Search::Statistics stats;
  147.           Move m = search(board,limit,type,stats);
  148.           int correct = 0;
  149.           for (int i = 0; i < move_count; ++i)
  150.           {
  151.               if (moves[i] == m)
  152.               {
  153.              ++correct; break;
  154.               }
  155.           }
  156.           if (correct)
  157.               cout << "\t++ correct" << endl;
  158.           else
  159.               cout << "\t** error" << endl;
  160.           
  161.           for (int j = 0; j < Constants::MaxPly; ++j)
  162.           {
  163.               char buf[30];
  164.               Move m = stats.best_line[j];
  165.               if (m.IsNull())
  166.              break;
  167.               Notation::Image(board,m,buf);
  168.               cout << buf << " ";
  169.               board.MakeMove(ExtendedMove(board,m));
  170.           }
  171.           cout << endl;
  172.           }
  173.           else if (strncmp(buf,"rtrn",4)==0)
  174.           break;
  175.           else
  176.           {
  177.         cout << "Warning: unrecognized command: " << buf << endl;
  178.           }
  179.  
  180.           char c;
  181.           while (!pos_file.eof())
  182.           {
  183.          c = pos_file.get();
  184.          if (!isspace(c) && c != '\n')
  185.          {
  186.             if (!pos_file.eof())
  187.             pos_file.putback(c);
  188.             break;
  189.          }
  190.           }
  191.        }
  192.        pos_file.close();
  193.     }
  194.     else
  195.     {
  196.       cout << "file not found: " << argv[arg] << endl;
  197.       return -1;
  198.     }
  199.     }
  200.  
  201.     return 0;
  202. }
  203.