SVN_SILENT made messages (.desktop file)
[kdegames.git] / bovo / ai / aiboard.h
blob5d7265c3f8f44f98f4397deeebb4b7841ff37cde
1 /*******************************************************************
3 * This file is part of the KDE project "Bovo"
5 * Bovo is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
10 * Bovo is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Bovo; see the file COPYING. If not, write to
17 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
20 ********************************************************************/
22 /**
23 * @file file declaring the class AiBoard, which in fact is the AI
26 #ifndef __AIBOARD_H__
27 #define __AIBOARD_H__
29 #include <kgamedifficulty.h>
31 #include "common.h"
33 using namespace bovo;
35 /** namespace for game engine */
36 namespace bovo {
37 class Coord;
38 class Dimension;
39 class Move;
40 } /* namespace bovo */
42 /** namespace for AI stuff */
43 namespace ai {
45 class AiSquare;
47 /**
48 * An AI player
50 * This class might be somewhat missnamed. It doesn't just keep track of a
51 * playing board on the behalf og the AI. It is the entire AI implementation.
52 * it implements two algorithms to calculate best moves and it selects which
53 * move to play. It also can tell if a move is a winning move, but it doesn't
54 * track a history yet (but maybe it should?).
56 * It uses the standard stuff in common.h way to little.
58 * It is supposed to be slaughtered and split up in two, easing the
59 * implementation of AdaptingAi (the great feature to come! =P )
61 * Perhaps we want to use Qt4 rather than STL in most of these cases. When we
62 * implement AdaptingAi we have to depend on Qt4 and/or ThreadWeaver anyways.
64 * @code
65 * Dimension dimension(width, height);
66 * AiBoard ai(dimension, Easy);
67 * Coord move = getMoveFromPlayerEitherByNetworkOrGui();
68 * Coord aiMove = ai.move(move);
69 * doSomethingWithAiMoveLikeDisplayingItInTheGui(aiMove);
70 * @endcode
72 class AiBoard {
73 public:
74 /**
75 * @brief Constructs an AiBoard with width, height and Skill
76 * @description Constructs a Board object with a specified width, height and
77 * skill
78 * @param width the width
79 * @param height the height
80 * @param skill the skill (difficulty level) the AI player will be playing with
82 AiBoard(const usi width, const usi height,
83 KGameDifficulty::standardLevel skill = KGameDifficulty::Medium,
84 Player player = O);
86 /**
87 * @brief Constructs an AiBoard with width, height and Skill
88 * @description Constructs a Board object with a specified width and height
89 * specified by a Dimension and a skill
90 * @param width the width
91 * @param height the height
92 * @param skill the skill (difficulty level) the AI player will be playing with
94 explicit AiBoard(const Dimension& dimension,
95 KGameDifficulty::standardLevel skill = KGameDifficulty::Medium,
96 Player player=O);
98 /**
99 * @brief destructs this AiBoard
100 * @description destructs this AiBoard object
102 ~AiBoard();
105 * @brief is a Coord empty or set?
106 * @description tells whether a given Coord is marked as empty or
107 * marked by a player
108 * @throw outOfBounds when coord is not on playing board
109 * @param coord Coord to check
110 * @return @c true if coord is empty, @c false otherwise
112 bool empty(const Coord&) const throw(outOfBounds);
115 * @brief is a Coord empty or set?
116 * @description tells whether a given Coord is marked as empty or
117 * marked by a player
118 * @throw outOfBounds when coord is not on playing board
119 * @param x X-part of coordinate to check
120 * @param y X-part of coordinate to check
121 * @return @c true if coord is empty, @c false otherwise
123 bool empty(const usi x, const usi y) const throw(outOfBounds);
126 * @brief height of AiBoard
127 * @description tells the number of rows in the playing board
128 * @return the number of rows
130 usi height() const;
133 * @brief get move from AI
134 * @description get the move the AI wants to play
135 * @return the move the AI wants to play
137 Coord move();
140 * @brief get move from AI
141 * @description Feed the latest move from the player to the AI and get a
142 * list of suggested AI moves in return. This should be a QList\<Coord>.
143 * @param coord the move the player played his latest turn
144 * @return the moves the AI suggest to play
145 * @todo Implement!
147 Coord* moves();
150 * @brief the player occupying a Coord
151 * @description tells which players occupies a certain square in the board
152 * @param coord the square to check
153 * @return @c X if player 1, @c O if player 2, @c No if empty
154 * @throw outOfBounds if coord isn't on the playing board
156 Player player(const Coord&) const throw(outOfBounds);
159 * @brief the player occupying a Coord
160 * @description tells which players occupies a certain square in the board
161 * @param x the X-part of the Coord to check
162 * @param y the Y-part of the Coord to check
163 * @return @c X if player 1, @c O if player 2, @c No if empty
164 * @throw outOfBounds if coord isn't on the playing board
166 Player player(const usi x, const usi y) const throw(outOfBounds);
169 * @brief set the player of a Coord
170 * @description sets which players should occupy a certain square in the
171 * playing board. Returns whether the game ends with this move (i.e. it
172 * was the winning move).
173 * @param move the Move to place
174 * @return @c true if this move resulted in a Game Over,
175 * @c false otherwise
176 * @throw busy if coord was already occupied
177 * @throw gameOver if game was already over
178 * @throw notValidPlayer if player wasn't X or O
180 bool setPlayer(const Move& move) throw(busy, gameover, notValidPlayer);
183 * @brief change Skill
184 * @description changes the Skill (difficulty level) of the AI player
186 void setSkill(KGameDifficulty::standardLevel skill);
189 * @brief width of Board
190 * @description tells the number of columns in the playing board
191 * @return the number of columns
193 usi width() const;
195 private:
196 /* Playing board property. */
197 AiSquare** m_board;
199 /* hasn't game really started? */
200 bool m_cleanBoard;
202 /* Dimension property of playing board. */
203 Dimension *m_dimension;
205 /* is Game Over? property */
206 bool m_gameover;
208 /* AI player property */
209 Player m_player;
211 /* AI Level property. */
212 KGameDifficulty::standardLevel m_skill;
214 /* Return the best-fitting coordinate according to the specs.
215 * Use this when all the board has been given points. */
216 Coord evaluate() const;
218 /* returns, adds och sets points on a given square. */
219 uli points(const Coord&) const throw(outOfBounds);
220 void addPoints(const Coord&, const uli points) throw(outOfBounds);
221 void setPoints(const Coord&, const uli points) throw(outOfBounds);
223 /* initialize this class */
224 void setup();
226 /* Do the real calculation of points for a given square.
227 * Player is the AI player you're optimizing. */
228 uli value(const Coord&, const usi player) const;
230 /* value2 performs a second evaluation in order to make out the tiny
231 * differences that haven't been spotted yet.
232 * Only run this when needed. */
233 uli value2(const Coord&) const;
235 /* is this move (coord) a winning move? */
236 bool win(const Coord&) const;
238 /* Marks all neighbour (5 squares in each
239 * direction) as in need of a recalculation
240 * of its points. */
241 void zero(const Coord&);
244 } /* namespace ai */
246 #endif /* __AIBOARD_H__ */