Example #1
0
 void Start()
 {
     CellController.Locked = false;
     CellController.Attack = false;
     ships            = new List <Ship>();
     playerController = GetComponent <PlayerController>();
     ShipAmount.Init();
 }
Example #2
0
    public int ValidateMap()
    {
        gameController.ships.Clear();
        ShipAmount.Init();
        List <CellController> availableCells = CellController.SelectedCells.ToList();

        foreach (CellController cell in CellController.SelectedCells)
        {
            if (availableCells.IndexOf(cell) != -1)
            {
                availableCells.Remove(cell);
                Debug.Log(cell.name);

                List <CellController> shipCells = new List <CellController>();

                for (int i = cell.ID_i - 1; i >= 0; i--)
                {
                    if (Cells[i][cell.ID_j].state != CellController.CellState.Selected)
                    {
                        break;
                    }

                    if (!CheckAvailability(shipCells.Count()))
                    {
                        break;
                    }

                    if (availableCells.IndexOf(Cells[i][cell.ID_j]) != -1)
                    {
                        CellController c = Cells[i][cell.ID_j];
                        Debug.Log(c.name);
                        availableCells.Remove(c);
                        shipCells.Add(c);
                    }
                }

                for (int i = cell.ID_i + 1; i < GRID_SIZE; i++)
                {
                    if (Cells[i][cell.ID_j].state != CellController.CellState.Selected)
                    {
                        break;
                    }

                    if (!CheckAvailability(shipCells.Count()))
                    {
                        break;
                    }

                    if (availableCells.IndexOf(Cells[i][cell.ID_j]) != -1)
                    {
                        CellController c = Cells[i][cell.ID_j];
                        Debug.Log(c.name);
                        availableCells.Remove(c);
                        shipCells.Add(c);
                    }
                }

                if (shipCells.Count == 0)
                {
                    for (int j = cell.ID_j - 1; j >= 0; j--)
                    {
                        if (Cells[cell.ID_i][j].state != CellController.CellState.Selected)
                        {
                            break;
                        }

                        if (!CheckAvailability(shipCells.Count()))
                        {
                            break;
                        }

                        if (availableCells.IndexOf(Cells[cell.ID_i][j]) != -1)
                        {
                            CellController c = Cells[cell.ID_i][j];
                            Debug.Log(c.name);
                            availableCells.Remove(c);
                            shipCells.Add(c);
                        }
                    }

                    for (int j = cell.ID_i + 1; j < GRID_SIZE; j++)
                    {
                        if (Cells[cell.ID_i][j].state != CellController.CellState.Selected)
                        {
                            break;
                        }

                        if (!CheckAvailability(shipCells.Count()))
                        {
                            break;
                        }

                        if (availableCells.IndexOf(Cells[cell.ID_i][j]) != -1)
                        {
                            CellController c = Cells[cell.ID_i][j];
                            Debug.Log(c.name);
                            availableCells.Remove(c);
                            shipCells.Add(c);
                        }
                    }
                }

                shipCells.Add(cell);

                bool invalidFormation = true;
                for (int x = ShipAmount.Types.Count() - 1; x >= 0; x--)
                {
                    Debug.Log("Type: " + ShipAmount.Types[x].PieceAmount + " => " + ShipAmount.Amount[x]);
                    if (ShipAmount.Amount[x] > 0 && ShipAmount.Types[x].PieceAmount == shipCells.Count)
                    {
                        ShipAmount.Amount[x]--;
                        invalidFormation = false;
                        break;
                    }
                }

                if (invalidFormation)
                {
                    return(404);
                }
                Ship ship = new Ship(shipCells.Count);
                ship.Cells = shipCells.ToArray();
                gameController.ships.Add(ship);
            }
        }

        //Debug.Log("SHIPS:");
        //foreach (Ship _s in gameController.ships)
        //    Debug.Log(_s.PieceAmount);

        if (CellController.SelectedCells.Count() < ShipAmount.TotalCellAmount)
        {
            return(100);
        }
        return(-1);
    }