How to Develop a Method to Return Pegs to topmost empty Positions?

I’m working on a game in Unity where I have a method ‘FindTopmostPeg’ that finds and removes the topmost peg from a list of positions in a specific quadrant of a grid. Here’s the method:

public GameObject FindTopmostPeg(List<Vector3Int> positions, string quadrant)
{
    Vector3Int topmostPosition = new Vector3Int(0, int.MinValue, 0);
    GameObject topmostPeg = null;

    foreach (Vector3Int position in positions)
    {
        if (pegInventory.TryGetValue(position, out GameObject peg) && peg != null && position.y > topmostPosition.y)
        {
            topmostPosition = position;
            topmostPeg = peg;
        } 
    }

    // Remove the peg from the pegInventory
    if (topmostPeg != null)
    {
        pegInventory[topmostPosition] = null;
        lastTopmostPositions[quadrant] = topmostPosition;
    }

    return topmostPeg;
}

I’m trying to develop a method ‘CapturePegs’ that will return pegs to the topmost empty positions. This method should work similarly to ‘FindTopmostPeg’, but instead of removing a peg from the topmost position, it should place a peg at the topmost empty position.

Here is the ‘CapturePegs’ method I currently have:

public void CapturePegs(GameObject loopPeg, List<Vector3Int> positions, string quadrant)
{
    Vector3Int topmostEmptyPosition = new Vector3Int(0, int.MinValue, 0);

    // Iterate over the positions from first to last to find the topmost empty position
    foreach (Vector3Int position in positions)
    {
        if (!pegInventory.ContainsKey(position) || pegInventory[position] == null)
        {
            // Update topmostEmptyPosition if the current position is higher
            if (position.y > topmostEmptyPosition.y)
            {
                topmostEmptyPosition = position;
            }
        }
    }

    // Check if the pegInventory already contains the topmostEmptyPosition
    if (pegInventory.ContainsKey(topmostEmptyPosition))
    {
        // If it does, update the peg at this position
        pegInventory[topmostEmptyPosition] = loopPeg;
    }
    else
    {
        // If it doesn't, add a new entry to the pegInventory
        pegInventory.Add(topmostEmptyPosition, loopPeg);
        // Update the lastTopmostPositions for the quadrant
        lastTopmostPositions[quadrant] = topmostEmptyPosition;

        Debug.Log($"<color=orange>[PegBehavior] Moved loopPeg: {loopPeg.name} back to position: {topmostEmptyPosition} in quadrant: {quadrant}</color>");
    }

    // Move the loopPeg to the topmostEmptyPosition
    loopPeg.transform.localScale *= 0.6f;
    Vector3 newPosition = pegSlotsTilemap.GetCellCenterWorld(topmostEmptyPosition);
    Vector3Int slotPosition = pegSlotsTilemap.WorldToCell(newPosition);
    slotPosition.z = -1;
    loopPeg.transform.position = slotPosition;

    // Update the lastTopmostPositions for the quadrant
    lastTopmostPositions[quadrant] = topmostEmptyPosition;

        // Create a StringBuilder to concatenate the lastTopmostPositions
    StringBuilder sb = new StringBuilder();
    sb.Append("Last topmost positions: ");
    foreach (var kvp in lastTopmostPositions)
    {
        sb.Append($"Quadrant: {kvp.Key}, Position: {kvp.Value}; ");
    }

    // Log the concatenated lastTopmostPositions
    Debug.LogError(sb.ToString());

    Debug.Log($"Moved loopPeg: {loopPeg.name} back to position: {topmostEmptyPosition} in quadrant: {quadrant}");
}

In the ‘CapturePegs’ method, I am trying to find the topmost empty position in a list of positions and place a loopPeg GameObject at this position:

  1. Initialize topmostEmptyPosition to a position with a y-coordinate of int.MinValue, which ensures that any position in the positions list will be higher than this initial position.

  2. Iterate over the positions list from first to last. For each position, check if it is empty (i.e., if it does not contain a peg in the pegInventory). If it is empty and its y-coordinate is higher than the y-coordinate of topmostEmptyPosition, update topmostEmptyPosition to this position.

  3. After finding the topmostEmptyPosition, check if the pegInventory already contains this position. If it does, update the peg at this position to loopPeg. If it doesn’t, add a new entry to the pegInventory with topmostEmptyPosition as the key and loopPeg as the value.

  4. Update the lastTopmostPositions for the given quadrant to topmostEmptyPosition.

  5. Move the loopPeg to the topmostEmptyPosition by setting loopPeg.transform.position to topmostEmptyPosition.

However, the pegs are not being placed in any coordinate that resembles the last coordinates they were removed from. Any help is appreciated, thanks!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật