kreversigame.h 6.61 KB
Newer Older
1 2 3 4
#ifndef KREVERSI_GAME_H
#define KREVERSI_GAME_H

#include <QObject>
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
5
#include <QStack>
6

Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
7 8 9
#include "commondefs.h"

class KReversiBoard;
10
class Engine;
11 12 13 14 15 16

/**
 *  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
17 18 19 20 21
 *
 *  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:
22
 *  startNextTurn() and  makePlayerMove()
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
23 24
 *
 *  See KReversiScene for example of working with KReversiGame
25 26 27
 */
class KReversiGame : public QObject
{
28
    Q_OBJECT
29
public:
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
30
    KReversiGame();
31
    ~KReversiGame();
32
    /**
33 34 35 36 37 38 39 40 41 42
     *  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.
43 44
     *
     *  If game is over it'll emit gameOver()
45 46 47 48
     *  
     *  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()
49 50 51
     *
     *  @param demoMode if true then computer will decide for player turn
     */
52
    void startNextTurn(bool demoMode);
53 54 55 56 57 58 59 60 61 62 63 64 65 66
    /**
     *  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
67
     *  (so after calling this function it will be player turn)
68
     *  @return number of undone moves
69
     */
70
    int undo();
71
    /**
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
72 73 74
     *  Sets the strength of game engine (1 to 7)
     */
    void setEngineStrength(uint strength);
75 76 77 78
    /**
     *  Sets the computer skill level. From 1 to 7
     */
    void setComputerSkill(int skill);
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
79 80 81 82 83 84 85
    /**
     *  @return strength of the game engine
     */
    uint strength() const;
    /**
     *  @return whether the game is already over
     */
86
    bool isGameOver() const; // perhaps this doesn't need to be public
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
87 88 89
    /**
     *  @return whether any player move is at all possible
     */
90
    bool isAnyPlayerMovePossible() const;// perhaps this doesn't need to be public
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
91 92 93
    /**
     *  @return whether any computer move is at all possible
     */
94
    bool isAnyComputerMovePossible() const;// perhaps this doesn't need to be public
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
95 96 97
    /**
     *  @return a color of the current player
     */
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
98
    ChipColor currentPlayer() const { return m_curPlayer; }
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
99
    
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
100 101 102
    // NOTE: this is just a wrapper around KReversiBoard::playerScore
    // Maybe consider merging KReversiBoard into this class?
    // same applies to chipColorAt
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
103 104 105
    /**
     *  @return score (number of chips) of the player
     */
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
106 107 108
    int playerScore( ChipColor player ) const;
    // NOTE: this is just a wrapper around KReversiBoard::playerScore
    ChipColor chipColorAt( int row, int col ) const;
109 110 111 112
    /**
     *  @return if undo is possible
     */
    bool canUndo() const { return !m_undoStack.isEmpty(); }
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
113 114 115 116
    /**
     *  Returns a hint to current player
     */
    KReversiMove getHint() const;
117 118 119 120
    /**
     *  @return last move made
     */
    KReversiMove getLastMove() const;
121 122 123
    /**
     *  Returns true, if it's computer's turn now
     */
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
124 125 126 127 128 129
    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
130
    MoveList changedChips() const { return m_changedChips; }
131 132 133 134
    /**
     *  @return a list of possible moves for current player
     */
    MoveList possibleMoves() const;
135
signals:
136
    void gameOver();
137
    void boardChanged();
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
138
    void moveFinished();
139
    void computerCantMove();
140
private:
141
    enum Direction { Up, Down, Right, Left, UpLeft, UpRight, DownLeft, DownRight };
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    /**
     * 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;
157 158 159 160 161
    /**
     *  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
162 163 164
    /**
     *  Board itself
     */
165
    KReversiBoard *m_board;
166 167 168 169 170
    /**
     *  Color of the current player
     */
    ChipColor m_curPlayer;
    /**
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
171 172 173 174
     *  The color of the human played chips
     */
    ChipColor m_playerColor;
    /**
175 176 177
     *  The color of the computer played chips
     */
    ChipColor m_computerColor;
178 179 180 181
    /**
     *  Our AI
     */
    Engine *m_engine;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
182 183 184 185 186 187 188 189 190 191
     // 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
192
    MoveList m_changedChips;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
193
    /**
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
194
     *  This is an undo stack.
195 196
     *  It contains a lists of chips changed with each turn.
     *  @see m_changedChips
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
197
     */
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
198
    QStack<MoveList> m_undoStack;
199 200
};
#endif