Return to Snippet

Revision: 33501
at October 9, 2010 04:23 by dunwitch


Updated Code
// The generic miner v0.0.2

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

int main()
{
    using namespace std;
    
    cout << "*** Choose an option. ***\n\n";
    cout << "1: - Mine\n";
    cout << "2: - Show Ore\n";
    cout << "3: - Exit\n\n";
    
    int choice;
    int totalOre = 0;
        
    do
    {   
        cout << "Choice: ";
        cin >> choice;
        
        // Roll a 20 sided dice to check if the player finds ore.
        srand(time(0));
        int randNum = rand();
        int randOre = (randNum % 20) + 1;
        
        switch (choice)
        {
               case 1:
                    if (choice == 1 && randOre > 10)
                    {
                               cout << randOre << ": Rolled\n" << "*** You found ore! ***\n\n";
                               ++totalOre;
                    }
                    else
                    {
                        cout << "Nothing was found\n\n";
                    }
                    break;        
               
               case 2:
                    if (choice == 2 && totalOre > 0)
                    {
                               cout << "Total Ore: " << totalOre << "\n\n";
                    }
                    else
                    {
                        cout << "You have no ore. Try to mine...\n\n";
                    }
                    break;
                                   
               default:
               cout << "Exiting...\n";
        }
    } while (choice == 1 || choice == 2);
    
    cin.clear();
    cin.ignore(255, '\n');
    cin.get();
    return 0;
}

Revision: 33500
at October 9, 2010 04:07 by dunwitch


Updated Code
// The generic miner v0.0.2

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

int main()
{
    using namespace std;
    
    cout << "*** Choose an option. ***\n\n";
    cout << "1: - Mine\n";
    cout << "2: - Show Ore\n";
    cout << "3: - Exit\n\n";
    
    int choice;
    int totalOre = 0;
        
    do
    {   
        cout << "Choice: ";
        cin >> choice;
        
        // Rolls a 20 sided dice to check if the player finds ore.
        srand(time(0));
        int randNum = rand();
        int randOre = (randNum % 20) + 1;
        
        switch (choice)
        {
               case 1:
                    if (choice == 1 && randOre > 10)
                    {
                               cout << randOre << ": Rolled\n" << "*** You found ore! ***\n\n";
                               ++totalOre;
                    }
                    else
                    {
                        cout << "Nothing was found\n\n";
                    }
                    break;        
               
               case 2:
                    if (choice == 2 && totalOre > 0)
                    {
                               cout << "Total Ore: " << totalOre << "\n\n";
                    }
                    else
                    {
                        cout << "You have no ore. Try to mine...\n\n";
                    }
                    break;
                                   
               default:
               cout << "Exiting...\n";
        }
    } while (choice == 1 || choice == 2);
    
    cin.clear();
    cin.ignore(255, '\n');
    cin.get();
    return 0;
}

Revision: 33499
at October 9, 2010 04:06 by dunwitch


Updated Code
// The generic miner v0.0.2

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

int main()
{
    using namespace std;
    
    cout << "*** Choose an option. ***\n\n";
    cout << "1: - Mine\n";
    cout << "2: - Show Ore\n";
    cout << "3: - Exit\n\n";
    
    int choice;
    int totalOre = 0;
        
    do
    {   
        cout << "Choice: ";
        cin >> choice;
        
        // Rolls a 20 sided dice to check if the player finds ore.
        srand(time(0));
        int randNum = rand();
        int randOre = (randNum % 20) + 1;
        
        switch (choice)
        {
               case 1:
                    if (choice == 1 && randOre > 10)
                    {
                               cout << randOre << ": Rolled\n" << "*** You found ore! ***\n\n";
                               ++totalOre;
                    }
                    else
                    {
                        cout << "Nothing was found\n\n";
                    }
                    break;        
               
               case 2:
                    if (choice == 2 && totalOre > 0)
                    {
                               cout << "Total Ore: " << totalOre << "\n\n";
                    }
                    else
                    {
                        cout << "You have no ore. Trying to mine...\n\n";
                    }
                    break;
                                   
               default:
               cout << "Exiting...\n";
        }
    } while (choice == 1 || choice == 2);
    
    cin.clear();
    cin.ignore(255, '\n');
    cin.get();
    return 0;
}

Revision: 33498
at October 9, 2010 04:00 by dunwitch


Initial Code
// The generic miner v0.0.1

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

int main()
{
    using namespace std;
    
    cout << "*** Choose an option. ***\n\n";
    cout << "1: - Mine\n";
    cout << "2: - Show Ore\n";
    cout << "3: - Exit\n\n";
    
    int choice;
    int totalOre = 0;
        
    do
    {   
        cout << "Choice: ";
        cin >> choice;
        
        // Rolls a 20 sided dice to check if the player finds ore.
        srand(time(0));
        int randNum = rand();
        int randOre = (randNum % 20) + 1;
        
        switch (choice)
        {
               case 1:
                    if (choice == 1 && randOre > 10)
                    {
                               cout << randOre << ": Rolled\n" << "*** You found ore! ***\n\n";
                               ++totalOre;
                    }
                    else
                    {
                        cout << "Nothing was found\n\n";
                    }
                    break;        
               
               case 2:
                    if (choice == 2 && totalOre > 0)
                    {
                               cout << "Total Ore: " << totalOre << "\n\n";
                    }
                    else
                    {
                        cout << "You have no ore. Trying to mine...\n\n";
                    }
                    break;
                                   
               default:
               cout << "Exiting...\n";
        }
    } while (choice == 1 || choice == 2);
    
    cin.clear();
    cin.ignore(255, '\n');
    cin.get();
    return 0;
}

Initial URL


Initial Description
Just a simple console program that mines ore or displays it depending on the users choice. This is the second version of the script using case and if statements. v0.0.1 had some issues. I\'m sure there is a better way to implement this, but I\'m still leaning .

Initial Title
Generic Miner - v0.0.2

Initial Tags


Initial Language
C++