Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Wieża Hanoi w wersji graficznej by poosiaq
- #include <iostream>
- #include <conio.h>
- #include <cstdio>
- #include <Windows.h>
- #define ILOSC 3 //Tu podaj liczbę krążków
- #define PREDKOSC 300 //Tu podaj prędkość w ms
- int t[ILOSC], u[ILOSC], w[ILOSC], g=0, rozwiazanie;
- using namespace std;
- void rysowanie (char a, char c){
- int e=0;
- int d=ILOSC-1;
- if(a=='A' && c=='B'){
- while(t[e]==0) e++;
- while(u[d]!=0) d--;
- swap(t[e], u[d]);
- }
- else if(a=='A' && c=='C'){
- while(t[e]==0) e++;
- while(w[d]!=0) d--;
- swap(t[e], w[d]);
- }
- else if(a=='B' && c=='A'){
- while(u[e]==0) e++;
- while(t[d]!=0) d--;
- swap(u[e], t[d]);
- }
- else if(a=='B' && c=='C'){
- while(u[e]==0) e++;
- while(w[d]!=0) d--;
- swap(u[e], w[d]);
- }
- else if(a=='C' && c=='A'){
- while(w[e]==0) e++;
- while(t[d]!=0) d--;
- swap(w[e], t[d]);
- }
- else if(a=='C' && c=='B'){
- while(w[e]==0) e++;
- while(u[d]!=0) d--;
- swap(w[e], u[d]);
- }
- system("CLS");
- for(int f=0; f<ILOSC; f++){
- cout.width(1);
- for(int g=0; g<t[f]; g++) cout<<"*";
- cout.width(ILOSC-t[f]+2);
- cout<<"| ";
- for(int g=0; g<u[f]; g++) cout<<"*";
- cout.width(ILOSC-u[f]+2);
- cout<<"| ";
- for(int g=0; g<w[f]; g++) cout<<"*";
- cout<<endl;
- }
- Sleep(PREDKOSC);
- }
- void gra(){
- char znak1, znak2;
- while(rozwiazanie==0){
- if(w[0]==1 && w[ILOSC-1]==ILOSC) {rozwiazanie=1;break;}
- cout<<"Z: ";
- cin>>znak1;
- cout<<"Do: ";
- cin>>znak2;
- rysowanie(znak1, znak2);
- g++;
- }
- }
- void hanoi(int n, char a, char b, char c){
- if(n==1){
- rysowanie(a, c);
- g++;
- }
- if(n>1){
- hanoi(n-1, a, c, b);
- rysowanie(a, c);
- g++;
- hanoi(n-1, b, a, c);
- }
- }
- int main(int argc, char **argv){
- int start;
- cout<<"Wieza Hanoi\n[1]Automatycznie\n[2]Zagraj samemu\n[3]Wyjscie\n";
- for(int b=0; b<ILOSC; b++){ //Wypełnienie 1-szej wieży i zerowanie pozostałych
- t[b]=b+1;
- u[b]=0;
- w[b]=0;
- }
- cin>>start;
- switch(start){
- case 1: {
- rysowanie(0, 0);
- hanoi(ILOSC, 'A', 'B', 'C');
- break;
- }
- case 2: {
- rysowanie(0, 0);
- gra();
- break;
- }
- case 3: return 0;
- }
- cout<<"\nTadam!\nLaczna ilosc przeniesien: "<<g;
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement