aboutsummaryrefslogtreecommitdiff
path: root/oop2w2
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-11-17 09:47:58 +0100
committerlonkaars <loek@pipeframe.xyz>2022-11-17 09:47:58 +0100
commit15d622aa542421c0e3f8eb448b69e1121d7a0738 (patch)
tree7db2cf583c42a24cd807ee4e268c87240fefc54d /oop2w2
parent8f9c3d8093cba3204e3ead853ee36820a201a1e7 (diff)
huiswerk week 2 klaar
Diffstat (limited to 'oop2w2')
-rw-r--r--oop2w2/AutomaticNumberGuesser.cpp21
-rw-r--r--oop2w2/AutomaticNumberGuesser.h18
-rw-r--r--oop2w2/NumberGuessGame.cpp113
-rw-r--r--oop2w2/NumberGuessGame.h35
-rw-r--r--oop2w2/main.cpp18
l---------oop2w2/makefile1
6 files changed, 206 insertions, 0 deletions
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 <iostream>
+
+#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 <cmath>
+#include <cstdlib>
+#include <ctime>
+
+#include <iomanip>
+#include <iostream>
+
+#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<INITRANDOM; n++ )
+ ::rand();
+}
+
+double
+NumberGuessGame::uniform() const
+{
+ return ::rand() / double(RAND_MAX);
+}
diff --git a/oop2w2/NumberGuessGame.h b/oop2w2/NumberGuessGame.h
new file mode 100644
index 0000000..62394d5
--- /dev/null
+++ b/oop2w2/NumberGuessGame.h
@@ -0,0 +1,35 @@
+#pragma once
+
+/*
+ * NO NOT ADAPT THIS FILE
+ */
+
+class NumberGuessGame
+{
+public:
+ NumberGuessGame();
+ NumberGuessGame( unsigned );
+ virtual ~NumberGuessGame();
+
+public:
+ virtual void createNewNumber();
+ virtual unsigned getLowerBound() const;
+ virtual unsigned getUpperBound() const;
+
+public:
+ enum RESULT { TOOLOW=-1, FOUND=0, TOOHIGH=1 };
+
+ virtual enum RESULT guess( unsigned );
+ virtual unsigned numberOfGuesses() const;
+
+private:
+ virtual void initialize( unsigned );
+ virtual double uniform() const;
+
+private:
+ unsigned m_uLowerBound;
+ unsigned m_uUpperBound;
+ unsigned m_uNumberToGuess;
+ unsigned m_uNumberOfGuesses;
+};
+
diff --git a/oop2w2/main.cpp b/oop2w2/main.cpp
new file mode 100644
index 0000000..e7e0925
--- /dev/null
+++ b/oop2w2/main.cpp
@@ -0,0 +1,18 @@
+/*
+ * NO NOT ADAPT THIS FILE
+ */
+
+#include "NumberGuessGame.h"
+#include "AutomaticNumberGuesser.h"
+
+
+int main()
+{
+ NumberGuessGame cGame;
+ AutomaticNumberGuesser cGuesser;
+
+ cGame.createNewNumber();
+ cGuesser.process( cGame );
+
+ return 0;
+}
diff --git a/oop2w2/makefile b/oop2w2/makefile
new file mode 120000
index 0000000..a4e84c6
--- /dev/null
+++ b/oop2w2/makefile
@@ -0,0 +1 @@
+../week.mk \ No newline at end of file