kreversigame.h 7.48 KB
Newer Older
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*******************************************************************
 *
 * Copyright 2006 Dmitry Suzdalev <dimsuz@gmail.com>
 *
 * This file is part of the KDE project "KReversi"
 *
 * KReversi 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, or (at your option)
 * any later version.
 *
 * KReversi 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 KReversi; see the file COPYING.  If not, write to
 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 ********************************************************************/
23 24 25 26
#ifndef KREVERSI_GAME_H
#define KREVERSI_GAME_H

#include <QObject>
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
27
#include <QStack>
28

Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
29 30
#include "commondefs.h"

31
class Engine;
32 33 34 35 36 37

/**
 *  KReversiGame incapsulates all of the game logic.
 *  It creates KReversiBoard and manages a chips on it.
 *  Whenever the board state changes it emits corresponding signals.
 *  The idea is also to abstract from any graphic representation of the game process
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
38 39 40 41 42
 *
 *  KReversiGame is supposed to be driven by someone from outside.
 *  I.e. it receives commands and emits events when it's internal state changes
 *  due to this commands dispatching.
 *  The main commands are:
43
 *  startNextTurn() and  makePlayerMove()
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
44 45
 *
 *  See KReversiScene for example of working with KReversiGame
46 47 48
 */
class KReversiGame : public QObject
{
49
    Q_OBJECT
50
public:
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
51
    KReversiGame();
52
    ~KReversiGame();
53
    /**
54 55 56 57 58 59 60 61 62 63
     *  Starts next player turn.
     *  If game isn't over yet, then this function do the following:
     *  - if it is computer turn and computer can move, it'll make that move.
     *  - if it is computer turn and computer can't move it'll emit "computerCantMove"
     *  signal and exit
     *  - if it is player turn and player can move then this function 
     *  will do nothing - you can call makePlayerMove(row,col) to make player move (but see last item)
     *  - if it is player turn and player can't move it'll make a computer move
     *  - in demo mode this function will make computer play player moves,
     *  so you don't need to call makePlayerMove.
64 65
     *
     *  If game is over it'll emit gameOver()
66 67 68 69
     *  
     *  If it's still unclear how to use it please see KReversiScene for working example.
     *  In short: it calls startNextTurn() at the end of each turn and makePlayerMove() 
     *  in mouseReleaseEvent()
70 71 72
     *
     *  @param demoMode if true then computer will decide for player turn
     */
73
    void startNextTurn(bool demoMode);
74 75 76 77 78 79 80 81 82 83 84 85 86 87
    /**
     *  This will make the player move at row, col.
     *  If that is possible of course
     *  If demoMode is true, the computer will decide on what move to take.
     *  row and col values do not matter in that case.
     */
    void makePlayerMove(int row, int col, bool demoMode);
    /**
     *  This function will make computer decide where he 
     *  wants to put his chip... and he'll put it there!
     */
    void makeComputerMove();
    /**
     *  Undoes all the computer moves and one player move
88
     *  (so after calling this function it will be player turn)
89
     *  @return number of undone moves
90
     */
91
    int undo();
92
    /**
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
93 94 95
     *  Sets the strength of game engine (1 to 7)
     */
    void setEngineStrength(uint strength);
96 97 98 99
    /**
     *  Sets the computer skill level. From 1 to 7
     */
    void setComputerSkill(int skill);
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
100 101 102 103 104 105 106
    /**
     *  @return strength of the game engine
     */
    uint strength() const;
    /**
     *  @return whether the game is already over
     */
107
    bool isGameOver() const; // perhaps this doesn't need to be public
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
108 109 110
    /**
     *  @return whether any player move is at all possible
     */
111
    bool isAnyPlayerMovePossible() const;// perhaps this doesn't need to be public
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
112 113 114
    /**
     *  @return whether any computer move is at all possible
     */
115
    bool isAnyComputerMovePossible() const;// perhaps this doesn't need to be public
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
116 117 118
    /**
     *  @return a color of the current player
     */
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
119
    ChipColor currentPlayer() const { return m_curPlayer; }
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
120 121 122 123
    
    /**
     *  @return score (number of chips) of the player
     */
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
124 125
    int playerScore( ChipColor player ) const;
    ChipColor chipColorAt( int row, int col ) const;
126 127 128 129
    /**
     *  @return if undo is possible
     */
    bool canUndo() const { return !m_undoStack.isEmpty(); }
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
130 131 132 133
    /**
     *  Returns a hint to current player
     */
    KReversiMove getHint() const;
134 135 136 137
    /**
     *  @return last move made
     */
    KReversiMove getLastMove() const;
138 139 140
    /**
     *  Returns true, if it's computer's turn now
     */
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
141 142 143 144 145 146
    bool isComputersTurn() const { return m_curPlayer == m_computerColor; }
    /**
     *  @return a list of chips which were changed during last move.
     *  First of them will be the move itself, and the rest - chips which
     *  were turned by that move
     */
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
147
    MoveList changedChips() const { return m_changedChips; }
148 149 150 151
    /**
     *  @return a list of possible moves for current player
     */
    MoveList possibleMoves() const;
152
signals:
153
    void gameOver();
154
    void boardChanged();
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
155
    void moveFinished();
156
    void computerCantMove();
157
private:
158
    enum Direction { Up, Down, Right, Left, UpLeft, UpRight, DownLeft, DownRight };
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    /**
     * This function will tell you if the move is possible.
     * That's why it was given such a name ;)
     */
    bool isMovePossible( const KReversiMove& move ) const; 
    /**
     *  Searches for "chunk" in direction dir for move.
     *  As my English-skills are somewhat limited, let me introduce 
     *  new terminology ;).
     *  I'll define a "chunk" of chips for color "C" as follows:
     *  (let "O" be the color of the opponent for "C")
     *  CO[O]C <-- this is a chunk
     *  where [O] is one or more opponent's pieces
     */
    bool hasChunk( Direction dir, const KReversiMove& move) const;
174 175 176 177 178
    /**
     *  Performs move, i.e. marks all the chips that player wins with
     *  this move with current player color
     */
    void makeMove( const KReversiMove& move );
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
179
    /**
180
     *  Sets the type of chip at (row,col)
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
181
     */
182 183 184 185 186 187 188 189 190
    void setChipColor(ChipColor type, int row, int col);
    /**
     *  The board itself
     */
    ChipColor m_cells[8][8];
    /**
     *  Score of each player
     */
    int m_score[2];
191 192 193 194 195
    /**
     *  Color of the current player
     */
    ChipColor m_curPlayer;
    /**
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
196 197 198 199
     *  The color of the human played chips
     */
    ChipColor m_playerColor;
    /**
200 201 202
     *  The color of the computer played chips
     */
    ChipColor m_computerColor;
203 204 205 206
    /**
     *  Our AI
     */
    Engine *m_engine;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
207 208 209 210 211 212 213 214 215 216
     // Well I'm not brief at all :). That's because I think that my
     // English is not well shaped sometimes, so I try to describe things
     // so that me and others can understand. Even simple things.
     // Espesially when I think that my description sucks :)
    /**
     *  This list holds chips that were changed/added during last move
     *  First of them will be the chip added to the board by the player
     *  during last move. The rest of them - chips that were turned by that
     *  move.
     */
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
217
    MoveList m_changedChips;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
218
    /**
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
219
     *  This is an undo stack.
220 221
     *  It contains a lists of chips changed with each turn.
     *  @see m_changedChips
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
222
     */
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
223
    QStack<MoveList> m_undoStack;
224 225
};
#endif