kingPieceTable.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
kingPieceTable.cc
Go to the documentation of this file.
1
4 #include "osl/eval/endgame/kingPieceTable.h"
5 #include "osl/state/simpleState.h"
6 #include "osl/container/pieceValues.h"
7 #include <cstdio>
8 #include <iostream>
9 #if defined(_WIN32)
10 # include <stdlib.h>
11 #endif
12
13 void osl::eval::endgame::
14 KingPieceTable::saveText(const char *filename) const
15 {
16 FILE *fp = fopen(filename, "w");
17 if (! fp)
18 return;
19 for (int x=1; x<=9; ++x) {
20 for (int y=1; y<=9; ++y) {
21 Square sq(x,y);
22 for (int i=0; i<2; ++i) {
23 for (int x2=0; x2<=9; ++x2) {
24 for (int y2=(x2 == 0) ? 0 : 1; y2<=9; ++y2) {
25 Square sq2(x2,y2);
26 if (x2 == 0 && y2 == 0)
27 sq2 = Square::STAND();
28 for (int j=0; j<PTYPE_SIZE; ++j)
29 fprintf(fp, "%d\n", data[sq.index()*2+i][sq2.index()*PTYPE_SIZE+j]);
30 if (sq2.isPieceStand())
31 break;
32 }
33 }
34 }
35 }
36 }
37 fclose(fp);
38 }
39
40 void osl::eval::endgame::
41 KingPieceTable::loadText(const char *filename)
42 {
43 CArray<int, EffectiveDimension> w;
44 FILE *fp = fopen(filename, "r");
45 if (! fp) {
46 std::cerr << "open failed " << filename << "\n";
47 return;
48 }
49 for (int i=0; i<EffectiveDimension; ++i) {
50 if (fscanf(fp, "%d", &w[i]) != 1) {
51 std::cerr << "read failed " << i << "\n";
52 }
53 }
54 fclose(fp);
55 resetWeights(&w[0]);
56 }
57
58 void osl::eval::endgame::
59 KingPieceTable::resetWeights(const int *w)
60 {
61 #ifndef NDEBUG
62 const int *src = w;
63 #endif
64 for (int x=1; x<=9; ++x) {
65 for (int y=1; y<=9; ++y) {
66 Square sq(x,y);
67 for (int i=0; i<2; ++i) {
68 for (int x2=0; x2<=9; ++x2) {
69 for (int y2=(x2 == 0) ? 0 : 1; y2<=9; ++y2) {
70 Square sq2(x2,y2);
71 if (x2 == 0 && y2 == 0)
72 sq2 = Square::STAND();
73 for (int j=0; j<PTYPE_SIZE; ++j) {
74 assert(effectiveIndexOf(sq, indexToPlayer(i), sq2, (Ptype)j)
75 == w-src);
76 data[sq.index()*2+i][sq2.index()*PTYPE_SIZE+j] = *w++;
77 }
78 if (sq2.isPieceStand())
79 break;
80 }
81 }
82 }
83 }
84 }
85 assert(w == src+dimension());
86 }
87
88 void osl::eval::endgame::
89 KingPieceTable::randomize()
90 {
91 for (int x=1; x<=9; ++x) {
92 for (int y=1; y<=9; ++y) {
93 Square sq(x,y);
94 for (int i=0; i<2; ++i) {
95 for (int x2=0; x2<=9; ++x2) {
96 for (int y2=(x2 == 0) ? 0 : 1; y2<=9; ++y2) {
97 Square sq2(x2,y2);
98 if (x2 == 0 && y2 == 0)
99 sq2 = Square::STAND();
100 for (int j=0; j<PTYPE_SIZE; ++j) {
101 data[sq.index()*2+i][sq2.index()*PTYPE_SIZE+j]
102 = (
103 #ifndef _WIN32
104 random()
105 #else
106 rand()
107 #endif
108 %1024)-512;
109 }
110 if (sq2.isPieceStand())
111 break;
112 }
113 }
114 }
115 }
116 }
117 }
118
119 void osl::eval::endgame::
120 KingPieceTable::clear()
121 {
122 data.fill(0);
123 }
124
125 bool osl::eval::endgame::
126 operator==(const KingPieceTable& l, KingPieceTable& r)
127 {
128 return l.data == r.data;
129 }
130
131 // ;;; Local Variables:
132 // ;;; mode:c++
133 // ;;; c-basic-offset:2
134 // ;;; End: