summaryrefslogtreecommitdiffstats
path: root/kjumpingcube/brain.h
blob: 8f2707052ec43ae561fc848f3eb15ebd8f7ba64f (plain)
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
/* ****************************************************************************
  This file is part of the game 'KJumpingCube'

  Copyright (C) 1998-2000 by Matthias Kiefer
                            <matthias.kiefer@gmx.de>

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

  This program 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

**************************************************************************** */
#ifndef BRAIN_H
#define BRAIN_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <krandomsequence.h>

#include "cubebox.h"

/** @internal */
struct coordinate
{
   int row;
   int column;
   
   int val;
};


/**
* Class Brain computes a (good) possibility to move
* for a given playingfield.
* 
* It puts a value on every cube by looking at its neighbours
* and searches the best cubes to move. It then simulates what would 
* happen, if you would click on these cubes. This is done recursively 
* to a certain depth and the playingfield will be valued. 
* 
* @short The games brain
*/
class Brain
{
public:
   /** 
   * @param initValue value to initialize the random number generator with
   *        if no value is given a truly random value is used
   */
   Brain(int initValue=0);

   /**
   * Computes a good possible move at the given field.
   * The index of this Cube is stored in given 'row' and 'column' 
   * 
   * @return false if computing was stopped
   * @see Brain#stop;  
   */
   bool getHint(int& row, int& column, CubeBox::Player player, CubeBox field);
   
   /** stops thinking */
   void stop();
   /** @return true if the Brain is thinking at the moment */
   bool isActive() const;

   /** skill according to Prefs::EnumSkill **/
   void setSkill(int);
   int skill() const;
   
private:
   /**
   * checks if a move is possible at cube row,column from player 'player' and
   * simulates this move. Then it checks the new playingfield for possible moves
   * and calls itself for every possible move until the maximum depth 'maxLevel'
   * is reached.
   *
   * If the maximum depth is reached, it puts a value on the playingfield and returns this.
   * @see CubeBox#simulateMove
   * @see CubeBox#assessField
   * @see Brain#findCubes2Move
   *
   * @param row,column coordinates of cube to increase
   * @param player for which player the cube has to be increased
   * @param box playingfield to do the moves on
   * @return the value put on the field
   */
    double doMove(int row,int column,CubeBox::Player player, CubeBox box);
   /**
   * Checks the given playingfield, which cubes are favourable to do a move
   * by checking every cubes neighbours. And looking for the difference to overflow.
   *
   * @param c2m Array in which the coordinates of the best cubes to move will be stored
   * @param player for which player to check
   * @param box playingfield to check
   * @param debug if debugmessages should be printed
   * @return number of found cubes to move
   */
   int findCubes2Move(coordinate* c2m,CubeBox::Player player,CubeBox& box);
   /**
   *
   */
   int assessCube(int row,int column,CubeBox::Player,CubeBox& box) const;
   int getDiff(int row,int column, CubeBox::Player player, CubeBox& box) const;

   /** current depth of recursive simulating of the moves */
   int currentLevel;
   /** maximum depth of recursive thinking */
   int maxLevel;
   /** the player for which to check the moves */
   CubeBox::Player currentPlayer;
	
	
   /** flag, if the engine has to be stopped */
   bool stopped;
   /** flag, if the engine is active */
   bool active;
   /** skill of the Brain, see Prefs::EnumSkill */
   int _skill;

   /** Sequence generator */
   KRandomSequence random;    
};

#endif //BRAIN_H