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

#include <QObject>

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

class KReversiBoard;
9
class Engine;
10 11 12 13 14 15 16 17 18 19

/**
 *  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.
 *  FIXME dimsuz: enumerate them briefly
 *  The idea is also to abstract from any graphic representation of the game process
 */
class KReversiGame : public QObject
{
20
    Q_OBJECT
21
public:
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
22
    KReversiGame();
23 24 25 26 27
    ~KReversiGame();
    /**
     *  @returns the board so the callers can examine its current state
     */
    const KReversiBoard& board() const;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
28 29 30 31 32 33 34
    ChipColor currentPlayer() const { return m_curPlayer; }
    // NOTE: this is just a wrapper around KReversiBoard::playerScore
    // Maybe consider merging KReversiBoard into this class?
    // same applies to chipColorAt
    int playerScore( ChipColor player ) const;
    // NOTE: this is just a wrapper around KReversiBoard::playerScore
    ChipColor chipColorAt( int row, int col ) const;
35 36 37 38 39 40 41 42 43 44
    /**
     *  This will put the player chip at row, col.
     *  If that is possible of course
     */
    void makePlayerMove(int row, int col);
    /**
     *  This function will make computer decide where he 
     *  wants to put his chip... and he'll put it there!
     */
    void makeComputerMove();
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
45 46 47 48
    /**
     *  Undoes the last player-computer move pair
     */
    void undo();
49 50 51 52
    /**
     *  Returns true, if it's computer's turn now
     */
    bool computersTurn() const { return m_curPlayer == m_computerColor; }
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
53 54

    QList<KReversiMove> changedChips() const { return m_changedChips; }
55 56
signals:
    void boardChanged();
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
57
    void moveFinished();
58
private:
59
    enum Direction { Up, Down, Right, Left, UpLeft, UpRight, DownLeft, DownRight };
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    /**
     * 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;
75 76 77 78 79
    /**
     *  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
80 81 82
    /**
     *  Board itself
     */
83
    KReversiBoard *m_board;
84 85 86 87 88 89 90 91
    /**
     *  Color of the current player
     */
    ChipColor m_curPlayer;
    /**
     *  The color of the computer played chips
     */
    ChipColor m_computerColor;
92 93 94 95
    /**
     *  Our AI
     */
    Engine *m_engine;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
96 97 98 99 100 101 102 103 104 105 106
     // 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.
     */
    QList<KReversiMove> m_changedChips;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
107 108 109 110 111 112 113 114 115 116
    /**
     *  This list holds chips changed with last computer move
     *  and is used while undoing
     */
    QList<KReversiMove> m_lastUndoComputer;
    /**
     *  This list holds chips changed with last player move
     *  and is used while undoing
     */
    QList<KReversiMove> m_lastUndoPlayer;
117 118
};
#endif