Skip to content

Commit

Permalink
databaseMove
Browse files Browse the repository at this point in the history
  • Loading branch information
plastovicka committed Dec 21, 2015
1 parent c3d0fa1 commit da83359
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
2 changes: 2 additions & 0 deletions AICarbon.cpp
Expand Up @@ -104,6 +104,8 @@ void AICarbon::yourTurn(int &x, int &y, int depth, int time)
return;
}

if(databaseMove(x, y)) return;

if(time > 0) info_timeout_turn = time * 1000;

if(depth > 0)
Expand Down
1 change: 1 addition & 0 deletions AICarbon.h
Expand Up @@ -86,6 +86,7 @@ class AICarbon : public OXPlayer
int getRank (char cfg);
int getPrior (UCHAR a, UCHAR b);
UCHAR getStatus4(UCHAR s0, UCHAR s1, UCHAR s2, UCHAR s3);
bool databaseMove(int &x0, int &y0);


// ------------------------------------------------------------------------
Expand Down
139 changes: 139 additions & 0 deletions AICarbonMove.cpp
@@ -1,6 +1,7 @@
#include "AICarbon.h"
#include <assert.h>
#include <stdio.h>
#include "Random.h"
// ----------------------------------------------------------------------------
// Podstawowe funkcje
// ----------------------------------------------------------------------------
Expand All @@ -14,6 +15,8 @@ void AICarbon::start(int width, int height)
int x, y, xx, yy, k;
UCHAR p;

_randomize();

boardWidth = width;
boardHeight = height;
// wypelnianie planszy wartosciami poczatkowymi
Expand Down Expand Up @@ -266,3 +269,139 @@ bool AICarbon::check()
if (n[i][j] != nSt[i][j]) return false;
return true;
}
// ----------------------------------------------------------------------------
static signed char data[] = {
15, 1, 4, 0, 0, 0, 1, 1, 2, 3, 2, 2, 4, 2, 5, 3, 3, 4, 3, 3, 5, 5, 4, 3, 3, 6, 4, 5, 6, 5, 5, 4, 3, 1,
11, 1, 1, 1, 0, 0, 2, 2, 3, 2, 3, 3, 1, 3, 2, 4, 1, 5, 3, 3, 3, 2, 5, 1, 4, 3,
11, 1, 1, 0, 0, 0, 1, 1, 3, 2, 2, 2, 2, 3, 3, 3, 2, 4, 2, 5, 1, 5, 1, 4, 0, 3,
9, 1, 0, 0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, 1, -1, 0, 2, 0, -2, 2, 2, 2, -2,
3, 3, 0, 0, 3, 2, 2, 1, 1, 0, 1, 1, 0, 1,
2, 3, 0, 0, 1, 1, 0, 2, 2, 0, 1, 2,
2, 8, 0, 0, 1, 0, -1, -1, 0, -1, 1, -1, 2, -1, -1, 1, 0, 1, 1, 1, 2, 1,
2, 2, 0, 0, 2, 2, 1, 3, 3, 1,
1, 8, 0, 0, -1, 0, 1, 0, 0, 1, 0, -1, 1, 1, -1, 1, 1, -1, -1, -1,
0, 0
};

bool AICarbon::databaseMove(int &x0, int &y0)
{
signed char *s, *sn;
int i, x, y, x1, y1, flip, len1, len2, left, top, right, bottom;

//board rectangle
left = upperLeftCand.x + 2;
top = upperLeftCand.y + 2;
right = lowerRightCand.x - 2;
bottom = lowerRightCand.y - 2;
//find current board in the database
for(s = data;; s = sn){
len1 = *s++;
len2 = *s++;
sn = s + 2 * (len1 + len2);
if(len1 != moveCount){
if(len1 < moveCount) return false; //data must be sorted by moveCount descending
continue;
}
//try all symmetries
for(flip = 0; flip < 8; flip++){
for(i = 0;; i++){
x1 = s[2 * i];
y1 = s[2 * i + 1];
if(i == len1){
s += 2 * (len1 + _random(len2));
x1 = *s++;
y1 = *s;
}
switch(flip){
case 0: x = left + x1; y = top + y1; break;
case 1: x = right - x1; y = top + y1; break;
case 2: x = left + x1; y = bottom - y1; break;
case 3: x = right - x1; y = bottom - y1; break;
case 4: x = left + y1; y = top + x1; break;
case 5: x = right - y1; y = top + x1; break;
case 6: x = left + y1; y = bottom - x1; break;
default: x = right - y1; y = bottom - x1; break;
}
if(x - 4 < 0 || x - 4 >= boardWidth || y - 4 < 0 || y - 4 >= boardHeight) break;
if(i == len1){
x0 = x - 4;
y0 = y - 4;
return true;
}
//compare current board and database
if(cell[x][y].piece != ((i & 1) ? XP : OP)) break;
}
}
}
}
1 change: 1 addition & 0 deletions CarbonAI.vcxproj
Expand Up @@ -156,6 +156,7 @@
<ClCompile Include="AICarbon.cpp" />
<ClCompile Include="AICarbonMove.cpp" />
<ClCompile Include="AIwrapper.cpp" />
<ClCompile Include="Random.cpp" />
<ClInclude Include="CONFIG.CPP">
<FileType>CppCode</FileType>
</ClInclude>
Expand Down
3 changes: 3 additions & 0 deletions CarbonAI.vcxproj.filters
Expand Up @@ -30,6 +30,9 @@
<ClCompile Include="pisqpipe.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Random.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="AICarbon.h">
Expand Down

0 comments on commit da83359

Please sign in to comment.