Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Napoleon - main.cpp

main.cpp

Caricato da: Crybot
Scarica il programma completo

  1. #include <iostream>
  2. #include "utils.h"
  3. #include "constants.h"
  4. #include "stopwatch.h"
  5. #include "piece.h"
  6. #include "compassrose.h"
  7. #include "board.h"
  8. #include "pawn.h"
  9. #include "knight.h"
  10. #include "king.h"
  11. #include "rook.h"
  12. #include "bishop.h"
  13. #include "queen.h"
  14. #include "movedatabase.h"
  15. #include "fenstring.h"
  16. #include "movegenerator.h"
  17. #include <boost/algorithm/string.hpp>
  18. #include <boost/lexical_cast.hpp>
  19. #include <cstdio>
  20. #include <ctime>
  21. #include <vector>
  22. #include "benchmark.h"
  23. #include "search.h"
  24. #include "evaluation.h"
  25. #include "console.h"
  26. #include "uci.h"
  27.  
  28. using namespace Napoleon;
  29. using namespace std;
  30.  
  31. void Divide(int depth, Board& board, Benchmark bench)
  32. {
  33.     int pos = 0;
  34.     unsigned long long total = 0;
  35.     int temp = 0;
  36.     int NumMoves = 0;
  37.  
  38.     Move moves[Constants::MaxMoves];
  39.     MoveGenerator::GetLegalMoves(moves, pos, board);
  40.  
  41.     std::cout << "Move\tNodes" << std::endl;
  42.  
  43.     for (int i = 0; i < pos; i++)
  44.     {
  45.         board.MakeMove(moves[i]);
  46.         temp = bench.Perft(depth - 1, board);
  47.         total += temp;
  48.         std::cout << moves[i].ToAlgebraic() << "\t" << temp << std::endl;
  49.         board.UndoMove(moves[i]);
  50.         NumMoves++;
  51.     }
  52.  
  53.     std::cout << "Total Nodes: " << total << std::endl;
  54.     std::cout << "Moves: " << NumMoves << std::endl;
  55. }
  56.  
  57. void SearchMove(int depth, Board& board)
  58. {
  59.     Move move;
  60.     Search::searchRoot(depth, -Constants::Infinity, Constants::Infinity, move, board);
  61.  
  62.     board.MakeMove(move);
  63.     board.Display();
  64.     cout << "I.A. Move: " << move.ToAlgebraic() << endl;
  65. }
  66.  
  67. int main()
  68. {
  69.     //#define MAIN
  70.  
  71. #ifndef MAIN
  72.  
  73.     Uci::Start();
  74.  
  75. #endif
  76.  
  77. #ifdef MAIN
  78.     using namespace Constants;
  79.     using namespace Constants::Squares;
  80.  
  81.     MoveDatabase::InitAttacks();
  82.     Zobrist::Init();
  83.  
  84.     StopWatch watch;
  85.     Board board;
  86.     Benchmark bench;
  87.     board.LoadGame();
  88.  
  89.     //    board.Display();
  90.  
  91.     //    Utils::BitBoard::Display(board.bitBoardSet[board.SideToMove][PieceType::Pawn]);
  92.     //    Utils::BitBoard::Display(board.bitBoardSet[board.SideToMove ^ 1][PieceType::Pawn]);
  93.  
  94.     //    cout << "Size HashEntry: " << sizeof(HashEntry) << endl;
  95.  
  96.     //    board.Display();
  97.     //    cin.get();
  98.  
  99.     //    Move move(Constants::Squares::IntE4, Constants::Squares::IntD5, Constants::CaptureMask);
  100.  
  101.     //    assert(!move.IsEnPassant());
  102.  
  103.     //    Utils::BitBoard::Display(board.bitBoardSet[Color::White][PieceType::Pawn]);
  104.     //    cin.get();
  105.     //    Utils::BitBoard::Display(board.bitBoardSet[Color::Black][PieceType::Pawn]);
  106.     //    cin.get();
  107.  
  108.     //    board.MakeMove(move);
  109.     //    board.Display();
  110.     //    cin.get();
  111.  
  112.     //    Utils::BitBoard::Display(board.bitBoardSet[Color::White][PieceType::Pawn]);
  113.     //    cin.get();
  114.     //    Utils::BitBoard::Display(board.bitBoardSet[Color::Black][PieceType::Pawn]);
  115.     //    cin.get();
  116.  
  117.     //    board.UndoMove(move);
  118.     //    board.Display();
  119.     //    cin.get();
  120.  
  121.     //    Utils::BitBoard::Display(board.bitBoardSet[Color::White][PieceType::Pawn]);
  122.     //    cin.get();
  123.     //    Utils::BitBoard::Display(board.bitBoardSet[Color::Black][PieceType::Pawn]);
  124.     //    cin.get();
  125.  
  126.  
  127.     //    cout << move.ToAlgebraic() << endl;
  128.  
  129.     //    if (move.IsCapture())
  130.     //        cout << "Cattura" << endl;
  131.  
  132.     //    cin.get();
  133.  
  134.     while(1)
  135.     {
  136.         cout << "Napoleon: ";
  137.         vector<string> fields;
  138.         string cmd;
  139.         std::getline(cin, cmd);
  140.  
  141.         boost::split(fields, cmd, boost::is_any_of(" "));
  142.  
  143.         if (fields[0] == "perft")
  144.         {
  145.             if (fields.size() > 1)
  146.             {
  147.                 int depth = boost::lexical_cast<int>(fields[1]);
  148.                 watch.Start();
  149.                 cout << "Perft(" << depth << "): ";
  150.                 cout << "Total Nodes: " << bench.Perft(depth, board) << endl;
  151.                 cout << "Time (ms): " << watch.Stop().ElapsedMilliseconds() << endl;
  152.             }
  153.         }
  154.  
  155.         else if (fields[0] == "divide")
  156.         {
  157.             if (fields.size() > 1)
  158.             {
  159.                 watch.Start();
  160.                 Divide(boost::lexical_cast<int>(fields[1]), board, bench);
  161.                 cout << "Time (ms): " << watch.Stop().ElapsedMilliseconds() << endl;
  162.             }
  163.         }
  164.  
  165.         else if (fields[0] == "search")
  166.         {
  167.             if (fields.size() > 1)
  168.             {
  169.                 int depth = boost::lexical_cast<int>(fields[1]);
  170.                 watch.Start();
  171.                 SearchMove(depth, board);
  172.                 cout << "Time (ms): " << watch.Stop().ElapsedMilliseconds() << endl;
  173.                 //                cout << "PV: " << board.Table.Table[board.zobrist % board.Table.Size].BestMove.ToAlgebraic() << endl;
  174.             }
  175.         }
  176.  
  177.         else if (fields[0] == "setboard")
  178.         {
  179.             if (fields.size() >= 5)
  180.             {
  181.                 board.LoadGame(cmd.substr(fields[0].size()+1));
  182.             }
  183.         }
  184.  
  185.         else if (cmd == "display")
  186.         {
  187.             board.Display();
  188.         }
  189.  
  190.         else if (cmd == "new")
  191.         {
  192.             board = Board();
  193.             board.LoadGame();
  194.         }
  195.  
  196.         else if (fields[0] == "move")
  197.         {
  198.             if (fields.size() == 2)
  199.             {
  200.                 Move move = board.ParseMove(fields[1]);
  201.                 if (move != Constants::NullMove)
  202.                 {
  203.  
  204.                     board.MakeMove(move);
  205.                     board.Display();
  206.                     //                    Move move = Search::iterativeSearch(board);
  207.                     //                    board.MakeMove(move);
  208.                     //                    board.Display();
  209.                 }
  210.                 else
  211.                 {
  212.                     cout << "Invalid Move" << endl;
  213.                 }
  214.             }
  215.         }
  216.  
  217.         //        else if (fields[0] == "undo")
  218.         //        {
  219.         //            board.UndoMove(board.moves[board.CurrentPly - 1]);
  220.         //            board.Display();
  221.         //        }
  222.  
  223.         else if (fields[0] == "bench")
  224.         {
  225.             bench.Start();
  226.         }
  227.         else if (fields[0] == "bench2")
  228.         {
  229.             bench.CutoffTest();
  230.         }
  231.  
  232.         else if (fields[0] == "play")
  233.         {
  234.             int pos;
  235.             watch.Start();
  236.             do
  237.             {
  238.                 Move legalMoves[Constants::MaxMoves + 2];
  239.                 pos = 0;
  240.                 MoveGenerator::GetLegalMoves(legalMoves, pos, board);
  241.                 if (pos > 0)
  242.                 {
  243.                     Search::iterativeSearch(board);
  244.                 }
  245.  
  246.             }while( pos > 0);
  247.  
  248.             board.Display();
  249.             cout << Console::Red << "#Mate for " << (board.SideToMove == Color::White ? "White#" : "Black#");
  250.             cout << endl << "Time (ms): " << watch.Stop().ElapsedMilliseconds() << endl;
  251.  
  252.         }
  253.  
  254.         else if (fields[0] == "iterate")
  255.         {
  256.             watch.Start();
  257.             Search::StartThinking(board);
  258.             cout << "Time (ms): " << watch.Stop().ElapsedMilliseconds() << endl;
  259.             //            cout << "PV: " << board.Table.Table[board.zobrist % board.Table.Size].BestMove.ToAlgebraic() << endl;
  260.         }
  261.  
  262.         else if (fields[0] == "zobrist")
  263.         {
  264.             cout << "Zobrist: " << board.zobrist << endl;
  265.         }
  266.  
  267.         else if (fields[0] == "fen")
  268.         {
  269.             cout << "FEN: " << board.GetFen() << endl;
  270.         }
  271.  
  272.         else
  273.         {
  274.             cout << "incorrect command: " << cmd << endl;
  275.         }
  276.  
  277.         cout << Console::Reset << endl;
  278.     }
  279. #endif
  280.  
  281.     return 0;
  282. }