The Pedigree Project  0.1
Engine.cc
1 /*
2  *
3  * Copyright (c) 2008-2014, Pedigree Developers
4  *
5  * Please see the CONTRIB file in the root of the source tree for a full
6  * list of contributors.
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #include "Engine.h"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include "Minimax.h"
25 #include "Negascout.h"
26 #include "Alphabeta.h"
27 
28 extern long g_nCacheHits, g_nCacheMisses, g_nCacheEntries, g_nCachePartials;
29 extern long g_nHashes, g_nIsLegal, g_nInCheck;
30 extern long g_nIsAttacking, g_nHeuristic;
31 
33 {
34  // Initialise the algorithm.
35 // search = new Minimax(&doSearch);
36  search = new Negascout(&doSearch);
37 // search = new Alphabeta(&doSearch);
38  doSearch = false;
39  minDepth = 0;
40  maxDepth = 0;
41 
42  pthread_t thread;
43  pthread_create(&thread, NULL, &threadTramp, (void*)this);
44 }
45 
47 {
48 }
49 
51 {
52  while (true)
53  {
54  while (!doSearch)
55  ;
56 
57  // Iterative deepening solution.
58  for (int ply = 1; ply <= maxDepth; ply++)
59  {
60  if (doSearch)
61  {
62  MoveList tmpList;
63  heuristic = search->search(side, ply, state, tmpList);
64  moveList = tmpList;
65  printf("[%d] ", ply);
66  printMoveList();
67  g_nCacheHits = 0;
68  g_nCacheMisses = 0;
69  g_nCachePartials = 0;
70  g_nHashes = 0;
71  g_nInCheck = 0;
72  g_nIsLegal = 0;
73  g_nIsAttacking = 0;
74  g_nHeuristic = 0;
75  }
76  }
77 
78  doSearch = false;
79  }
80 }
81 
82 void Engine::ponder(bool b)
83 {
84 }
85 
86 void Engine::startSearch(Side s, int minD, int maxD)
87 {
88  side = s;
89  minDepth = minD;
90  maxDepth = maxD;
91  doSearch = true;
92 }
93 
95 {
96  doSearch = false;
97 }
98 
99 bool Engine::searchComplete() volatile
100 {
101  return !doSearch; // If we're still searching, it's not complete.
102 }
103 
105 {
106  return *moveList.begin();
107 }
108 
110 {
111 }
112 
114 {
115  for (MoveList::iterator it = moveList.begin();
116  it != moveList.end();
117  it ++)
118  {
119  Move m = *it;
120  m.print();
121  printf(" ");
122  }
123  printf("(Heuristic %d - Searched %d nodes with %d cache hits, %d partials and %d misses (%d entries))\n", heuristic, search->nodesSearched, g_nCacheHits, g_nCachePartials, g_nCacheMisses, g_nCacheEntries);
124  printf("{%d hashes %d islegal %d incheck %d isattacking %d heuristic}\n", g_nHashes, g_nIsLegal, g_nInCheck, g_nIsAttacking, g_nHeuristic);
125 }
126 
127 void *Engine::threadTramp(void *ptr)
128 {
129  Engine *engine = (Engine*)ptr;
130  engine->run();
131  return 0;
132 }
volatile int minDepth
Definition: Engine.h:115
bool searchComplete() volatile
Definition: Engine.cc:99
void ponder(bool b)
Definition: Engine.cc:82
BoardState state
Definition: Engine.h:90
Move getMove()
Definition: Engine.cc:104
SearchAlgorithm * search
Definition: Engine.h:100
Definition: Engine.h:29
~Engine()
Definition: Engine.cc:46
void stopSearch()
Definition: Engine.cc:94
volatile Side side
Definition: Engine.h:110
void startSearch(Side s, int minD, int maxD)
Definition: Engine.cc:86
Definition: Move.h:29
volatile bool doSearch
Definition: Engine.h:105
static void * threadTramp(void *ptr)
Definition: Engine.cc:127
MoveList moveList
Definition: Engine.h:120
void move(Move m)
Definition: Engine.cc:109
void printMoveList()
Definition: Engine.cc:113
void run()
Definition: Engine.cc:50
Engine()
Definition: Engine.cc:32