-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTowerOfHanoi.cpp
214 lines (180 loc) · 5.65 KB
/
TowerOfHanoi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <windows.h>
// variable global
int jumlahcount=0;
void pagoda_solver(int n,char S,char A,char D);
bool validasiinput(char angka);
void Title();
void Title2();
void mainmenu();
void pagodamenu(int jumlah_disk,bool inputan,char inputcontinue);
void exitmenu();
int main()
{
// FYI v
/* asumsi pegs static hanya butuh 3 pegs saja, maksimal jumlah_disk hanya 9
terdiri dari 3 pegs yaitu S(Source), A(Auxiliary) dan D(Destination)
| | | */
// variabel-variabel yang dibutuhkan
char jumlah_disk;
bool inputan;
char inputmenu;
char inputcontinue;
Title();
mainmenu();
printf(">>");
scanf(" %c",&inputmenu);
getchar();
//convert char to int
inputmenu-= '0';
switch(inputmenu)
{
case 1:
system("cls");
Title2();
pagodamenu(jumlah_disk,inputan,inputcontinue);
break;
case 2:
system("cls");
exitmenu();
exit(0);
break;
default:
system("cls");
main();
break;
}
return 0;
}
void pagoda_solver(int n,char S,char A,char D)
{
if(n==1)
{
jumlahcount++;
printf("move from peg %c => to peg %c\n",S,D);
printf("total jumlah gerak : %d\n",jumlahcount);
printf("======\n");
}
else{
//Top (kecil)
pagoda_solver(n-1,S,D,A);
//middle
pagoda_solver(1,S,A,D);
//Down (gede)
pagoda_solver(n-1,A,S,D);
}
}
bool validasiinput(char angka)
{
if(!isalpha(angka)) return true;
else return false;
}
void Title()
{
puts("/$$$$$$$ /$$ ");
puts("| $$__ $$ | $$ ");
puts("| $$ \\ $$ /$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ ");
puts("| $$$$$$$/|____ $$ /$$__ $$ /$$__ $$ /$$__ $$ |____ $$ ||");
puts("| $$____/ /$$$$$$$| $$ \\ $$| $$ \\ $$| $$ | $$ /$$$$$$$ ======");
puts("| $$ /$$__ $$| $$ | $$| $$ | $$| $$ | $$ /$$__ $$ ========== ");
puts("| $$ | $$$$$$$| $$$$$$$| $$$$$$/| $$$$$$$| $$$$$$$ ===============");
puts("|__/ \\_______/ \\____ $$ \\______/ \\_______/ \\_______/");
puts(" /$$ \\ $$ ");
puts(" | $$$$$$/ ");
puts(" \\______/ ");
puts(" /$$$$$$$ /$$ /$$ ");
puts("| $$__ $$ | $$ | $$ ");
puts("| $$ \\ $$ /$$$$$$ /$$$$$$ /$$$$$$ /$$ /$$ /$$$$$$/$$$$ /$$$$$$ /$$ /$$| $$ /$$");
puts("| $$$$$$$ /$$__ $$ /$$__ $$|_ $$_/ | $$ | $$| $$_ $$_ $$ /$$__ $$| $$ | $$| $$ /$$/");
puts("| $$__ $$| $$$$$$$$| $$ \\__/ | $$ | $$ | $$| $$ \\ $$ \\ $$| $$ \\ $$| $$ | $$| $$$$$$/ ");
puts("| $$ \\ $$| $$_____/| $$ | $$ /$$| $$ | $$| $$ | $$ | $$| $$ | $$| $$ | $$| $$_ $$ ");
puts("| $$$$$$$/| $$$$$$$| $$ | $$$$/| $$$$$$/| $$ | $$ | $$| $$$$$$$/| $$$$$$/| $$ \\ $$");
puts("|_______/ \\_______/|__/ \\___/ \\______/ |__/ |__/ |__/| $$____/ \\______/ |__/ \\__/");
puts(" | $$ ");
puts(" | $$ ");
puts(" |__/ ");
puts("");
}
void Title2()
{
Title();
puts("Program Solusi Pagoda Bertumpuk [Tower of Hanoinya Genshin Impact]");
puts("--------------------------------------------------------------------");
puts("| || || || |");
puts("| ====== || || | ");
puts("| ========== ========== || |");
puts("| =============== =============== =============== |");
puts("--------------------------------------------------------------------");
puts("");
}
void mainmenu()
{
puts("Hallo Traveler! Selamat Datang ^o^ b ");
puts("Silahkan pilih salah satu menu di bawah ini :) ");
puts("");
puts("1. Cari Solusi Pagoda Bertumpuk!");
puts("2. Exit Program");
}
void pagodamenu(int jumlah_disk,bool inputan,char inputcontinue)
{
do{
jumlah_disk='\0';
puts("Input jumlah disk [1-9]: ");
printf(">>");
scanf(" %c",&jumlah_disk);
getchar();
inputan = validasiinput(jumlah_disk);
//Debug Purpose
//printf("%d",inputan);
// Covert char to int
jumlah_disk = jumlah_disk - '0';
if(inputan == false) printf("Mohon input angka dengan range [1-9]!\n\n");
}while(inputan == false);
puts("");
printf("============== Solusi untuk %d Disk =============\n\n",jumlah_disk);
pagoda_solver(jumlah_disk,'X','Y','Z');
puts("");
puts("===================================================================");
printf("Total minimal jumlah gerak yang diperlukan : %d langkah |\n",jumlahcount);
puts("===================================================================");
do{
inputcontinue='\0';
puts("Apakah ingin hitung ulang kembali? [Y/N] ");
printf(">>");
scanf(" %c",&inputcontinue);
getchar();
if(inputcontinue == 'Y')
{
//reset jumlahcount
jumlahcount = 0;
system("cls");
Title2();
pagodamenu(jumlah_disk,inputan,inputcontinue);
}
else if(inputcontinue == 'N')
{
//reset jumlahcount
jumlahcount = 0;
system("cls");
main();
}
else
{
printf("Mohon input huruf Y/N !\n\n");
}
}while(inputcontinue != 'Y' || inputcontinue != 'N');
}
void exitmenu()
{
Title();
puts("Terima kasih telah menggunakan program ini ^o^");
puts(" Program created by : Xenoids ");
puts("");
puts(" ~ Hoyolab & Github : Xenoids ~");
puts("");
puts("================================================");
system("pause");
}