kreversigame.h 2.71 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 45 46 47 48
    /**
     *  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();
    /**
     *  Returns true, if it's computer's turn now
     */
    bool computersTurn() const { return m_curPlayer == m_computerColor; }
49 50
signals:
    void boardChanged();
51
    void currentPlayerChanged();
52
private:
53
    enum Direction { Up, Down, Right, Left, UpLeft, UpRight, DownLeft, DownRight };
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    /**
     * 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;
69 70 71 72 73
    /**
     *  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
74 75 76
    /**
     *  Board itself
     */
77
    KReversiBoard *m_board;
78 79 80 81 82 83 84 85
    /**
     *  Color of the current player
     */
    ChipColor m_curPlayer;
    /**
     *  The color of the computer played chips
     */
    ChipColor m_computerColor;
86 87 88 89
    /**
     *  Our AI
     */
    Engine *m_engine;
90 91
};
#endif