kreversigame.h 7.55 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 31
#include "commondefs.h"

class KReversiBoard;
32
class Engine;
33 34 35 36 37 38

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