#include #include #define n 4 int A[n], B[n], C[n], topA, topB, topC; void awal(); void pushA(int X); void pushB(int X); void pushC(int X); int popA(); int popB(); int popC(); void tampil(); bool cekmenang(); int main() { int pil, kondisi=1; awal(); while (kondisi) { //system("cls"); printf("\n\n||| TOWER OF HANOI GAME |||"); tampil(); if(cekmenang()) { printf("\nSELAMAT. ANDA MENANG. KEREEEN!!"); } else { printf("\n1. Tower A ke Tower B"); printf("\n2. Tower A ke Tower C"); printf("\n3. Tower B ke Tower A"); printf("\n4. Tower B ke Tower C"); printf("\n5. Tower C ke Tower A"); printf("\n6. Tower C ke Tower B"); } printf("\n7. Reset Game"); printf("\n8. Keluar"); printf("\nMasukkan pilihan (1-8) : "); scanf("%i", &pil); switch(pil) { case 1 : //A--> B if (topA==-1) { printf("Tower A kosong. Ulangi langkah kamu!"); } else { if (topB > -1 && A[topA] > B[topB]) { printf("Kesalahan langkah. Ulangi!"); } else { int X = popA(); pushB(X); } } break; case 2 : //A--> C if (topA==-1) { printf("Tower A kosong. Ulangi langkah kamu!"); } else { if (topC > -1 && A[topA] > C[topC]) { printf("Kesalahan langkah. Ulangi!"); } else { int X = popA(); pushC(X); } } break; case 3 : //B--> A if (topB==-1) { printf("Tower B kosong. Ulangi langkah kamu!"); } else { if (topA > -1 && B[topB] > A[topA]) { printf("Kesalahan langkah. Ulangi!"); } else { int X = popB(); pushA(X); } } break; case 4 : //B--> C if (topB==-1) { printf("Tower B kosong. Ulangi langkah kamu!"); } else { if (topC > -1 && B[topB] > C[topC]) { printf("Kesalahan langkah. Ulangi!"); } else { int X = popB(); pushC(X); } } break; case 5 : //C--> A if (topC==-1) { printf("Tower C kosong. Ulangi langkah kamu!"); } else { if (topA > -1 && C[topC] > A[topA]) { printf("Kesalahan langkah. Ulangi!"); } else { int X = popC(); pushA(X); } } break; case 6 : //C--> B if (topC==-1) { printf("Tower C kosong. Ulangi langkah kamu!"); } else { if (topB > -1 && C[topC] > B[topB]) { printf("Kesalahan langkah. Ulangi!"); } else { int X = popC(); pushB(X); } } break; case 7 : awal(); break; case 8 : kondisi=0; break; } } return 0; } void awal() { topA = -1; topB = -1; topC = -1; //isi default nilai dari tower A for(int i=n; i>0; i--) { pushA(i); B[i-1] = 0; C[i-1] = 0; } } void pushA(int X) { if (topA < n-1) { topA++; A[topA] = X; } else { printf("\nSTACK PENUH"); } } void pushB(int X) { if (topB < n-1) { topB++; B[topB] = X; } else { printf("\nSTACK PENUH"); } } void pushC(int X) { if (topC < n-1) { topC++; C[topC] = X; } else { printf("\nSTACK PENUH"); } } int popA() { int X=0; if (topA > -1) { X = A[topA]; A[topA] = 0; topA--; } return X; } int popB() { int X=0; if (topB > -1) { X = B[topB]; B[topB] = 0; topB--; } return X; } int popC() { int X=0; if (topC > -1) { X = C[topC]; C[topC] = 0; topC--; } return X; } void tampil() { printf("\nTower A: "); for (int i=0; i