-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.ts
70 lines (61 loc) · 1.8 KB
/
board.ts
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
export class Board {
board_length: number;
items: number[];
space_remaining: number;
space_used: number = 0;
constructor(material_size: number) {
this.board_length = material_size;
this.items = [];
this.space_remaining = this.board_length;
}
public insert(piece_length: number) {
//Add a item to the Board
if (this.space_remaining >= piece_length) {
this.items.push(piece_length);
this.space_remaining -= piece_length;
this.space_used += piece_length;
} else {
throw new Error('piece of length too long to be inserted');
}
}
public remove(piece_length: number) {
//Remove an item from the Board
if (piece_length in this.items) {
const index = this.items.indexOf(piece_length);
const x = this.items.splice(index, 1);
this.space_remaining += piece_length;
} else {
throw new Error('piece not on the Board!');
}
}
toString() {
return `Board with items ${this.items}, unused space: ${this.space_remaining}`;
}
}
export class BoardCollection {
/*
/* Represents a collection of Boards, representing the result of calculation
/* BoardCollection inializes its contents to []
/* num_boards is supported
/* append adds a Board to the collection
*/
contents: Board[];
constructor() {
this.contents = [];
}
get num_boards() {
return this.contents.length;
}
get last() {
// Returns the last Board on a BoardCollection
if (this.contents.at(-1)) return this.contents.at(-1);
}
append(board: Board) {
// Adds a Board at the end of a BoardCollection
if (board instanceof Board) {
this.contents.push(board);
} else {
throw new Error('Only Board can be appended to BoardCollection');
}
}
}