Initial commit - C++
This commit is contained in:
commit
8fa1c87da0
4 changed files with 107 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
.vs/
|
||||
.vscode/
|
||||
build/
|
||||
.kdev4/
|
5
CMakeLists.txt
Normal file
5
CMakeLists.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
cmake_minimum_required (VERSION 3.1)
|
||||
set (CMAKE_CXX_STANDARD 11)
|
||||
project (NumberGuessingGame)
|
||||
add_executable(NumberGuessingGame ngg.cpp)
|
||||
|
19
README.md
Executable file
19
README.md
Executable file
|
@ -0,0 +1,19 @@
|
|||
# Number-Guessing Game
|
||||
----------------------
|
||||
## Gameplay:
|
||||
1. Computer generates random number
|
||||
2. Computer starts "I'm thinking of a number..."
|
||||
3. Player inputs number
|
||||
4. Computer responds higher, lower, or exact for input
|
||||
5. 2 & 3 repeat until player guesses number
|
||||
6. Computer says how many guesses it took
|
||||
|
||||
## Requirements:
|
||||
* player
|
||||
* ability to give input
|
||||
* player's guessed numbers
|
||||
* computer
|
||||
* computer's random number
|
||||
* ability to receive input
|
||||
* number comparison calculations
|
||||
* give reply on number
|
79
ngg.cpp
Executable file
79
ngg.cpp
Executable file
|
@ -0,0 +1,79 @@
|
|||
#include <chrono>
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
int getRandomNum()
|
||||
{
|
||||
srand(time(0));
|
||||
return rand() % 100 + 1;
|
||||
}
|
||||
|
||||
int playGame()
|
||||
{
|
||||
// Set up initial values
|
||||
int playerGuess = 0, computerNum = 0, totalGuesses = 0;
|
||||
computerNum = getRandomNum();
|
||||
std::cout << "I'm thinking of a number between 1 and 100..." << std::endl;
|
||||
while (playerGuess != computerNum) {
|
||||
std::cout << "Enter a number: ";
|
||||
if (std::cin >> playerGuess) {
|
||||
if (playerGuess > 100 || playerGuess < 1) {
|
||||
std::cout << "You do know what \"between 1 and 100\" means, right?" << std::endl;
|
||||
}
|
||||
else if (playerGuess > computerNum) {
|
||||
std::cout << "That number is too big!" << std::endl;
|
||||
}
|
||||
else if (playerGuess < computerNum) {
|
||||
std::cout << "That number is too small!" << std::endl;
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::cout << "Dafuq is that?" << std::endl;
|
||||
std::cin.clear();
|
||||
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
||||
}
|
||||
totalGuesses++;
|
||||
}
|
||||
std::cout << "You won! It took you " << totalGuesses << " tries to guess the right number!" << std::endl;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int playAgain()
|
||||
{
|
||||
int playAgain = 0;
|
||||
std::string playerResponse = "";
|
||||
while (playAgain == 0) {
|
||||
std::cout << "Would you like to play again? (Yes/No)" << std::endl;
|
||||
if (std::cin >> playerResponse) {
|
||||
std::string::iterator it;
|
||||
for (it = playerResponse.begin(); it < playerResponse.end(); it++) {
|
||||
*it = std::tolower(*it);
|
||||
}
|
||||
if (playerResponse == "yes" || playerResponse == "y") {
|
||||
playGame();
|
||||
}
|
||||
else if (playerResponse == "no" || playerResponse == "n") {
|
||||
break;
|
||||
}
|
||||
else {
|
||||
std::cout << "wut?" << std::endl;
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::cout << "Dafuq is that?" << std::endl;
|
||||
std::cin.clear();
|
||||
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
playGame();
|
||||
playAgain();
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue