Commit 263854ce authored by Dmitry Suzdalev's avatar Dmitry Suzdalev

Moved KReversiScene::beginNextTurn() to KReversiGame::nextTurn() where it really belongs

Added computerCantMove() signal to KReversiGame

svn path=/branches/work/kreversi_rewrite/; revision=581610
parent dad34ea6
......@@ -38,6 +38,48 @@ void KReversiGame::makePlayerMove( int row, int col, bool demoMode )
m_undoStack.push( m_changedChips );
}
void KReversiGame::nextTurn(bool demoMode)
{
if( !isGameOver() )
{
if( isComputersTurn() )
{
if(isAnyComputerMovePossible())
{
makeComputerMove();
}
else if( demoMode )
{
makePlayerMove(-1, -1, true );
}
else // no comp move possible and not in demo mode
{
kDebug() << "Computer can't move!" << endl;
emit computerCantMove();
}
}
else
{
// if player cant move let the computer play again!
if( !isAnyPlayerMovePossible() )
{
// FIXME dimsuz: emit something like "playerCantMove()" for gui to catch this?
kDebug() << "Player can't move!" << endl;
makeComputerMove();
}
else if( demoMode ) // let the computer play instead of player
{
makePlayerMove( -1, -1, true );
}
}
}
else
{
kDebug() << "GAME OVER" << endl;
emit gameOver();
}
}
void KReversiGame::makeComputerMove()
{
m_curPlayer = m_computerColor;
......
......@@ -15,6 +15,7 @@ class Engine;
* Whenever the board state changes it emits corresponding signals.
* The idea is also to abstract from any graphic representation of the game process
*
* FIXME dimsuz: re-document this! :)
* 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.
......@@ -35,6 +36,15 @@ class KReversiGame : public QObject
public:
KReversiGame();
~KReversiGame();
/**
* Makes next player turn.
* Checks if players can move and if they can then:
* if it's time for computer to move or user is locked and can't move,
* this function performs a computer move.
*
* @param demoMode if true then computer will decide for player turn
*/
void nextTurn(bool demoMode);
/**
* This will make the player move at row, col.
* If that is possible of course
......@@ -117,8 +127,10 @@ public:
*/
MoveList possibleMoves() const;
signals:
void gameOver();
void boardChanged();
void moveFinished();
void computerCantMove();
private:
enum Direction { Up, Down, Right, Left, UpLeft, UpRight, DownLeft, DownRight };
/**
......
......@@ -179,7 +179,7 @@ void KReversiScene::toggleDemoMode( bool toggle )
// if we are currently waiting for user mouse input and not animating,
// let's do the turn right now!
if( !m_game->isComputersTurn() && !m_animTimer->isActive() )
beginNextTurn(); // it will take m_demoMode into account
m_game->nextTurn(m_demoMode);
}
void KReversiScene::slotGameMoveFinished()
......@@ -226,8 +226,8 @@ void KReversiScene::slotAnimationStep()
displayLastAndPossibleMoves();
// some better name maybe?
beginNextTurn();
// FIXME dimsuz: set m_demoMode=false on GAME OVER somewhere!
m_game->nextTurn(m_demoMode);
}
}
}
......@@ -238,54 +238,6 @@ void KReversiScene::slotAnimationStep()
}
}
void KReversiScene::beginNextTurn()
{
if( !m_game->isGameOver() )
{
if( m_game->isComputersTurn() )
{
if(m_game->isAnyComputerMovePossible())
{
m_game->makeComputerMove();
}
else
{
// FIXME dimsuz: display this in GUI
kDebug() << "Computer can't move!" << endl;
if( m_demoMode )
m_game->makePlayerMove(-1, -1, true );
}
// else we'll just do nothing and wait for
// player's mouse input
}
else
{
// if player cant move there's no sence in waiting for mouseInput.
// Let the computer play again!
if( !m_game->isAnyPlayerMovePossible() )
{
// FIXME dimsuz: display this in GUI
kDebug() << "Player can't move!" << endl;
m_game->makeComputerMove();
}
else // else if we're in the demo-mode let the computer play instead of player
{
// "true" means "let the computer find best row and col for move"
if( m_demoMode )
m_game->makePlayerMove( -1, -1, true );
}
}
}
else
{
kDebug() << "GAME OVER" << endl;
m_demoMode = false;
// is something else needed?
emit gameOver();
}
}
void KReversiScene::displayLastAndPossibleMoves()
{
// ==== Show What Last Move Was ====
......@@ -431,14 +383,17 @@ void KReversiScene::stopHintAnimation()
m_showingHint = false;
update(m_hintChip->sceneBoundingRect());
}
else // scene is animating move now...
kDebug() << "Don't you see I'm animating? Be patient, human child..." << endl;
}
}
void KReversiScene::mousePressEvent( QGraphicsSceneMouseEvent* ev )
{
stopHintAnimation();
if( m_animTimer->isActive() )
{
kDebug() << "Don't you see I'm animating? Be patient, human child..." << endl;
return;
}
if( !m_boardRect.contains(ev->scenePos()) )
return;
......
......@@ -77,7 +77,6 @@ public slots:
*/
void toggleDemoMode(bool toggle);
signals:
void gameOver();
/**
* emitted when Scene finishes displaying last move
*/
......@@ -89,13 +88,6 @@ private:
*/
virtual void drawBackground( QPainter *p, const QRectF& rect );
virtual void mousePressEvent( QGraphicsSceneMouseEvent* );
/**
* Checks if players can move and if they can then:
* if it's time for computer to move or user is locked and can't move,
* this function tells m_game to perform computer move.
* Else it just sits and wait for user's mouse input (= his turn)
*/
void beginNextTurn();
/**
* Visually displays last move and possible moves
* (if the scene is set up to show them)
......
......@@ -215,6 +215,8 @@ void KReversiMainWindow::slotNewGame()
{
delete m_game;
m_game = new KReversiGame;
connect( m_game, SIGNAL(gameOver()), SLOT(slotGameOver()) );
connect( m_game, SIGNAL(computerCantMove()), SLOT(slotComputerCantMove()) );
if(m_hintAct)
m_hintAct->setEnabled( true );
......@@ -230,7 +232,6 @@ void KReversiMainWindow::slotNewGame()
{
// FIXME dimsuz: if chips.png not found give error end exit
m_scene = new KReversiScene(m_game, KStandardDirs::locate("appdata", "pics/chips.png"));
connect( m_scene, SIGNAL(gameOver()), SLOT(slotGameOver()) );
connect( m_scene, SIGNAL(moveFinished()), SLOT(slotMoveFinished()) );
}
else
......@@ -276,6 +277,11 @@ void KReversiMainWindow::slotMoveFinished()
statusBar()->changeItem( i18n("Computer: %1", m_game->playerScore(White) ), COMP_STATUSBAR_ID);
}
void KReversiMainWindow::slotComputerCantMove()
{
statusBar()->changeItem( i18n("Computer can not make move. Your turn."), 0 );
}
void KReversiMainWindow::slotUndo()
{
if( !m_scene->isBusy() )
......
......@@ -28,6 +28,7 @@ public slots:
void slotDemoMode(bool);
void slotUseColoredChips(bool);
void slotShowMovesHistory(bool);
void slotComputerCantMove();
private:
void setupActions();
void loadSettings();
......
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