Skip to content

Commit 656e94d

Browse files
committed
refatoring
1 parent 46a13df commit 656e94d

File tree

8 files changed

+165
-151
lines changed

8 files changed

+165
-151
lines changed

.gitignore

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Compiled Object files
5+
*.slo
6+
*.lo
7+
*.o
8+
*.obj
9+
10+
# Precompiled Headers
11+
*.gch
12+
*.pch
13+
14+
# Compiled Dynamic libraries
15+
*.so
16+
*.dylib
17+
*.dll
18+
19+
# Fortran module files
20+
*.mod
21+
*.smod
22+
23+
# Compiled Static libraries
24+
*.lai
25+
*.la
26+
*.a
27+
*.lib
28+
29+
# Executables
30+
*.exe
31+
*.out
32+
*.app

Hanoi/Mostra

-50
This file was deleted.

Hanoi/GeraDiscos Hanoi/generateDisks.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ int getDiscos() {
2727
return discos;
2828
}
2929

30-
int getJogadas(Pilha *p, int discos) {
30+
int getJogadas(Stack *stack, int discos) {
3131
for(int i = discos; i >= 1; i--) {
32-
Push(p, i);
32+
push(stack, i);
3333
}
3434

3535
int dificuldade = 1;

Hanoi/Jogada Hanoi/play.hpp

+23-23
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
/** Função que executa as jogadas */
44

5-
bool Jogada(Pilha *H1, Pilha *H2, Pilha *H3) {
5+
bool Jogada(Stack *H1, Stack *H2, Stack *H3) {
66
string escolha;
77
string enviar;
8-
int pop;
8+
int popValue;
99

1010
cout << " [O]rigem [T]rabalho [D]estino" << endl;
1111
cout << "\nEscolha a pilha de onde deseja retirar um disco: ";
@@ -14,7 +14,7 @@ bool Jogada(Pilha *H1, Pilha *H2, Pilha *H3) {
1414

1515

1616
if(escolha == "o" || escolha == "O") {
17-
if(Underflow(H1))
17+
if(underflow(H1))
1818
return false;
1919

2020
cout << "\nVocê escolheu remover um disco da ORIGEM!" << endl;
@@ -24,18 +24,18 @@ bool Jogada(Pilha *H1, Pilha *H2, Pilha *H3) {
2424

2525
if(enviar == "t" || enviar == "T") {
2626
///Pop na H1 e PUSH na H2
27-
if(Peek(H1) < Peek(H2) || Underflow(H2)) {
28-
pop = Pop(H1);
29-
Push(H2, pop);
27+
if(peek(H1) < peek(H2) || underflow(H2)) {
28+
popValue = pop(H1);
29+
push(H2, popValue);
3030
return true;
3131
} else {
3232
return false;
3333
}
3434
} else if(enviar == "d" || enviar == "D") {
3535
///Pop na H1 e PUSH na H3
36-
if(Peek(H1) < Peek(H3) || Underflow(H3)) {
37-
pop = Pop(H1);
38-
Push(H3, pop);
36+
if(peek(H1) < peek(H3) || underflow(H3)) {
37+
popValue = pop(H1);
38+
push(H3, popValue);
3939
return true;
4040
} else {
4141
return false;
@@ -45,7 +45,7 @@ bool Jogada(Pilha *H1, Pilha *H2, Pilha *H3) {
4545
}
4646

4747
} else if(escolha == "t" || escolha == "T") {
48-
if(Underflow(H2))
48+
if(underflow(H2))
4949
return false;
5050

5151
cout << "\nVocê escolheu remover um disco do TRABALHO!" << endl;
@@ -55,18 +55,18 @@ bool Jogada(Pilha *H1, Pilha *H2, Pilha *H3) {
5555

5656
if(enviar == "o" || enviar == "O") {
5757
///Pop na H2 e PUSH na H1
58-
if(Peek(H2) < Peek(H1) || Underflow(H1)) {
59-
pop = Pop(H2);
60-
Push(H1, pop);
58+
if(peek(H2) < peek(H1) || underflow(H1)) {
59+
popValue = pop(H2);
60+
push(H1, popValue);
6161
return true;
6262
} else {
6363
return false;
6464
}
6565
} else if(enviar == "d" || enviar == "D") {
6666
///Pop na H2 e PUSH na H3
67-
if(Peek(H2) < Peek(H3) || Underflow(H3)) {
68-
pop = Pop(H2);
69-
Push(H3, pop);
67+
if(peek(H2) < peek(H3) || underflow(H3)) {
68+
popValue = pop(H2);
69+
push(H3, popValue);
7070
return true;
7171
} else {
7272
return false;
@@ -76,7 +76,7 @@ bool Jogada(Pilha *H1, Pilha *H2, Pilha *H3) {
7676
}
7777
}
7878
if(escolha == "d" || escolha == "D") {
79-
if(Underflow(H3))
79+
if(underflow(H3))
8080
return false;
8181

8282
cout << "\nVocê escolheu remover um disco do DESTINO!" << endl;
@@ -86,18 +86,18 @@ bool Jogada(Pilha *H1, Pilha *H2, Pilha *H3) {
8686

8787
if(enviar == "t" || enviar == "T") {
8888
///Pop na H2 e PUSH na H2
89-
if(Peek(H3) < Peek(H2)|| Underflow(H2)) {
90-
pop = Pop(H3);
91-
Push(H2, pop);
89+
if(peek(H3) < peek(H2)|| underflow(H2)) {
90+
popValue = pop(H3);
91+
push(H2, popValue);
9292
return true;
9393
} else {
9494
return false;
9595
}
9696
} else if(enviar == "o" || enviar == "O") {
9797
///Pop na H2 e PUSH na H1
98-
if(Peek(H3) < Peek(H1) || Underflow(H1)) {
99-
pop = Pop(H3);
100-
Push(H1, pop);
98+
if(peek(H3) < peek(H1) || underflow(H1)) {
99+
popValue = pop(H3);
100+
push(H1, popValue);
101101
return true;
102102
} else {
103103
return false;

Hanoi/show.hpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <cstdlib>
2+
3+
/** Função que mostra as torres de Hanoi */
4+
5+
void showWhiteSpace(int value) {
6+
for(int j = 0; j < value; j++) {
7+
cout << " ";
8+
}
9+
}
10+
11+
void showStackValue(int value) {
12+
string hifen = "";
13+
14+
for(int j = 0; j < value; j++) {
15+
hifen += "-";
16+
}
17+
18+
cout << hifen << "|" << hifen;
19+
}
20+
21+
void showStackContext(Stack *stack, int value) {
22+
showWhiteSpace(stack->sizeT - stack->data[value]);
23+
showStackValue(stack->data[value]);
24+
showWhiteSpace(stack->sizeT - stack->data[value]);
25+
}
26+
27+
void showAll(Stack *leftStack, Stack *centerStack, Stack *rightStack, int remainingPlays, bool hasWin) {
28+
system("cls");
29+
30+
if(!hasWin && remainingPlays != 0) {
31+
cout << "Você possui " << remainingPlays << " jogadas!" << endl << endl;
32+
}
33+
34+
for(int x = leftStack->sizeT-1; x > leftStack->base; x--) {
35+
showStackContext(leftStack, x);
36+
showStackContext(centerStack, x);
37+
showStackContext(rightStack, x);
38+
cout << endl;
39+
}
40+
41+
cout << "---------------------------------------------------" << endl;
42+
}

Main.cpp

+17-17
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,40 @@
22

33
using namespace std;
44

5-
#include "Pilha/ModeloPilha.hpp"
6-
#include "Hanoi/Jogada.hpp"
7-
#include "Hanoi/Mostra.hpp"
8-
#include "Hanoi/GeraDiscos.hpp"
5+
#include "Stack.hpp"
6+
#include "Hanoi/play.hpp"
7+
#include "Hanoi/show.hpp"
8+
#include "Hanoi/generateDisks.hpp"
99

1010
int main() {
1111
setlocale(LC_ALL, "portuguese");
1212

13-
Pilha H1, H2, H3;
13+
Stack leftStack, centerStack, rightStack;
1414

15-
Set(&H1);
16-
Set(&H2);
17-
Set(&H3);
15+
set(&leftStack);
16+
set(&centerStack);
17+
set(&rightStack);
1818

19-
H1.discos = H2.discos = H3.discos = getDiscos();
19+
leftStack.disks = centerStack.disks = rightStack.disks = getDiscos();
2020

21-
int jogadas = getJogadas(&H1, H1.discos);
21+
int remainingPlays = getJogadas(&leftStack, leftStack.disks);
2222

23-
while(jogadas > 0) {
24-
Mostra(&H1, &H2, &H3, jogadas, false);
23+
while(remainingPlays > 0) {
24+
showAll(&leftStack, &centerStack, &rightStack, remainingPlays, false);
2525

26-
if(Jogada(&H1, &H2, &H3)) {
27-
jogadas--;
26+
if(Jogada(&leftStack, &centerStack, &rightStack)) {
27+
remainingPlays--;
2828
} else {
2929
cout << "\nOpção inválida!" << endl;
3030
getchar();
3131
}
3232

33-
if(Overflow(&H3)) {
34-
Mostra(&H1, &H2, &H3, jogadas, true);
33+
if(overflow(&rightStack)) {
34+
showAll(&leftStack, &centerStack, &rightStack, remainingPlays, true);
3535
cout << "\nVocê venceu o jogo, Parabéns!!!!" << endl;
3636
break;
3737
} else {
38-
if(jogadas <= 0) {
38+
if(remainingPlays <= 0) {
3939
system("cls");
4040
cout << "\nVocê perdeu, tente novamente!" << endl;
4141
break;

Pilha/ModeloPilha.hpp

-59
This file was deleted.

0 commit comments

Comments
 (0)