Esempio n. 1
0
// ShipData 구조체를 사용해서 맵 데이터 생성하는 방법
void MakeMapData2(char* const mapData)
{
	ShipData shipData;

	char map[MAP_SIZE] = { 0, };

	int size, sx, sy, dir, listIdx = 0;
	bool placeable;
	Coord posArr[MAX_SHIP_LEN];	// ShipData에 넣을 배의 위치 배열

	for (int type = MD_NONE + 1; type < MD_END; ++type)
	{
		while (true)
		{
			size = ShipData::SHIP_LEN[type];
			placeable = true;
			dir = rand() % 2;
			if (dir == 0) // hori
			{
				sx = rand() % (MAP_WIDTH - size);
				sy = rand() % MAP_HEIGHT;
			}
			else // vert
			{
				sx = rand() % MAP_WIDTH;
				sy = rand() % (MAP_HEIGHT - size);
			}

			for (int i = 0; i < size && placeable; ++i)
			{
				if (dir == 0 && map[sx + i + sy * MAP_WIDTH])
					placeable = false;
				else if (dir == 1 && map[sx + (sy + i) * MAP_WIDTH])
					placeable = false;
			}

			if (placeable)
				break;
		}

		// 1.2 배의 좌표 배열을 가져와서...
		 Coord* shipPosArr = shipData.GetShipCoordArray((ShipType)type);

		for (int i = 0; i < size && placeable; ++i)
		{
			int x, y;
			Coord coord;
			if (dir == 0) { x = sx + i; y = sy; }
			else  { x = sx; y = sy + i; }
			map[x + y * MAP_WIDTH] = type;

			coord = Coord(x, y);
			// 1. 배의 좌표를 하나씩 넣는 방법
			// 1.1. 함수 사용
			shipData.SetShipCoord((ShipType)type, i, coord);
			// 1.2. 배열을 가져와서 넣기
			// shipPosArr[i] = coord;

			// 2. Coord 배열을 만들어서...
			// posArr[i] = coord;
		}

		// 2. 배의 좌표를 배열로 한 번에 넣는 방법
		// shipData.SetShip((ShipType)type, posArr);

		// 3. 배의 시작지점과 방향만 넣는 방법
		// shipData.SetShip((ShipType)type, Coord(sx, sy), dir==0 ? DIR_HORIZONTAL:DIR_VERTICAL);
	}

	// 배의 배치를 맵 데이터로 변환
	shipData.ToMapData(mapData);
}