From 80462fe831c39b577a37afa8476a48191fc8675e Mon Sep 17 00:00:00 2001
From: Francesco Bariatti <francesco.bariatti@insa-rennes.fr>
Date: Sat, 30 Apr 2016 17:21:23 +0200
Subject: [PATCH] Added function to GameState to query penguin positions and
 moves

---
 gui/src/controller/Controller.java   |  4 +-
 gui/src/controller/UpdateThread.java |  6 +--
 gui/src/model/GameState.java         | 72 ++++++++++++++++++++++++++--
 3 files changed, 73 insertions(+), 9 deletions(-)

diff --git a/gui/src/controller/Controller.java b/gui/src/controller/Controller.java
index 501b215..6c4c6d7 100644
--- a/gui/src/controller/Controller.java
+++ b/gui/src/controller/Controller.java
@@ -70,9 +70,7 @@ public class Controller implements Initializable
 		upT.start();
 
 		//We tell the game that we will start
-		gameInput.print("h");
-		gameInput.println();
-		gameInput.println("0");
+		gameInput.println("h");
 	}
 
 
diff --git a/gui/src/controller/UpdateThread.java b/gui/src/controller/UpdateThread.java
index fb2d517..14f84ca 100644
--- a/gui/src/controller/UpdateThread.java
+++ b/gui/src/controller/UpdateThread.java
@@ -26,7 +26,7 @@ public class UpdateThread extends Thread
 		this.board = board;
 		this.boardView = boardView;
 	}
-	
+
 
 	public void run()
 	{
@@ -52,8 +52,8 @@ public class UpdateThread extends Thread
 					}
 					for (int i = 0; i < 4; i++)
 					{
-						board[gameState.getPenguinPos(i, Player.Red)].setPenguinPresence(Tile.PenguinPresence.RED_PENGUIN);
-						board[gameState.getPenguinPos(i, Player.Blue)].setPenguinPresence(Tile.PenguinPresence.BLUE_PENGUIN);
+						board[gameState.getPenguinPos(Player.Red, i)].setPenguinPresence(Tile.PenguinPresence.RED_PENGUIN);
+						board[gameState.getPenguinPos(Player.Blue, i)].setPenguinPresence(Tile.PenguinPresence.BLUE_PENGUIN);
 					}
 
 					//UPDATE VIEW
diff --git a/gui/src/model/GameState.java b/gui/src/model/GameState.java
index df2dbec..ae3e8df 100644
--- a/gui/src/model/GameState.java
+++ b/gui/src/model/GameState.java
@@ -62,10 +62,76 @@ public class GameState
 		return 0;
 	}
 
-	public int getPenguinPos(int i, Player player)
+	/**
+	 * @param player The player for which we are searching the penguin position
+	 * @param i      The number of the penguin for this player
+	 * @return The position (0-59) of the penguin on the board
+	 */
+	public int getPenguinPos(Player player, int i)
+	{ return (penguins.get(player)[i] & 63); }
+
+	/**
+	 * @param player The penguin's owner
+	 * @param i The number of the penguin for this player
+	 * @return the total moves that this penguin can do
+	 */
+	public int getPenguinTotalMoves(Player player, int i)
+	{ return ((penguins.get(player)[i] >>> 6) & 63); }
+
+	public enum Direction
 	{
-		//TODO
-		return 0;
+		A,B,C,D,E,F;
+	}
+
+	/**
+	 * Get maximum number of tiles that a penguin can do in one specific direction
+	 */
+	public int getNbMoves(Player player, int penguinNumber, Direction direction)
+	{
+		int shift = 0;
+		switch (direction)
+		{
+			case A:
+				shift = 12;
+				break;
+			case B:
+				shift = 15;
+				break;
+			case C:
+				shift = 18;
+				break;
+			case D:
+				shift = 21;
+				break;
+			case E:
+				shift = 24;
+				break;
+			case F:
+				shift = 27;
+				break;
+		}
+		return ((penguins.get(player)[penguinNumber] >>> shift) & 7);
+	}
+
+	/**
+	 * "I want to move penguin 1 in direction B for 5 steps"
+	 * "This is you move number 10 out of all your possible moves"
+	 * Note: moves start at 0. Steps start at 1 for this function
+	 */
+	public int getPlayerMoveNumber(Player player, int penguinNumber, Direction direction, int nbSteps)
+	{
+		int[] peng = penguins.get(player);
+		int total = 0;
+		for(int i = 0; i < penguinNumber; i++) //We add the moves of all preceding penguins
+			total += getPenguinTotalMoves(player, i);
+		for(Direction d : Direction.values())
+		{
+			if(d.equals(direction))
+				return total+nbSteps-1;
+			else
+				total += getNbMoves(player, penguinNumber, d);
+		}
+		return -1;
 	}
 
 	public boolean getCan_play_red()
-- 
GitLab