kreversigame.h 7.15 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 30
#include <KDebug>
#include <QTimer>
31

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

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

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

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

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