-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.java
105 lines (82 loc) · 2.33 KB
/
node.java
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
/* Class node is used to create objects that represent nodes/agents
* in a network of players that play prisoner's dilemma game
*/
import java.util.*;
public class node {
// instance fields
private int ID;
private int initialNumberOfNeighbours;
private int currentNumberOfNeighbours;
private boolean isDefector;
private boolean isDead;
private LinkedList<node> neighbourList = new LinkedList<>();
private double totalScore;
private double threshold;
// constructor
public node(int ID) {
this.ID = ID;
initialNumberOfNeighbours = 0;
isDefector = false;
isDead = false;
this.totalScore = 0;
}
// METHODS
public static void recordNeighbours(node node1, node node2) {
// iterates node1 initial and current # of neighbours
node1.initialNumberOfNeighbours++;
node1.currentNumberOfNeighbours++;
// iterates node2 initial and current # of neighbours
node2.initialNumberOfNeighbours++;
node2.currentNumberOfNeighbours++;
// updates the neighbour lists of both node1 and node2
node1.neighbourList.add(node2);
node2.neighbourList.add(node1);
}
// ID
public int getID() {
return ID;
}
// strategy: defector or cooperator
public void setDefector(boolean defects) {
isDefector = defects;
}
public boolean defects() {
return isDefector;
}
// threshold of elimination
public void setThreshold(double threshold) {
this.threshold = threshold;
}
public double getThreshold() {
return threshold;
}
// scores
public void addToScore(double score) {
totalScore += score;
}
public static void resetScores(node[] nodes) {
for (node n : nodes) {
n.totalScore = 0;
}
}
public double getTotalScore() {
return totalScore;
}
// state: dead or alive
public void setDeath() {
isDead = true;
}
public boolean isDead() {
return isDead;
}
// neighbours
public int getInitialNumberOfNeighbours() {
return initialNumberOfNeighbours;
}
public LinkedList<node> getNeighbours() {
return neighbourList;
}
public void decrementNeighbourNumber() {
currentNumberOfNeighbours--;
}
}