File indexing completed on 2024-11-24 03:43:17

0001 /*******************************************************************
0002  *
0003  * Copyright 2009  Pelladi Gabor <pelladigabor@gmail.com>
0004  *
0005  * Bovo is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation; either version 2, or (at your option)
0008  * any later version.
0009  *
0010  * Bovo is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with Bovo; see the file COPYING.  If not, write to
0017  * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  *
0020  ********************************************************************/
0021 
0022 #ifndef BOVO_NODE_H
0023 #define BOVO_NODE_H
0024 
0025 #include "ai_interface.h"
0026 #include "standing.h"
0027 
0028 #include <list>
0029 
0030 // list of following steps
0031 using steps_T = std::list<Standing *>;
0032 // type of hash value
0033 using hash_T = unsigned long long;
0034 
0035 // hash table entry type
0036 using entry_type_T = index_T;
0037 enum { exact = 1, lower_bound = 2, upper_bound = 3 };
0038 
0039 // pragmas are not in the C++ standard and Sun Studio does not support the pack(x, y) one
0040 #if defined(__SUNPRO_CC) || defined(__SUNPRO_C)
0041 #pragma pack()
0042 #else
0043 #pragma pack(push, 1)
0044 #endif
0045 // a hash table entry
0046 struct NodeHashData {
0047     // is this data for the current position?
0048     hash_T checksum;
0049     // the result we are storing in the hash table
0050     heur_T value;
0051     // how deep we searched when we stored this result
0052     index_T remaining_depth;
0053     // type of the entry
0054     entry_type_T entry_type;
0055 };
0056 #if defined(__SUNPRO_CC) || defined(__SUNPRO_C)
0057 #pragma(pack)
0058 #else
0059 #pragma pack(pop)
0060 #endif
0061 
0062 // hash table maximum memory
0063 const hash_T nodeHashMem = 16 * 1024 * 1024;
0064 // how many entries we can store in the memory limit
0065 const hash_T nodeHashSize = nodeHashMem / sizeof(NodeHashData);
0066 
0067 // a node of the alphabeta tree
0068 class Node
0069 {
0070 public:
0071     // the standing this ndoe refers to
0072     Standing *standing;
0073 
0074     // parent node in the tree
0075     Node *parent;
0076     // child node in the tree
0077     Node *child;
0078     // depth of the current node
0079     index_T depth;
0080 
0081     // 1 for starting player, -1 for the other
0082     heur_T signum;
0083     // alpha value
0084     heur_T alpha;
0085     // beta value
0086     heur_T beta;
0087     // is this alpha/beta value exact, or coming from an upper level?
0088     bool is_exact;
0089 
0090     // true if this node has been fully evaluated
0091     bool evaluated;
0092 
0093     // the good steps that can be done from here
0094     steps_T steps;
0095 
0096     // how deep we can go in the tree
0097     int depth_limit;
0098     // limits the amount of following steps to be investigated
0099     unsigned int max_branch;
0100 
0101     // construct the root node
0102     Node(Standing *_standing, AiImpl *ai);
0103     // construct a child node
0104     Node(Standing *_standing, Node *_parent);
0105 
0106     // destructor
0107     ~Node();
0108 
0109     // generate following steps
0110     void generateSteps();
0111     // calculate hash data
0112     void calcHash(hash_T *hash, NodeHashData *data);
0113 };
0114 
0115 #endif // BOVO_NODE_H