Advertisement
hugol

Untitled

Dec 8th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.97 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <fstream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <iostream>
  7. #include <string>
  8.  
  9. #define KOLORY 4
  10. #define FIGURY 13
  11. #define AVAILABLE 134
  12. #define TAKEN 1
  13. #define TRUE 1
  14. #define FALSE 0
  15. #define NUM_RANKS 13
  16.  
  17. /* external variables */
  18. using namespace std;
  19.  
  20. int Strit, Kolor, Kareta, Trojka;
  21. int Para = 0;
  22.  
  23. void menu()
  24. {
  25.     system("cls");
  26.     cout << "Menu"<< endl<<endl;
  27.     cout << "1. Rozpocznij gre"<<endl;
  28.     cout << "2. Zasady gry"<<endl;
  29.     cout << "3. Zakoncz gre" << endl << endl;
  30.  
  31. }
  32.  
  33. bool wyswietlZawartoscPliku( string Zasady )
  34. {
  35.     ifstream plik;
  36.     plik.open( Zasady.c_str() );
  37.     if( !plik.good() )
  38.         return false;
  39.  
  40.     string wiersz;
  41.     while( getline( plik, wiersz ) )
  42.         cout << wiersz << endl;
  43.  
  44.     plik.close();
  45.     return true;
  46. }
  47.  
  48. void tasowanie( int talia[][ 13 ] )
  49. {
  50.  
  51.     int wiersz; /* represents suit value of card */
  52.  
  53.     int kolumna; /* represents face value of card */
  54.  
  55.     int karta; /* loop counter */
  56.  
  57.  
  58.  
  59.     /* for each of the 52 cards, choose a slot of the deck randomly */
  60.  
  61.     for ( karta = 1; karta <= 52; karta++ )
  62.  
  63.     {
  64.  
  65.         do /* choose a new random location until unoccupied slot is found */
  66.  
  67.         {
  68.  
  69.             wiersz = rand() % 4; /* randomly select the row */
  70.  
  71.             kolumna = rand() % 13; /* randomly select the column */
  72.  
  73.         } while( talia[ wiersz ][ kolumna ] != 0 ); /* end do...while */
  74.  
  75.  
  76.  
  77.         /* place card number in chosen slot of deck */
  78.  
  79.         talia[ wiersz ][ kolumna ] = karta;
  80.  
  81.     } /* end for */
  82.  
  83. } /* end function shuffle */
  84.  
  85.  
  86. void rozdanieKart(int koloryWRece[], int figuryWRece[],char *kolory[], char *figury[], int talia[][FIGURY])
  87. {
  88.  
  89.     int kolorIndex, figuraIndex;
  90.  
  91.  
  92.  
  93.     kolorIndex = rand() % 4;
  94.  
  95.     figuraIndex = rand() %13;
  96.  
  97.  
  98.  
  99.  
  100.  
  101.     while( talia[kolorIndex][figuraIndex] == TAKEN ){
  102.  
  103.         kolorIndex = rand() % 4;
  104.  
  105.         figuraIndex = rand() %13;
  106.  
  107.     }
  108.  
  109.     talia[kolorIndex][figuraIndex] = TAKEN;
  110.  
  111.  
  112.  
  113.     figuryWRece[figuraIndex]++;
  114.  
  115.     koloryWRece[kolorIndex]++;
  116.  
  117.  
  118.  
  119.     printf("%s %s \n", figury[figuraIndex], kolory[kolorIndex]);
  120.  
  121. }
  122.  
  123. void rozdanieReki(int koloryWRece[], int figuryWRece[], char *kolory[], char *figury[], int talia[][FIGURY])
  124. {
  125.     int i;
  126.  
  127.     for(i = 0; i < 5; i++)
  128.  
  129.         rozdanieKart(koloryWRece, figuryWRece, kolory, figury, talia);
  130.  
  131.  
  132.  
  133.     cout << "\n";
  134.  
  135. }
  136. void sprawdzanieReki(int koloryWRece[], int figuryWRece[])
  137. {
  138.  
  139.     int kolejne_figury = 0;
  140.  
  141.     int rank, kolor;
  142.  
  143.  
  144.  
  145.     Strit = FALSE;
  146.  
  147.     Kolor = FALSE;
  148.  
  149.     Kareta = FALSE;
  150.  
  151.     Trojka = FALSE;
  152.  
  153.     Para = 0;
  154.  
  155.  
  156.  
  157.     for (kolor = 0; kolor < KOLORY; kolor++)
  158.  
  159.         if (koloryWRece[kolor] == 5)
  160.  
  161.             Kolor = TRUE;
  162.  
  163.  
  164.  
  165.     rank = 0;
  166.  
  167.     while (figuryWRece[rank] == 0)
  168.  
  169.         rank++;
  170.  
  171.  
  172.  
  173.     for (; rank < FIGURY && figuryWRece[rank]; rank++)
  174.  
  175.         kolejne_figury++;
  176.  
  177.  
  178.  
  179.     if (kolejne_figury == 5) {
  180.  
  181.         Strit = TRUE;
  182.  
  183.         return;
  184.  
  185.     }
  186.  
  187.  
  188.  
  189.     for (rank = 0; rank < NUM_RANKS; rank++) {
  190.  
  191.         if (figuryWRece[rank] == 4)
  192.  
  193.             Kareta = TRUE;
  194.  
  195.         if (figuryWRece[rank] == 3)
  196.  
  197.             Trojka = TRUE;
  198.  
  199.         if (figuryWRece[rank] == 2)
  200.  
  201.             Para++;
  202.  
  203.     }
  204.  
  205.  
  206.  
  207. }
  208.  
  209.  
  210.  
  211.  
  212. void wyswietlanieWynikow(int koloryWRece[], int figuryWRece[], int *wartosc)
  213. {
  214.  
  215.     sprawdzanieReki(koloryWRece, figuryWRece);
  216.  
  217.  
  218.  
  219.     if (Strit && Kolor) {
  220.  
  221.         cout << "POKER\n\n";
  222.  
  223.         *wartosc = 9;
  224.  
  225.     }
  226.  
  227.     else if (Kareta) {      
  228.  
  229.         cout << "KARETA\n\n";
  230.  
  231.         *wartosc = 8;
  232.  
  233.     }
  234.  
  235.     else if (Trojka && Para == 1) {
  236.  
  237.         cout << "FUL\n\n";
  238.  
  239.         *wartosc = 7;
  240.  
  241.     }                
  242.  
  243.     else if (Kolor) {      
  244.  
  245.         cout << "KOLOR\n\n";
  246.  
  247.         *wartosc = 6;
  248.  
  249.     }
  250.  
  251.     else if (Strit) {    
  252.  
  253.         cout << "STRIT\n\n";
  254.  
  255.         *wartosc = 5;
  256.  
  257.     }
  258.  
  259.     else if (Trojka) {      
  260.  
  261.         cout << "Trojka\n\n";
  262.  
  263.         *wartosc = 4;
  264.  
  265.     }
  266.  
  267.     else if (
  268.         Para == 2) {
  269.  
  270.             cout << "DWIE PARY\n\n";
  271.  
  272.             *wartosc = 3;
  273.  
  274.     }
  275.  
  276.     else if (Para == 1) {  
  277.  
  278.         cout << "PARA\n\n";
  279.  
  280.         *wartosc = 2;
  281.  
  282.     }
  283.  
  284.     else                  
  285.  
  286.         cout << "WYSOKA KARTA\n\n";
  287.  
  288.     *wartosc = 1;
  289.  
  290. }
  291.  
  292. int Gra()
  293. {
  294.     char *kolory[4] = { "Serce", "Karo", "Pik", "Trefl" };
  295.     char *figury[13] = { "2", "3", "4", "5", "6", "7",
  296.         "8", "9", "10", "Walet", "Dama", "Krol", "As" };
  297.     int talia[4][13] = { { AVAILABLE } };
  298.  
  299.     int koloryWRece[4] = {0} ;
  300.     int figuryWRece[13] = {0};
  301.  
  302.     int koloryWRece2[4] = {0};
  303.     int figuryWRece2[13] = {0};
  304.  
  305.     int punkty1gracza;
  306.     int punkty2gracza;
  307.  
  308.     srand( time( NULL ) );
  309.  
  310.     /* shuffle the deck */
  311.  
  312.     tasowanie( talia );
  313.  
  314.     cout << "Gracz wylosowal:\n\n";
  315.     rozdanieReki(koloryWRece, figuryWRece, kolory, figury, talia);
  316.     sprawdzanieReki(koloryWRece, figuryWRece);
  317.  
  318.     wyswietlanieWynikow(koloryWRece, figuryWRece, &punkty1gracza);
  319.  
  320.     cout << "Komputer wylosowal:\n\n";
  321.  
  322.     rozdanieReki(koloryWRece2, figuryWRece2, kolory, figury, talia);
  323.  
  324.     sprawdzanieReki(koloryWRece2, figuryWRece2);
  325.     wyswietlanieWynikow(koloryWRece2, figuryWRece2, &punkty2gracza);
  326.  
  327.     if(punkty1gracza > punkty2gracza) {
  328.  
  329.         cout << "Gratulacje, wygrales to rozdanie!\n\n" ;
  330.  
  331.     }
  332.  
  333.     else if(punkty1gracza< punkty2gracza) {
  334.  
  335.         cout << "Niestety przegrales ta runde...\n\n" ;
  336.     }
  337.  
  338.     else
  339.  
  340.         cout << "Remis\n\n";
  341.     cout << "\n";
  342.     system("pause");
  343.  
  344.  
  345.  
  346.     void tasowanie( int talia[][ 13 ] );
  347.  
  348.     void rozdanieReki(int koloryWRece[], int figuryWRece[], char *kolory[], char *figury[], int talia[][FIGURY]);
  349.  
  350.     void rozdanieKart(int koloryWRece[], int figuryWRece[], char *kolory[], char *figury[], int talia[][FIGURY]);
  351.  
  352.     void sprawdzanieReki(int koloryWRece[], int figuryWRece[]);
  353.  
  354.  
  355.     /* assign value to hand and print results */
  356.  
  357.     void wyswietlanieWynikow(int koloryWRece[], int figuryWRece[], int *wartosc);
  358.     return 0;
  359. }
  360.  
  361. int main()
  362. {
  363.     int wybor;
  364.     while(1)
  365.     {
  366.         menu();
  367.         cin>>wybor;
  368.         system("cls");
  369.  
  370.  
  371.         switch (wybor)
  372.         {
  373.         case 1:
  374.  
  375.             Gra();
  376.             system("PAUSE");
  377.             break;
  378.         case 2:
  379.             if( !wyswietlZawartoscPliku( "Zasady.txt" ) )
  380.                 cout << "Nie udalo sie otworzyc pliku o podanej nazwie." << endl;
  381.             cout << endl << endl;
  382.             system("PAUSE");
  383.             break;
  384.         case 3:
  385.             return 0;
  386.             break;
  387.         default:
  388.             cout << "Zla opcja";
  389.             cout << endl << endl;
  390.             system("PAUSE");
  391.             break;
  392.         }
  393.     }
  394. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement