From 2392d36dfa15a262d69aeb6356408d90d5a32ab2 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 11 May 2016 13:41:25 +0200 Subject: Add PlayerAction and PlayerID byte conversion --- src/ch/epfl/xblast/PlayerAction.java | 13 ++++++++++++- src/ch/epfl/xblast/PlayerID.java | 13 +++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/ch/epfl/xblast/PlayerAction.java b/src/ch/epfl/xblast/PlayerAction.java index 703bc9d..67920eb 100644 --- a/src/ch/epfl/xblast/PlayerAction.java +++ b/src/ch/epfl/xblast/PlayerAction.java @@ -14,6 +14,17 @@ public enum PlayerAction { MOVE_S, MOVE_W, STOP, - DROP_BOMB + DROP_BOMB; + + public static PlayerAction fromByte(byte b) { + if ((int) b < 0 || (int) b >= PlayerAction.values().length) + throw new IllegalArgumentException(); + + return PlayerAction.values()[(int) b]; + } + + public byte toByte() { + return (byte) this.ordinal(); + } } diff --git a/src/ch/epfl/xblast/PlayerID.java b/src/ch/epfl/xblast/PlayerID.java index 9377fdf..96b98e4 100644 --- a/src/ch/epfl/xblast/PlayerID.java +++ b/src/ch/epfl/xblast/PlayerID.java @@ -13,6 +13,8 @@ public enum PlayerID { PLAYER_3, PLAYER_4; + public static final byte OBSERVER = -1; + public Cell initialPosition() { switch (this) { case PLAYER_1: @@ -28,4 +30,15 @@ public enum PlayerID { } } + public static PlayerID fromByte(byte b) { + if ((int) b < 0 || (int) b >= PlayerAction.values().length) + throw new IllegalArgumentException(); + + return PlayerID.values()[(int) b]; + } + + public byte toByte() { + return (byte) this.ordinal(); + } + } -- cgit v1.2.3