-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.js
111 lines (79 loc) · 2.08 KB
/
stack.js
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
/*
* Basketball ADT
*
*
* Represents the canvas element and handles the functionality of the Stack.
*
*
* @author Brandon Salines
*
*
*/
function Stack(x, y, w, h, c) {
this.x = x;
this.y = y;
this.width = w;
this.height = h;
this.color = c;
this.sBalls = [];
this.index = 0;
this.peek = false;
this.show = false;
};
Stack.prototype.draw = function(context) {
context.save();
context.fillStyle = "black";
context.strokeStyle = "black";
var text = "Stack"
context.font = "30px Arial";
context.fillText(text, (this.x + this.width / 2) - (context.measureText(text).width / 2), 60);
if (this.show == true) {
for (var i = 0; i < this.sBalls.length && i < 6; i++) {
this.sBalls[i].draw(context);
}
} else {
//do not draw balls
}
if (this.peek == true) {
this.sBalls[this.sBalls.length - 1].draw(context);
} else {
//do not draw balls
}
context.restore();
};
Stack.prototype.hit = function(x, y, obj) {
var dx, dy;
dx = this.x + this.width;
dy = this.y + this.height;
if (obj.maxX() > this.x && obj.minX() < dx &&
obj.maxY() > this.y && obj.minY() < dy) {
return true;
}
return false;
}
Stack.prototype.put = function(b) {
var bParent = b.getParent();
if (bParent != this) {
this.sBalls.push(b);
this.sBalls[this.index].setParent(this);
this.sBalls[this.index].x = this.x + (this.width / 2);
this.sBalls[this.index].y = ((this.y + this.height - this.sBalls[this.index].radius - 25)) - (this.index * (2 * this.sBalls[this.index].radius + 5));
this.index = this.index + 1;
console.log("Push: " + this.index);
}
}
Stack.prototype.get = function() {
if (this.sBalls.length == 0){
alert("Stack is empty");
}
else{
ball = this.sBalls.pop();
this.index = this.index - 1;
console.log("Pop: " + this.index);
return ball;
}
}
Stack.prototype.removeAll = function() {
this.sBalls.splice(0, this.sBalls.length);
this.index = 0;
}