Commit aa33d669 authored by Denis Kuplyakov's avatar Denis Kuplyakov

Added prepare-ready rutine to Game-Player interaction

Added ready() signal to player and made KReversiGame to wait until both
players are ready.
Also added some documentation.
parent 82ae185d
......@@ -54,7 +54,7 @@ struct KReversiMove: public KReversiPos {
}
};
static ChipColor opponentColorFor(ChipColor color)
ChipColor opponentColorFor(ChipColor color)
{
if (color == NoColor)
return NoColor;
......
......@@ -15,6 +15,8 @@ void KReversiComputerPlayer::prepare(KReversiGame *game)
{
m_game = game;
m_state = WAITING;
emit ready();
}
void KReversiComputerPlayer::takeTurn()
......
......@@ -28,6 +28,8 @@ const int KReversiGame::DY[KReversiGame::DIRECTIONS_COUNT] = {1, -1, 1, 0, -1, 1
KReversiGame::KReversiGame(KReversiPlayer *blackPlayer, KReversiPlayer *whitePlayer)
: m_curPlayer(Black)
{
m_isReady[White] = m_isReady[Black] = false;
// reset board
for (int r = 0; r < 8; ++r)
for (int c = 0; c < 8; ++c)
......@@ -38,7 +40,6 @@ KReversiGame::KReversiGame(KReversiPlayer *blackPlayer, KReversiPlayer *whitePla
m_score[White] = m_score[Black] = 2;
// TODO: prepare maybe long: need to make ready signal and wait until two such signals
whitePlayer->prepare(this);
blackPlayer->prepare(this);
......@@ -49,15 +50,15 @@ KReversiGame::KReversiGame(KReversiPlayer *blackPlayer, KReversiPlayer *whitePla
connect(this, SIGNAL(blackPlayerTurn()), blackPlayer, SLOT(takeTurn()));
connect(this, SIGNAL(gameOver()), blackPlayer, SLOT(gameOver()));
connect(blackPlayer, SIGNAL(makeMove(KReversiMove)), this, SLOT(blackPlayerMove(KReversiMove)));
connect(blackPlayer, SIGNAL(ready()), this, SLOT(blackReady()));
connect(this, SIGNAL(whitePlayerCantMove()), whitePlayer, SLOT(skipTurn()));
connect(this, SIGNAL(whitePlayerTurn()), whitePlayer, SLOT(takeTurn()));
connect(this, SIGNAL(gameOver()), whitePlayer, SLOT(gameOver()));
connect(whitePlayer, SIGNAL(makeMove(KReversiMove)), this, SLOT(whitePlayerMove(KReversiMove)));
connect(whitePlayer, SIGNAL(ready()), this, SLOT(whiteReady()));
m_engine = new Engine(1);
blackPlayer->takeTurn();
}
KReversiGame::~KReversiGame()
......@@ -284,6 +285,20 @@ void KReversiGame::onDelayTimer()
startNextTurn();
}
void KReversiGame::blackReady()
{
m_isReady[Black] = true;
if (m_isReady[White])
blackPlayer->takeTurn();
}
void KReversiGame::whiteReady()
{
m_isReady[White] = true;
if (m_isReady[Black])
blackPlayer->takeTurn();
}
KReversiMove KReversiGame::getHint() const
{
/// FIXME: dimsuz: don't use true, use m_competitive
......
......@@ -131,6 +131,15 @@ private slots:
* Slot to handle end of animations with m_delayTimer
*/
void onDelayTimer();
/**
* Slot to handle ready-status of black player
*/
void blackReady();
/**
* Slot to handle ready-status of white player
*/
void whiteReady();
signals:
void gameOver();
void boardChanged();
......@@ -143,6 +152,9 @@ private:
static const int DIRECTIONS_COUNT = 8;
static const int DX[];
static const int DY[];
/**
* Used to make player think about his move again after unpossible move
*/
void kickCurrentPlayer();
/**
* This will make the player move
......@@ -177,6 +189,13 @@ private:
* Delay time
*/
int m_delay;
/**
* 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
*/
ChipColor m_lastPlayer;
/**
* The board itself
......@@ -191,7 +210,8 @@ private:
*/
Engine *m_engine;
/**
* Color of the current player
* Color of the current player.
* @c NoColor if it is interchange for animations
*/
ChipColor m_curPlayer;
// Well I'm not brief at all :). That's because I think that my
......@@ -216,6 +236,9 @@ private:
*/
QTimer m_delayTimer;
/**
* Actual players, who play the game
*/
KReversiPlayer *m_player[2];
};
#endif
......@@ -10,6 +10,8 @@ void KReversiHumanPlayer::prepare(KReversiGame* game)
// ready to start
m_game = game;
m_state = WAITING;
emit ready();
}
void KReversiHumanPlayer::takeTurn()
......
......@@ -51,6 +51,11 @@ signals:
*/
void makeMove(KReversiMove);
/**
* Player emit it after he has prepared after getting prepare command
*/
void ready();
protected:
/**
* Game player is playing
......
......@@ -44,7 +44,7 @@ KReversiView::KReversiView(KReversiGame* game, QWidget *parent) :
void KReversiView::setGame(KReversiGame *game)
{
// disconnect signals from previous game if it exists,
// disconnect signals from previous game if they exist,
// we are not interested in them anymore
if (m_game) {
disconnect(m_game, SIGNAL(boardChanged()), this, SLOT(updateBoard()));
......@@ -105,7 +105,6 @@ void KReversiView::setAnimationSpeed(int speed)
}
m_delay = value;
m_qml_root->setProperty("chipsAnimationTime", value);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment