Advertisement
hugol

Untitled

Dec 8th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.33 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 0
  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.     int wiersz; /* represents suit value of card */
  51.     int kolumna; /* represents face value of card */
  52.     int karta; /* loop counter */
  53.  
  54.     /* for each of the 52 cards, choose a slot of the deck randomly */
  55.     for ( karta = 1; karta <= 52; karta++ )
  56.     {
  57.         do /* choose a new random location until unoccupied slot is found */
  58.         {
  59.             wiersz = rand() % 4; /* randomly select the row */
  60.             kolumna = rand() % 13; /* randomly select the column */
  61.         } while( talia[ wiersz ][ kolumna ] != 0 ); /* end do...while */
  62.         /* place card number in chosen slot of deck */
  63.         talia[ wiersz ][ kolumna ] = karta;
  64.     } /* end for */
  65. } /* end function shuffle */
  66.  
  67.  
  68. void rozdanieKart(int koloryWRece[], int figuryWRece[],char *kolory[], char *figury[], int talia[][FIGURY])
  69. {
  70.     int kolorIndex, figuraIndex;
  71.     kolorIndex = rand() % 4;
  72.     figuraIndex = rand() %13;
  73.     while( talia[kolorIndex][figuraIndex] == TAKEN )
  74.     {
  75.         kolorIndex = rand() % 4;
  76.         figuraIndex = rand() %13;
  77.     }
  78.  
  79.     talia[kolorIndex][figuraIndex] = TAKEN;
  80.     figuryWRece[figuraIndex]++;
  81.     koloryWRece[kolorIndex]++;
  82.     printf("%s %s \n", figury[figuraIndex], kolory[kolorIndex]);
  83.  
  84. }
  85.  
  86. void rozdanieReki(int koloryWRece[], int figuryWRece[], char *kolory[], char *figury[], int talia[][FIGURY])
  87. {
  88.     int i;
  89.     for(i = 0; i < 5; i++)
  90.     {
  91.         rozdanieKart(koloryWRece, figuryWRece, kolory, figury, talia);
  92.     }
  93.  
  94.     cout << "\n";
  95. }
  96. void sprawdzanieReki(int koloryWRece[], int figuryWRece[])
  97. {
  98.     int kolejne_figury = 0;
  99.     int rank, kolor;
  100.     Strit = FALSE;
  101.     Kolor = FALSE;
  102.     Kareta = FALSE;
  103.     Trojka = FALSE;
  104.     Para = 0;
  105.  
  106.     for (kolor = 0; kolor < KOLORY; kolor++)
  107.     {
  108.         if (koloryWRece[kolor] == 5)
  109.             Kolor = true;
  110.     }
  111.  
  112.     rank = 0;
  113.     while (figuryWRece[rank] == 0)
  114.     {
  115.         rank++;
  116.     }
  117.  
  118.     for (; rank < FIGURY && figuryWRece[rank]; rank++)
  119.     {
  120.         kolejne_figury++;
  121.     }
  122.  
  123.     if (kolejne_figury == 5)
  124.     {
  125.         Strit = TRUE;
  126.         return;
  127.     }
  128.  
  129.     for (rank = 0; rank < NUM_RANKS; rank++)
  130.     {
  131.         if (figuryWRece[rank] == 4)
  132.             Kareta = TRUE;
  133.  
  134.         if (figuryWRece[rank] == 3)
  135.             Trojka = TRUE;
  136.  
  137.         if (figuryWRece[rank] == 2)
  138.             Para++;
  139.  
  140.     }
  141.  
  142. }
  143.  
  144.  
  145.  
  146.  
  147. void wyswietlanieWynikow(int koloryWRece[], int figuryWRece[], int *wartosc)
  148. {
  149.  
  150.     sprawdzanieReki(koloryWRece, figuryWRece);
  151.  
  152.  
  153.  
  154.     if (Strit && Kolor)
  155.     {
  156.         cout << "POKER\n\n";
  157.         *wartosc = 9;
  158.     }
  159.  
  160.     else if (Kareta)
  161.     {      
  162.         cout << "KARETA\n\n";
  163.         *wartosc = 8;
  164.     }
  165.  
  166.     else if (Trojka && Para == 1)
  167.     {
  168.         cout << "FUL\n\n";
  169.         *wartosc = 7;
  170.     }                
  171.  
  172.     else if (Kolor)
  173.     {      
  174.         cout << "KOLOR\n\n";
  175.         *wartosc = 6;
  176.     }
  177.  
  178.     else if (Strit)
  179.     {    
  180.         cout << "STRIT\n\n";
  181.         *wartosc = 5;
  182.     }
  183.  
  184.     else if (Trojka)
  185.     {      
  186.         cout << "Trojka\n\n";
  187.         *wartosc = 4;
  188.     }
  189.  
  190.     else if (Para == 2)
  191.     {
  192.             cout << "DWIE PARY\n\n";
  193.             *wartosc = 3;
  194.     }
  195.  
  196.     else if (Para == 1)
  197.     {  
  198.         cout << "PARA\n\n";
  199.         *wartosc = 2;
  200.     }
  201.  
  202.     else                  
  203.         cout << "WYSOKA KARTA\n\n";
  204.  
  205.     *wartosc = 1;
  206. }
  207.  
  208. int Gra()
  209. {
  210.     char *kolory[4] = { "Serce", "Karo", "Pik", "Trefl" };
  211.     char *figury[13] = { "2", "3", "4", "5", "6", "7",
  212.         "8", "9", "10", "Walet", "Dama", "Krol", "As" };
  213.     int talia[4][13] = { { AVAILABLE } };
  214.  
  215.     int koloryWRece[4] = {0} ;
  216.     int figuryWRece[13] = {0};
  217.  
  218.     int koloryWRece2[4] = {0};
  219.     int figuryWRece2[13] = {0};
  220.  
  221.     int punkty1gracza;
  222.     int punkty2gracza;
  223.  
  224.     srand( time( NULL ) );
  225.  
  226.     tasowanie( talia );
  227.  
  228.     cout << "Gracz wylosowal:\n\n";
  229.     rozdanieReki(koloryWRece, figuryWRece, kolory, figury, talia);
  230.     sprawdzanieReki(koloryWRece, figuryWRece);
  231.  
  232.     wyswietlanieWynikow(koloryWRece, figuryWRece, &punkty1gracza);
  233.  
  234.     cout << "Komputer wylosowal:\n\n";
  235.  
  236.     rozdanieReki(koloryWRece2, figuryWRece2, kolory, figury, talia);
  237.  
  238.     sprawdzanieReki(koloryWRece2, figuryWRece2);
  239.     wyswietlanieWynikow(koloryWRece2, figuryWRece2, &punkty2gracza);
  240.  
  241.     if(punkty1gracza > punkty2gracza) {
  242.         cout << "Gratulacje, wygrales to rozdanie!\n\n" ;
  243.     }else if(punkty1gracza< punkty2gracza) {
  244.         cout << "Niestety przegrales ta runde...\n\n" ;
  245.     }else
  246.     {
  247.     cout << "Remis\n\n";
  248.     }
  249.  
  250.     cout << "\n";
  251.     system("pause");
  252.  
  253.     return 0;
  254. }
  255.  
  256. int main()
  257. {
  258.     int wybor;
  259.     while(1)
  260.     {
  261.         menu();
  262.         cin>>wybor;
  263.         system("cls");
  264.         switch (wybor)
  265.         {
  266.         case 1:
  267.             Gra();
  268.             system("PAUSE");
  269.             break;
  270.         case 2:
  271.             if( !wyswietlZawartoscPliku( "Zasady.txt" ) )
  272.                 cout << "Nie udalo sie otworzyc pliku o podanej nazwie." << endl;
  273.             cout << endl << endl;
  274.             system("PAUSE");
  275.             break;
  276.         case 3:
  277.             return 0;
  278.             break;
  279.         default:
  280.             cout << "Zla opcja";
  281.             cout << endl << endl;
  282.             system("PAUSE");
  283.             break;
  284.         }
  285.     }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement