1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <sstream>
#include <windows.h>
void playervsplayer();
void ClearScreen();
int main()
{
srand(static_cast<unsigned int>(time(NULL)));
bool running = true;
int menuchoice, singleplayer, multiplayer;
std::string input;
do
{
while(true)
{
ClearScreen();
std::cout << ":: Enter an option ::" << "\n\n";
std::cout << "1. Single Player game" << "\n";
std::cout << "2. Two Player game" << "\n";
std::cout << "3. Automated game" << "\n";
std::cout << "4. Exit" << "\n\n";
getline(std::cin, input);
std::stringstream myStream(input);
if (myStream >> menuchoice)
break;
std::cout << "Please enter a valid number" << "\n";
std::cout << "Press Enter to continue..." << "\n";
std::cin.clear();
std::cin.ignore(10000, '\n');
}
switch(menuchoice)
{
case 2:
while(true)
{
ClearScreen();
std::cout << "Choose who would you like to play against:" << "\n";
std::cout << "1. Your friend" << "\n";
std::cout << "2. The computer" << "\n";
getline(std::cin, input);
std::stringstream myStream(input);
if (myStream >> multiplayer)
break;
std::cout << "Please enter a valid number" << "\n";
std::cout << "Press Enter to continue..." << "\n";
std::cin.clear();
std::cin.ignore(10000, '\n');
}
switch(multiplayer)
{
case 1: playervsplayer(); break;
default:
std::cout << "Please choose one of the two games" << "\n\n";
std::cin.clear();
std::cin.ignore(10000, '\n');
break;
}
break;
case 4: running = false; break;
default:
std::cout << "Please choose one of the 3 types of games" << "\n\n";
std::cin.clear();
std::cin.ignore(10000, '\n');
break;
}
} while (running);
return 0;
}
void playervsplayer()
{
std::string input, player1, player2;
unsigned short player1guess, player2guess, gumballs = 0, turn = 0;
gumballs = rand() % 1000 + 1;
ClearScreen();
std::cout << "Player 1, please enter your name: ";
getline(std::cin, player1);
std::cout << "\n" << "Welcome, " << player1 << "." << "\n\n";
std::cout << "Player 2, please enter your name: ";
getline(std::cin, player2);
while (player2 == player1)
{
std::cout << "Player 1 has already chosen this name!" << "\n";
std::cout << "Player 2, please enter your name: ";
getline(std::cin, player2);
}
std::cout << "\n" << "Welcome, " << player2 << "." << "\n\n";
std::cout << "In this game there is a gumball jar, which can hold up to 1000 gumballs." << "\n";
std::cout << "Each player will have their turns to guess the amount of gumballs." << "\n";
std::cout << "The player who gets it right first, wins!" << "\n\n";
std::cout << "Let's begin!" << "\n\n";
std::cout << "Press any key to continue..." << "\n";
std::cout << "(Now where's the 'any' key?)" << "\n\n\n";
std::cin.clear();
std::cin.ignore(10000, '\n');
std::cout << "Let's see who shall start... ";
turn = rand() % 2 + 1;
while (turn == 1)
{
std::cout << "It's " << player1 << "'s turn!" << "\n";
while(true)
{
std::cout << "Enter your guess: ";
getline(std::cin, input);
std::stringstream myStream(input);
if (myStream >> player1guess)
break;
std::cout << "Invalid number, please try again" << "\n";
}
if (player1guess > gumballs)
{
std::cout << "Nope, there are less!" << "\n\n";
turn = 2;
}
else if (player1guess < gumballs)
{
std::cout << "Nope, there are more!" << "\n\n";
turn = 2;
}
else
{
std::cout << "Hooray! You got it! You guessed the right amount of gumballs!!!" << "\n";
std::cout << "Congratulations, " << player1 << ", you win!";
std::cin.clear();
std::cin.ignore(10000, '\n');
}
}
while (turn == 2)
{
std::cout << "It's " << player2 << "'s turn!" << "\n";
while(true)
{
std::cout << "Enter your guess: ";
getline(std::cin, input);
std::stringstream myStream(input);
if (myStream >> player2guess)
break;
std::cout << "Invalid number, please try again" << "\n";
}
if (player2guess > gumballs)
{
std::cout << "Nope, there are less!" << "\n\n";
turn = 1;
}
else if (player2guess < gumballs)
{
std::cout << "Nope, there are more!" << "\n\n";
turn = 1;
}
else
{
std::cout << "Hooray! You got it! You guessed the right amount of gumballs!!!" << "\n";
std::cout << "Congratulations, " << player2 << ", you win!";
std::cin.clear();
std::cin.ignore(10000, '\n');
}
}
}
void ClearScreen()
{
//.. Let's save some space, shall we :P
}
|