From 15d622aa542421c0e3f8eb448b69e1121d7a0738 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Thu, 17 Nov 2022 09:47:58 +0100 Subject: huiswerk week 2 klaar --- oop2w2/AutomaticNumberGuesser.cpp | 21 +++++++ oop2w2/AutomaticNumberGuesser.h | 18 ++++++ oop2w2/NumberGuessGame.cpp | 113 ++++++++++++++++++++++++++++++++++++++ oop2w2/NumberGuessGame.h | 35 ++++++++++++ oop2w2/main.cpp | 18 ++++++ oop2w2/makefile | 1 + 6 files changed, 206 insertions(+) create mode 100644 oop2w2/AutomaticNumberGuesser.cpp create mode 100644 oop2w2/AutomaticNumberGuesser.h create mode 100644 oop2w2/NumberGuessGame.cpp create mode 100644 oop2w2/NumberGuessGame.h create mode 100644 oop2w2/main.cpp create mode 120000 oop2w2/makefile diff --git a/oop2w2/AutomaticNumberGuesser.cpp b/oop2w2/AutomaticNumberGuesser.cpp new file mode 100644 index 0000000..433252c --- /dev/null +++ b/oop2w2/AutomaticNumberGuesser.cpp @@ -0,0 +1,21 @@ +#include + +#include "AutomaticNumberGuesser.h" +#include "NumberGuessGame.h" + +AutomaticNumberGuesser::AutomaticNumberGuesser() { } +AutomaticNumberGuesser::~AutomaticNumberGuesser() { } + +void AutomaticNumberGuesser::process(NumberGuessGame &game) { + unsigned low = game.getLowerBound(); + unsigned high = game.getUpperBound(); + bool found = false; + while (!found) { + unsigned guess = (low + high) / 2; + enum NumberGuessGame::RESULT r = game.guess(guess); + if (r == NumberGuessGame::TOOLOW) low = guess; + else if (r == NumberGuessGame::TOOHIGH) high = guess; + else found = true; + } +} + diff --git a/oop2w2/AutomaticNumberGuesser.h b/oop2w2/AutomaticNumberGuesser.h new file mode 100644 index 0000000..a0a99eb --- /dev/null +++ b/oop2w2/AutomaticNumberGuesser.h @@ -0,0 +1,18 @@ +#pragma once + +/* + * NO NOT ADAPT THIS FILE + */ + +class NumberGuessGame; + +class AutomaticNumberGuesser +{ +public: + AutomaticNumberGuesser(); + virtual ~AutomaticNumberGuesser(); + +public: + virtual void process( NumberGuessGame& ); +}; + diff --git a/oop2w2/NumberGuessGame.cpp b/oop2w2/NumberGuessGame.cpp new file mode 100644 index 0000000..ec1fc4e --- /dev/null +++ b/oop2w2/NumberGuessGame.cpp @@ -0,0 +1,113 @@ +/* + * NO NOT ADAPT THIS FILE + */ + +#include "NumberGuessGame.h" + +#include +#include +#include + +#include +#include + +#define INITRANDOM 100 + +/////////////////////////////////////////////////////////////////////////////// +// constructors / destructor + +NumberGuessGame::NumberGuessGame() +{ + initialize( unsigned(::time(nullptr)) ); +} + +NumberGuessGame::NumberGuessGame( unsigned uSeed ) +{ + initialize( uSeed ); +} + +NumberGuessGame::~NumberGuessGame() +{ +} + +/////////////////////////////////////////////////////////////////////////////// +// create new game + boundaries + +void +NumberGuessGame::createNewNumber() +{ + double dInterval = ::pow( 20.0, 1.0 + 2.0*uniform() ); + + m_uLowerBound = unsigned( dInterval * uniform() ); + m_uUpperBound = unsigned( m_uLowerBound + dInterval ); + m_uNumberToGuess = m_uLowerBound + unsigned( dInterval * uniform() ); + + m_uNumberOfGuesses = 0; +} + +unsigned +NumberGuessGame::getLowerBound() const +{ + return m_uLowerBound; +} + +unsigned +NumberGuessGame::getUpperBound() const +{ + return m_uUpperBound; +} + +/////////////////////////////////////////////////////////////////////////////// +// guessing process + +enum NumberGuessGame::RESULT +NumberGuessGame::guess( unsigned uValue ) +{ + m_uNumberOfGuesses += 1; + + if ( uValue == m_uNumberToGuess ) + { + std::cout << std::setw(3) << m_uNumberOfGuesses << ": number guessed : " << uValue << std::endl; + return FOUND; + } + + if ( uValue < m_uNumberToGuess ) + { + std::cout << std::setw(3) << m_uNumberOfGuesses << ": number too low : " << uValue << std::endl; + return TOOLOW; + } + else + { + std::cout << std::setw(3) << m_uNumberOfGuesses << ": number too high: " << uValue << std::endl; + return TOOHIGH; + } +} + +unsigned +NumberGuessGame::numberOfGuesses() const +{ + return m_uNumberOfGuesses; +} + +/////////////////////////////////////////////////////////////////////////////// +// private members + +void +NumberGuessGame::initialize( unsigned uSeed ) +{ + m_uLowerBound = 0; + m_uUpperBound = 1; + m_uNumberToGuess = unsigned( -1 ); + m_uNumberOfGuesses = unsigned( -1 ); + + ::srand( uSeed ); + + for (unsigned n=0; n