C++ 入門指南 4.01
練習 31.8 31.9 參考程式 - 練習猜數字遊戲的 Qt Quick 專案
#include "guesscontroller.h" GuessController::GuessController(QObject *parent) : QObject(parent) { setGuessObject(); } QString GuessController::userInput() { return m_userInput; } void GuessController::setUserInput(const QString &userInput) { if (userInput == m_userInput) return; m_userInput = userInput; emit userInputChanged(); } QString GuessController::s2q(const string &s) { return QString(QString::fromLocal8Bit(s.c_str())); } string GuessController::q2s(const QString &s) { return string((const char *)s.toLocal8Bit()); } void GuessController::setGuessObject() { guessObject = new Guess(); } QString GuessController::getGuessObject() { return s2q(guessObject->GetAnswer()); } bool GuessController::test() { if (guessObject->Test(q2s(m_userInput))) { return true; } else { return false; } } bool GuessController::findNumber() { if (guessObject->FindNumber(q2s(m_userInput))) { return true; } else { return false; } } QString GuessController::getAB() { return s2q(guessObject->GetAB()); } QString GuessController::getTimes() { return s2q(to_string(guessObject->GetTimes())); } /*《程式語言教學誌》的範例程式 http://kaiching.org/ 檔名:guesscontroll.cpp 功能:猜數字遊戲的 Controller 實作檔 作者:張凱慶 */