Game ini saya buat ketika saya kuliah semester 2. Saya dan teman-teman satu kelompok yaitu, alto, apriyandy, dan wisnu membuat game ini berdasarkan referensi dari beberapa game c++ lain. Kami melihat codingnya, untuk referensi. Yang paling kami tekankan untuk dipelajari adalah cara membuat pengacakan (random) pada angkanya. Berikut ini coding lengkapnya.
GAME C++ “TEBAK ANGKA(Hangman)”
CODING:
#include <stdlib.h> // untuk rand dan srand
#include <iostream.h> // untuk cin dan cout
#include <ctype.h> // untuk toupper
#include <conio.h> //untuk clrsc,getch,cprintf,textcolor dan textbackground
void tianggantung(int gambar);
void tianggantung(int gambar)
{
textcolor(YELLOW);
if(gambar==6)
{
gotoxy(1,10);cprintf(" +----+ ");
gotoxy(1,11);cprintf(" | | ");
gotoxy(1,12);cprintf(" | O ");
gotoxy(1,13);cprintf(" | /|\\ ");
gotoxy(1,14);cprintf(" | / \\ ");
gotoxy(1,15);cprintf(" | ");
gotoxy(1,16);cprintf(" ============");
textcolor(RED+BLINK);
gotoxy(8,15);cprintf("MATI");
}
else if(gambar==5)
{
gotoxy(1,10);cprintf(" +----+ ");
gotoxy(1,11);cprintf(" | | ");
gotoxy(1,12);cprintf(" | O ");
gotoxy(1,13);cprintf(" | /|\\ ");
gotoxy(1,14);cprintf(" | \\ ");
gotoxy(1,15);cprintf(" | ");
gotoxy(1,16);cprintf(" ============");
}
else if(gambar==4)
{
gotoxy(1,10);cprintf(" +----+ ");
gotoxy(1,11);cprintf(" | | ");
gotoxy(1,12);cprintf(" | O ");
gotoxy(1,13);cprintf(" | /|\\ ");
gotoxy(1,14);cprintf(" | ");
gotoxy(1,15);cprintf(" | ");
gotoxy(1,16);cprintf(" ============");
}
else if(gambar==3)
{
gotoxy(1,10);cprintf(" +----+ ");
gotoxy(1,11);cprintf(" | | ");
gotoxy(1,12);cprintf(" | O ");
gotoxy(1,13);cprintf(" | /| ");
gotoxy(1,14);cprintf(" | ");
gotoxy(1,15);cprintf(" | ");
gotoxy(1,16);cprintf(" ============");
}
else if(gambar==2)
{
gotoxy(1,10);cprintf(" +----+ ");
gotoxy(1,11);cprintf(" | | ");
gotoxy(1,12);cprintf(" | O ");
gotoxy(1,13);cprintf(" | | ");
gotoxy(1,14);cprintf(" | ");
gotoxy(1,15);cprintf(" | ");
gotoxy(1,16);cprintf(" ============");
}
else if(gambar==1)
{
gotoxy(1,10);cprintf(" +----+ ");
gotoxy(1,11);cprintf(" | | ");
gotoxy(1,12);cprintf(" | O ");
gotoxy(1,13);cprintf(" | ");
gotoxy(1,14);cprintf(" | ");
gotoxy(1,15);cprintf(" | ");
gotoxy(1,16);cprintf(" ============");
}
else if(gambar==0)
{
gotoxy(1,10);cprintf(" +----+ ");
gotoxy(1,11);cprintf(" | | ");
gotoxy(1,12);cprintf(" | ");
gotoxy(1,13);cprintf(" | ");
gotoxy(1,14);cprintf(" | ");
gotoxy(1,15);cprintf(" | ");
gotoxy(1,16);cprintf(" ============");
}
}
//=======================================================================
void tampil();
void tampil()
{
textcolor(RED);
gotoxy(36,3);cprintf("PERMAINAN");
gotoxy(35,4);cprintf("TEBAK ANGKA");
textcolor(BLUE);
gotoxy(30,2);cprintf("=====================");
gotoxy(30,3);cprintf("||");
gotoxy(30,4);cprintf("||");
gotoxy(49,3);cprintf("||");
gotoxy(49,4);cprintf("||");
gotoxy(30,5);cprintf("=====================");
}
//========================================================================
void garis1();
void garis1()
{
int a;
for(a=1;a<=80;a++)
{
textcolor(WHITE);
textbackground(RED);
gotoxy(a,7);
cprintf("-");
textbackground(BLACK);
}
}
//========================================================================
void garis2();
void garis2()
{
int a;
for(a=1;a<=80;a++)
{
textcolor(WHITE);
textbackground(RED);
gotoxy(a,24);
cprintf("-");
textbackground(BLACK);
}
}
//=========================================================================
void penutup();
void penutup()
{
int a,b,c,d,e;
for(a=1;a<=80;a++)
{
textcolor(WHITE);
textbackground(RED);
gotoxy(a,3);
cprintf(">");
textbackground(BLACK);
}
for(b=1;b<=80;b++)
{
textcolor(RED);
textbackground(WHITE);
gotoxy(b,4);
cprintf("<");
textbackground(BLACK);
}
for(c=1;c<=80;c++)
{
textcolor(WHITE);
textbackground(RED);
gotoxy(c,5);
cprintf(">");
textbackground(BLACK);
}
for(d=1;d<=80;d++)
{
textcolor(RED);
textbackground(WHITE);
gotoxy(a,6);
cprintf("<");
textbackground(BLACK);
}
textcolor(RED+BLINK);
gotoxy(35,10);cprintf("Created By");
textcolor(BLUE);
gotoxy(26,13);cprintf("WISNU");
gotoxy(26,15);cprintf("APRIANDY");
gotoxy(26,17);cprintf("RIO DESTALINDO");
gotoxy(26,19);cprintf("ALTO AUGUSTIN");
gotoxy(43,13);cprintf("DBC 106 009");
gotoxy(43,15);cprintf("DBC 106 051");
gotoxy(43,17);cprintf("DBC 106 056");
gotoxy(43,19);cprintf("DBC 106 057");
textcolor(YELLOW);
gotoxy(28,21);cprintf("PALANGKARAYA @JUNI 2007");
for(e=1;e<=80;e++)
{
textcolor(LIGHTGREEN);
textbackground(BLUE);
gotoxy(e,24);
cprintf("=");
textbackground(BLACK);
}
}
//=========================================================================
void selamatdatang();
void selamatdatang()
{
textcolor(GREEN);
gotoxy(20,11);cprintf("Selamat Datang!");
gotoxy(20,13);cprintf("Tebaklah Angka yang Saya Pikirkan!");
gotoxy(20,14);cprintf("Kesempatan Anda Menebak Hanya 6 Kali!");
gotoxy(20,15);cprintf("Selamatkan Nyawa Anda dari Tiang Gantung!!");
gotoxy(20,19);cprintf("Tekan Sembarang Tombol Untuk Mulai");
textcolor(WHITE);
}
//=======================================================================
void main()
{ clrscr();
char keluar='T'; //Membaca Y atau T
while(toupper(keluar) != 'Y') //Melakukan perulangan sampai user ingin berhenti
{
int angka; //Menyimpan angka antara 1 s/d 100
int kesempatan=7; //Menghitung sisa kesempatan
int jawab; //Menyimpan jawaban
int jumlah=1; //Menghitung jumlah jawaban
int gambar=0; //Variabel gambar untuk fungsi tianggantung
int i=0; //Untuk gotoxy
//Mengacak Nomor Tebakan
srand((unsigned)time( NULL ));
angka = (rand() % 99) + 1; //Menghindari nilai 0
clrscr();
tampil();
garis1();
selamatdatang();
garis2();
getch();
clrscr();
tampil();
garis1();
garis2();
gotoxy(30,10);cout<<"Angka yang saya pikirkan antara 1 s/d 100";
while(gambar!=6) //Perulangan s/d gambar terakhir
{ i++;
tianggantung(gambar); //Menampilkan gambar
gambar++;
kesempatan--;
gotoxy(3,18);cout<<"Kesempatan:"<<kesempatan;
gotoxy(30,10+i);cout<<"Berapakah angka yang saya pikirkan? ";
cin>>jawab; //Menginputkan jawaban
if((jawab > 100) || (jawab< 1)) //Bila inputan tidak sesuai
{
gotoxy(20,18);cout<<"Angka tersebut antara 1 s/d 100!!!";
jumlah = jumlah + 1;
}
else if(jawab == angka) //Menggunakan operator sama dengan
{
gotoxy(3,19);cout<<"Hebat, Anda Selamat dan Menang!";
gotoxy(3,20);cout<<"Anda berhasil dalam "<<jumlah<<" kali tebakan!";
break; //Menghentikan perulangan bila benar
}
else if(jawab > angka) //Terlalu tinggi
{
gotoxy(3,19);cout<<"Terlalu Tinggi! "<<endl;
jumlah = jumlah + 1; //Menambah 1 pada jumlah
}
else if(jawab < angka) //Terlalu rendah
{
gotoxy(3,19);cout<<"Terlalu Rendah! "<<endl;
jumlah = jumlah + 1; //Menambah 1 pada jumlah
}
tianggantung(gambar); //Menampilkan gambar
}
gotoxy(3,21);cout<<"Angka yang saya pikirkan adalah "<<angka; //Memberitahukan jawaban
gotoxy(3,22);cout<<"Apakah anda menyerah (Y/T)? "; //Menanyakan bila berhenti
cin>>keluar;
}
clrscr();
penutup();
getch();
}
SCREEENSHOT:
Batanjung - Kapuas Kuala
7 tahun yang lalu