kreversigame.h 7.19 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 40

/**
 *  KReversiGame incapsulates all of the game logic.
 *  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
41 42 43 44 45
 *
 *  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:
46
 *  startNextTurn() and  makePlayerMove()
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
47
 *
48
 *  See KReversiView for example of working with KReversiGame
49 50 51
 */
class KReversiGame : public QObject
{
52
    Q_OBJECT
53
public:
Denis Kuplyakov's avatar
Denis Kuplyakov committed
54
    KReversiGame(KReversiPlayer *blackPlayer, KReversiPlayer *whitePlayer);
55
    ~KReversiGame();
56
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
57
     *  @return if undo is possible
58
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
59
    bool canUndo() const;
60
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
61 62
     *  Undoes all the opponent of current player moves and one his move
     *  (so after calling this function it will be still current player turn)
63
     *  @return number of undone moves
64
     */
65
    int undo();
66
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
67
     *  @return score (number of chips) of the player
68
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
69
    int playerScore(ChipColor player) const;
70
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
71
     *  @return color of the chip at position [row, col]
72
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
73
    ChipColor chipColorAt(KReversiPos pos) const;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
74
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
75
     *  @return a hint to current player
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
76
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
77
    KReversiMove getHint() const;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
78
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
79
     *  @return last move made
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
80
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
81
    KReversiMove getLastMove() const;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
82
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
83 84 85
     *  @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
86
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
87 88 89
    MoveList changedChips() const {
        return m_changedChips;
    }
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
90
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
91
     *  @return a list of possible moves for current player
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
92
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
93
    MoveList possibleMoves() const;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
94
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
95
     *  @return whether the game is already over
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
96
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
97
    bool isGameOver() const;
98
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
99
     *  @return whether any player move is at all possible
100
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
101
    bool isAnyPlayerMovePossible(ChipColor player) const;
102
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
103
     *  @return a color of the current player
104
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
105 106 107
    ChipColor currentPlayer() const {
        return m_curPlayer;
    }
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
108
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
109
     *  Sets animation times from players
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
110
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
111
    void setDelay(int delay);
112 113 114 115 116

    /**
     *  @return History of moves as MoveList
     */
    MoveList getHistory() const;
Denis Kuplyakov's avatar
Denis Kuplyakov committed
117 118 119 120 121

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

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

246 247 248
    /**
     *  Actual players, who play the game
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
249
    KReversiPlayer *m_player[2];
250 251
};
#endif