Advertisement
lietschie

Game Batu Gunting Kertas

Dec 11th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.16 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<windows.h>
  4. #include<conio.h>
  5. #include <locale>
  6. #include<string.h>
  7. #include<algorithm>
  8. #include <stdlib.h>
  9. using namespace std;
  10.  
  11. void makeLine(int length){
  12.     for(int i=0; i < length; i++){
  13.         cout<<"=";
  14.     }
  15.     cout<<endl;
  16. }
  17.  
  18. void waitSecond(unsigned milliseconds){
  19.     Sleep(milliseconds);
  20. }
  21.  
  22. void openHeader(){
  23.     makeLine(25);
  24.     cout<<"  WELCOME TO Batu Gunting Kertas GAME!"<<endl;
  25.     cout<<"  Buatan Joshua"<<endl;
  26.     makeLine(25);
  27. }
  28. void showRound(int round){
  29.     cout<<"    RONDE " << round << " DIMULAI!"<<endl;
  30.     makeLine(25);
  31. }
  32.  
  33. string compareBGK(string player,string bot){
  34.     transform(player.begin(),player.end(),player.begin(),::towlower);
  35.     transform(bot.begin(),bot.end(),bot.begin(),::towlower);
  36.     string status;
  37.  
  38.     if(player == "gunting"){
  39.  
  40.         if (bot == "gunting"){
  41.             status = "Seri";
  42.  
  43.         }
  44.         else if (bot == "kertas"){
  45.             status = "Menang";
  46.  
  47.         }
  48.         else if (bot == "batu"){
  49.             status = "Kalah";
  50.  
  51.         }
  52.  
  53.     }else if(player =="kertas"){
  54.         if (bot == "gunting"){
  55.             status = "Kalah";
  56.  
  57.         }
  58.         else if (bot == "kertas"){
  59.             status = "Seri";
  60.  
  61.         }
  62.         else if (bot == "batu"){
  63.             status = "Menang";
  64.  
  65.         }
  66.  
  67.     }else if(player =="batu"){
  68.         if (bot == "gunting"){
  69.             status = "Menang";
  70.  
  71.         }
  72.         else if (bot == "kertas"){
  73.             status = "Kalah";
  74.  
  75.         }
  76.         else if (bot == "batu"){
  77.             status = "Seri";
  78.  
  79.         }
  80.     }
  81.     cout<<"Bot Memilih "<<bot<<endl;
  82.  
  83.     return status;
  84. }
  85.  
  86.  
  87. string botPilihBGK()
  88. {
  89.  
  90.     cout<<"Bot Memilih ";
  91.     int pilihanBot = rand() % 3+1;
  92.     string statusPilihan;
  93.     switch(pilihanBot){
  94.         case 1:
  95.             statusPilihan = "Gunting";
  96.             break;
  97.         case 2:
  98.             statusPilihan = "Kertas";
  99.             break;
  100.         case 3:
  101.             statusPilihan = "Batu";
  102.             break;
  103.         default:
  104.             statusPilihan  = "Gunting";
  105.             break;
  106.     }
  107.     waitSecond(2000);
  108.     cout<<"..."<<endl;
  109.     waitSecond(2000);
  110.     return statusPilihan;
  111.  
  112. }
  113. string pilihBGK(string pilihan[],int length){
  114.     cout<<"Beberapa opsi yg dapat anda pilih"<<endl;
  115.     for(int i = 0 ; i<length;i++){
  116.         cout<<"-"<<pilihan[i]<<endl;
  117.     }
  118.     string pilihanPlayer;
  119.     cin>>pilihanPlayer;
  120.     makeLine(25);
  121.     cout<<"Anda Memilih "<<pilihanPlayer<<endl;
  122.     return pilihanPlayer;
  123. }
  124. template <size_t array_length>
  125. int getArrayLength(string (&array)[array_length]){
  126.     return array_length;
  127. }
  128.  
  129.  
  130. int main(){
  131.     //int mulai;
  132.     string mulai;
  133.     bool isPlayed = false;
  134.     string pilihanBot;
  135.     string pilihanPlayer;
  136.     string bgk[3] = {"Gunting","Batu","Kertas"};
  137.     int round = 0;
  138.     int maxRound = 3;
  139.     int playerPoint = 0 , botPoint = 0;
  140.     cout<<endl;
  141.     cout<<"Apakah anda ingin memulai Permainan Y/N";
  142.     cout<<endl;
  143.     cin>>mulai;
  144.     if(mulai == "y" || mulai == "Y"){
  145.         openHeader();
  146.         isPlayed = true;
  147.         while(isPlayed && round < maxRound){
  148.             round++;
  149.             showRound(round);
  150.             waitSecond(2000);
  151.  
  152.             string stat = compareBGK(pilihBGK(bgk,getArrayLength(bgk)),botPilihBGK());
  153.  
  154.             if (stat == "Menang"){
  155.                 playerPoint++;
  156.                 cout<<"Anda "<<stat<<"!"<<endl;
  157.             }
  158.             else if (stat == "Seri" ){
  159.                 //Nothing happend
  160.                 cout<<"Anda "<<stat<<"!"<<endl;
  161.             }
  162.             else{
  163.                 botPoint++;
  164.                 cout<<"Anda "<<stat<<"!"<<endl;
  165.             }
  166.             waitSecond(2000);
  167.             makeLine(25);
  168.         }
  169.         cout<<"Player Point: "<<playerPoint<<endl;
  170.         cout<<"Bot Point: "<<botPoint<<endl;
  171.         makeLine(25);
  172.         if (playerPoint > botPoint){
  173.             cout<<"ANDA MENANG!"<<endl;
  174.         }
  175.         else if (playerPoint == botPoint){
  176.             cout<<"ANDA SERI!"<<endl;
  177.         }
  178.         else {
  179.             cout<<"ANDA KALAH!"<<endl;
  180.         }
  181.  
  182.  
  183.     }else{
  184.         getch();
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement