Advertisement
Guest User

Untitled

a guest
May 27th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <time.h>
  5. #include <string>
  6. using namespace std;
  7. int numPlayer = 0; int numComp = 0;
  8. int numCount = 0 ; int numCountTwo = 0;
  9. char answer;
  10. void player(int &num1) { //generiranje broja random po referenci za igraca
  11.         num1 += rand() % 21 + 1;
  12.     cout << "\nPlayer has: " << num1 << " points";
  13. };
  14. void comp(int &num2) {//generiranje broja random po referenci za komp
  15.             num2+=rand() % 21 + 1;
  16.         cout << "\nComp has: "<<num2<<" points.";
  17. };
  18. int main() {
  19.    
  20.     cout << "WELLCOME TO 21\n\n\n LETS START, press y for play, n for not.\n";
  21.     cin>>answer;
  22.     if (answer == 'y') {
  23.         do//game loop
  24.         {
  25.             srand(static_cast<unsigned int>(time(NULL)));
  26.             cout << "\nHit or stand? ";
  27.             cin >> answer; 
  28.             if (answer == 'y') {
  29.                 player(numPlayer);
  30.                 if (numPlayer > numComp || numComp > 13) { //komp mora imat manji broj, ili manji od 13 da bi igrao
  31.                     comp(numComp);
  32.                 }
  33.             }
  34.             else if (answer='n') { //ako je odgovor n onda izadi
  35.                 if (numPlayer > numComp) {
  36.                     cout << "\nPlayer has won\n";
  37.                     break;
  38.                     return 0;
  39.                 }
  40.                 else {
  41.                     cout << "\nComp has won.\n";
  42.                     break;
  43.                     return 0;
  44.                 }
  45.             }
  46.         //uvjeti za prekid igre!
  47.         // ako je igrac na 21
  48.             if(numPlayer==21){
  49.             cout << "\nWinner is Player, he had: " << numPlayer << " points.\n ";
  50.             break; 
  51.             return 0;
  52.         }//Ako je igrac iznad 21 a komp ispod
  53.         else if (numPlayer>21&&numComp<21) {
  54.             cout << "\nYou lose, your num is higher than 21. \n";
  55.             cout << "Comp won, you are loser man\n.";
  56.             break;
  57.             return 0;
  58.         }//ako je komp na 21
  59.         else if (numComp == 21) {
  60.             cout << "\nWinner is Comp, he had: " << numComp << " points.\n";
  61.             break;
  62.             return 0;
  63.         }//ako je komp veci od 21
  64.         else if (numComp > 21) {
  65.             cout << "\nComp has lost.. \n";
  66.             break;
  67.             return 0;
  68.        
  69.         }//ako su oba iznad 21
  70.         else if(numComp>21&&numPlayer>21){
  71.             cout << "\nYou both lost morons. Don't gamble.\n";
  72.             break;
  73.             return 0;
  74.         }
  75.         } while (!(numPlayer == 21 || numComp == 21)); //!() negacija while petlje, dobio sam untill
  76.     }
  77.     system("pause");
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement