Skip to content

Commit

Permalink
Homework 3 solved.
Browse files Browse the repository at this point in the history
Using cmake and a library, this might throw off the homework rating
script.
  • Loading branch information
jbouze committed Jul 24, 2024
1 parent 0205fbf commit 3ad3d50
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
11 changes: 11 additions & 0 deletions homeworks/homework_3/guessing_game/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.30)
project(guess)

set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD_REQUIRED YES)
set (CMAKE_CXX_EXTENSIONS OFF)

add_executable(${PROJECT_NAME} guessing_game.cpp)

target_link_libraries(${PROJECT_NAME} myRandom)
add_subdirectory(lib)
50 changes: 50 additions & 0 deletions homeworks/homework_3/guessing_game/guessing_game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>
#include <string>
#include "myRandom.h"
using std::cout;
using std::cin;
using std::endl;
using std::string;

void printWelcomeStatement()
{
cout << "Welcome to the GUESSING GAME!" << endl;
cout << "I will generate a number and you will guess it!" << endl;
}

int getNumber(string prompt)
{
int tmpInt;
cout << prompt << endl;
cin >> tmpInt;
return tmpInt;
}

int main(int argc, char* argv[])
{
printWelcomeStatement();

int smallest = getNumber("Please provide the smallest number:");
int largest = getNumber("Please provide the largest number:");

int target = randomNumber(smallest, largest);
cout << "I've generated a number. Try to guess it!" << endl;

int counter = 0;
unsigned int guess = -1;

while (guess != target) {
cout << "Please provide the next guess:" << endl;
cin >> guess;
counter++;
if (guess > target) {
cout << "Your number is too big. Try again!" << endl;
}
else if (guess < target) {
cout << "Your number is too small. Try again!" << endl;
}
}
cout << "You've done it! You guessed the number " << target <<
" in " << counter << " guesses!" << endl;
return 0;
}
5 changes: 5 additions & 0 deletions homeworks/homework_3/guessing_game/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_library(myRandom STATIC myRandom.cpp)

target_include_directories(myRandom
INTERFACE
$(CMAKE_CURRENT_SOURCE_DIR))
20 changes: 20 additions & 0 deletions homeworks/homework_3/guessing_game/lib/myRandom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include <random>
#include "myRandom.h"

// Todo: seed once and maintain state (use a class probably)

int randomNumber(int lowerBound, int upperBound)
{
std::random_device random_device;
std::mt19937 random_engine{random_device()};
std::uniform_int_distribution<> distribution{lowerBound, upperBound};
int num = distribution(random_engine);
return num;
}

// int main()
// {
// std::cout << randomNumber(1, 5) << std::endl;
// return 0;
// }
3 changes: 3 additions & 0 deletions homeworks/homework_3/guessing_game/lib/myRandom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

int randomNumber(int lowerBound, int upperBound);

0 comments on commit 3ad3d50

Please sign in to comment.