Commit 288c346a authored by Julian Helfferich's avatar Julian Helfferich 💬
Browse files

Create new method in game logic: hasSingleGames()

Using this new functionality, the KBlocksWin::stopGame() method can be
improved and made more readable. The check if single games were created
are now made explicit.
parent 1d2ae0cc
......@@ -9,6 +9,13 @@ ecm_add_test(
NAME_PREFIX "UnitTest-"
)
ecm_add_test(
test_KBlocksGameLogic.cpp
TEST_NAME KBlocksGameLogic
LINK_LIBRARIES Qt5::Test KBlocksCore
NAME_PREFIX "UnitTest-"
)
ecm_add_test(
test_KBlocksItemGroup.cpp
TEST_NAME KBlocksItemGroup
......
/******************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2009-2021 Julian Helfferich <julian.helfferich@mailbox.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
******************************************************************************/
#include <QTest>
#include "KBlocksGameLogic.h"
class testKBlocksGameLogic : public QObject
{
Q_OBJECT
private slots:
void test_hasSingleGames();
};
void testKBlocksGameLogic::test_hasSingleGames()
{
/**
* KBlocksGameLogic::hasSingleGames() should return true if single
* games have been created and false otherwise.
*/
KBlocksGameLogic gameLogic(1);
QVERIFY(!gameLogic.hasSingleGames());
gameLogic.startGame(1);
QVERIFY(gameLogic.hasSingleGames());
gameLogic.deleteSingleGames();
QVERIFY(!gameLogic.hasSingleGames());
gameLogic.startGame(0);
QVERIFY(!gameLogic.hasSingleGames());
}
QTEST_MAIN(testKBlocksGameLogic)
#include "test_KBlocksGameLogic.moc"
......@@ -38,6 +38,7 @@ public:
virtual void pauseGame(bool) = 0;
virtual void continueGame() = 0;
virtual bool hasSingleGames() = 0;
virtual bool deleteSingleGames() = 0;
signals:
......
/***************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
/******************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2010-2021 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* Julian Helfferich <julian.helfferich@mailbox.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
******************************************************************************/
#include "KBlocksGameLogic.h"
#include <QRandomGenerator>
......@@ -265,6 +266,11 @@ void KBlocksGameLogic::continueGame()
}
}
bool KBlocksGameLogic::hasSingleGames()
{
return mGameCount != 0;
}
void KBlocksGameLogic::createSingleGames(int gameCount)
{
if (gameCount > mGameMax) {
......
......@@ -55,6 +55,10 @@ public:
void pauseGame(bool pauseFlag) override;
void continueGame() override;
/**
* Return whether single games have been created.
*/
bool hasSingleGames() override;
bool deleteSingleGames() override;
public slots:
......
......@@ -140,8 +140,7 @@ void KBlocksWin::startGame()
void KBlocksWin::stopGame()
{
if (mpGameLogic->getSingleGame(0)) {
// If singleGame(s) exist
if (mpGameLogic->hasSingleGames()) {
mpGameScene->stopGame();
mpGameLogic->deleteSingleGames();
// Clear the game field
......
......@@ -37,6 +37,7 @@ public:
void pauseGame(bool) override {}
void continueGame() override {}
bool hasSingleGames() override { return true; }
bool deleteSingleGames() override { singleGamesDeleted = true; return true; }
public:
......
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