Commit 293082b7 authored by Denis Kuplyakov's avatar Denis Kuplyakov

Restored undo functionality.

Now we are tracking how many times undo has been used in addition to old
functionality.
parent 6f286cde
......@@ -24,7 +24,7 @@
#include "kreversicomputerplayer.h"
KReversiComputerPlayer::KReversiComputerPlayer(ChipColor color, QString name):
KReversiPlayer(color, name, false), m_lowestSkill(100) // setting it big enough
KReversiPlayer(color, name, false, false), m_lowestSkill(100) // setting it big enough
{
m_engine = new Engine(1);
}
......
......@@ -67,6 +67,13 @@ KReversiGame::~KReversiGame()
delete m_engine;
}
bool KReversiGame::canUndo() const
{
if (m_curPlayer == NoColor)
return false;
return (m_player[m_curPlayer]->isUndoAllowed() && !m_undoStack.isEmpty());
}
void KReversiGame::makeMove(const KReversiMove &move)
{
if (!move.isValid()) {
......@@ -117,8 +124,7 @@ void KReversiGame::startNextTurn()
int KReversiGame::undo()
{
// TODO: calc number of undos for each player and use it
m_player[m_curPlayer]->undoUsed();
// 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
......
......@@ -57,9 +57,7 @@ public:
/**
* @return if undo is possible
*/
bool canUndo() const {
return !m_undoStack.isEmpty();
}
bool canUndo() const;
/**
* Undoes all the opponent of current player moves and one his move
* (so after calling this function it will be still current player turn)
......
......@@ -24,7 +24,7 @@
#include "kreversihumanplayer.h"
KReversiHumanPlayer::KReversiHumanPlayer(ChipColor color, QString name):
KReversiPlayer(color, name, true)
KReversiPlayer(color, name, true, true)
{
}
......
......@@ -23,8 +23,11 @@
#include "kreversiplayer.h"
KReversiPlayer::KReversiPlayer(ChipColor color, QString name, bool hintAllowed):
m_state(UNKNOWN), m_color(color), m_name(name), m_hintAllowed(hintAllowed), m_hintCount(0)
KReversiPlayer::KReversiPlayer(ChipColor color, QString name,
bool hintAllowed, bool undoAllowed):
m_state(UNKNOWN), m_color(color), m_name(name),
m_hintAllowed(hintAllowed), m_hintCount(0), m_undoAllowed(undoAllowed),
m_undoCount(0)
{
}
......@@ -52,3 +55,18 @@ int KReversiPlayer::getHintsCount()
{
return m_hintCount;
}
bool KReversiPlayer::isUndoAllowed() const
{
return m_undoAllowed;
}
void KReversiPlayer::undoUsed()
{
m_undoCount++;
}
int KReversiPlayer::getUndoCount()
{
return m_undoCount;
}
......@@ -39,7 +39,8 @@ class KReversiPlayer: public QObject
{
Q_OBJECT
public:
explicit KReversiPlayer(ChipColor color, QString name, bool hintAllowed);
explicit KReversiPlayer(ChipColor color, QString name,
bool hintAllowed, bool undoAllowed);
/**
* Used to get player color
......@@ -64,10 +65,25 @@ public:
void hintUsed();
/**
* @return how many times player has used hints
* @return how many times player has used hints
*/
int getHintsCount();
/**
* @return is undo allowed for player
*/
bool isUndoAllowed() const;
/**
* KReversiGame triggers it to to increase used undos count
*/
void undoUsed();
/**
* @return how many times player has used undo
*/
int getUndoCount();
public slots:
/**
* Triggered by KReversiGame before game starts
......@@ -132,6 +148,16 @@ protected:
* How many times player has used hint
*/
int m_hintCount;
/**
* Can player request undo or not
*/
bool m_undoAllowed;
/**
* How many times player has used undo
*/
int m_undoCount;
};
#endif // KREVERSIPLAYER_H
......@@ -129,11 +129,11 @@ void KReversiMainWindow::setupActionsInit()
// KStandardGameAction::highscores(this, SLOT(slotHighscores()), actionCollection());
KStandardGameAction::quit(this, SLOT(close()), actionCollection());
// // Undo
// m_undoAct = KStandardGameAction::undo(this, SLOT(slotUndo()), actionCollection());
// m_undoAct->setEnabled(false); // nothing to undo at the start of the game
// Undo
m_undoAct = KStandardGameAction::undo(this, SLOT(slotUndo()), actionCollection());
m_undoAct->setEnabled(false); // nothing to undo at the start of the game
// // Hint
// Hint
m_hintAct = KStandardGameAction::hint(m_view, SLOT(slotHint()), actionCollection());
m_hintAct->setEnabled(false);
// m_demoAct = KStandardGameAction::demo(this, SLOT(slotToggleDemoMode()), actionCollection());
......@@ -249,6 +249,7 @@ void KReversiMainWindow::slotNewGame()
void KReversiMainWindow::slotGameOver()
{
m_hintAct->setEnabled(false);
m_undoAct->setEnabled(m_game->canUndo());
// //TODO: only if it is not computer-computer match
// m_undoAct->setEnabled(m_game->canUndo());
......@@ -335,6 +336,7 @@ void KReversiMainWindow::slotMoveFinished()
updateStatusBar();
m_hintAct->setEnabled(m_game->isHintAllowed());
m_undoAct->setEnabled(m_game->canUndo());
}
void KReversiMainWindow::updateHistory() {
......@@ -353,16 +355,14 @@ void KReversiMainWindow::updateHistory() {
void KReversiMainWindow::slotUndo()
{
// // scene will automatically notice that it needs to update
// m_game->undo();
// scene will automatically notice that it needs to update
m_game->undo();
// updateHistory();
// updateStatusBar();
updateHistory();
updateStatusBar();
// 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 ;)
// m_hintAct->setEnabled(true);
m_undoAct->setEnabled(m_game->canUndo());
m_hintAct->setEnabled(m_game->isHintAllowed());
}
void KReversiMainWindow::slotHighscores()
......@@ -472,4 +472,5 @@ void KReversiMainWindow::receivedGameStartInformation(GameStartInformation info)
updateHistory();
m_hintAct->setEnabled(m_game->isHintAllowed());
m_undoAct->setEnabled(m_game->canUndo());
}
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