generated from cpp-for-yourself/homeworks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using cmake and a library, this might throw off the homework rating script.
- Loading branch information
Showing
5 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
|
||
int randomNumber(int lowerBound, int upperBound); |