2
2
3
3
enum TYPE {
4
4
ORIGIN,
5
- DESTINY,
6
5
WORK,
6
+ DESTINY,
7
7
};
8
8
9
- void logic (Stack context, Stack helper1, Stack helper2) {
10
-
9
+ bool logic (Stack *toPop, Stack *toPush) {
10
+ if (toPop->peek () < toPush->peek () || toPush->underflow ()) {
11
+ int popValue = toPop->pop ();
12
+ toPush->push (popValue);
13
+ return true ;
14
+ }
11
15
16
+ return false ;
12
17
}
13
18
14
19
bool playLogic (Stack *leftStack, Stack *centerStack, Stack *rightStack) {
15
20
string firstChoice;
16
21
string secondChoice;
17
22
int popValue;
18
23
19
- cout << " [O]rigem [T]rabalho [D]estino " << endl;
24
+ cout << " Origem [O] Trabalho [T] Destino [D]" << endl;
20
25
cout << " \n Escolha a pilha de onde deseja retirar um disco: " ;
21
26
getline (cin, firstChoice);
22
27
fflush (stdin);
23
28
24
-
25
-
26
29
if (firstChoice == " o" || firstChoice == " O" ) {
27
- if (underflow (leftStack )) {
30
+ if (leftStack-> underflow ()) {
28
31
return false ;
29
32
}
30
33
@@ -33,80 +36,47 @@ bool playLogic(Stack *leftStack, Stack *centerStack, Stack *rightStack) {
33
36
getline (cin, secondChoice);
34
37
fflush (stdin);
35
38
39
+
36
40
if (secondChoice == " t" || secondChoice == " T" ) {
37
- if (peek (leftStack) < peek (centerStack) || underflow (centerStack)) {
38
- popValue = pop (leftStack);
39
- push (centerStack, popValue);
40
- return true ;
41
- } else {
42
- return false ;
43
- }
41
+ return logic (leftStack, centerStack);
44
42
} else if (secondChoice == " d" || secondChoice == " D" ) {
45
- if (peek (leftStack) < peek (rightStack) || underflow (rightStack)) {
46
- popValue = pop (leftStack);
47
- push (rightStack, popValue);
48
- return true ;
49
- } else {
50
- return false ;
51
- }
43
+ return logic (leftStack, rightStack);
52
44
} else {
53
45
return false ;
54
46
}
55
47
56
48
} else if (firstChoice == " t" || firstChoice == " T" ) {
57
- if (underflow (centerStack))
49
+ if (centerStack-> underflow ()) {
58
50
return false ;
51
+ }
59
52
60
53
cout << " \n Você escolheu remover um disco do TRABALHO!" << endl;
61
54
cout << " \n Escolha para onde deseja enviar o disco: " ;
62
55
getline (cin, secondChoice);
63
56
fflush (stdin);
64
57
65
58
if (secondChoice == " o" || secondChoice == " O" ) {
66
- if (peek (centerStack) < peek (leftStack) || underflow (leftStack)) {
67
- popValue = pop (centerStack);
68
- push (leftStack, popValue);
69
- return true ;
70
- } else {
71
- return false ;
72
- }
59
+ return logic (centerStack, leftStack);
73
60
} else if (secondChoice == " d" || secondChoice == " D" ) {
74
- if (peek (centerStack) < peek (rightStack) || underflow (rightStack)) {
75
- popValue = pop (centerStack);
76
- push (rightStack, popValue);
77
- return true ;
78
- } else {
79
- return false ;
80
- }
61
+ return logic (centerStack, rightStack);
81
62
} else {
82
63
return false ;
83
64
}
84
65
}
85
66
if (firstChoice == " d" || firstChoice == " D" ) {
86
- if (underflow (rightStack))
67
+ if (rightStack-> underflow ()) {
87
68
return false ;
69
+ }
88
70
89
71
cout << " \n Você escolheu remover um disco do DESTINO!" << endl;
90
72
cout << " \n Escolha para onde deseja enviar o disco: " ;
91
73
getline (cin, secondChoice);
92
74
fflush (stdin);
93
75
94
76
if (secondChoice == " t" || secondChoice == " T" ) {
95
- if (peek (rightStack) < peek (centerStack)|| underflow (centerStack)) {
96
- popValue = pop (rightStack);
97
- push (centerStack, popValue);
98
- return true ;
99
- } else {
100
- return false ;
101
- }
77
+ return logic (rightStack, centerStack);
102
78
} else if (secondChoice == " o" || secondChoice == " O" ) {
103
- if (peek (rightStack) < peek (leftStack) || underflow (leftStack)) {
104
- popValue = pop (rightStack);
105
- push (leftStack, popValue);
106
- return true ;
107
- } else {
108
- return false ;
109
- }
79
+ return logic (rightStack, leftStack);
110
80
} else {
111
81
return false ;
112
82
}
0 commit comments