Commit 3aeaf662 authored by Dmitry Suzdalev's avatar Dmitry Suzdalev

And here it is: Moves History Widget. Welcome! :)

svn path=/branches/work/kreversi_rewrite/; revision=581378
parent 72d3b82f
......@@ -50,13 +50,12 @@ void KReversiGame::makeComputerMove()
m_undoStack.push( m_changedChips );
}
void KReversiGame::undo()
int KReversiGame::undo()
{
// we're undoing all moves (if any) until we meet move done by a player.
// We undo that player move too and we're done.
// Simply put: we're undoing all_moves_of_computer + one_move_of_player
// FIXME dimsuz: temporary debug only. Remove this
int movesUndone = 0;
while( !m_undoStack.isEmpty() )
......@@ -97,6 +96,8 @@ void KReversiGame::undo()
kDebug() << "Current player changed to " << (m_curPlayer == White ? "White" : "Black" )<< endl;
emit boardChanged();
return movesUndone;
}
void KReversiGame::makeMove( const KReversiMove& move )
......
......@@ -49,8 +49,9 @@ public:
void makeComputerMove();
/**
* Undoes all the computer moves and one player move
* @return number of undone moves
*/
void undo();
int undo();
/**
* Sets the strength of game engine (1 to 7)
*/
......@@ -173,9 +174,8 @@ private:
MoveList m_changedChips;
/**
* This is an undo stack.
* Note that on each undo action a <b>pair</b> of move lists
* will be popped from top. I.e. player turn and computer turn will
* be undone in one go.
* It contains a lists of chips changed with each turn.
* @see m_changedChips
*/
QStack<MoveList> m_undoStack;
};
......
......@@ -20,6 +20,7 @@ KReversiScene::KReversiScene( KReversiGame* game , const QPixmap& chipsPixmap )
setBackgroundBrush( Qt::lightGray );
QFont font; // it'll be initialised to default application font
font.setBold(true);
// NOTE we assume that fontMetrics in drawBackground() will be the same as here
int fontHeight = QFontMetrics(font).height();
......@@ -55,6 +56,9 @@ void KReversiScene::setChipsPixmap( const QPixmap& chipsPixmap )
if( chip )
chip->setColor( chip->color() ); // this will reread pixmap
}
if(m_hintChip)
m_hintChip->setColor( m_hintChip->color() );
}
}
......@@ -383,27 +387,30 @@ void KReversiScene::drawBackground( QPainter *p, const QRectF& rc)
for(qreal y=starty; y<=endy; y+=CHIP_SIZE)
p->drawLine( QPointF(startx, y), QPointF(endx, y) );
QFont f = p->font();
f.setBold(true);
p->setFont(f);
int fontHeight = p->fontMetrics().height();
QString horLabels("ABCDEFGH");
QString verLabels("12345678");
const char horLabels[] = "ABCDEFGH";
const char verLabels[] = "12345678";
QRect rect;
// draw top+bottom labels
for(int c=0; c<8;++c)
{
rect = QRect((int)startx+c*CHIP_SIZE, (int)starty-fontHeight, CHIP_SIZE, fontHeight );
p->drawText( rect, Qt::AlignCenter | Qt::AlignTop, horLabels.at(c) );
p->drawText( rect, Qt::AlignCenter | Qt::AlignTop, QChar(horLabels[c]) );
rect.moveTop( (int)endy );
p->drawText( rect, Qt::AlignCenter | Qt::AlignTop, horLabels.at(c) );
p->drawText( rect, Qt::AlignCenter | Qt::AlignTop, QChar(horLabels[c]) );
}
// draw left+right labels
for(int r=0; r<8;++r)
{
rect = QRect( (int)startx-fontHeight, (int)starty+r*CHIP_SIZE, fontHeight, CHIP_SIZE );
p->drawText( rect, Qt::AlignCenter | Qt::AlignVCenter, verLabels.at(r) );
p->drawText( rect, Qt::AlignCenter | Qt::AlignVCenter, QChar(verLabels[r]) );
rect.moveLeft( (int)endx );
p->drawText( rect, Qt::AlignCenter | Qt::AlignVCenter, verLabels.at(r) );
p->drawText( rect, Qt::AlignCenter | Qt::AlignVCenter, QChar(verLabels[r]) );
}
}
......
......@@ -8,6 +8,9 @@
<Action name="demo" />
<Action name="quit" />
</Menu>
<Menu name="view"><text>&amp;View</text>
<Action name="show_moves" />
</Menu>
<Menu name="edit"><text>&amp;Edit</text>
<Action name="undo" />
</Menu>
......
......@@ -16,22 +16,56 @@
#include <kselectaction.h>
#include <QGraphicsView>
#include <QListWidget>
#include <QGridLayout>
static QString moveToString( const KReversiMove& move )
{
QString moveString;
if( Preferences::useColoredChips() )
moveString = (move.color == Black ? i18n("Blue") : i18n("Red") );
else
moveString = (move.color == Black ? i18n("Black") : i18n("White") );
const char labelsHor[] = "ABCDEFGH";
const char labelsVer[] = "12345678";
moveString += " ";
moveString += labelsHor[move.col];
moveString += labelsVer[move.row];
return moveString;
}
KReversiMainWindow::KReversiMainWindow(QWidget* parent)
: KMainWindow(parent), m_scene(0), m_game(0), m_undoAct(0), m_hintAct(0), m_demoAct(0)
{
statusBar()->insertItem( i18n("Your turn."), 0 );
slotNewGame();
// m_scene is created in slotNewGame();
m_view = new KReversiView(m_scene, this);
QWidget *mainWid = new QWidget;
QGridLayout *lay = new QGridLayout(mainWid);
lay->setMargin(1);
m_view = new KReversiView(m_scene, mainWid);
m_view->show();
lay->addWidget(m_view, 0, 0, 2, 1);
m_historyLabel = new QLabel( i18n("Moves history"), mainWid );
lay->addWidget( m_historyLabel, 0, 1, Qt::AlignCenter );
m_historyView = new QListWidget( mainWid );
lay->addWidget(m_historyView, 1, 1);
m_historyLabel->hide();
m_historyView->hide();
setupActions();
loadSettings();
setCentralWidget(m_view);
setCentralWidget(mainWid);
statusBar()->insertItem( i18n("Your turn."), 0 );
setupGUI();
}
......@@ -87,6 +121,10 @@ void KReversiMainWindow::setupActions()
m_coloredChipsAct = new KToggleAction( i18n("Use colored chips"), actionCollection(), "use_colored_chips" );
connect( m_coloredChipsAct, SIGNAL(triggered(bool)), SLOT(slotUseColoredChips(bool)) );
// FIXME dimsuz: read/write this from/to config file? Or not necessary?
KToggleAction *showMovesAct = new KToggleAction( i18n("Show moves history"), actionCollection(), "show_moves" );
connect( showMovesAct, SIGNAL(triggered(bool)), SLOT(slotShowMovesHistory(bool)) );
addAction(newGameAct);
addAction(quitAct);
addAction(m_undoAct);
......@@ -151,9 +189,14 @@ void KReversiMainWindow::slotUseColoredChips(bool toggled)
Preferences::writeConfig();
}
void KReversiMainWindow::slotShowMovesHistory(bool toggled)
{
m_historyLabel->setVisible(toggled);
m_historyView->setVisible(toggled);
}
void KReversiMainWindow::slotDemoMode(bool toggled)
{
kDebug() << k_funcinfo << endl;
m_scene->toggleDemoMode(toggled);
m_undoAct->setEnabled( !toggled );
......@@ -202,6 +245,14 @@ void KReversiMainWindow::slotMoveFinished()
if( !m_demoAct->isChecked() )
m_undoAct->setEnabled( m_game->canUndo() );
// add last move to history list
KReversiMove move = m_game->getLastMove();
QString numStr = QString::number( m_historyView->count()+1 ) + ". ";
m_historyView->addItem( numStr + moveToString(move) );
QListWidgetItem *last = m_historyView->item( m_historyView->count() - 1 );
m_historyView->setCurrentItem( last );
m_historyView->scrollToItem( last );
statusBar()->changeItem( m_game->isComputersTurn() ? i18n("Computer turn.") : i18n("Your turn."), 0 );
}
......@@ -210,7 +261,15 @@ void KReversiMainWindow::slotUndo()
if( !m_scene->isBusy() )
{
// scene will automatically notice that it needs to update
m_game->undo();
int numUndone = m_game->undo();
// remove last numUndone items from historyView
for(int i=0;i<numUndone; ++i)
delete m_historyView->takeItem( m_historyView->count()-1 );
QListWidgetItem *last = m_historyView->item( m_historyView->count() - 1 );
m_historyView->setCurrentItem( last );
m_historyView->scrollToItem( last );
m_undoAct->setEnabled( m_game->canUndo() );
// if the user hits undo after game is over
// let's give him a chance to ask for a hint ;)
......
......@@ -9,6 +9,8 @@ class KReversiView;
class KAction;
class KSelectAction;
class KToggleAction;
class QListWidget;
class QLabel;
class KReversiMainWindow : public KMainWindow
{
......@@ -25,13 +27,16 @@ public slots:
void slotGameOver();
void slotDemoMode(bool);
void slotUseColoredChips(bool);
void slotShowMovesHistory(bool);
private:
void setupActions();
void loadSettings();
KReversiScene *m_scene;
KReversiView *m_view;
KReversiGame *m_game;
KReversiGame *m_game;
QListWidget *m_historyView;
QLabel *m_historyLabel;
KAction* m_undoAct;
KAction* m_hintAct;
......
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