A straight conversion of what you've written would be to use a BitSet
. It allows you to get the intersection/or, and, andNot of the bits. You could make:
BitSet move = new BitSet(9);
move.set(1); // going up
move.set(2); // also going down?
You can send this relatively packed (EG over a network) by streaming the toByteArray()
and reconstructing it on the recieving side with new BitSet(inputByteArray)
How many of these booleans would or should be set true in a valid expression of state?
If we guess one, we can do something simpler. E.G. when continue is true all other values are ignored and it doesn't make sense (to me) to have both up and down or both left and right true. Though it may make sense to have either or both mouse buttons down with the other states and similarly to have something like up and left and turnQ on at the same time the issue can be simplified if we allow one state only.
I.E. If you're representing state, I'd define an enum of the states and consider that done, effectively passing the bool array equivalent as single number, the cardinality of the enum.
public enum SingleAiMoves {
NONE, STOP,
LEFT, RIGHT,
UP, DOWN,
TURN_Q, TURN_E,
MOUSE1, MOUSE2
}
That allows only one state at a time.
If you needed to combine multiple states, but only the valid ones, you could define a logical group of states like:
public enum AiMove {
// u/d l/r q/e m1 m2 stop
STOP (null, null, null, false, false, true),
NONE (null, null, null, false, false, false),
UP (true, null, null, false, false, false),
DOWN (false, null, null, false, false, false),
LEFT (null, true, null, false, false, false),
RIGHT (null, false, null, false, false, false),
TURN_Q (null, null, true, false, false, false),
TURN_E (null, null, false, false, false, false),
MOUSE_1 (null, null, null, true, false, false),
MOUSE_2 (null, null, null, false, true, false),
MOUSE_1_2 (null, null, null, true, true, false),
UP_LEFT (true, true, null, false, false, false),
DOWN_LEFT (false, true, null, false, false, false),
... // some 108(?) combinations listed here.... ;
private final Boolean upDown;
private final Boolean leftRight;
private final Boolean turnQE;
private final boolean mouse1;
private final boolean mouse2;
private final boolean stop;
AiMove(Boolean upDown, Boolean leftRight, Boolean turnQE,
boolean mouse1, boolean mouse2, boolean stop) {
this.upDown = upDown;
this.leftRight = leftRight;
this.turnQE = turnQE;
this.mouse1 = mouse1;
this.mouse2 = mouse2;
this.stop = stop;
}
public boolean isStopped() { return stop; }
public boolean hasUp() { return Boolean.TRUE.equals(upDown); }
public boolean hasDown() { return Boolean.FALSE.equals(upDown); }
public boolean hasLeft() { return Boolean.TRUE.equals(leftRight); }
public boolean hasRight() { return Boolean.FALSE.equals(leftRight); }
public boolean hasTurnQ() { return Boolean.TRUE.equals(turnQE); }
public boolean hasTurnE() { return Boolean.FALSE.equals(turnQE); }
public boolean hasMouse1() { return mouse1; }
public boolean hasMouse2() { return mouse2; }
}
Personally I think if you go this route you may consider representing more detailed states than some internal controller inputs.
Also you could get fancy with bit flags or masking, or the cleaner looking, non-fancy, EnumSet<SingleAiMoves>
but unlike the enum above, and like your array and the BitSet
, that would allow states that go both up and down, and/or left and right to exist.
Lastly, because I find writing the enum of supposedly valid combinations tedious and relatively unreadable in my above example, you can use a BitSet inside the enum and clarify the construction of the enum. It probably uses a bit more memory this way.
public enum AiMove {
NONE (),
UP (0),
DOWN (1),
LEFT (2),
RIGHT (3),
TURN_Q (4),
TURN_E (5),
MOUSE_1 (6),
MOUSE_2 (7),
STOP (8),
MOUSE_1_2 (MOUSE_1, MOUSE_2),
UP_LEFT (UP, LEFT),
DOWN_LEFT (DOWN, LEFT),
... // some 108(?) combinations listed here.... ;
private final BitSet bitsUDLRQE12S = new BitSet(9);
AiMove(int index) {
bitsUDLRQE12S.set(index);
}
AiMove(AiMove... moves) {
for (AiMove move : moves) {
bitsUDLRQE12S.or(move.getBitSet());
}
}
private BitSet getBitSet() { return bitsUDLRQE12S; }
public boolean hasUp() { return bitsUDLRQE12S.get(0); }
public boolean hasDown() { return bitsUDLRQE12S.get(1); }
public boolean hasLeft() { return bitsUDLRQE12S.get(2); }
public boolean hasRight() { return bitsUDLRQE12S.get(3); }
public boolean hasTurnQ() { return bitsUDLRQE12S.get(4); }
public boolean hasTurnE() { return bitsUDLRQE12S.get(5); }
public boolean hasMouse1() { return bitsUDLRQE12S.get(6); }
public boolean hasMouse2() { return bitsUDLRQE12S.get(7); }
public boolean isStopped() { return bitsUDLRQE12S.get(8); }
}