I've completed my first program/game. Any thing I could do to make it more efficient? I don't want to start developing any bad habits now. Like, should I use an array of strings instead of a switch? Also, any idea's on a program I could write that would be harder than this I would be really thankful for!
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <windows.h>
#define CHOICES 7
#define TIME 10
void drinks();
int rand0toN(int n);
usingnamespace std;
int main(){
int p;
srand(time(NULL)); // Set seed for Randomizing
//Prompt user with instructions and ask for players
cout << "~~~~~Welcome to the drinking game!!~~~~~\n\n";
cout << "Instructions:\n";
cout << "1: Enter number of players and assign each player a number.\n";
cout << "2: Each player has 10 seconds to complete drinking task. Start when\n";
cout << "it says go.\n";
cout << "3: If can/glass is not on the table before time is up\n";
cout << "players loses and is out of the game.\n\n";
cout << "Enter number of players(0 to exit): ";
cin >> p;
//Cycle through number of players, 0 to exit.
while(p!=0){
for(int i=1;i<=p;i++){
cout << "Player " << i << endl;
drinks();
cout << " Ready... Set...\n";
Sleep(6000);//Give them some time to read the challenge.
cout << "GOOOOO!\n";
//Count down timer.
for(int s=1;s<=TIME;s++){
Sleep(1000);
cout << s << endl;
}
cout << "Time's up!!!\n\n";
Sleep(1000);
}
cout << "Enter new number of players(0 to exit): ";
cin >> p;
}
system("pause");
return 0;
}
//This function makes a random choice for the player.
void drinks(){
int c = rand0toN(CHOICES);
switch(c){
case 1:
cout << "Take 1 gulps!";
break;
case 2:
cout << "Take 2 gulps!";
break;
case 3:
cout << "Take 3 gulps, or 1 shot!";
break;
case 4:
cout << "Slam your drink!";
break;
case 5:
cout << "Choose someone for 2 gulps! Pick quick!";
break;
case 6:
cout << "Choose someone for 3 gulps or 1 shot! Pick quick!";
break;
case 7:
cout << "Take shots until the clock runs out!(Or slam your drink)";
break;
default:
break;
}
}
//Random number selection for function "drinks"
int rand0toN(int n){
return (rand()%n)+1;
}
- Using "system("pause")" is sort of a bad habit, I'm sure you've heard this from us before. I want to emphasize that this is made worse by your inclusion of windows.h which offers far cooler solutions for holding the window open.
- You seem to be including Windows.h soley for the purpose of using the "Sleep()" funciton even though you are already including the ctime header to seed "srand()". Seriously check out MSDN, this is a waste this header files potential
As far as a next project, why not build off of this one:
- Create an object that will keep track of each player. Be sure to include their Name, Weight and currently estimated BAC. Note that this calculation is dependent on the rate of consumption so you should read up on that ctime header. If you REALLY want to get fancy put in an alarm to warn people when they should stop playing and make the game automatically declare a winner and quit.
- In order to calculate their potential BAC you need to know the Alchohal content AND volume of what ever you are drinking so there's another object.
- Use the information I mentioned above to define what a "shot" would be for each player depending on what they are drinking.