[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-games-devel
Subject:    [Kde-games-devel] Bugfix for KDiamond
From:       Paul B <happysmileman () googlemail ! com>
Date:       2008-08-08 15:50:22
Message-ID: 200808081650.23182.happysmileman () gmail ! com
[Download RAW message or body]

I have here a patch to fix two bugs in KDiamond I came across.

The first bug was that after completing a game it was not possible to play 
another game without restarting the application because the update timer 
would be stopped at the end of each game but not started again when a new 
game was started, this was fixed by starting the update timers in 
MainWindow::startNewGame() instead of MainWindow::MainWindow().

Second bug was that when playing an untimed game the game would end on 
your first move after 200 seconds were up. This happens when 
KDiamond::GameState::addPoints() is called and it calls update(true), which 
forces recalculation of the time even if you're playing an untimed game, and 
it doesn't check whether the game is untimed before ending it based on the 
time.

In addition to fixing these it also changes it so that "Untimed Game" is 
displayed in the statusbar instead of "Time Remaining: %1 seconds" if you're 
playing an untimed game.

["patch.diff" (text/x-patch)]

Index: src/mainwindow.cpp
===================================================================
--- src/mainwindow.cpp	(revision 844027)
+++ src/mainwindow.cpp	(working copy)
@@ -47,12 +47,8 @@
 MainWindow::MainWindow(QWidget *parent)
 	: KXmlGuiWindow(parent)
 {
-	//init timers and randomizer (necessary for the board)
 	m_updateTimer = new QTimer;
-	connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(updateTime()), \
                Qt::DirectConnection);
-	m_updateTimer->start(KDiamond::UpdateInterval);
 	m_updateTime = new QTime;
-	m_updateTime->start();
 	qsrand(time(0));
 	//init GUI - actions
 	KStandardGameAction::gameNew(this, SLOT(startGame()), actionCollection());
@@ -72,7 +68,14 @@
 	connect(untimed, SIGNAL(triggered(bool)), this, SLOT(untimedAction(bool)));
 	//init GUI - statusbar etc.
 	statusBar()->insertPermanentItem(i18n("Points: %1", 0), 1, 1);
-	statusBar()->insertPermanentItem(i18np("Time left: 1 second", "Time left: %1 \
seconds", 0), 2, 1); +	if (Settings::untimed())
+	{
+		statusBar()->insertPermanentItem(i18n("Untimed Game"), 2, 1);
+	}
+	else
+	{
+		statusBar()->insertPermanentItem(i18np("Time left: 1 second", "Time left: %1 \
seconds", 0), 2, 1); +	}
 	statusBar()->insertPermanentItem(i18n("Possible moves: %1", 0), 3, 1);
 	setAutoSaveSettings();
 	//init GUI - center area
@@ -108,6 +111,10 @@
 
 void MainWindow::startGame()
 {
+	//init timers and randomizer (necessary for the board)
+	connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(updateTime()), \
Qt::DirectConnection); +	m_updateTimer->start(KDiamond::UpdateInterval);
+	m_updateTime->start();
 	//save (eventually changed) difficulty level
 	KGameDifficulty::standardLevel level = KGameDifficulty::level();
 	Settings::setSkill((int) level);
@@ -182,6 +189,10 @@
 void MainWindow::untimedAction(bool untimed)
 {
 	m_game->state()->setMode(untimed ? KDiamond::UntimedGame : KDiamond::NormalGame);
+	if (untimed)
+	{
+		statusBar()->changeItem(i18n("Untimed Game"), 2);
+	}
 }
 
 void MainWindow::updateTime()
@@ -203,33 +214,36 @@
 
 void MainWindow::updateRemainingTime(int remainingSeconds)
 {
-	//store the time: if remainingSeconds == -1, the old time is just re-rendered (used \
                by the configuration action MainWindow::showMinutesOnTimer)
-	static int storeRemainingSeconds = 0;
-	if (remainingSeconds == -1)
-		remainingSeconds = storeRemainingSeconds;
-	else
-		storeRemainingSeconds = remainingSeconds;
-	//split time in seconds and minutes if wanted
-	int seconds, minutes;
-	if (Settings::showMinutes())
+	if (m_game->state()->mode() != KDiamond::UntimedGame)
 	{
-		seconds = remainingSeconds % 60;
-		minutes = remainingSeconds / 60;
+		//store the time: if remainingSeconds == -1, the old time is just re-rendered \
(used by the configuration action MainWindow::showMinutesOnTimer) +		static int \
storeRemainingSeconds = 0; +		if (remainingSeconds == -1)
+			remainingSeconds = storeRemainingSeconds;
+		else
+			storeRemainingSeconds = remainingSeconds;
+		//split time in seconds and minutes if wanted
+		int seconds, minutes;
+		if (Settings::showMinutes())
+		{
+			seconds = remainingSeconds % 60;
+			minutes = remainingSeconds / 60;
+		}
+		else
+		{
+			seconds = remainingSeconds;
+			minutes = 0; //the minutes do not appear in the output when minutes == 0
+		}
+		//compose new string
+		QString sOutput;
+		if (minutes == 0)
+			sOutput = i18n("Time left: %1", i18np("1 second", "%1 seconds", seconds));
+		else if (seconds == 0)
+			sOutput = i18n("Time left: %1", i18np("1 minute", "%1 minutes", minutes));
+		else
+			sOutput = i18nc("The two parameters are strings like '2 minutes' or '1 second'.", \
"Time left: %1, %2", i18np("1 minute", "%1 minutes", minutes), i18np("1 second", "%1 \
seconds", seconds)); +		statusBar()->changeItem(sOutput, 2);
 	}
-	else
-	{
-		seconds = remainingSeconds;
-		minutes = 0; //the minutes do not appear in the output when minutes == 0
-	}
-	//compose new string
-	QString sOutput;
-	if (minutes == 0)
-		sOutput = i18n("Time left: %1", i18np("1 second", "%1 seconds", seconds));
-	else if (seconds == 0)
-		sOutput = i18n("Time left: %1", i18np("1 minute", "%1 minutes", minutes));
-	else
-		sOutput = i18nc("The two parameters are strings like '2 minutes' or '1 second'.", \
"Time left: %1, %2", i18np("1 minute", "%1 minutes", minutes), i18np("1 second", "%1 \
                seconds", seconds));
-	statusBar()->changeItem(sOutput, 2);
 }
 
 void MainWindow::showMinutesOnTimer(bool showMinutes)
Index: src/game-state.cpp
===================================================================
--- src/game-state.cpp	(revision 844027)
+++ src/game-state.cpp	(working copy)
@@ -169,7 +169,7 @@
 	//calculate new time
 	const int leftMilliseconds = 1000 * KDiamond::GameDuration + \
p->m_earnedMilliseconds + p->m_pausedMilliseconds - p->m_gameTime.elapsed();  const \
                int leftSeconds = leftMilliseconds / 1000;
-	if (leftSeconds <= 0)
+	if (leftSeconds <= 0 && p->m_mode != KDiamond::UntimedGame)
 		setState(KDiamond::Finished);
 	if (p->m_leftMilliseconds / 1000 != leftSeconds)
 		emit leftTimeChanged(leftSeconds);



_______________________________________________
kde-games-devel mailing list
kde-games-devel@kde.org
https://mail.kde.org/mailman/listinfo/kde-games-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic