| CODE |
// Kyle Junes // 1/25/07 // Dice Game #include <iostream> #include <time.h> #include <stdlib.h> #include <iomanip> #include <fstream> #include <string> using namespace std; void cards(int flipped[14]); void getHighScores(); void printHighScores(); void sortHighScores(int inScore, string inName); void saveHighScores(); int score[10]; string name[10]; int main() { // Random number gen srand(time(0)); getHighScores(); int play=1, roll[2], card[2], ctotal=78; int choice[14]; string user_name; while(1) { for(int n=0; n<14; n++) { choice[n]=n; } // Intro cout<<"Dice Game"<<endl; cout<<"Do you want to play the dice game?"<<endl; cout<<"Play - 1"<<endl; cout<<"Don't play - Any other number"<<endl; cin>>play; if(play!=1) { break; } // Directions cout<<"\n\tTwo dice will be rolled."<<endl; cout<<"\tYou will flip over one or more cards depending on the total of the dice."<<endl; cout<<"\tFor example: If you roll double 6's, you could choose card 12,"<<endl; cout<<"\tcards 11 and 1, cards 10 and 2, 9 and 3, etc. so that the sum of the"<<endl; cout<<"\tcards chosen equals the total of your roll."<<endl<<endl; cout<<"\tOnce a card is flipped, it cannot be reused."<<endl; cout<<"\tIf you roll and the cards remaining can't be used for your roll,"<<endl; cout<<"\tenter 13 and 0 as your card choices."<<endl; cout<<"\tYou can also do this if you just want to quit."<<endl<<endl; cout<<"\tThe cards left will add up to be your score."<<endl; cout<<"\tYou want as low of a score as possible."<<endl<<endl; cout<<"Please enter your name: "; cin.ignore(80,'\n'); getline(cin, user_name); printHighScores(); int turns=0; // One round of the game bool goodsum=true; while (goodsum) { // Loop to display roll for(int a=0; a<2; a++) { roll[a]=1+rand()%6; for(int b=0; b<a; b++) { if(roll[a]==roll[b]) { roll[a]=1+rand()%6; b=-1; } } } turns++; cout<<"Results of roll #"<<turns<<"!"<<endl; // Check to see if there are possible choices or if game is over. goodsum=false; for(int n=0; n<14; n++) { for(int m=0; m<n; m++) { if(roll[0]+roll[1]==choice[n]+choice[m]) { goodsum=true; } } } // Loop to pick cards bool okay=false; while(!okay && goodsum) { cout<<"\n\tYour dice are: "; for(int n=0; n<2; n++) { cout<<roll[n]<<" "; } cout<<"\n\tFor a sum of: "; int sum=roll[0]+roll[1]; cout<<sum<<endl; // Displays cards cards(choice); okay=true; cout<<"\nChoose your first card."<<endl; cin>>card[0]; cout<<"\nChoose your second card."<<endl; cin>>card[1]; // Outside choice range for(int n=0; n<2; n++) { if(card[n]<0 || card[n]>13) { cout<<"Choose again. 0 to 13 only."<<endl; okay=false; } } // Cards picked don't match roll if(card[0]+card[1] != roll[0]+roll[1]) { cout<<"Choose again. The cards you picked aren't equal to your roll."<<endl; okay=false; } // Same card used twice if(card[0]==card[1]) { cout<<"Choose again. You can't use the same card twice."<<endl; okay=false; } // Card has been choosen for(int n=0; n<2; n++) { if(card[n]!=0) { if(choice[card[n]]==0) { cout<<"Choose again, card has been chosen before."<<endl; okay=false; } } } } // Displays score int user_score=ctotal-(card[0]+card[1]); cout<<"Your score is "<<user_score<<endl<<endl; ctotal=user_score; // Blank out choice if(goodsum) { for(int n=0; n<2; n++) { choice[card[n]]=0; } } if(!goodsum) { cout<<"\n\tYour dice are: "; for(int n=0; n<2; n++) { cout<<roll[n]<<" "; } cout<<"\n\tFor a sum of: "; int sum=roll[0]+roll[1]; cout<<sum<<endl; // Displays cards cards(choice); cout<<"No possible choices match your roll."<<endl; cout<<"Thank you for playing."<<endl<<endl; // Displays score int user_score=ctotal-(card[0]+card[1]); cout<<"\tYour score is "<<user_score<<endl<<endl; ctotal=user_score; sortHighScores(user_score, user_name); printHighScores(); } } } saveHighScores(); return (0); } void cards(int flipped[12]) { cout<<"____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____"<<endl; cout<<"| | | | | | | | | | | | | | | | | | | | | | | |"<<endl; for (int n=1; n<13; n++) { cout<<"|"<<setw(2)<<flipped[n]<<"| "; } cout<<endl; cout<<"|__| |__| |__| |__| |__| |__| |__| |__| |__| |__| |__| |__|"<<endl<<endl; } void getHighScores() { // Declare identifiers for input/output files ifstream best_scores_in; // Opens file that holds best scores best_scores_in.open("High_Scores.dat", ios::in); if (best_scores_in) { for (int w=0; w<10; w++) { best_scores_in>>score[w]; best_scores_in.ignore(80,'\n'); getline(best_scores_in, name[w]); } best_scores_in.close(); } else { for (int w=0; w<10; w++) { score[w]=99; name[w]="Maybe you!"; } best_scores_in.close(); cout<<"An error occured while opening the file to get best scores."<<endl; } } void printHighScores() { cout.setf(ios::left); cout<<"TOP SCORES"<<endl; cout<<setw(15)<<"Name"<<setw(6)<<"Score"<<endl; cout<<setw(15)<<"____"<<setw(6)<<"_____"<<endl; for (int w=0; w<10; w++) { cout<<setw(15)<<name[w]<<setw(6)<<score[w]<<endl; } cout<<endl; } void sortHighScores(int inScore, string inName) { // Variable and loop for sorting scores array string tempName; int tempScore; for (int w=0; w<10; w++) { if (inScore<score[w]) { tempScore=score[w]; tempName=name[w]; score[w]=inScore; name[w]=inName; inScore=tempScore; inName=tempName; } } } void saveHighScores() { // Declare identifiers for input/output files ofstream best_scores; // Opens file for saving number of guesses in this game best_scores.open("High_Scores.dat", ios::out); if (best_scores) { for (int w=0; w<10; w++) { best_scores<<score[w]<<endl; best_scores<<name[w]<<endl; } best_scores.close(); } else { cout<<"An error occured while opening the file to save best scores."<<endl; } } |