kreversigame.h 7.26 KB
Newer Older
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
1 2 3
/*******************************************************************
 *
 * Copyright 2006 Dmitry Suzdalev <dimsuz@gmail.com>
Denis Kuplyakov's avatar
Denis Kuplyakov committed
4
 * Copyright 2013 Denis Kuplaykov <dener.kup@gmail.com>
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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.
 *
 ********************************************************************/
24 25 26 27
#ifndef KREVERSI_GAME_H
#define KREVERSI_GAME_H

#include <QObject>
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
28
#include <QStack>
Denis Kuplyakov's avatar
Denis Kuplyakov committed
29
#include <QTimer>
30

Denis Kuplyakov's avatar
Denis Kuplyakov committed
31
#include <Engine.h>
32
class Engine;
Denis Kuplyakov's avatar
Denis Kuplyakov committed
33 34
#include <commondefs.h>
#include <kreversiplayer.h>
Denis Kuplyakov's avatar
Denis Kuplyakov committed
35
class KReversiPlayer;
36 37 38 39

/**
 *  KReversiGame incapsulates all of the game logic.
 *  Whenever the board state changes it emits corresponding signals.
40 41
 *  The idea is also to abstract from any graphic representation of the game
 *  process
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
42
 *
43
 *  KReversiGame receives signals from KReversiPlayer classes.
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
44 45 46
 *  I.e. it receives commands and emits events when it's internal state changes
 *  due to this commands dispatching.
 *
47 48 49 50
 *  See KReversiView, KReversiPlayer, KReversiHumanPlayer
 *  and KReversiComputerPlayer for example of working with KReversiGame
 *
 *  @see KReversiView, KReversiPlayer, KReversiHumanPlayer
51 52 53
 */
class KReversiGame : public QObject
{
54
    Q_OBJECT
55
public:
56 57 58
    /**
     * Constructs game with two specified players.
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
59
    KReversiGame(KReversiPlayer *blackPlayer, KReversiPlayer *whitePlayer);
60
    ~KReversiGame();
61
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
62
     *  @return if undo is possible
63
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
64
    bool canUndo() const;
65
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
66 67
     *  Undoes all the opponent of current player moves and one his move
     *  (so after calling this function it will be still current player turn)
68
     *  @return number of undone moves
69
     */
70
    int undo();
71
    /**
72
     *  @return score (number of chips) of the @p player
73
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
74
    int playerScore(ChipColor player) const;
75
    /**
76
     *  @return color of the chip at position @p pos
77
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
78
    ChipColor chipColorAt(KReversiPos pos) const;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
79
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
80
     *  @return a hint to current player
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
81
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
82
    KReversiMove getHint() const;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
83
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
84
     *  @return last move made
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
85
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
86
    KReversiMove getLastMove() const;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
87
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
88 89 90
     *  @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
91
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
92 93 94
    MoveList changedChips() const {
        return m_changedChips;
    }
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
95
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
96
     *  @return a list of possible moves for current player
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
97
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
98
    MoveList possibleMoves() const;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
99
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
100
     *  @return whether the game is already over
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
101
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
102
    bool isGameOver() const;
103
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
104
     *  @return whether any player move is at all possible
105
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
106
    bool isAnyPlayerMovePossible(ChipColor player) const;
107
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
108
     *  @return a color of the current player
109
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
110 111 112
    ChipColor currentPlayer() const {
        return m_curPlayer;
    }
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
113
    /**
114
     *  Sets animation times from players to @p delay milliseconds
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
115
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
116
    void setDelay(int delay);
Denis Kuplyakov's avatar
Denis Kuplyakov committed
117 118 119 120
    /**
     *  Get wait time for given cell before animating. Used for sequental turning of chips
     */
    int getPreAnimationDelay(KReversiPos pos) const;
121 122 123 124
    /**
     *  @return History of moves as MoveList
     */
    MoveList getHistory() const;
Denis Kuplyakov's avatar
Denis Kuplyakov committed
125 126 127 128 129

    /**
     *  @return Is hint allowed for current player
     */
    bool isHintAllowed() const;
Denis Kuplyakov's avatar
Denis Kuplyakov committed
130
private slots:
131
    /**
132
     *  Starts next player's turn.
133
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
134
    void startNextTurn();
135
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
136 137
     *  Slot used to handle moves from black player
     *  @param move Move of black player
138
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
139
    void blackPlayerMove(KReversiMove move);
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
140
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
141 142
     *  Slot used to handle moves from white player
     *  @param move Move of white player
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
143
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
144
    void whitePlayerMove(KReversiMove move);
145
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
146
     *  Slot to handle end of animations with m_delayTimer
147
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
148
    void onDelayTimer();
149 150 151 152 153 154 155 156 157
    /**
     *  Slot to handle ready-status of black player
     */
    void blackReady();
    /**
     *  Slot to handle ready-status of white player
     */
    void whiteReady();

158
signals:
159
    void gameOver();
160
    void boardChanged();
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
161
    void moveFinished();
Denis Kuplyakov's avatar
Denis Kuplyakov committed
162 163 164 165
    void whitePlayerCantMove();
    void blackPlayerCantMove();
    void whitePlayerTurn();
    void blackPlayerTurn();
166
private:
167
    // predefined direction arrays for easy implementation
Denis Kuplyakov's avatar
Denis Kuplyakov committed
168 169 170
    static const int DIRECTIONS_COUNT = 8;
    static const int DX[];
    static const int DY[];
171 172 173
    /**
     *  Used to make player think about his move again after unpossible move
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
174 175
    void kickCurrentPlayer();
    /**
176 177
     *  This will make the player @p move
     *  If that is possible, of course
Denis Kuplyakov's avatar
Denis Kuplyakov committed
178 179
     */
    void makeMove(const KReversiMove &move);
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
180 181 182
    /**
     * This function will tell you if the move is possible.
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
183
    bool isMovePossible(const KReversiMove &move) const;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
184
    /**
185
     *  Searches for "chunk" in direction @p dirNum for @p move.
186
     *  As my English-skills are somewhat limited, let me introduce
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
187 188
     *  new terminology ;).
     *  I'll define a "chunk" of chips for color "C" as follows:
189
     *  (let "O" be the color of the opponent for color "C")
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
190 191 192
     *  CO[O]C <-- this is a chunk
     *  where [O] is one or more opponent's pieces
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
193
    bool hasChunk(int dirNum, const KReversiMove &move) const;
194
    /**
195
     *  Performs @p move, i.e. marks all the chips that player wins with
196 197
     *  this move with current player color
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
198 199
    void turnChips(const KReversiMove &move);
    /**
200
     *  Sets the type of chip according to @p move
Denis Kuplyakov's avatar
Denis Kuplyakov committed
201 202
     */
    void setChipColor(const KReversiMove &move);
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
203
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
204
     *  Delay time
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
205
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
206
    int m_delay;
207 208 209 210 211
    /**
     *  Status flags used to know when both players are ready
     */
    bool m_isReady[2];
    /**
212
     *  Last player who has made a move. Cannot be NoColor after the first move
213
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
214
    ChipColor m_lastPlayer;
215 216 217 218 219 220 221 222
    /**
     *  The board itself
     */
    ChipColor m_cells[8][8];
    /**
     *  Score of each player
     */
    int m_score[2];
223
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
224
     *  AI to give hints
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
225
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
226
    Engine *m_engine;
227
    /**
228 229
     *  Color of the current player.
     *  @c NoColor if it is interchange for animations
230
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
231
    ChipColor m_curPlayer;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
232 233 234 235 236 237
    /**
     *  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.
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
238
    MoveList m_changedChips;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
239
    /**
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
240
     *  This is an undo stack.
241 242
     *  It contains a lists of chips changed with each turn.
     *  @see m_changedChips
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
243
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
244 245 246 247 248 249
    QStack<MoveList> m_undoStack;
    /**
     *  Used to handle end of player's animations or other stuff
     */
    QTimer m_delayTimer;

250 251 252
    /**
     *  Actual players, who play the game
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
253
    KReversiPlayer *m_player[2];
254 255
};
#endif