Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1102589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:12:35+00:00 2026-05-17T01:12:35+00:00

Here’s my function: static Map AddFormation(Map _map, Tile tile, int x, int y, int

  • 0

Here’s my function:

static Map AddFormation(Map _map, Tile tile, int x, int y, int length, 
    Random rand, Tile endTile = (Tile)Int32.MaxValue)
{
    //so a call to AddFormation without the endTile will work, if I don't want a border.
    if ((int)endTile == Int32.MaxValue) endTile = tile;

    if (x >= 0 && x < _map.Data.GetLength(0) && y >= 0 && y < _map.Data.GetLength(1))
    {
        if (_map.Data[x, y].Tile != tile)
        {
            if (length > 0)
            {
                _map.Data[x, y].Tile = tile;
                int newlength = length - 1;
                AddFormation(_map, tile, x, y - 1, newlength, rand, endTile); // ^
                AddFormation(_map, tile, x, y + 1, newlength, rand, endTile); // v
                AddFormation(_map, tile, x - 1, y, newlength, rand, endTile); // <-
                AddFormation(_map, tile, x + 1, y, newlength, rand, endTile); // ->

            }
            else
            {
                _map.Data[x, y].Tile = endTile;
            }
        }
    }

    return _map;
}

I have a Tile enum which is to make my life easier when working with the tiles.
I have a Cell class which contains a Tile enum called “Tile” and other info (unimportant to this)
The Map class contains a Cell[,] group called Data.

What I am trying to achieve is to create a block of the specific tile at a specific point, I will later incorporate Randomisation into this (so it wouldn’t be just a diamond) but I took it out to see if that was the cause of my issue.

The problem is a call to this function always produces blocks taller than they are wide and I can’t for the life of me see why..

I created a test function to see what happens if I use something like:

    public static int[,] Add(int[,] grid, int x, int y, int length, int value)
    {

        if (x >= 0 && y >= 0 && x < grid.GetLength(0) && y < grid.GetLength(1))
        {
            if(grid[x,y] != value)
            {
                if(length > 0)
                {
                    grid[x, y] = value;

                    Add(grid, x - 1, y, length - 1, value);
                    Add(grid, x + 1, y, length - 1, value);
                    Add(grid, x, y - 1, length - 1, value);
                    Add(grid, x, y + 1, length - 1, value);
                }
            }
        }

        return grid;
    }

Which seems to suffer from the same problem if you go big enough (5 produces a perfect diamond, 6 produces a strange shape and something like 11 even stranger)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:12:36+00:00Added an answer on May 17, 2026 at 1:12 am

    Ok, after spending a long time on this (I do like recursion), here is partway to the solution (it may be hard to explain):

    The problem is that you are allowing the “path” to backtrack along the cells that have already been allocated as endTiles. If you take a look at your first method, you make the search point go down straight after it has searched up. You simply need to remove that.

    This is the code I am using (notice that it calls AddFormationAlt twice, once for going up, once for going down):

    class Program
    {
        static string left;
        static string right;
    
        static void Main(string[] args)
        {
            int size = 20;
            int sizem = size*2 + 1;
            Map m = new Map(new int[sizem,sizem]);
            AddFormationAlt(m, 1, size, size, size-1, 2);
    
            var l = left;
            var r = right;
        }
    
        private class Map
        {
            public int[,] Data { get; set; }
    
            public Map(int[,] data)
            {
                Data = data;
            }
    
            public string Print()
            {
                StringBuilder sb = new StringBuilder();
    
                for (int x = 0; x < Data.GetLength(0); x++)
                {
                    for (int y = 0; y < Data.GetLength(1); y++)
                        sb.Append(Data[y, x] == 0 ? " " : Data[y,x] == 1 ? "." : "#");
                    sb.AppendLine();
                }
    
                return sb.ToString();
            }
        }
    
        static void AddFormationAlt(Map _map, int tile, int x, int y, int length, int endTile)
        {
            // You may need to change the cloning method when you change the tiles from ints
            Map m1 = new Map((int[,])_map.Data.Clone());
            Map m2 = new Map((int[,])_map.Data.Clone());
    
            // Contains the left and right half of the Map you want, you need to join these together.
            Map aleft = AddFormationAlt(m1, true, tile, x, y, length, endTile);
            Map aright = AddFormationAlt(m2, false, tile, x, y + 1, length, endTile);
    
            left = aleft.Print();
            right = aright.Print();
        }
    
        static Map AddFormationAlt(Map _map, bool up, int tile, int x, int y, int length, int endTile)
        {
            if (x >= 0 && x < _map.Data.GetLength(0) && y >= 0 && y < _map.Data.GetLength(1))
            {
                if (_map.Data[y, x] != tile)
                {
                    if (length > 0)
                    {
                        _map.Data[y, x] = tile;
                        int newlength = length - 1;
    
                        // Either go 'up' or 'down'
                        if(up)
                            AddFormationAlt(_map, true, tile, x, y - 1, newlength, endTile); // ^
                        else
                            AddFormationAlt(_map, false, tile, x, y + 1, newlength, endTile); // v
    
                        AddFormationAlt(_map, up, tile, x - 1, y, newlength, endTile); // <-
                        AddFormationAlt(_map, up, tile, x + 1, y, newlength, endTile); // ->
    
                    }
                    else
                        _map.Data[y, x] = endTile;
                }
            }
    
            return _map;
        }
    }
    

    I changed all your Data[x, y] to Data[y, x] because that’s how I usually store them and then it worked xD.

    In aleft and aright you have the left half and the right half of the diamond you want in separate Maps, you need to join them together somehow (shouldn’t be too hard for a clever guy like you :). left and right show the textual representation of Maps (note the overlap in the centre):

    left:

                 #              
                #.
               #..
              #...
             #....
            #.....
           #......
          #.......
         #........
        #.........
       #..........
      #...........
     #............
    #.............
     #............
      #...........
       #..........
        #.........
         #........
          #.......
           #......
            #.....
             #....
              #...
               #..
                #.
                 #              
    

    right:

                 #                      
                 .#            
                 ..#           
                 ...#          
                 ....#         
                 .....#        
                 ......#       
                 .......#      
                 ........#     
                 .........#    
                 ..........#   
                 ...........#  
                 ............# 
                 .............#
                 ............# 
                 ...........#  
                 ..........#   
                 .........#    
                 ........#     
                 .......#      
                 ......#       
                 .....#        
                 ....#         
                 ...#          
                 ..#           
                 .#            
                 #   
    

    You need to clean this up and change all the classes back to your own ones. I hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code in a function I'm trying to revise. This example works
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is my SQL script CREATE TABLE tracks( track_id int NOT NULL AUTO_INCREMENT, account_id
Here is a code snippet I was working with: int *a; int p =
Here is my class: public class A{ private void doIt(int[] X, int[] Y){ //change
here's my code public String path; public String fileName; public static void readData() throws
Here's my test function (c#, visual studio 2010): [TestMethod()] public void TestGetRelevantWeeks() { List<sbyte>
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.