/**
   This class represents a Battleship Game.  It includes most of the parsing of commands sent to the server. 
   
   /home1/ugrads/t172q/CS2013/BGServer/BGGame.java
   
   Created: Wed May 19 15:39:03 2004
   
   @author  Stuart MacGillivray #3148021 (email: <t172q@unb.ca>)
   
*/

import java.util.StringTokenizer;

public class BGGame
{
  private static final int MAXPLAYERS = 2;
  private BGPlayer[] players;
  private int currentTurn;

  public BGGame () 
  {
    players = new BGPlayer[MAXPLAYERS];
    currentTurn = 0;
  }

  public String createPlayer(String shipsIn, int currentSocket)
  {
    try
      {
	players[currentSocket] = new BGPlayer();
	StringTokenizer shipList = new StringTokenizer(shipsIn, ",");
	while (shipList.hasMoreTokens())
	  {
	    StringTokenizer coordList = new StringTokenizer(shipList.nextToken(), " ");
	    String shipType = coordList.nextToken();
	    int shipLength;

	    if (shipType.equals("AC")) shipLength = 5;
	    else if (shipType.equals("CR")) shipLength = 4;
	    else if (shipType.equals("SB")) shipLength = 3;
	    else if (shipType.equals("FR")) shipLength = 2;
	    else throw new BGErrorException();
	    
	    int[][] coords = new int[shipLength][2];

	    for (int i = 0; i < shipLength; i++)
	      {
		if (!coordList.hasMoreTokens()) throw new BGErrorException();
		String loc = coordList.nextToken();
		coords[i][0] = (int)(loc.charAt(0) - 'A');
		coords[i][1] = (int)(loc.charAt(1) - '0');	
	      }
	    if (coordList.hasMoreTokens()) throw new BGErrorException();
	    players[currentSocket].placeShip(coords);
	  }
	
	return "" + (currentSocket + 1);
      }
    catch (BGErrorException e)
      {
	return "err";
      }
  }

  public String[] fireAt (String coordinates)
  {
    coordinates = coordinates.trim().toUpperCase();
    String[] response = new String[MAXPLAYERS];
    if (coordinates.length() != 2)
      {
	response[currentTurn] = "err";
	return response;
      }
    int x = (int)(coordinates.charAt(0) - 'A');
    int y = (int)(coordinates.charAt(1) - '0');
    int result = 0;

    try
      {
	result = players[(currentTurn + 1) % MAXPLAYERS].fireAtLoc(x, y);
      }
    catch (BGErrorException e)
      {
	response[currentTurn] = "err";
	return response;
      }
    finally
      {
	for (int i = 0; i < MAXPLAYERS; i++)
      {
	if (i != currentTurn)
	  {
	    response[i] = coordinates + ",";
	  }
	else
	  {
	    response[i] = "";
	  }
	switch (result)
	  {
	  case 0:
	    response[i] += "miss";
	    break;
	  case 1:
	    response[i] += "hit";
	    break;
	  case 2:
	    response[i] += "sunk FR";
	    break;
	  case 3:
	    response[i] += "sunk SB";
	    break;
	  case 4:
	    response[i] += "sunk CR";
	    break;
	  case 5:
	    response[i] += "sunk AC";
	    break;
	  }
	if (players[(currentTurn + 1) % MAXPLAYERS].isDefeated())
	  {
	    if (currentTurn == i)
	      response[i] += ",you won";
	    else
	      response[i] += ",you lost";
	  }
      }
    currentTurn = (currentTurn + 1)%MAXPLAYERS;
    return response;
      }
  }

  public boolean gameOn()
  {
    return !(players[0] == null || players[1] == null ||
	     players[0].isDefeated() || players[1].isDefeated());
  }
  
  public int getCurrent()
  {
    return currentTurn;
  }
  
}// BGGame
