Go to the documentation of this file.00001
00004 #include "osl/eval/endgame/kingPieceTable.h"
00005 #include "osl/state/simpleState.h"
00006 #include "osl/container/pieceValues.h"
00007 #include <cstdio>
00008 #include <iostream>
00009 #if defined(_WIN32)
00010 # include <stdlib.h>
00011 #endif
00012
00013 void osl::eval::endgame::
00014 KingPieceTable::saveText(const char *filename) const
00015 {
00016 FILE *fp = fopen(filename, "w");
00017 if (! fp)
00018 return;
00019 for (int x=1; x<=9; ++x) {
00020 for (int y=1; y<=9; ++y) {
00021 Square sq(x,y);
00022 for (int i=0; i<2; ++i) {
00023 for (int x2=0; x2<=9; ++x2) {
00024 for (int y2=(x2 == 0) ? 0 : 1; y2<=9; ++y2) {
00025 Square sq2(x2,y2);
00026 if (x2 == 0 && y2 == 0)
00027 sq2 = Square::STAND();
00028 for (int j=0; j<PTYPE_SIZE; ++j)
00029 fprintf(fp, "%d\n", data[sq.index()*2+i][sq2.index()*PTYPE_SIZE+j]);
00030 if (sq2.isPieceStand())
00031 break;
00032 }
00033 }
00034 }
00035 }
00036 }
00037 fclose(fp);
00038 }
00039
00040 void osl::eval::endgame::
00041 KingPieceTable::loadText(const char *filename)
00042 {
00043 CArray<int, EffectiveDimension> w;
00044 FILE *fp = fopen(filename, "r");
00045 if (! fp) {
00046 std::cerr << "open failed " << filename << "\n";
00047 return;
00048 }
00049 for (int i=0; i<EffectiveDimension; ++i) {
00050 if (fscanf(fp, "%d", &w[i]) != 1) {
00051 std::cerr << "read failed " << i << "\n";
00052 }
00053 }
00054 fclose(fp);
00055 resetWeights(&w[0]);
00056 }
00057
00058 void osl::eval::endgame::
00059 KingPieceTable::resetWeights(const int *w)
00060 {
00061 #ifndef NDEBUG
00062 const int *src = w;
00063 #endif
00064 for (int x=1; x<=9; ++x) {
00065 for (int y=1; y<=9; ++y) {
00066 Square sq(x,y);
00067 for (int i=0; i<2; ++i) {
00068 for (int x2=0; x2<=9; ++x2) {
00069 for (int y2=(x2 == 0) ? 0 : 1; y2<=9; ++y2) {
00070 Square sq2(x2,y2);
00071 if (x2 == 0 && y2 == 0)
00072 sq2 = Square::STAND();
00073 for (int j=0; j<PTYPE_SIZE; ++j) {
00074 assert(effectiveIndexOf(sq, indexToPlayer(i), sq2, (Ptype)j)
00075 == w-src);
00076 data[sq.index()*2+i][sq2.index()*PTYPE_SIZE+j] = *w++;
00077 }
00078 if (sq2.isPieceStand())
00079 break;
00080 }
00081 }
00082 }
00083 }
00084 }
00085 assert(w == src+dimension());
00086 }
00087
00088 void osl::eval::endgame::
00089 KingPieceTable::randomize()
00090 {
00091 for (int x=1; x<=9; ++x) {
00092 for (int y=1; y<=9; ++y) {
00093 Square sq(x,y);
00094 for (int i=0; i<2; ++i) {
00095 for (int x2=0; x2<=9; ++x2) {
00096 for (int y2=(x2 == 0) ? 0 : 1; y2<=9; ++y2) {
00097 Square sq2(x2,y2);
00098 if (x2 == 0 && y2 == 0)
00099 sq2 = Square::STAND();
00100 for (int j=0; j<PTYPE_SIZE; ++j) {
00101 data[sq.index()*2+i][sq2.index()*PTYPE_SIZE+j]
00102 = (
00103 #ifndef _WIN32
00104 random()
00105 #else
00106 rand()
00107 #endif
00108 %1024)-512;
00109 }
00110 if (sq2.isPieceStand())
00111 break;
00112 }
00113 }
00114 }
00115 }
00116 }
00117 }
00118
00119 void osl::eval::endgame::
00120 KingPieceTable::clear()
00121 {
00122 data.fill(0);
00123 }
00124
00125 bool osl::eval::endgame::
00126 operator==(const KingPieceTable& l, KingPieceTable& r)
00127 {
00128 return l.data == r.data;
00129 }
00130
00131
00132
00133
00134