C

Qt Quick Ultralite swipe_game Demo

/****************************************************************************** ** ** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Quick Ultralite module. ** ** $QT_BEGIN_LICENSE:COMM$ ** ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see http://www.qt.io/terms-conditions. For further ** information use the contact form at http://www.qt.io/contact-us. ** ** $QT_END_LICENSE$ ** ******************************************************************************/
#include "Globals.h" #include <platform/platform.h> namespace { const uint16_t maxTimeout = 5000; const uint16_t minTimeout = 1000; const uint16_t interval = 500; const uint8_t minTries = 1; const uint8_t maxTries = 4; } // namespace // For the game we use a random list of countries sorted by their total size in square kilometers static const char *countries[] = {"Vatican City", "Monaco", "Liechtenstein", "Malta", "Andorra", "Singapore", "Tonga", "Mauritius", "Luxembourg", "Hong Kong", "Samoa", "Lebanon", "Kosovo", "Jamaica", "Qatar", "Slovenia", "Israel", "Haiti", "Albania", "Belgium", "Taiwan", "Switzerland", "Netherlands", "Denmark", "Costa Rica", "Croatia", "Ireland", "Austria", "Portugal", "South Korea", "Cuba", "Bulgaria", "Greece", "Romania", "United Kingdom", "New Zealand", "Italy", "Poland", "Finland", "Germany", "Japan", "Norway", "Sweden", "Spain", "Thailand", "Kenya", "France", "Turkey", "Mexico", "Argentina", "Australia", "Brazil", "United States", "China", "Canada", "Russia"}; static const int numCountries = sizeof(countries) / sizeof(*countries); Globals::Globals() : m_startNumber(0) , m_currentNumber(0) { time.setValue(3000); tries.setValue(3); currentGameMode.setValue(GameMode::Numbers); score.setValue(0); fails.setValue(0); useSmallTouchAreas.setValue(false); showTouchAreas.setValue(false); useViewAnimations.setValue(false); } void Globals::increaseTime() { if (time.value() < maxTimeout) { time.setValue(time.value() + interval); } } void Globals::decreaseTime() { if (time.value() > minTimeout) { time.setValue(time.value() - interval); } } void Globals::increaseTries() { if (tries.value() < maxTries) { tries.setValue(tries.value() + 1); } } void Globals::decreaseTries() { if (tries.value() > minTries) { tries.setValue(tries.value() - 1); } } void Globals::switchGameMode() { initGameData(); GameMode newMode = isNumberMode() ? GameMode::Countries : GameMode::Numbers; currentGameMode.setValue(newMode); } bool Globals::isNumberMode() { return (currentGameMode.value() == GameMode::Numbers); } void Globals::requestNewValue() { m_currentNumber = generateNewNumber(); gameCurrentNumber.setValue(m_currentNumber); gameCurrentCountry.setValue(countries[m_currentNumber]); } void Globals::initGameData() { m_startNumber = generateRandomNumber(); gameStartNumber.setValue(m_startNumber); gameStartCountry.setValue(countries[m_startNumber]); score.setValue(0); fails.setValue(0); } void Globals::updateScore(bool correctGuess) { if (correctGuess) { score.setValue(score.value() + 1); } else { addFail(); } } void Globals::checkAnswer(bool guessedGreater) { if (guessedGreater) { updateScore(m_currentNumber > m_startNumber); } else { updateScore(m_currentNumber < m_startNumber); } } void Globals::addFail() { fails.setValue(fails.value() + 1); } int Globals::generateRandomNumber() const { const int randomNumber = static_cast<int>(static_cast<double>(std::numeric_limits<int>::max()) * Qul::Platform::getPlatformInstance()->rand()); return 1 + (randomNumber % (numCountries - 3)); } int Globals::generateNewNumber() const { int newNumber = generateRandomNumber(); while (newNumber == m_currentNumber || newNumber == m_startNumber) { newNumber = generateRandomNumber(); } return newNumber; }