File: kwin4proc.h

package info (click to toggle)
kfourinline 4:18.04.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,784 kB
  • sloc: cpp: 7,339; sh: 8; makefile: 5
file content (197 lines) | stat: -rw-r--r-- 5,687 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
/*
   This file is part of the KDE games kwin4 program
   Copyright (c) 1995-2007 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.
*/

#ifndef _KWIN4PROC_H_
#define _KWIN4PROC_H_

#include <cmath>

// Qt includes
#include <QHash>

// Local includes
#include "kgameprocess.h"
#include "kwin4global.h"
#include "aiboard.h"

// Save storage for color arrays
typedef char FARBE;

#define SIZE_Y_ALL 36
#define SIZE_X 6
#define SIZE_Y 5

/** The computer (AI) player. 
  * It is used via the KGameIO framework and serves as abstract input device
  * to the game.
  */
class KComputer : public QObject
{

  Q_OBJECT

  public:
    /** Constructor
      */
    KComputer();

    /** The KGameProcess is the main program and event loop.
      */
    KGameProcess proc;

  public slots:
    /** KGameIO slot to reveice a command from the main program.
      * @param in       The data stream
      * @param msgid    The message id
      * @param received The received (we)
      * @param sender   The sender (the game)
      */
    void slotCommand(QDataStream& in, int msgid,int receiver,int sender);

    /** KGameIO slot to init a KGameIO device.
      * @param in  The data stream
      * @param id  The message id
      */
    void slotInit(QDataStream& in, int id);

    /** KGameIO slot to receive the setTurn() command, that is a turn is
      * started or aboerted.
      * @param in    The data stream
      * @parma turn  True if a turn is started
      */
    void slotTurn(QDataStream& in, bool turn);

  protected:
    /** Move result structure. Combines move position and value.
      */
    typedef struct 
    {
      // Move value
      long value;
      // Move x-position
      int move;
    }MoveResult;

    /** Sends the game value/rating back to the main program.
      * @param value  The rating
      * @param moveNo Which move
      */
    void sendValue(long value, int moveNo );

    /** Generate a random number from 0..max
      * @param max The range [0..max[
      */
    long random(long max);

    /** Start thinking about a new move. Will read the input stream and
      * then start the MinMax AI algorithm.
      * @param in    The data stream
      * @param out   The outgoing data stream
      * @param hint  True if it is a hint move (unused)
      */
    MoveResult think(QDataStream &in,QDataStream &out,bool hint);
    
    /** Check whether the current board is a game over situation.
      * @param field   The board
      * @param numbers The number matrix
      * @return The winner color or Nobody.
      */
    COLOUR isGameOver(FARBE field[][SIZE_X+1],char numbers[]);

    /** Execute move in given color to the given position on the given game board.
      * @param move    The move x coordinate
      * @param color   The move color
      * @param field   The game board
      * @param numbers The number board
      */
    void DoMove(int move, COLOUR color, FARBE field[][SIZE_X+1], char numbers[]);

    /** Execute the AI MinMax code. The best move and its (negative) value are returned.
      * @param color    The current player's color.
      * @param field    The game board
      * @param numbers  The number board
      * @param reklev   The recursion level
      * @param moveNo   The current move number
      * @param first    First recursion
      * @return The move result (best move and its value).
      */
    MoveResult MinMax(COLOUR color,FARBE field[][SIZE_X+1],char numbers[],int reklev,int moveNo, bool first);

    /** Evaluate a position at the end of the recursion.
      * @param curColor The current players' color
      * @param field The game board
      * @return The position value.
      */
    long PositionEvaluation(COLOUR curColor, FARBE field[][SIZE_X+1]);

    /** Load the computer brain from disk.
      */
    void loadBrain();

    /** Save the computer brain to disk.
      */
    void saveBrain();

  private:
    // Move number in game 0..41
    int mCurMoveNo;          
    
    // Maximum recursion of MinMax search (level)
    int mLevel;    

    // Amount of positions evaluated (Statistic)
    int mPosEvaluations; 
    
    // How many pos calculated per [ms] (Statistic)
    float mCalcPosPerMS; 

    // Speed up matrix access
    // rows: 0-5  =6 : horiz(i:0-6) 
    //       6-12 =7 : vert(i:0-5)  
    //      13-24 =12: diag-45(i:...)
    //      25-36 =12: diag45(i:...)
    // Length of the rows (number meaning above)
    char mRowLengths[38];
    // Start of the rows (number meaning above)
    char mStartOfRows[38];


    // Learning of AI:  
    // Store learned boards
    QHash<AIBoard,AIValue> mBrain;

    // Best board in move
    AIBoard mMaxAIBoard;  

    // Learning on or off
    bool mIsLearning;  

    // Is the brain already loaded
    bool mBrainLoaded; 
    
    // Path to the brain file
    QString mBrainDir;  
    
    // Statistics on brain usage
    long mBrainUsed;    
};

#endif // _KWIN4PROC_H_