-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.cpp
115 lines (113 loc) · 2.52 KB
/
source.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
#include<iostream>
using namespace std;
struct tower {
int data = 0;
tower* next;
};
int won;
tower* head[3];
void insert(int n);
void push(int a, int b);
void display();
void displayt(int i);
void changelocation(int old, int newt);
int pop(int loc, int ttt);
bool checkwin();
int main() {
int run;
do {
int t; int shifttower; int old, newt = 0;
cout << "At which tower do you want to start the game:(1-3): "; cin >> t;
insert(t);
cout << "Enter at which tower to end the game: "; cin >> won;
do {
int in = 0;
system("cls");
display();
do {
cout << "Enter where to push the value and from(old _ new): "; cin >> old >> newt;
if (old <= 3 && old >= 1 && newt <= 3 && newt >= 1) { changelocation(old, newt); in = 1; }
else cout << "Invalid enter again; ";
} while (!in);
run = 1;
system("pause");
}//doend
while (!checkwin());
cout << "Do you want to play again:(y:1/N:0) "; cin >> run;
} while (run);
}//mainend
void insert(int a) {
a -= 1;
for (int i = 3; i > 0; i--) {
push(i, a);
}//forloop
}//pushend
void push(int i, int a) {
tower* temp = new tower;
temp->data = i;
if (head[a] == NULL) {
head[a] = temp;
head[a]->next = NULL;
temp = NULL; delete temp;
}
else {
if (head[a]->data > temp->data) {
temp->next = head[a];
head[a] = temp;
temp = NULL; delete temp;
}
else cout << "Invalid Step pushing in Low value tower..." << endl;
}
}
void display() {
cout << "------------TOWER OF HONOI------------------" << endl;
for (int i = 0; i < 3; i++) {
displayt(i); cout << " Tower: " << i + 1 << endl;
}//forend
}//funend
void displayt(int i) {
tower* thead = head[i];
while (thead != NULL) {
cout << thead->data << " "; thead = thead->next;
}//whileend
}
void changelocation(int old, int newt) {
newt -= 1; old -= 1;
if (head[old] != NULL)
push(pop(old, newt), newt);
else { cout << "Tower is empty pls select another tower: " << endl; }
}//funend;
int pop(int loc, int ttt) {
int tdata;
if (head[ttt] != NULL) {
if (head[ttt]->data > head[loc]->data) {
tower* tempd = head[loc];
tdata = head[loc]->data;
head[loc] = head[loc]->next;
delete tempd;
return tdata;
}
else return 10;
}
else {
tower* tempd = head[loc];
tdata = head[loc]->data;
head[loc] = head[loc]->next;
delete tempd;
return tdata;
}
}
bool checkwin() {
int count = 0;
tower* thead = head[won - 1];
while (thead != NULL)
{
thead = thead->next;
count++;
}
if (count == 3) {
cout << "Congrats you have won the game: " << endl;
return true;
}
else return false;
}