C# 生成代码在下一行继续

标签 c# mysql database random

我的世界生成代码有一点问题。我将发布以下代码:

    #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

还有温和的方法

    #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

我的问题是它似乎在下一行继续,但它不应该继续。一个例子:

an example of my generation code

如果您仔细观察图像,您会发现绿色(0 作为图 block 类型)和黄色(1 作为图 block 类型)继续在下一行,而我认为它不会检查最后一行平铺在上一行...

有人可以告诉我我做错了什么吗?

最佳答案

MySqlCommand cmd2 = new MySqlCommand(sql2,connection2);
MySqlDataReader reader2 = cmd.ExecuteReader();

现在发现问题了吗? (复制粘贴也让情况变得更糟)我不能绝对肯定地说这是你的问题,但我不会进一步挖掘,直到你说这不能解决你的问题。

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

添加代码审查。

如果您将“世界”保存在内存中并避免所有数据库命中直到最后,调试就会变得更加容易。

如果 ReSharper 没有指出这一点(所以明白了),我可能不会发现这一点,因为这段代码在所有事情上都做得很糟糕,而不是在一件事情上做得很好。

<小时/>

简化代码:

[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;
}

关于C# 生成代码在下一行继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12043204/

相关文章:

c# - 在 VB.NET 中使用非整数增量进行循环

c# - 如何使用 C# 仅获取目录中的文件名?

MySql - 从表 2 中获取字段,如果没有,则从表 1 中获取字段

php - 使用 "If"语句的 mysql 查询搜索

PHP 我应该将图像路径存储在数据库中吗?

c# - 如何防止 TextBox 中的退格键击键?

c# - 将十六进制代码发送到串口

mysql - Laravel 无法选择最后一行。 "Call to a member function first() on a non-object"错误

php - CActiveForm 及其行为没有名为 "getErrors"的方法或闭包。

php - 数据库查询未按预期运行