Exemplo n.º 1
0
    public void LoadMap(string path)
    {
        using (StreamReader streamReader = new StreamReader(path))
        {
            string line = streamReader.ReadLine();
            ShipCounts = System.Array.ConvertAll(line.Trim().Split(), s => int.Parse(s));

            line = streamReader.ReadLine();
            int[] wh = System.Array.ConvertAll(line.Trim().Split(), s => int.Parse(s));
            width  = wh[0];
            height = wh[1];
            Tiles  = new Tile.ETile[height, width];
            Ships  = new Ship.EShip[height, width];

            Debug.Log($"Loading map: B{ShipCounts[(int)Ship.EShip.BATTLESHIP]} C{ShipCounts[(int)Ship.EShip.CRUISER]} D{ShipCounts[(int)Ship.EShip.DESTROYER]} {width}X{height}");

            int y = height - 1;
            while ((line = streamReader.ReadLine()) != null)
            {
                int[] vals = System.Array.ConvertAll(line.Trim().Split(), s => int.Parse(s));
                int   x    = 0;
                foreach (int val in vals)
                {
                    Tiles[y, x] = (Tile.ETile)val;
                    x++;
                }
                y--;
                if (y == -1)
                {
                    break;           // 이후 파일 내용 무시
                }
            }
        }
    }
Exemplo n.º 2
0
    /// <summary>
    /// 위치 hex에 방향 eDirec으로 함선 eShip을 배치할 수 있는지 확인합니다.
    /// </summary>
    public bool CanPlace(Ship.EShip eShip, Hex _hex, Hex.EDirec eDirec)
    {
        Hex hex = new Hex(_hex);  // Clone

        int shipSize = Ship.GetSizeOf(eShip);

        for (int i = 0; i < shipSize; i++)
        {
            if (!IsValidTile(hex))
            {
                Debug.Log("Tile out of bound");
                return(false);
            }

            Tile.ETile eTile = Tiles[hex.y, hex.x];
            Debug.Log($"{i}: {hex}, {eTile}");

            if (Ships[hex.y, hex.x] != Ship.EShip.NONE)
            {
                Debug.Log("There is already another ship in the place");
                return(false);
            }

            switch (eShip)
            {
            case Ship.EShip.BATTLESHIP:
            case Ship.EShip.CRUISER:
                if (eTile != Tile.ETile.OCEAN)
                {
                    return(false);
                }
                break;

            case Ship.EShip.DESTROYER:
                if (eTile != Tile.ETile.OCEAN && eTile != Tile.ETile.SHORE)
                {
                    return(false);
                }
                break;

            default:
                throw new System.ComponentModel.InvalidEnumArgumentException();
            }

            hex.Move(eDirec);  // 방향에 따른 hex 값 조정
        }

        return(true);
    }