Advertisement
hugol

Untitled

Dec 8th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.32 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], bool AI = false)
  235. {
  236.     int ile_odrzucic = 5;
  237.     if (!AI)
  238.         cin >> ile_odrzucic;
  239.     int kartyOdrzucone[5][2];
  240.     for (int i=0; i< ile_odrzucic; i++)
  241.     {
  242.         int co_odrzucic = rand()%5;
  243.         if (!AI)
  244.         {
  245.             cin >> co_odrzucic;
  246.             co_odrzucic--;
  247.         }
  248.         kartyOdrzucone[i][0] = kartyWRece[co_odrzucic][0];
  249.         kartyOdrzucone[i][1] = kartyWRece[co_odrzucic][1];
  250.         rozdanieKart(koloryWRece, figuryWRece, kartyWRece, kolory, figury, talia, co_odrzucic);
  251.     }
  252.  
  253.     /*talia[kolorIndex][figuraIndex] = TAKEN;
  254.     figuryWRece[figuraIndex]++;
  255.     koloryWRece[kolorIndex]++;
  256.     kartyWRece[nrkarty][0] = figuraIndex;
  257.     kartyWRece[nrkarty][1] = kolorIndex;
  258.     */
  259.     // oddawanie karty do talli
  260.     for (int i=0; i< ile_odrzucic; i++)
  261.     {
  262.         talia[kartyOdrzucone[i][1]][kartyOdrzucone[i][0]] = 0;
  263.         figuryWRece[kartyOdrzucone[i][0]]--;
  264.         koloryWRece[kartyOdrzucone[i][1]]--;
  265.     }
  266.     return true;
  267. }
  268.  
  269. int Gra()
  270. {
  271.     char *kolory[4] = { "Serce", "Karo", "Pik", "Trefl" };
  272.     char *figury[13] = { "2", "3", "4", "5", "6", "7",
  273.         "8", "9", "10", "Walet", "Dama", "Krol", "As" };
  274.     int talia[4][13] = { { 0 } };
  275.  
  276.     int koloryWRece[4] = {0} ;
  277.     int figuryWRece[13] = {0};
  278.     int kartyWRece[5][2] = {0};
  279.  
  280.     int koloryWRece2[4] = {0};
  281.     int figuryWRece2[13] = {0};
  282.     int kartyWRece2[5][2] = {0};
  283.  
  284.     int punkty1gracza;
  285.     int punkty2gracza;
  286.  
  287.     srand( time( NULL ) );
  288.  
  289.     tasowanie( talia );
  290.  
  291.     cout << "Twoja talia:\n\n";
  292.     rozdanieReki(koloryWRece, figuryWRece, kartyWRece, kolory, figury, talia, 5);
  293.     wyswietlenieReki(koloryWRece, figuryWRece, kartyWRece, kolory, figury, talia, 5);
  294.     sprawdzanieReki(koloryWRece, figuryWRece);
  295.     wyswietlanieWynikow(koloryWRece, figuryWRece, &punkty1gracza);
  296.  
  297.     cout << "Komputera talia:\n\n";
  298.  
  299.     rozdanieReki(koloryWRece2, figuryWRece2, kartyWRece2, kolory, figury, talia, 5);
  300.     wyswietlenieReki(koloryWRece2, figuryWRece2, kartyWRece2, kolory, figury, talia, 5);
  301.     sprawdzanieReki(koloryWRece2, figuryWRece2);
  302.     wyswietlanieWynikow(koloryWRece2, figuryWRece2, &punkty2gracza);
  303.  
  304.     cout << "Podaj ile (0-5), a nastepnie jakie karty wymienic (1-5):\n";
  305.     OdrzucKarty(koloryWRece, figuryWRece, kartyWRece, kolory, figury, talia);
  306.     //system("cls");
  307.     cout << "\nTwoja 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.     if(punkty1gracza>=punkty2gracza)
  314.     {
  315.         // jezeli AI ma mniej lub tyle samo, pkt niz gracz, wymienia losowa ilosc kart;
  316.         OdrzucKarty(koloryWRece2, figuryWRece2, kartyWRece2, kolory, figury, talia, true);
  317.     }
  318.     wyswietlenieReki(koloryWRece2, figuryWRece2, kartyWRece2, kolory, figury, talia, 5);
  319.     sprawdzanieReki(koloryWRece2, figuryWRece2);
  320.     wyswietlanieWynikow(koloryWRece2, figuryWRece2, &punkty2gracza);
  321.  
  322.     if(punkty1gracza > punkty2gracza) {
  323.         cout << "Gratulacje, wygrales to rozdanie!\n\n" ;
  324.     }else if(punkty1gracza< punkty2gracza) {
  325.         cout << "Niestety przegrales ta runde...\n\n" ;
  326.     }else
  327.     {
  328.         cout << "Remis\n\n";
  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