-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
80 lines (63 loc) · 1.91 KB
/
LinkedList.java
File metadata and controls
80 lines (63 loc) · 1.91 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
// Linked List class, consists of several functions to add a piece, find a piece, see whether its attacking another piece, and check for validity
class LinkedList {
protected Node head;
protected Node tail;
private int length;
void LinkedList() {
head = null;
tail = null;
length = 0;
}
// Function that inserts an element at the end
public void addPiece(ChessPiece value)
{
Node node = new Node(value);
if(head == null){
head = node;
tail = head;
}
tail.setNext(node);
tail = node;
length++;
}
// function that checks for validity aka whether two pieces are not in the same place
public boolean checkPiece() {
Node ptr = head;
ChessPiece p1, p2;
do {
p1 = ptr.getData();
p2 = this.findPiece(p1.getRow(), p1.getCol());
if(p2 != null && p1 != p2){
return false;
}
ptr = ptr.getNext();
} while(ptr != null && ptr != head);
return true;
}
// finds the piece that is wanted by the input file
public ChessPiece findPiece(int row, int col) {
Node location = head;
do {
if(location.getData().getRow() == row && location.getData().getCol() == col ) {
return location.getData();
}
location = location.getNext();
} while(location != null && location != head);
return null;
}
// checks if a piece is in the attacking range of another piece by first checking whether they are different colors and proceeding to see if one is attacking the other. Resets the node at the end to the next node.
public boolean attacking(ChessPiece piece) {
Node node1 = head;
ChessPiece p1;
while(node1 != null) {
p1 = node1.getData();
if((p1.getColor() != piece.getColor()) && (p1 != piece)) {
if(piece.isAttacking(p1)) {
return true;
}
}
node1 = node1.getNext();
}
return false;
}
}