/*
 Copyright (c) 2015 Justin White <jw@justinwhite.net>
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:

 1. Redistributions of source code must retain the above copyright notice,
 this list of conditions and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright notice,
 this list of conditions and the following disclaimer in the documentation
 and/or other materials provided with the distribution.

 3. Neither the name of the copyright holder nor the names of its
 contributors may be used to endorse or promote products derived from this
 software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.

 */

package net.justinwhite.scoreit_p10;

import java.util.ArrayList;
import java.util.UUID;

class Game {
    private final UUID id;
    private Integer numPlayers = 0;
    private String name = "";
    private ArrayList<Player> players = new ArrayList<Player>(0);
    private Integer winner;

    public Game(Integer _numPlayers) {
        this.id = UUID.randomUUID();
        for (Integer i = 0; i < _numPlayers; i++) {
            addPlayer(i);
        }
    }

    public String toString() {
        return String.format("Game: %s\nUUID: %s\nPlayer count: %d\nPlayers: %s\n",
                             getName(),
                             getID(),
                             getNumPlayers(),
                             players
        );
    }

    public UUID getID() {
        return id;
    }

    public void buildName() {
        name = "";
        for (Player p : players) {
            name += p.getInitials();
        }
    }

    public String getName() {
        return name;
    }

    public Integer getNumPlayers() {
        return numPlayers;
    }

    public void addPlayer(Integer _index, String _name) {
        players.ensureCapacity(numPlayers + 1);
        Player newPlayer = new Player(_index);
        players.add(numPlayers++, newPlayer);
        newPlayer.setName(_name);
    }

    public void addPlayer(Integer _index) {
        addPlayer(_index, String.format("Player %d", numPlayers + 1));
    }

    public ArrayList<Player> getPlayerList() {
        return players;
    }

    public Player getPlayer(Integer index) {
        return players.get(index);
    }

    public void setWinner(Integer _index) {
        winner = _index;
    }



    class Player {
        private Integer index;
        private String name;
        private Integer score = 0;
        private Integer phase = 1;

        public Player(Integer _index) {
            index = _index;
            setName("Player X");
        }

        public Player(Integer _index, String _name) {
            index = _index;
            setName(_name);
        }

        public String toString() {
            return String.format(
                    "Name: '%s', Score: %d, Phase: %d",
                    getName(),
                    getScore(),
                    getPhase()
            );
        }

        public Integer getIndex() {
            return index;
        }
        public void setName(String _name) {
            name = _name;
            Game.this.buildName();
        }

        public String getName() {
            return name;
        }

        public String getInitial() {
            return name.substring(0, 1);
        }

        public String getInitials() {
            String names[] = name.split(" ");
            if (names.length > 1) {
                return String.join("", names[0].substring(0, 1), names[1].substring(0, 1));
            } else {
                return getInitial();
            }
        }

        public void addScore(Integer _score) {
            score += _score;
        }

        public Integer getScore() {
            return score;
        }

        public void nextPhase() {
            phase++;
            if (phase == 10) {
                Game.this.setWinner(this.getIndex());
            }
        }

        public Integer getPhase() {
            return phase;
        }
    }

}

@@ -1802,21 +1802,20 @@
 private 
-Integ
+Play
 er winne
@@ -3144,37 +3144,583 @@
 lic 
-void setWinner(Integ
+String getScores() %7B%0A        String out = %22%22;%0A        for (Player p : players) %7B%0A            out += String.format(%22%25s: %254d Points%22, p.getName(), p.getScore());%0A            if (p.getPhase() %3E 0) %7B%0A                out += String.format(%22, Phase #%25d completed%5Cn%22, p.getPhase());%0A            %7D else %7B%0A                out += %22%5Cn%22;%0A            %7D%0A        %7D%0A        return out;%0A    %7D%0A%0A    public Boolean hasWinner() %7B%0A        return winner != null;%0A    %7D%0A%0A    public Player getWinner() %7B%0A        return (winner != null) ? winner : null;%0A    %7D%0A%0A    public void setWinner(Play
 er _
+w
 in
-dex
+ner
 ) %7B%0A
@@ -3733,29 +3733,30 @@
   winner = _
+w
 in
-dex
+ner
 ;%0A    %7D%0A%0A%0A%0A 
@@ -3752,18 +3752,16 @@
 %0A    %7D%0A%0A
-%0A%0A
     clas
@@ -3898,17 +3898,17 @@
 phase = 
-1
+0
 ;%0A%0A     
@@ -4233,33 +4233,30 @@
 Name
-:
  '%25s'
-,
+;
  Score
-:
  %25
-d,
+s;
  Phase
-:
  %25d%22
@@ -4441,32 +4441,33 @@
 ndex;%0A        %7D%0A
+%0A
         public v
@@ -5257,17 +5257,17 @@
  (phase 
-=
+%3E
 = 10) %7B%0A
@@ -5310,19 +5310,8 @@
 this
-.getIndex()
 );%0A 

Add winner methods to Game; Phase indicates completed phases now.