File: aiboard.cpp

package info (click to toggle)
kfourinline 4:18.04.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,784 kB
  • sloc: cpp: 7,339; sh: 8; makefile: 5
file content (235 lines) | stat: -rw-r--r-- 5,174 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
   This file is part of the KDE games kwin4 program
   Copyright (c) 2006 Martin Heni <kde@heni-online.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

// Header includes
#include "aiboard.h"

// Standard includes
#include <cstdio>

// KDE includes
#include "kfourinline_debug.h"

// Construct a board object
AIBoard::AIBoard()
{
}


// Construct a board object from a given game field.
AIBoard::AIBoard(uchar current, const char field[][7])
{
  fromField(current, false, field);
}


// Compare two boards
bool AIBoard::operator==(const AIBoard& other) const
{
  if (mCurrent != other.mCurrent) return false;
  for (int i=0; i<6; i++) 
  {
    if (mBoard[i] != other.mBoard[i]) return false;
  }
  return true;
}


// Create a board from a given game field. Support mirroring the field.
void AIBoard::fromField(uchar current, bool mirror, const char field[][7])
{
  // Store current player
  mCurrent = current;

  // Loop rows
  for (int y=0;y<=5;y++)
  {
    ushort tmp = 0;
    // Loop columns
    for (int x=0; x<=6; x++)
    {
      tmp = tmp << 2;
      // Mirror board?
      if (mirror)
      {
        if (field[y][6-x] == Yellow)   tmp |= 1;
        else if (field[y][6-x] == Red) tmp |= 2;
        // else tmp |= 0; 
      }
      else
      {
        if (field[y][x] == Yellow)   tmp |= 1;
        else if (field[y][x] == Red) tmp |= 2;
        // else tmp |= 0; 
      }
    }
    mBoard[y] = tmp;
  }
}


// Retrieve a mirrored board.
AIBoard AIBoard::mirror() const
{
  AIBoard other;
  other.mCurrent = mCurrent;

  // Loop rows
  for (int y=0;y<=5;y++)
  {
    ushort tmp  = mBoard[y];
    ushort ntmp = 0;
    // Loop columns
    for (int x=0; x<=6; x++)
    {
      ntmp = ntmp << 2;
      ntmp |= (tmp >> (x<<1)) & 3; 
    }
    other.mBoard[y] = ntmp;
  }
  return other;
}


// Debug function to output the board.
void AIBoard::print() const
{
  fprintf(stderr," AIBOARD ======== COLOR %d ========\n", mCurrent);
  for (int i=5;i>=0;i--)
  {
    ushort tmp = mBoard[i];
    fprintf(stderr, "   Row %d: [%04x]", i, tmp);
    for (int x=6; x>=0; x--)
    {
      if (((tmp >> (x*2)) & 3) == 1) fprintf(stderr," 1 ");
      else if (((tmp>>(x*2) & 3)) == 2) fprintf(stderr," 2 ");
      else fprintf(stderr," 0 ");
    }
    fprintf(stderr,"\n");
  }
  fflush(stderr);
}
    

// Stream the board
QDataStream& operator<<(QDataStream& s, const AIBoard& board)
{
    s << (qint8)board.mCurrent;
    for (int i=0;i<6;i++) s << (qint16)board.mBoard[i]; 
    return s;
}


// Stream the board
QDataStream& operator>>(QDataStream& s, AIBoard& board)
{
    qint8 tmp8;
    qint16 tmp16;
    s >> tmp8;
    board.mCurrent = tmp8;
    for (int i=0;i<6;i++) {s >> tmp16; board.mBoard[i] = tmp16;}
    return s;
}


// Create hashkey for a board
uint qHash(const AIBoard& key)
{
    uint h;
    h = (key.mCurrent&1) << 15;

    h += key.mBoard[0];
    h += key.mBoard[1] << 1;
    h += key.mBoard[2];
    h += (~(key.mBoard[3])     )&0x7fff;
    h += (~(key.mBoard[4] << 1))&0x7fff;
    h += (~(key.mBoard[5]))     &0x7fff;
    
    return h;
}


// Create a value structure. Fill it with an unused value as
// marker for 'unused'.
AIValue::AIValue()
{
  for (int i=0; i<NO_OF_LEVELS;i++)
  {
    mValue[i] = 0x40000000;
  }
}


// Store a value for a given level.
void AIValue::setValue(int level, long value)
{
  if (level<0 || level>=NO_OF_LEVELS)
  {
    fprintf(stderr, "AIValue::setValue: Level %d not supported\n", level);
    fflush(stderr);
    return;
  }
  mValue[level] = value;
}


// Retrieve a value for a given level.
long AIValue::value(int level) const
{
  if (level<0 || level>=NO_OF_LEVELS)
  {
    fprintf(stderr, "AIValue::value: Level %d not supported\n", level);
    fflush(stderr);
    return 0;
  }
  return mValue[level];
}


// Check whether a given level has a stored value.
bool AIValue::isSet(int level) const
{
  if (level<0 || level>=NO_OF_LEVELS)
  {
    fprintf(stderr, "AIValue::isSet: Level %d not supported\n", level);
    fflush(stderr);
    return 0;
  }
  if (mValue[level] == 0x40000000) return false;
  return true;
}


// Stream a value
QDataStream& operator<<(QDataStream& s, const AIValue& value)
{
    for (int i=0;i<AIValue::NO_OF_LEVELS;i++) s << (qint32)value.mValue[i];
    return s;
}


// Stream a value
QDataStream& operator>>(QDataStream& s, AIValue& value)
{
    qint32 tmp32;
    for (int i=0;i<AIValue::NO_OF_LEVELS;i++) {s >> tmp32; value.mValue[i] = tmp32;}
    return s;
}