/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boardgame;

import java.util.ArrayList;
import java.util.TreeSet;

/**
 *
 * @author csstudent
 */
public class TurnPhases {
    
    private static Game g;
    
    public static void setConnectedGame(Game game) {
        g = game;
    }
    
    public TurnPhases() {
    }
    
    public static void reinforce(Player p) {
        g.setCurrentPhase("reinforce");
        int numArmies = p.getUnitsPerTurn();
        //g.outText("Place armies on your territories. Left: " + numArmies);
        Territory t = new Territory(0, "", null);
        for (int i = 0; i < numArmies; i++) {
            t = new Territory(0, "", null);
            while (!(t.getOccupied().equals(p))) {
                //wait for player to click country and save it to 't'
            }
            t.changeUnits(t.getUnits()+ 1);
        }
    }
    
    public static void attack(Player p) {
        g.setCurrentPhase("attack");
        //g.outText("To attack, first select the territory you would like to attack from");
        Territory from = new Territory(0, "", null); 
        while (!(from.getOccupied().equals(p) && from.getUnits()>=2)) {
            //wait for player to click country and save it to 't'
            //If ENDPHASE button is pressed, return
        }
        Territory to = new Territory(0, "", null);
        while (to.getOccupied().equals(p) || !(from.isAdjacent(to))) {
            //wait for player to click country and save it to 't'
        }
        while (to.getUnits() > 0 && from.getUnits() > 0) {
            ArrayList<Integer> attackingDice = rollNumDice(Math.min(from.getUnits() - 1, 3));
            ArrayList<Integer> defendingDice = rollNumDice(Math.min(to.getUnits() - 1, 2));
            for (int i = 0; i < Math.min(attackingDice.size(), defendingDice.size()); i++) {
                if (attackingDice.get(i) > defendingDice.get(i)) {
                    to.changeUnits(to.getUnits()-1);
                } else {
                    from.changeUnits(from.getUnits()-1);
                }
                //wait for player to continue attack or press ENDPHASE(retreat) button
            }
        }
    }
    
    public static void move(Player p) {
        g.setCurrentPhase("move");
    }
    
    public static void takeTurn(Player p) {
        reinforce(p);
        attack(p);
        move(p);
        g.setCurrentTurn((g.getCurrentTurn()+1) % g.getNumPlayers());
    }
    
    public static int diceRoll() {
        return (int)Math.random()*6 + 1;
    }
    public static ArrayList<Integer> rollNumDice(int num) {
        TreeSet<Integer> sorted = new TreeSet<Integer>();
        for (int i = 0; i < num; i++) {
            sorted.add(diceRoll());
        }
        ArrayList<Integer> out = new ArrayList<Integer>();
        for (int i : sorted) {
            out.add(i);
        }
        return out;
    }
}

@@ -1181,24 +1181,51 @@
 ack from%22);%0A
+        while (true) %7B%0A    
         Terr
@@ -1266,16 +1266,20 @@
 null); %0A
+    
         
@@ -1346,32 +1346,36 @@
 ) %7B%0A            
+ 
+   
 //wait for playe
@@ -1424,16 +1424,20 @@
         
+   
+ 
 //If END
@@ -1468,32 +1468,36 @@
  return%0A        
+    
 %7D%0A        Territ
@@ -1482,32 +1482,36 @@
       %7D%0A        
+ 
+   
 Territory to = n
@@ -1537,32 +1537,36 @@
  null);%0A        
+   
+ 
 while (to.getOcc
@@ -1616,32 +1616,36 @@
 ) %7B%0A            
+    
 //wait for playe
@@ -1682,34 +1682,42 @@
  to 't'%0A        
+  
-%7D%0A
+  %7D%0A    
         while (t
@@ -1763,32 +1763,36 @@
 ) %7B%0A            
+    
 ArrayList%3CIntege
@@ -1861,32 +1861,36 @@
 ));%0A            
+    
 ArrayList%3CIntege
@@ -1949,24 +1949,28 @@
 ) - 1, 2));%0A
+    
             
@@ -2066,16 +2066,20 @@
         
+    
 if (atta
@@ -2141,16 +2141,20 @@
         
+    
 to.chang
@@ -2170,32 +2170,36 @@
 .getUnits()-1);%0A
+    
                 
@@ -2227,16 +2227,20 @@
         
+    
 from.cha
@@ -2276,34 +2276,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -2381,16 +2381,34 @@
  button%0A
+                %7D%0A
         
@@ -2505,24 +2505,694 @@
 se(%22move%22);%0A
+        Territory from = new Territory(0, %22%22, null);%0A        while (!from.getOccupied().equals(p) %7C%7C from.getUnits() %3C 2) %7B%0A            //wait for player to click country and save it to 't'%0A        %7D%0A        Territory to = new Territory(0, %22%22, null);%0A        while (!from.isAdjacent(to) %7C%7C !to.getOccupied().equals(p)) %7B%0A            //wait for player to click country and save it to 't'%0A        %7D%0A        int units = -1;%0A        while (units %3C 1 %7C%7C units %3E= from.getUnits()) %7B%0A            //Ask player how many units they want to move and save it to int 'units'%0A        %7D%0A        from.changeUnits(from.getUnits() - units);%0A        to.changeUnits(to.getUnits() + units);%0A
     %7D%0A    %0A 

Add move method, loop attack method