-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAIPieceList.java
More file actions
99 lines (85 loc) · 2.2 KB
/
Copy pathAIPieceList.java
File metadata and controls
99 lines (85 loc) · 2.2 KB
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
/**
* AIPieceList
*
* @author Faiza Salami, 7941056
* <p>
* REMARKS: A list which stores a node which contains a piece and its location on the board
*/
public class AIPieceList {
private Node top;
private int size;
public AIPieceList() {
top = null;
size = 0;
}
/**
* adds a piece with its location to the end of the list
*
* @param piece the piece to be added
* @param row the row of the piece to be added
* @param col the col of the piece to be added
*/
public void add(Piece piece, int row, int col) {
Node data = new Node(piece, row, col);
if (top == null) {
top = data;
} else {
Node curr = top;
while (curr.getNext() != null) {
curr = curr.getNext();
}
curr.setNext(data);
}
size++;
}
/**
* gets an item from the list
*
* @param i the position of the item to be removed
* @return Node-the item to be gotten
*/
public Node get(int i) {
Node curr = top;
int count = 1;
while (curr != null) {
if (count == i) {
return curr;
}
curr = curr.getNext();
count++;
}
return null;
}
/**
* removes a piece from the list
*
* @param piece the piece to be removed
* @param row the row of the piece to be removed
* @param col the col of the piece to be removed
*/
public void remove(Piece piece, int row, int col) {
Node prev = null;
Node curr = top;
while (curr != null) {
if (piece.name().equals(curr.getPiece().name()) && row == curr.getRow() && col == curr.getCol()) {
if (prev == null) {
top = top.getNext();
} else {
prev.setNext(curr.getNext());
}
size--;
return;
}
prev = curr;
curr = curr.getNext();
}
}
/**
* gets the size of the list
*
* @return returns the size of the list
*/
public int size() {
return size;
}
}