Advertisement
hugol

Untitled

Dec 8th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.04 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.  
  114. void wyswietlenieReki(int koloryWRece[], int figuryWRece[], int kartyWRece[][2], char *kolory[], char *figury[], int talia[][FIGURY], int ilosckart)
  115. {
  116.     int i;
  117.     for(i = 0; i < ilosckart; i++)
  118.     {
  119.         printf("%d. ",i+1);
  120.         //rozdanieKart(koloryWRece, figuryWRece, kartyWRece, kolory, figury, talia, i);
  121.         printf("%5s %s \n", figury[kartyWRece[i][0]], kolory[kartyWRece[i][1]]);
  122.     }
  123.  
  124.     cout << "\n";
  125. }
  126.  
  127. void sprawdzanieReki(int koloryWRece[], int figuryWRece[])
  128. {
  129.     int kolejne_figury = 0;
  130.     int rank, kolor;
  131.     Strit = FALSE;
  132.     Kolor = FALSE;
  133.     Kareta = FALSE;
  134.     Trojka = FALSE;
  135.     Para = 0;
  136.  
  137.     for (kolor = 0; kolor < KOLORY; kolor++)
  138.     {
  139.         if (koloryWRece[kolor] == 5)
  140.             Kolor = true;
  141.     }
  142.  
  143.     rank = 0;
  144.     while (figuryWRece[rank] == 0)
  145.     {
  146.         rank++;
  147.     }
  148.  
  149.     for (; rank < FIGURY && figuryWRece[rank]; rank++)
  150.     {
  151.         kolejne_figury++;
  152.     }
  153.  
  154.     if (kolejne_figury == 5)
  155.     {
  156.         Strit = TRUE;
  157.         return;
  158.     }
  159.  
  160.     for (rank = 0; rank < NUM_RANKS; rank++)
  161.     {
  162.         if (figuryWRece[rank] == 4)
  163.             Kareta = TRUE;
  164.  
  165.         if (figuryWRece[rank] == 3)
  166.             Trojka = TRUE;
  167.  
  168.         if (figuryWRece[rank] == 2)
  169.             Para++;
  170.  
  171.     }
  172.  
  173. }
  174.  
  175.  
  176.  
  177.  
  178. void wyswietlanieWynikow(int koloryWRece[], int figuryWRece[], int *wartosc)
  179. {
  180.     sprawdzanieReki(koloryWRece, figuryWRece);
  181.  
  182.     if (Strit && Kolor)
  183.     {
  184.         cout << "POKER\n\n";
  185.         *wartosc = 9;
  186.     }   else if (Kareta)
  187.     {      
  188.         cout << "KARETA\n\n";
  189.         *wartosc = 8;
  190.     }
  191.     else if (Trojka && Para == 1)
  192.     {
  193.         cout << "FUL\n\n";
  194.         *wartosc = 7;
  195.     }                
  196.  
  197.     else if (Kolor)
  198.     {      
  199.         cout << "KOLOR\n\n";
  200.         *wartosc = 6;
  201.     }
  202.  
  203.     else if (Strit)
  204.     {    
  205.         cout << "STRIT\n\n";
  206.         *wartosc = 5;
  207.     }
  208.  
  209.     else if (Trojka)
  210.     {      
  211.         cout << "Trojka\n\n";
  212.         *wartosc = 4;
  213.     }
  214.  
  215.     else if (Para == 2)
  216.     {
  217.             cout << "DWIE PARY\n\n";
  218.             *wartosc = 3;
  219.     }
  220.  
  221.     else if (Para == 1)
  222.     {  
  223.         cout << "PARA\n\n";
  224.         *wartosc = 2;
  225.     }
  226.  
  227.     else    
  228.     {
  229.         cout << "WYSOKA KARTA\n\n";
  230.         *wartosc = 1;
  231.     }
  232. }
  233.  
  234. bool OdrzucKarty(int koloryWRece[], int figuryWRece[], int kartyWRece[][2], char *kolory[], char *figury[], int talia[][FIGURY])
  235. {
  236.     int ile_odrzucic = 0;
  237.     cin >> ile_odrzucic;
  238.     int kartyOdrzucone[5][2];
  239.     for (int i=0; i< ile_odrzucic; i++)
  240.     {
  241.         int co_odrzucic;
  242.         cin >> co_odrzucic;
  243.         co_odrzucic--;
  244.         kartyOdrzucone[i][0] = kartyWRece[co_odrzucic][0];
  245.         kartyOdrzucone[i][1] = kartyWRece[co_odrzucic][1];
  246.         rozdanieKart(koloryWRece, figuryWRece, kartyWRece, kolory, figury, talia, co_odrzucic);
  247.     }
  248.  
  249.     /*talia[kolorIndex][figuraIndex] = TAKEN;
  250.     figuryWRece[figuraIndex]++;
  251.     koloryWRece[kolorIndex]++;
  252.     kartyWRece[nrkarty][0] = figuraIndex;
  253.     kartyWRece[nrkarty][1] = kolorIndex;
  254.     */
  255.     // oddawanie karty do talli
  256.     for (int i=0; i< ile_odrzucic; i++)
  257.     {
  258.         talia[kartyOdrzucone[i][1]][kartyOdrzucone[i][0]] = 0;
  259.         figuryWRece[kartyOdrzucone[i][0]]--;
  260.         koloryWRece[kartyOdrzucone[i][1]]--;
  261.     }
  262.     return true;
  263. }
  264.  
  265. int Gra()
  266. {
  267.     char *kolory[4] = { "Serce", "Karo", "Pik", "Trefl" };
  268.     char *figury[13] = { "2", "3", "4", "5", "6", "7",
  269.         "8", "9", "10", "Walet", "Dama", "Krol", "As" };
  270.     int talia[4][13] = { { 0 } };
  271.  
  272.     int koloryWRece[4] = {0} ;
  273.     int figuryWRece[13] = {0};
  274.     int kartyWRece[5][2] = {0};
  275.  
  276.     int koloryWRece2[4] = {0};
  277.     int figuryWRece2[13] = {0};
  278.     int kartyWRece2[5][2] = {0};
  279.  
  280.     int punkty1gracza;
  281.     int punkty2gracza;
  282.  
  283.     srand( time( NULL ) );
  284.  
  285.     tasowanie( talia );
  286.  
  287.     cout << "Twoja talia:\n\n";
  288.     rozdanieReki(koloryWRece, figuryWRece, kartyWRece, kolory, figury, talia, 5);
  289.     wyswietlenieReki(koloryWRece, figuryWRece, kartyWRece, kolory, figury, talia, 5);
  290.     //sprawdzanieReki(koloryWRece, figuryWRece);
  291.     //wyswietlanieWynikow(koloryWRece, figuryWRece, &punkty1gracza);
  292.  
  293.     cout << "Komputera talia:\n\n";
  294.  
  295.     rozdanieReki(koloryWRece2, figuryWRece2, kartyWRece2, kolory, figury, talia, 5);
  296.     wyswietlenieReki(koloryWRece, figuryWRece2, kartyWRece2, kolory, figury, talia, 5);
  297.     //sprawdzanieReki(koloryWRece2, figuryWRece2);
  298.     //wyswietlanieWynikow(koloryWRece2, figuryWRece2, &punkty2gracza);
  299.  
  300.     cout << "Podaj ile, a nastepnie jakie karty odrzucic:\n";
  301.     OdrzucKarty(koloryWRece, figuryWRece, kartyWRece, kolory, figury, talia);
  302.     system("cls");
  303.     cout << "Twoja talia\n\n";
  304.     wyswietlenieReki(koloryWRece, figuryWRece, kartyWRece, kolory, figury, talia, 5);
  305.     sprawdzanieReki(koloryWRece, figuryWRece);
  306.     wyswietlanieWynikow(koloryWRece, figuryWRece, &punkty1gracza);
  307.  
  308.     cout << "Komputera talia\n\n";
  309.     wyswietlenieReki(koloryWRece, figuryWRece2, kartyWRece2, kolory, figury, talia, 5);
  310.     sprawdzanieReki(koloryWRece2, figuryWRece2);
  311.     wyswietlanieWynikow(koloryWRece2, figuryWRece2, &punkty2gracza);
  312.  
  313.     if(punkty1gracza > punkty2gracza) {
  314.         cout << "Gratulacje, wygrales to rozdanie!\n\n" ;
  315.     }else if(punkty1gracza< punkty2gracza) {
  316.         cout << "Niestety przegrales ta runde...\n\n" ;
  317.     }else
  318.     {
  319.         cout << "Remis\n\n";
  320.     }
  321.     return 0;
  322. }
  323.  
  324. int main()
  325. {
  326.     int wybor;
  327.     while(1)
  328.     {
  329.         menu();
  330.         cin>>wybor;
  331.         system("cls");
  332.         switch (wybor)
  333.         {
  334.         case 1:
  335.             Gra();
  336.             system("PAUSE");
  337.             break;
  338.         case 2:
  339.             if( !wyswietlZawartoscPliku( "Zasady.txt" ) )
  340.                 cout << "Nie udalo sie otworzyc pliku o podanej nazwie." << endl;
  341.             cout << endl << endl;
  342.             system("PAUSE");
  343.             break;
  344.         case 3:
  345.             return 0;
  346.             break;
  347.         default:
  348.             cout << "Zla opcja";
  349.             cout << endl << endl;
  350.             system("PAUSE");
  351.             break;
  352.         }
  353.     }
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement