-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiece.h
94 lines (77 loc) · 1.59 KB
/
Piece.h
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
#ifndef _Piece_h_
#define _Piece_h_
using namespace std;
class Piece
{
public:
Piece(int radius)
{
Radius = radius;
}
~Piece();
int getRadius() { return Radius; }
private:
int Radius;
};
class Peg
{
public:
Peg(int height, int distanceBetweenPegs)
{
Width = distanceBetweenPegs;
Pstack.resize(height, NULL);
}
~Peg();
void printDisc(int discNum)
{
int radius = Pstack[discNum]->getRadius();
for(int i = 0; i < Width; i++)
{
if(Width / 2 == i) cout << char(178);
if(i >= (Width / 2) - radius && i <= (Width / 2) + radius - 1)
cout << char(178);
else
cout << char(255);
}
}
int getTopDiscIndex()
{
int i;
for(i = 0; i < Pstack.size(); i++)
if(Pstack[i] != NULL)
break;
while(i >= Pstack.size())
i--;
return i;
}
void moveDiscFromPegToPeg(Peg *B)
{
int oldIndex = this->getTopDiscIndex();
int newIndex = B->getTopDiscIndex();
if(this->Pstack[oldIndex] == NULL)
return;
int oldRadius = this->Pstack[oldIndex]->getRadius();
int newRadius = -1;
// cout << oldIndex << " " << newIndex << endl;
if(B->Pstack[newIndex] != NULL)
newRadius = B->Pstack[newIndex]->getRadius();
else
newRadius = 0;
// cout << oldIndex << " " << newIndex << " " << oldRadius << " " << newRadius << endl;
if(newRadius == 0)
{
B->Pstack[newIndex] = this->Pstack[oldIndex];
this->Pstack[oldIndex] = NULL;
}
else if( oldRadius < newRadius )
{
B->Pstack[newIndex - 1] = this->Pstack[oldIndex];
this->Pstack[oldIndex] = NULL;
}
}
friend class TowersofHanoi;
private:
int Width;
vector<Piece*> Pstack;
};
#endif //_Piece_h_