C

Qt Quick Ultralite swipegame 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 <ctime> namespace { static constexpr std::uint16_t maxTimeout = 5000; static constexpr std::uint16_t minTimeout = 1000; static constexpr std::uint16_t interval = 500; static constexpr std::uint8_t minTries = 1; static constexpr std::uint8_t maxTries = 4; } // namespace Globals::Globals() { time.setValue(3000); tries.setValue(3); useNumberMode.setValue(true); score.setValue(0); fails.setValue(0); useSmallTouchAreas.setValue(false); showTouchAreas.setValue(false); useViewAnimations.setValue(false); std::uint8_t countryIndex{0}; // for the game we use a random list of countries sorted by their total size in square kilometers m_countries[countryIndex++] = "Vatican City"; // 0.49 m_countries[countryIndex++] = "Monaco"; // 2.02 m_countries[countryIndex++] = "Liechtenstein"; // 160 m_countries[countryIndex++] = "Malta"; // 316 m_countries[countryIndex++] = "Andorra"; // 468 m_countries[countryIndex++] = "Singapore"; // 728 m_countries[countryIndex++] = "Tonga"; // 747 m_countries[countryIndex++] = "Mauritius"; // 2,040 m_countries[countryIndex++] = "Luxembourg"; // 2,586 m_countries[countryIndex++] = "Hong Kong"; // 2,755 m_countries[countryIndex++] = "Samoa"; // 2,842 m_countries[countryIndex++] = "Lebanon"; // 10,452 m_countries[countryIndex++] = "Kosovo"; // 10,887 m_countries[countryIndex++] = "Jamaica"; // 10,991 m_countries[countryIndex++] = "Qatar"; // 11,586 m_countries[countryIndex++] = "Slovenia"; // 20,273 m_countries[countryIndex++] = "Israel"; // 20,770 m_countries[countryIndex++] = "Haiti"; // 27,750 m_countries[countryIndex++] = "Albania"; // 28.748 m_countries[countryIndex++] = "Belgium"; // 30,355 m_countries[countryIndex++] = "Taiwan"; // 36,193 m_countries[countryIndex++] = "Switzerland"; // 41,284 m_countries[countryIndex++] = "Netherlands"; // 41,850 m_countries[countryIndex++] = "Denmark"; // 43,094 m_countries[countryIndex++] = "Costa Rica"; // 51,100 m_countries[countryIndex++] = "Croatia"; // 56,594 m_countries[countryIndex++] = "Ireland"; // 70,273 m_countries[countryIndex++] = "Austria"; // 83,871 m_countries[countryIndex++] = "Portugal"; // 92,226 m_countries[countryIndex++] = "South Korea"; // 100,210 m_countries[countryIndex++] = "Cuba"; // 109,884 m_countries[countryIndex++] = "Bulgaria"; // 111,002 m_countries[countryIndex++] = "Greece"; // 131,957 m_countries[countryIndex++] = "Romania"; // 238,533 m_countries[countryIndex++] = "United Kingdom"; // 242,495 m_countries[countryIndex++] = "New Zealand"; // 270,467 m_countries[countryIndex++] = "Italy"; // 301,339 m_countries[countryIndex++] = "Poland"; // 312,696 m_countries[countryIndex++] = "Finland"; // 338,425 m_countries[countryIndex++] = "Germany"; // 357,114 m_countries[countryIndex++] = "Japan"; // 377,976 m_countries[countryIndex++] = "Norway"; // 385,207 m_countries[countryIndex++] = "Sweden"; // 450,295 m_countries[countryIndex++] = "Spain"; // 505,992 m_countries[countryIndex++] = "Thailand"; // 513,120 m_countries[countryIndex++] = "Kenya"; // 580,367 m_countries[countryIndex++] = "France"; // 640,679 m_countries[countryIndex++] = "Turkey"; // 783,562 m_countries[countryIndex++] = "Mexico"; // 1,964,375 m_countries[countryIndex++] = "Argentina"; // 2,780,400 m_countries[countryIndex++] = "Australia"; // 7,692,024 m_countries[countryIndex++] = "Brazil"; // 8,515,767 m_countries[countryIndex++] = "United States"; // 9,833,517 m_countries[countryIndex++] = "China"; // 9,596,961 m_countries[countryIndex++] = "Canada"; // 9,984,670 m_countries[countryIndex++] = "Russia"; // 17,098,246 //init random generation distribution m_randGenerator = std::mt19937(m_randDevice()); m_startNumberDistribution = distribution(1, m_countries.size() - 2); //there is always possible bigger nad smaller number m_newNumberDistribution = distribution(0, m_countries.size() - 1); } 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(); useNumberMode.setValue(!useNumberMode.value()); } void Globals::requestNewValue() { m_currentNumber = generateNewNumber(); gameCurrentNumber.setValue(m_currentNumber); gameCurrentCountry.setValue(m_countries.at(m_currentNumber)); } void Globals::initGameData() { m_startNumber = m_startNumberDistribution(m_randGenerator); gameStartNumber.setValue(m_startNumber); gameStartCountry.setValue(m_countries.at(m_startNumber)); score.setValue(0); fails.setValue(0); } void Globals::checkAnswer(bool guessedGreater) { if (guessedGreater) { if (m_currentNumber > m_startNumber) { score.setValue(score.value() + 1); } else { fails.setValue(fails.value() + 1); } } else { if (m_currentNumber > m_startNumber) { fails.setValue(fails.value() + 1); } else { score.setValue(score.value() + 1); } } } void Globals::addFail() { fails.setValue(fails.value() + 1); } int Globals::generateNewNumber() { std::uint8_t result = m_currentNumber; while (result == m_currentNumber || result == m_startNumber) { result = m_startNumberDistribution(m_randGenerator); } return result; }