Advertisement
hugol

Untitled

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