Ada yang belum tahu? Yang kaya gini..
Apa tujuan gadget ini?
Ini adalah sebuah permainan. Anda berkewajiban untuk menggerakkan kotak2 ini sehingga tersusun kaya gini
Cuma mainin itu aja ga seru. Yang seru itu bikinnya. Maka, kita bikin yuk!
Buka program C, masukkin kodingnya:
//beginning of code
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
void randomise(int kot[4][4], int *curx, int *cury);
void kiri(int kot[4][4], int *curx, int *cury);
void kanan(int kot[4][4], int *curx, int *cury);
void atas(int kot[4][4], int *curx, int *cury);
void bawah(int kot[4][4], int *curx, int *cury);
void tampil(int kot[4][4], int *curx, int *cury);
void initialise(int kot[4][4], int *curx, int *cury);
void input(int kot[4][4], int *curx, int *cury);
int cek(int kot[4][4], int *curx, int *cury);
int main(){
int waktu,kot[4][4], curx, cury;
initialise(kot, &curx, &cury);
tampil(kot, &curx, &cury);
printf("\nDENGAN KEY aswd\nSUSUN KOTAK SEPERTI INI\n\nTEKAN SEBARANG TOMBOL UNTUK MULAI");
randomise(kot, &curx, &cury);
getch();
waktu=time(NULL);
while(1){
tampil(kot, &curx, &cury);
printf("TIME : %d",time(NULL)-waktu);
input(kot, &curx, &cury);
if (cek(kot, &curx, &cury)==16) break;
}
tampil(kot, &curx, &cury);
printf("TIME : %d",time(NULL)-waktu);
printf("\nBERHASIL");
getch();
return 0;
}
void initialise(int kot[4][4], int *curx, int *cury){
for (int y=0;y<4;y++){
for (int x=0;x<4;x++){
kot[x][y] = 4*y + x +1;
}
}
*curx=3;*cury=3;
}
void input(int kot[4][4], int *curx, int *cury){
switch(getch()){
case 'a':kiri(kot, curx, cury);break;
case 'd':kanan(kot, curx, cury);break;
case 's':bawah(kot, curx, cury);break;
case 'w':atas(kot, curx, cury);break;
}
}
void kiri(int kot[4][4], int *curx, int *cury){
if(*curx==3){
return;
}
else{
kot[*curx][*cury]=kot[(*curx)+1][*cury];
kot[*(curx)+1][*cury]=16;
*curx=*(curx)+1;
}
return;
}
void kanan(int kot[4][4], int *curx, int *cury){
if(*curx==0){
return;
}
else{
kot[*curx][*cury]=kot[*(curx)-1][*cury];
kot[*(curx)-1][*cury]=16;
*curx= *(curx)-1;
}
return;
}
void bawah(int kot[4][4], int *curx, int *cury){
if(*cury==0){
return;
}
else{
kot[*curx][*cury]=kot[*curx][*(cury)-1];
kot[*curx][*(cury)-1]=16;
*cury=*(cury)-1;
}
return;
}
void atas(int kot[4][4], int *curx, int *cury){
if(*cury==3){
return;
}
else{
kot[*curx][*cury]=kot[*curx][*(cury)+1];
kot[*curx][*(cury)+1]=16;
*cury=*cury+1;
}
return;
}
void randomise(int kot[4][4], int *curx, int *cury){
int acak;
srand(time(NULL));
for(int ulang=1;ulang<1000;ulang++){
acak = rand()%4;
switch(acak){
case 0:kiri(kot, curx, cury);break;
case 1:kanan(kot, curx, cury);break;
case 2:bawah(kot, curx, cury);break;
case 3:atas(kot, curx, cury);break;
}
}
}
void tampil(int kot[4][4], int *curx, int *cury){
system("cls");
for (int y=0;y<4;y++){
printf(" | | | |\n");
for (int x=0;x<4;x++){
if(kot[x][y]==16){
printf(" |");
}
else{
printf(" %02d |",kot[x][y]);
}
}
printf("\n____|____|____|____|\n");
}
}
int cek(int kot[4][4], int *curx, int *cury){
int counter = 0;
for(int y=0;y<4;y++){
for(int x=0; x<4;x++){
if(kot[x][y]==(4*y + x + 1)){
counter++;
}
}
}
return counter;
}
#include <conio.h>
#include <time.h>
#include <stdlib.h>
void randomise(int kot[4][4], int *curx, int *cury);
void kiri(int kot[4][4], int *curx, int *cury);
void kanan(int kot[4][4], int *curx, int *cury);
void atas(int kot[4][4], int *curx, int *cury);
void bawah(int kot[4][4], int *curx, int *cury);
void tampil(int kot[4][4], int *curx, int *cury);
void initialise(int kot[4][4], int *curx, int *cury);
void input(int kot[4][4], int *curx, int *cury);
int cek(int kot[4][4], int *curx, int *cury);
int main(){
int waktu,kot[4][4], curx, cury;
initialise(kot, &curx, &cury);
tampil(kot, &curx, &cury);
printf("\nDENGAN KEY aswd\nSUSUN KOTAK SEPERTI INI\n\nTEKAN SEBARANG TOMBOL UNTUK MULAI");
randomise(kot, &curx, &cury);
getch();
waktu=time(NULL);
while(1){
tampil(kot, &curx, &cury);
printf("TIME : %d",time(NULL)-waktu);
input(kot, &curx, &cury);
if (cek(kot, &curx, &cury)==16) break;
}
tampil(kot, &curx, &cury);
printf("TIME : %d",time(NULL)-waktu);
printf("\nBERHASIL");
getch();
return 0;
}
void initialise(int kot[4][4], int *curx, int *cury){
for (int y=0;y<4;y++){
for (int x=0;x<4;x++){
kot[x][y] = 4*y + x +1;
}
}
*curx=3;*cury=3;
}
void input(int kot[4][4], int *curx, int *cury){
switch(getch()){
case 'a':kiri(kot, curx, cury);break;
case 'd':kanan(kot, curx, cury);break;
case 's':bawah(kot, curx, cury);break;
case 'w':atas(kot, curx, cury);break;
}
}
void kiri(int kot[4][4], int *curx, int *cury){
if(*curx==3){
return;
}
else{
kot[*curx][*cury]=kot[(*curx)+1][*cury];
kot[*(curx)+1][*cury]=16;
*curx=*(curx)+1;
}
return;
}
void kanan(int kot[4][4], int *curx, int *cury){
if(*curx==0){
return;
}
else{
kot[*curx][*cury]=kot[*(curx)-1][*cury];
kot[*(curx)-1][*cury]=16;
*curx= *(curx)-1;
}
return;
}
void bawah(int kot[4][4], int *curx, int *cury){
if(*cury==0){
return;
}
else{
kot[*curx][*cury]=kot[*curx][*(cury)-1];
kot[*curx][*(cury)-1]=16;
*cury=*(cury)-1;
}
return;
}
void atas(int kot[4][4], int *curx, int *cury){
if(*cury==3){
return;
}
else{
kot[*curx][*cury]=kot[*curx][*(cury)+1];
kot[*curx][*(cury)+1]=16;
*cury=*cury+1;
}
return;
}
void randomise(int kot[4][4], int *curx, int *cury){
int acak;
srand(time(NULL));
for(int ulang=1;ulang<1000;ulang++){
acak = rand()%4;
switch(acak){
case 0:kiri(kot, curx, cury);break;
case 1:kanan(kot, curx, cury);break;
case 2:bawah(kot, curx, cury);break;
case 3:atas(kot, curx, cury);break;
}
}
}
void tampil(int kot[4][4], int *curx, int *cury){
system("cls");
for (int y=0;y<4;y++){
printf(" | | | |\n");
for (int x=0;x<4;x++){
if(kot[x][y]==16){
printf(" |");
}
else{
printf(" %02d |",kot[x][y]);
}
}
printf("\n____|____|____|____|\n");
}
}
int cek(int kot[4][4], int *curx, int *cury){
int counter = 0;
for(int y=0;y<4;y++){
for(int x=0; x<4;x++){
if(kot[x][y]==(4*y + x + 1)){
counter++;
}
}
}
return counter;
}
//end of code
Compile (pencet F9), hasilnya kurang lebih begini:
Not bad for a beginner...
Jika ada yang pingin coba main, ingat ini tidak pake mouse. Gunakan key w a s d.
Selamat mencoba!
No comments:
Post a Comment