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

  • Home
  • SEARCH
  • 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 8417659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:07:04+00:00 2026-06-10T02:07:04+00:00

I have a little problem with my world generation code. I will post the

  • 0

I have a little problem with my world generation code. I will post the code below:

    #region generate world
    private void genworld()
    {
        string world = tb_value1.Text;
        int worldID = 9999;
        int size = 0;

        int col = 0;
        int row = 0;

        string sql = "SELECT * FROM " + variables.tbl_worlds + " WHERE worlds_name='" + world + "'";
        MySqlCommand cmd = new MySqlCommand(sql, connection);
        MySqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            worldID = int.Parse(reader["worlds_ID"].ToString());
            size = int.Parse(reader["worlds_x"].ToString());
        }
        reader.Dispose();

        if (worldID != 9999)
        {
            addtolog("server", "World " + world + " exists!");

            while (row < size)
            {
                while (col < size)
                {
                    int above = row - 1;
                    int nextto = col - 1;
                    int tileabove = 9999;
                    int tilenextto = 9999;
                    int tile = 9999;


                    if (above >= 0)
                    {
                        string sql2 = "SELECT * FROM " + variables.tbl_tiles + "WHERE tiles_col='" + col + "' AND tiles_row='" + above + "' AND tiles_world='" + worldID + "'";
                        MySqlCommand cmd2 = new MySqlCommand(sql2,connection2);
                        MySqlDataReader reader2 = cmd.ExecuteReader();

                        while (reader2.Read())
                        {
                            if (reader2.IsDBNull(1))
                            {
                                tileabove = int.Parse(reader["tiles_type"].ToString());
                            }
                        }

                        reader2.Dispose();
                    }

                    if (nextto >= 0)
                    {
                        string sql2 = "SELECT * FROM " + variables.tbl_tiles + "WHERE tiles_col='" + nextto + "' AND tiles_row='" + row + "' AND tiles_world='" + worldID + "'";
                        MySqlCommand cmd2 = new MySqlCommand(sql2, connection2);
                        MySqlDataReader reader2 = cmd.ExecuteReader();

                        while (reader2.Read())
                        {
                            if (reader2.IsDBNull(1))
                            {
                                tilenextto = int.Parse(reader["tiles_type"].ToString());
                            }
                        }
                        reader2.Dispose();
                    }

                    if (tile == 9999 && (tileabove == 9999 || tilenextto == 9999))
                    {
                        tile = gentile(10, 10);
                    }
                    if (tile == 9999 && tileabove == 0 && tilenextto == 0)
                    {
                        tile = gentile(200, 10);
                    }
                    if (tile == 9999 && tileabove == 1 && tilenextto == 1)
                    {
                        tile = gentile(10, 200);
                    }
                    if (tile == 9999 && tileabove == 1)
                    {
                        tile = gentile(10, 10000);
                    }
                    if (tile == 9999 && (tileabove == 1 || tilenextto == 1))
                    {
                        tile = gentile(20,80);
                    }


                    string sql4 = "INSERT INTO " + variables.tbl_tiles + " (tiles_ID,tiles_row,tiles_column,tiles_world,tiles_type) VALUES ('','" + row + "','" + col + "','" + worldID + "','" + tile + "')";
                    MySqlCommand cmd3 = new MySqlCommand(sql4, connection2);
                    cmd3.ExecuteNonQuery();

                    col++;
                }
                row++;
                col = 0;
            }

        }
        else
        {
            addtolog("server","World " + world + " does not exist!");
        }
    }
    #endregion

And the gentile method

    #region generate tile
    private int gentile(int grass, int desert)
    {
        int totalchance = grass + desert;

        //addtolog("server","TEST");

        Random random = new Random();
        int number = random.Next(1,totalchance);

        if (number <= grass)
        {
            return 0;
        }
        else if (number <= grass + desert)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    #endregion
    #endregion

My problem with this is that it seems to continue on the next row while it shouldn’t. An example of this:

an example of my generation code

If you have a close look at the image you will see that the green (a 0 as the tiletype) and yellow (a 1 as the tiletype) continue on the next row, while I though that it does not check the last tile on the previous row…

Could someone please tell me what I am doing wrong?

  • 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-06-10T02:07:06+00:00Added an answer on June 10, 2026 at 2:07 am
    MySqlCommand cmd2 = new MySqlCommand(sql2,connection2);
    MySqlDataReader reader2 = cmd.ExecuteReader();
    

    See a problem now? (copy-paste made it worse too) I can’t say with absolute certainty that it is your problem but I’m not going to dig any further until you say this doesn’t solve your problem.

    MySqlCommand cmd2 = new MySqlCommand(sql2,connection2);
    MySqlDataReader reader2 = cmd2.ExecuteReader();  //<-- using cmd2 instead
    

    Adding a code review.

    If you hold the “world” in memory and avoid all the database hits until the end, debugging this becomes way easier.

    I likely wouldn’t have caught this had ReSharper not pointed it out (so get it) because this code does everything poorly instead of one thing well.


    Simplified code:

    [Test]
    public void X()
    {
        var tiles = genworld();
    
        for(int i = 0; i < tiles.Length; i++)
        {
            for(int j = 0; j < tiles[i].Length; j++)
            {
                Console.Write(tiles[i][j]);
            }
    
            Console.WriteLine();
        }
    }
    
    private int[][] genworld(int size = 25)
    {
        /* the solution with the database "worked" because it was slow. 
           going in memory will cause this to run faster so you need to
           declare a Random first and use it later.  this approach avoids
           a class-level variable.
        */
        var r = new Random();
        Func<int, int, int> gentile = (grass, desert) =>
                                            {
                                                var number = r.Next(1, grass + desert);
                                                return number < desert ? 1 : 0;
                                            };
    
        var tiles = new int[size][];
    
        for (int i = 0; i < size; i++)
        {
            tiles[i] = new int[size];
        }
    
        for (var row = 0; row < size; row++)
        {
            for (var col = 0; col < size; col++ )
            {
                int tileabove = 9999;
                int tilenextto = 9999;
                int tile = 9999;
    
                if (row >= 1)
                {
                    tileabove = tiles[row - 1][col];
                }
    
                if (col >= 1)
                {
                    tilenextto = tiles[row][col - 1];
                }
    
                if (tile == 9999 && (tileabove == 9999 || tilenextto == 9999))
                {
                    tile = gentile(10, 10);
                }
                if (tile == 9999 && tileabove == 0 && tilenextto == 0)
                {
                    tile = gentile(200, 10);
                }
                if (tile == 9999 && tileabove == 1 && tilenextto == 1)
                {
                    tile = gentile(10, 200);
                }
                if (tile == 9999 && tileabove == 1)
                {
                    tile = gentile(10, 10000);
                }
                if (tile == 9999 && (tileabove == 1 || tilenextto == 1))
                {
                    tile = gentile(20, 80);
                }
    
                tiles[row][col] = tile;
            }
        }
    
        return tiles;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little problem. This is my code: stackPanelShapesContainer = new StackPanel(); InitializeStackPanle();
i have a little problem with my java socket code. I'm writing an android
I have a little problem where I would like to insert a svn diff
I have little problem with a replacement of a little part in an url.
I have little problem in regular expressin creation. Expected input: blahblahblah, blahblahblah, 'blahblahblah', blahblahblah,
I have little unusual problem to solve. I need some hint or links to
I have a little problem about the sort direction of a specific column in
I have a little problem with Jquery getJSON function. my json here { entries:
I have a little Problem with the app that I'm developing. In the App
I have a little problem about Google Maps. I'm using Geocoding (xml) for getting

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.