Skip to content

Instantly share code, notes, and snippets.

@CodeMouse92
Created June 21, 2017 01:28
Show Gist options
  • Save CodeMouse92/931c5111b3db4dd06959a855ba5cfe25 to your computer and use it in GitHub Desktop.
Save CodeMouse92/931c5111b3db4dd06959a855ba5cfe25 to your computer and use it in GitHub Desktop.
Perilous Guess
/* Perilous Guess
* Written by Jason C. McDonald (CodeMouse92)
* Intended as an example for PawLIB trilean.
* Relies on PawLIB: mousepawmedia.com/pawlib
*/
/* LICENSE
* Copyright (c) 2017 Jason C. McDonald.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <iostream>
#include <random>
#include "pawlib/core_types.hpp"
using namespace pawlib;
int main()
{
srand(time(NULL)); // seed random
tril status = maybe; // the outcome and loop control variable
int attempts = 0; // the number of guesses
int number = rand() % 100 + 1; // the number to guess
int poisoned[3] = {0, 0, 0};
int guess = 0; // the player's guess
// Generate the poison numbers.
for(int i = 0; i < 3; ++i)
{
int random;
do
random = rand() % 100 + 1;
while(random == number || random == poisoned[0] || random == poisoned[1]);
poisoned[i] = random;
}
std::cout << "======= PERILOUS GUESS =======" << std::endl;
std::cout << "RULES: Guess the number I'm thinking of. (1-100)" << std::endl;
std::cout << "But, DON'T guess any of the three poison numbers!" << std::endl;
// while the status is 'maybe', the player can still guess
while(~status)
{
std::cin >> guess; // get the guess
++attempts; // increment the number of guesses
// If the player guessed right
if(guess == number)
status = true; // report success, thereby exiting loop
// Else if the player guessed a poison number...
else if(guess == poisoned[0] || guess == poisoned[1] || guess == poisoned[2])
status = false; // report death, thereby exiting loop
// Else if the player below target...
else if(guess < number)
std::cout << "Your guess is too low." << std::endl;
// Else if the player above target...
else if(guess > number)
std::cout << "Your guess is too high." << std::endl;
}
std::cout << "======================" << std::endl;
if(status)
std::cout << "You won in " << attempts << " attempts." << std::endl;
else if(!status)
{
std::cout << "You guessed a poison number and died." << std::endl;
std::cout << "The answer was " << number << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment