From 1a09945128ecd2ec786fd503bec913982847b3e7 Mon Sep 17 00:00:00 2001
From: hirwamihigo <raissa.hirwamihigo@student.uclouvain.be>
Date: Sun, 12 Dec 2021 23:06:08 +0100
Subject: [PATCH] Merged the simultanious and corrected various problems in the
 GUI

---
 GUI.oz   |  28 ++++++------
 Input.oz |   5 +++
 Main.oz  | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 149 insertions(+), 15 deletions(-)

diff --git a/GUI.oz b/GUI.oz
index 7f69fc3..381fd93 100644
--- a/GUI.oz
+++ b/GUI.oz
@@ -47,21 +47,21 @@ in
 		{Window show}
 
 		% configure rows and set headers
-		{Grid rowconfigure(1 minsize:50 weight:0 pad:5)}
+		{Grid rowconfigure(1 minsize:50 weight:0 pad:2)}
 		for N in 1..NRow do
-			{Grid rowconfigure(N+1 minsize:50 weight:0 pad:5)}
+			{Grid rowconfigure(N+1 minsize:50 weight:0 pad:2)}
 			{Grid configure({Label N} row:N+1 column:1 sticky:wesn)}
 		end
 		% configure columns and set headers
-		{Grid columnconfigure(1 minsize:50 weight:0 pad:5)}
+		{Grid columnconfigure(1 minsize:50 weight:0 pad:2)}
 		for N in 1..NColumn do
-			{Grid columnconfigure(N+1 minsize:50 weight:0 pad:5)}
+			{Grid columnconfigure(N+1 minsize:50 weight:0 pad:2)}
 			{Grid configure({Label N} row:1 column:N+1 sticky:wesn)}
 		end
 		% configure scoreboard
-		{GridScore rowconfigure(1 minsize:50 weight:0 pad:5)}
+		{GridScore rowconfigure(1 minsize:50 weight:0 pad:2)}
 		for N in 1..(Input.nbPlayer) do
-			{GridScore columnconfigure(N minsize:50 weight:0 pad:5)}
+			{GridScore columnconfigure(N minsize:50 weight:0 pad:2)}
 		end
 
 		{DrawMap Grid}
@@ -76,7 +76,7 @@ in
 
 %%%%% Labels for rows and columns
 	fun{Label V}
-		label(text:V borderwidth:5 relief:raised bg:c(255 51 51) ipadx:5 ipady:5)
+		label(text:V borderwidth:5 relief:raised bg:c(255 51 51) ipadx:5 ipady:3)
 	end
 
 %%%%% Function to draw the map
@@ -108,8 +108,8 @@ in
 		pt(x:X y:Y) = Position
 		id(id:Id color:Color name:_) = ID
 
-		LabelSub = label(text:"S" handle:Handle borderwidth:5 relief:raised bg:Color ipadx:5 ipady:5)
-		LabelScore = label(text:Input.maxDamage borderwidth:5 handle:HandleScore relief:solid bg:Color ipadx:5 ipady:5)
+		LabelSub = label(text:"S" handle:Handle borderwidth:2 relief:raised bg:Color ipadx:2 ipady:2)
+		LabelScore = label(text:Input.maxDamage borderwidth:2 handle:HandleScore relief:solid bg:Color ipadx:2 ipady:2)
 		HandlePath = {DrawPath Grid Color X Y}
 		{Grid.grid configure(LabelSub row:X+1 column:Y+1 sticky:wesn)}
 		{Grid.score configure(LabelScore row:1 column:Id sticky:wesn)}
@@ -139,7 +139,7 @@ in
 			in
 			guiPlayer(id:ID score:HandleScore submarine:Handle mines:Mine path:Path) = State
 			pt(x:X y:Y) = Position
-			LabelMine = label(text:"M" handle:HandleMine borderwidth:5 relief:raised bg:ID.color ipadx:5 ipady:5)
+			LabelMine = label(text:"M" handle:HandleMine borderwidth:5 relief:raised bg:ID.color ipadx:2 ipady:2)
 			{Grid.grid configure(LabelMine row:X+1 column:Y+1)}
 			{HandleMine 'raise'()}
 			{Handle 'raise'()}
@@ -152,8 +152,8 @@ in
 			case List
 			of nil then nil
 			[] H|T then
-				if (H.1 == Position) then
-					{RemoveItem Grid H.2}
+				if (H.2 == Position) then %ERREUR CORRIGE
+					{RemoveItem Grid H.1}
 					T
 				else
 					H|{RmMine Grid Position T}
@@ -221,9 +221,9 @@ in
 	fun{RemovePlayer Grid WantedID State}
 		case State
 		of nil then nil
-		[] guiPlayer(id:ID score:HandleScore submarine:Handle mines:M path:P)|Next then
-			{HandleScore set(0)}
+		[] guiPlayer(id:ID score:HandleScore submarine:Handle mines:M path:P)|Next then	
 			if (ID == WantedID) then
+				{HandleScore set(0)} %ERREUR CORRIGE
 				for H in P do
 			 		{RemoveItem Grid H}
 				end
diff --git a/Input.oz b/Input.oz
index 2a56b7d..1fb664b 100644
--- a/Input.oz
+++ b/Input.oz
@@ -121,6 +121,11 @@ in
 %%%% Waiting time for the GUI between each effect %%%%
     GUIDelay = 100 % ms
 
+
+%%%%% ThinkMaxn ThinkMin 
+    ThinkMax = 2
+    ThinkMin = 1
+
 %%%% Description of the map %%%%
    
 NRow = ({OS.rand} mod 5) + (NbPlayers+(NbPlayers div 2))
diff --git a/Main.oz b/Main.oz
index 49e4434..9adface 100644
--- a/Main.oz
+++ b/Main.oz
@@ -67,6 +67,10 @@ define
         end
     end
 
+    % 1. Check if the submarine can play or at the surface.
+    % 2. If this is the first round, or if in the previous turn the submarine was surface, 
+    %    send the dive message to the submarine
+
     %% NextPlayers      : The next players to play
     %% CurrentIndex     : The index of the next player
     proc {TurnByTurn2 Players CurrentIndex}
@@ -86,6 +90,7 @@ define
         end
     end
 
+    %  Broadcasting the Message to the other players
     %% Players          : List of players cell
     %% Msg              : The message to broadcast
     proc {Broadcast Players Msg}
@@ -96,6 +101,10 @@ define
         end
     end
 
+    % Manage the explosion of a Mine
+    % 1. Send messages to all players that a mine has explode
+    % 2. Broadcast if a player has died
+    % 3. Broadcast if a player has been damaged
     proc {ManageMineExplosion Id Players Position}
         {List.forAll Players    proc{$ Player} Resp in
                                     {Send Player.port sayMineExplode(Id Position Resp)}
@@ -110,6 +119,10 @@ define
                                 end}
     end
 
+    % Manage the explosion of a Missile
+    % 1. Send messages to all players that a missile has explode
+    % 2. Broadcast if a player has died
+    % 3. Broadcast if a player has been damaged
     proc {ManageMissileExplosion Id Players Position}
         {List.forAll Players    proc{$ Player} Resp in
                                     {Send Player.port sayMissileExplode(Id Position Resp)}
@@ -125,6 +138,9 @@ define
     end
 
 
+    % Manage the Drone firing
+    % 1. Send messages to all players that a drone is passing
+    % 2. Send to the sender all answers
     proc{ManagingDroneFiring Players SenderPlayer Drone}
         {List.forAll Players    proc{$ Player} ID Resp in
                                     {Send Player.port sayPassingDrone(Drone ID Resp)}
@@ -132,6 +148,11 @@ define
                                 end}
     end
 
+
+
+    % Manage the Sonar firing
+    % 1. Send messages to all players that a sonar is passing
+    % 2. Send to the sender all answers
     proc{ManagingSonarFiring Players SenderPlayer}
         {List.forAll Players    proc{$ Player} ID Resp in
                                     {Send Player.port sayPassingSonar(ID Resp)}
@@ -139,6 +160,9 @@ define
                                 end}
     end
 
+    % 3. The submarine choose the direction
+    % 4. [If surface] wait for turnSurface turns
+    % 5. [else] Broadcast the direction to the radio
     proc {TurnByTurn345 Players CurrentIndex}
         CurrentPlayer={List.nth Players CurrentIndex}
         ID Pos Direction NewPlayers
@@ -162,6 +186,7 @@ define
         end
     end
 
+    % 6. Submarine can charge an Item
     proc {TurnByTurn6 Players CurrentIndex}
         CurrentPlayer={List.nth Players CurrentIndex}
         ID KindItem 
@@ -179,6 +204,7 @@ define
         end
     end
 
+    % 7/8. Submarine can fire and/or explode an item
     proc {TurnByTurn78 Players CurrentIndex}
         CurrentPlayer={List.nth Players CurrentIndex}
         ID KindFire
@@ -193,8 +219,8 @@ define
                 {TurnByTurn Players (CurrentIndex mod Input.nbPlayer)+1}
             [] mine(PosMineExploded) then  
                 % {Send GUI_PORT explosion(Id PosMineExploded)} 
-                {Send GUI_PORT removeMine(Id PosMineExploded)}
                 {ManageMineExplosion Id Players PosMineExploded}
+                {Send GUI_PORT removeMine(Id PosMineExploded)}
                 {TurnByTurn Players (CurrentIndex mod Input.nbPlayer)+1}
             end
         [] missile(Position) then 
@@ -215,6 +241,107 @@ define
         %% 1.
         {TurnByTurn1 Players CurrentIndex}
     end
+
+    %-----------------------SIMULTANEOUS FUNCTIONS------------------------------------
+
+    % The beginning
+    proc{Simultaneous Player PlayersList}
+        if {WeHaveAWinner PlayersList 0} then 
+            skip 
+        else
+            {Simultaneous12 Player PlayersList}
+        end
+    end
+
+    % Submarine dives and thinks
+    proc {Simultaneous12 Player PlayersList}
+        {Send Player.port dive}
+        {Delay Input.thinkMax * 100}
+        {Simultaneous3456 Player PlayersList}
+    end
+
+    % 3. Submarine chooses the direction
+    % 4. [If surface] Restart the game after turnSurface delay
+    % 5. [else] Broadcast the direction to the radio
+    % 6. Think
+    proc{Simultaneous3456 Player PlayersList}
+        ID Pos Direction
+        fun{DirectionOnSurface Player} player(port:Player.port nbSurface:1) end
+    in
+        {Send Player.port move(ID Pos Direction)}
+        case ID#Direction
+        of null#_ then
+            % When player dead , restart the game for him
+            {Simultaneous Player PlayersList}
+        [] _#surface then 
+            {Broadcast PlayersList saySurface(ID)}
+            {Send GUI_PORT surface(ID)}
+            {Delay Input.turnSurface * 100}
+            {Simultaneous Player PlayersList}
+        else
+            {Broadcast PlayersList sayMove(ID Direction)}
+            {Send GUI_PORT movePlayer(ID Pos)}
+            {Delay Input.thinkMax * 100}
+            {Simultaneous78 Player PlayersList}
+
+        end
+    end
+
+    % 7. Submarine charge an item
+    % 8. Think
+    proc{Simultaneous78 Player PlayersList}
+        ID KindItem
+    in
+        {Send Player.port chargeItem(ID KindItem)}
+        case KindItem of null then 
+            {Delay Input.thinkMax * 100}
+            {Simultaneous91011 Player PlayersList}
+        else
+            {Broadcast PlayersList sayCharge(ID KindItem)}
+            {Delay Input.thinkMax * 100}
+            {Simultaneous91011 Player PlayersList}
+        end
+
+    end
+
+    % 9. Submarine fire and/or explode an item
+    % 10. Think
+
+    proc{Simultaneous91011 Player PlayersList}
+        ID KindFire
+    in
+        {Send Player.port fireItem(ID KindFire)}
+        case KindFire of null then
+            {Delay Input.thinkMax * 100}
+            {Simultaneous Player PlayersList}
+        [] mine(Position) then
+            Id Mine 
+        in
+            {Send GUI_PORT putMine(ID Position)}
+            {Broadcast PlayersList sayMinePlaced(ID)}
+            {Send Player.port fireMine(Id Mine)}
+            case Mine of null then 
+                {Delay Input.thinkMax * 100}
+                {Simultaneous Player PlayersList}
+            [] mine(PosMineExploded) then 
+                {Delay Input.thinkMax * 100}
+                {Send GUI_PORT removeMine(Id PosMineExploded)}
+                {ManageMineExplosion Id PlayersList Position}
+                {Simultaneous Player PlayersList}
+            end
+        [] missile(Position) then
+            {ManageMissileExplosion ID PlayersList Position}
+            {Simultaneous Player PlayersList}
+        [] drone(U V) then
+            {Send GUI_PORT drone(ID KindFire)}
+            {ManagingDroneFiring PlayersList Player KindFire}
+            {Simultaneous Player PlayersList}
+        [] sonar then 
+            {Send GUI_PORT sonar(ID)}
+            {ManagingSonarFiring PlayersList Player}
+            {Simultaneous Player PlayersList}
+        end
+    end
 in
     %%1. 
     {Send GUI_PORT buildWindow}
@@ -236,6 +363,8 @@ in
     %%4. Launch the game 
     if Input.isTurnByTurn then 
         {TurnByTurn PlayersList 1}
+    else
+        {List.forAll PlayersList proc{$ Player} thread {Simultaneous Player PlayersList} end end}
     end
 
 end
-- 
GitLab