-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBitField.java
135 lines (115 loc) · 2.65 KB
/
BitField.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import java.util.Random;
public class BitField {
private boolean finished;
private int piecesCountDowned;
private boolean[] bitField;
private final int piecesCount;
public BitField(int piecesCount) {
finished = false;
piecesCountDowned = 0;
this.piecesCount = piecesCount;
bitField = new boolean[piecesCount];
for (int i = 0; i < piecesCount; i++) {
bitField[i] = false;
}
}
public void turnOnBit(int which) {
if (bitField[which] == false) {
bitField[which] = true;
piecesCountDowned++;
if (piecesCountDowned == piecesCount) {
finished = true;
}
}
}
public void turnOnAll() {
for (int i = 0; i < piecesCount; i++) {
bitField[i] = true;
}
piecesCountDowned = piecesCount;
finished = true;
}
public boolean isFinished() {
return finished;
}
public byte[] toBytes() {
int numBytes;
if (piecesCount%8 == 0 ) {
numBytes = piecesCount/8;
} else {
numBytes = piecesCount/8 + 1;
}
byte[] bytes = new byte[numBytes];
for (int i = 0; i < numBytes; i++) {
bytes[i] = (byte)0;
}
for (int i = 0; i < piecesCount; i++) {
int whichByte = i/8;
int whichBit = i%8;
if (bitField[i] == true) {
bytes[whichByte] = (byte) (bytes[whichByte] | (1 << whichBit));
} else {
bytes[whichByte] = (byte) (bytes[whichByte] & ~(1 << whichBit));
}
}
return bytes;
}
public void setBitField(byte[] bytes) {
piecesCountDowned = 0;
for (int i = 0; i < piecesCount; i++) {
int whichByte = i/8;
int whichBit = i%8;
if ((bytes[whichByte] & (1 << whichBit)) == 0) {
bitField[i] = false;
} else {
bitField[i] = true;
piecesCountDowned++;
}
}
if (piecesCountDowned == piecesCount) {
finished = true;
}
}
public int getInterestingIndex(BitField b) {
int index = -1 ;
for (int i = 0; i < piecesCount; i++) {
if ((bitField[i] == false) && b.bitField[i] == true) {
return i;
}
}
return index;
}
public int getRandomNeededIndex(BitField b) {
int[] indexList = new int[piecesCount];
int j = 0;
for (int i = 0; i < piecesCount; i++) {
if ((bitField[i] == false) && b.bitField[i] == true) {
indexList[j] = i;
j++;
}
}
System.out.println("Number of uncommon stuff " + j);
Random rand = new Random();
int randIndex = rand.nextInt(j+1);
if (j!=0) {
return indexList[randIndex];
}
else {
return -1;
}
}
public String getText() {
StringBuffer text = new StringBuffer();
for (int i = 0 ; i < this.piecesCount; i++) {
if (bitField[i]) {
text.append("1");
} else {
text.append("0");
}
}
return text.toString();
}
public int getPiecesCountDowned() {
return piecesCountDowned;
}
}