Advertisement
Guest User

CardPredict

a guest
Aug 31st, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.95 KB | None | 0 0
  1. // ConsoleApplication7.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4.  
  5. #include <iostream>
  6.  
  7. #include <string>
  8. #include <stdlib.h>
  9. #include <fstream>
  10. #include <sstream>
  11. using namespace std;
  12.  
  13. const int NoOfRecentScores=3;
  14.  
  15. struct TCard
  16. {
  17.     int suit;
  18.     int rank;
  19. };
  20.  
  21. struct TRecentScore{
  22.     string Name;
  23.     int Score;
  24. };
  25.  
  26. string GetRank(int RankNo)
  27. {
  28.     string Rank=" ";
  29.    
  30.     switch (RankNo) {
  31.            
  32.         case 1:
  33.             Rank="Ace";
  34.             break;
  35.            
  36.         case 2:
  37.             Rank="Two";
  38.             break;
  39.            
  40.         case 3:
  41.             Rank="Three";
  42.             break;
  43.            
  44.         case 4:
  45.             Rank="Four";
  46.             break;
  47.            
  48.         case 5:
  49.             Rank="Five";
  50.             break;
  51.            
  52.         case 6:
  53.             Rank="Six";
  54.             break;
  55.            
  56.         case 7:
  57.             Rank="Seven";
  58.             break;
  59.            
  60.         case 8:
  61.             Rank="Eight";
  62.             break;
  63.            
  64.         case 9:
  65.             Rank="Nine";
  66.             break;
  67.            
  68.         case 10:
  69.             Rank="Ten";
  70.             break;
  71.         case 11:
  72.             Rank="Jack";
  73.             break;
  74.            
  75.         case 12:
  76.             Rank="Queen";
  77.             break;
  78.            
  79.         case 13:
  80.             Rank="King";
  81.             break;
  82.            
  83.         default:
  84.             break;
  85.     }
  86.  
  87.     return Rank;
  88. }
  89.  
  90. string GetSuit(int SuitNo){
  91.     string suit=" ";
  92.    
  93.     switch(SuitNo){
  94.         case 1:
  95.             suit="Clubs";
  96.             break;
  97.         case 2:
  98.             suit="Diamonds";
  99.             break;
  100.         case 3:
  101.             suit="Hearts";
  102.             break;
  103.         case 4:
  104.             suit="Spades";
  105.             break;
  106.            
  107.         default:
  108.             break;
  109.     }
  110.     return suit;
  111. }
  112.  
  113. void DisplayMenu(){
  114.     cout<<endl;
  115.     cout<<"Main Menu"<<endl;
  116.     cout<<endl;
  117.     cout<<"1. Play game (with shuffle)"<<endl;
  118.     cout<<"2. Play game (without shuffle)"<<endl;
  119.     cout<<"3. Display recent scores"<<endl;
  120.     cout<<"4. Reset recent scores"<<endl;
  121.     cout<<endl;
  122.     cout<<"Select an option from the menu (or enter q to quit): ";
  123. }
  124.  
  125. char GetMenuChoice(){
  126.     char choice;
  127.     cin>>choice;
  128.     cout<<endl;
  129.     return choice;
  130. }
  131.  
  132. void LoadDeck(TCard Deck[]){
  133.  int count=1;
  134.    ifstream infile;
  135.    infile.open("deck.txt");
  136.    // check for error
  137.    if (infile.fail()){
  138.     cerr<<"Error Opening file"<<endl;
  139.     exit(1);
  140.    }
  141.    while(!infile.eof()){
  142.     infile>>Deck[count].suit;
  143.     infile>>Deck[count].rank;
  144.     count++;
  145.    }
  146.  
  147.    infile.close();
  148. }
  149.  
  150. void ShuffleDeck(TCard Deck[]){
  151.     int NoOfSwaps,Position1,Position2,NoOfSwapsSoFar;
  152.     TCard SwapSpace;
  153.     NoOfSwaps=1000;
  154.     for(NoOfSwapsSoFar=1;NoOfSwapsSoFar<=NoOfSwaps;NoOfSwaps++){
  155.         Position1=(rand()*52)+1;
  156.         Position2=(rand()*52)+1;
  157.         SwapSpace=Deck[Position1];
  158.         Deck[Position1]=Deck[Position2];
  159.         Deck[Position2]=SwapSpace;
  160.     }
  161. }
  162.  void DisplayCard(TCard ThisCard){
  163.     cout<<endl;
  164.     cout<<"Card is the "<<GetRank(ThisCard.rank)<< " of " << GetSuit(ThisCard.suit)<<endl;
  165.     cout<<endl;
  166.  }
  167.  
  168.  void GetCard(TCard ThisCard,TCard Deck[],int NoOfCardsTurnedOver){
  169.     int count;
  170.     ThisCard=Deck[1];
  171.     for(count=1;count<=(51-NoOfCardsTurnedOver);count++){
  172.         Deck[count]=Deck[count+1];
  173.      }
  174.      Deck[(52-NoOfCardsTurnedOver)].suit=0;
  175.      Deck[(52-NoOfCardsTurnedOver)].rank=0;
  176. }
  177. bool IsNextCardHigher(TCard LastCard,TCard NextCard){
  178.     bool higher;
  179.     higher=false;
  180.     if(NextCard.rank>LastCard.rank){
  181.         higher=true;
  182.     }
  183.     return higher;
  184. }
  185. string GetPlayerName(){
  186.     string PlayerName;
  187.     cout<<endl;
  188.     cout<<"Please enter your name: ";
  189.     getline(cin,PlayerName);
  190.     cout<<endl;
  191.     return PlayerName;
  192. }
  193. char GetChoiceFromUser(){
  194.     char choice;
  195.     cout<<"Do you think the next card will be higher than the last card(enter y or n): ";
  196.     cin>>choice;
  197.     return choice;
  198. }
  199.  
  200. void DisplayEndOfGameMessage(int Score)
  201. {
  202.     cout<<endl;
  203.     cout<<("Game Over!")<<endl;
  204.     cout<<"Your score was: "<<Score<<endl;
  205.     if(Score=51){
  206.         cout<<"WOW! You completed a perfect game.";
  207.     }
  208.     cout<<endl;
  209. }
  210. void DisplayCorrectGuessMessage(int Score){
  211.     cout<<endl;
  212.     cout<<"Well done! You guessed correctly"<<endl;
  213.     cout<<"Your score is now: "<<Score<<endl;
  214.     cout<<endl;
  215. }
  216. void ResetRecentScores(TRecentScore RecentScores[]){
  217.     for (int count=1;count<=NoOfRecentScores;count++){
  218.         RecentScores[count].Name="";
  219.         RecentScores[count].Score=0;
  220.     }
  221. }
  222. void DisplayRecentScores(TRecentScore RecentScores[]){
  223.     cout<<endl;
  224.     cout<<"Recent Scores: "<<endl;
  225.     cout<<endl;
  226.     for(int count=1;count<=NoOfRecentScores;count++){
  227.         cout<<RecentScores[count].Name<< " got a score of "<<RecentScores[count].Score<<endl;
  228.     }
  229.     cout<<endl;
  230.     cout<<"Press the Enter key to return to the main menu"<<endl;
  231.     cout<<endl;
  232.     //console.readline?
  233. }
  234. void UpdateRecentScores(TRecentScore RecentScores[],int Score){
  235.     string PlayerName;
  236.     int count=1;
  237.     bool FoundSpace;
  238.     PlayerName=GetPlayerName();
  239.     FoundSpace=false;
  240.     while((FoundSpace==false) && count<=NoOfRecentScores){
  241.         if(RecentScores[count].Name==""){
  242.             FoundSpace=true;
  243.         }
  244.         else{
  245.             count=count+1;
  246.         }
  247.        
  248.     }
  249.     if(FoundSpace=false){
  250.         for(count=1;count<=(NoOfRecentScores-1);count++)
  251.     {
  252.         RecentScores[count]=RecentScores[count+1];
  253.     }
  254.     count=NoOfRecentScores;
  255.     }
  256.     RecentScores[count].Name=PlayerName;
  257.     RecentScores[count].Score=Score;
  258. }
  259. void PlayGame(TCard Deck[],TRecentScore RecentScores[]){
  260.  
  261.  
  262.     int NoOfCardsTurnedOver;
  263.     bool GameOver;
  264.     TCard NextCard;
  265.     TCard LastCard;
  266.     bool higher;
  267.     char choice;
  268.     GameOver=false;
  269.     GetCard(LastCard,Deck,0);
  270.     DisplayCard(LastCard);
  271.     NoOfCardsTurnedOver=1;
  272.     while((NoOfCardsTurnedOver<52)&&(GameOver=false)){
  273.         GetCard(NextCard,Deck,NoOfCardsTurnedOver);
  274.         do{
  275.             choice=GetChoiceFromUser();
  276.         }while((choice!='y')&&(choice!='n'));
  277.    
  278.     DisplayCard(NextCard);
  279.     NoOfCardsTurnedOver=NoOfCardsTurnedOver+1;
  280.     higher=IsNextCardHigher(LastCard,NextCard);
  281.     if (((higher==true)&&(choice=='y'))||((higher=false)&&(choice=='n'))){
  282.         DisplayCorrectGuessMessage(NoOfCardsTurnedOver-1);
  283.         LastCard=NextCard;
  284.     }
  285.     else{
  286.         GameOver=true;
  287.     }
  288. }
  289. if(GameOver=true){
  290.     DisplayEndOfGameMessage(NoOfCardsTurnedOver - 2);
  291.     UpdateRecentScores(RecentScores, (NoOfCardsTurnedOver - 2));
  292. }
  293. else{
  294.     DisplayEndOfGameMessage(51);
  295.     UpdateRecentScores(RecentScores, 51);
  296. }
  297. }
  298.    
  299. int main(){
  300.     char choice;
  301.     TCard Deck[52];
  302.     TRecentScore RecentScores[NoOfRecentScores];
  303.     do{
  304.         DisplayMenu();
  305.         choice=GetMenuChoice();
  306.         switch(choice){
  307.         case '1':
  308.           LoadDeck(Deck);
  309.           ShuffleDeck(Deck);
  310.           PlayGame(Deck, RecentScores);
  311.         break;
  312.         case '2':
  313.           LoadDeck(Deck);
  314.           PlayGame(Deck, RecentScores);
  315.           break;
  316.          
  317.         case '3':
  318.           DisplayRecentScores(RecentScores);
  319.           break;
  320.         case '4':
  321.           ResetRecentScores(RecentScores);
  322.           break;
  323.            
  324.         }
  325.     }while(choice!='q');
  326.    
  327.     return 0;
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement