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:

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?
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.
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: