kingPieceTable.cc
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 
00010 void osl::eval::endgame::
00011 KingPieceTable::saveText(const char *filename) const
00012 {
00013   FILE *fp = fopen(filename, "w");
00014   if (! fp)
00015     return;
00016   for (int x=1; x<=9; ++x) {
00017     for (int y=1; y<=9; ++y) {
00018       Square sq(x,y);
00019       for (int i=0; i<2; ++i) {
00020         for (int x2=0; x2<=9; ++x2) {
00021           for (int y2=(x2 == 0) ? 0 : 1; y2<=9; ++y2) {
00022             Square sq2(x2,y2);
00023             if (x2 == 0 && y2 == 0)
00024               sq2 = Square::STAND();
00025             for (int j=0; j<PTYPE_SIZE; ++j)
00026               fprintf(fp, "%d\n", data[sq.index()*2+i][sq2.index()*PTYPE_SIZE+j]);
00027             if (sq2.isPieceStand())
00028               break;
00029           }
00030         }
00031       }
00032     }
00033   }
00034   fclose(fp);
00035 }
00036 
00037 void osl::eval::endgame::
00038 KingPieceTable::loadText(const char *filename)
00039 {
00040   CArray<int, EffectiveDimension> w;
00041   FILE *fp = fopen(filename, "r");
00042   if (! fp) {
00043     std::cerr << "open failed " << filename << "\n";
00044     return;
00045   }
00046   for (int i=0; i<EffectiveDimension; ++i) {
00047     if (fscanf(fp, "%d", &w[i]) != 1) {
00048       std::cerr << "read failed " << i << "\n";
00049     }
00050   }
00051   fclose(fp);
00052   resetWeights(&w[0]);
00053 }
00054 
00055 void osl::eval::endgame::
00056 KingPieceTable::resetWeights(const int *w)
00057 {
00058 #ifndef NDEBUG
00059   const int *src = w;
00060 #endif
00061   for (int x=1; x<=9; ++x) {
00062     for (int y=1; y<=9; ++y) {
00063       Square sq(x,y);
00064       for (int i=0; i<2; ++i) {
00065         for (int x2=0; x2<=9; ++x2) {
00066           for (int y2=(x2 == 0) ? 0 : 1; y2<=9; ++y2) {
00067             Square sq2(x2,y2);
00068             if (x2 == 0 && y2 == 0)
00069               sq2 = Square::STAND();
00070             for (int j=0; j<PTYPE_SIZE; ++j) {
00071               assert(effectiveIndexOf(sq, indexToPlayer(i), sq2, (Ptype)j)
00072                      == w-src);
00073               data[sq.index()*2+i][sq2.index()*PTYPE_SIZE+j] = *w++;
00074             }
00075             if (sq2.isPieceStand())
00076               break;
00077           }
00078         }
00079       }
00080     }
00081   }
00082   assert(w == src+dimension());
00083 }
00084 
00085 void osl::eval::endgame::
00086 KingPieceTable::randomize()
00087 {
00088   for (int x=1; x<=9; ++x) {
00089     for (int y=1; y<=9; ++y) {
00090       Square sq(x,y);
00091       for (int i=0; i<2; ++i) {
00092         for (int x2=0; x2<=9; ++x2) {
00093           for (int y2=(x2 == 0) ? 0 : 1; y2<=9; ++y2) {
00094             Square sq2(x2,y2);
00095             if (x2 == 0 && y2 == 0)
00096               sq2 = Square::STAND();
00097             for (int j=0; j<PTYPE_SIZE; ++j) {
00098               data[sq.index()*2+i][sq2.index()*PTYPE_SIZE+j]
00099                 = (random()%1024)-512;
00100             }
00101             if (sq2.isPieceStand())
00102               break;
00103           }
00104         }
00105       }
00106     }
00107   }
00108 }
00109 
00110 void osl::eval::endgame::
00111 KingPieceTable::clear()
00112 {
00113   data.fill(0);
00114 }
00115 
00116 bool osl::eval::endgame::
00117 operator==(const KingPieceTable& l, KingPieceTable& r)
00118 {
00119   return l.data == r.data;
00120 }
00121 
00122 // ;;; Local Variables:
00123 // ;;; mode:c++
00124 // ;;; c-basic-offset:2
00125 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines