我的世界代码有点问题。我将发布以下代码:
#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
我的问题是它似乎在下一行继续,而不应该。一个例子:
如果您仔细查看图像,您会看到绿色(一个0作为tiletype)和黄色(一个1作为tiletype)继续下一行,而我虽然它没有检查最后一行上一行的瓷砖......
有人可以告诉我我做错了吗?
答案 0 :(得分:2)
MySqlCommand cmd2 = new MySqlCommand(sql2,connection2);
MySqlDataReader reader2 = cmd.ExecuteReader();
现在看到问题? (复制粘贴也使它变得更糟)我不能绝对肯定地说这是你的问题,但是在你说这不能解决你的问题之前我不会再深入挖掘。
MySqlCommand cmd2 = new MySqlCommand(sql2,connection2);
MySqlDataReader reader2 = cmd2.ExecuteReader(); //<-- using cmd2 instead
添加代码审核。
如果你在内存中保留“world”并避免所有数据库命中直到最后,调试它会变得更容易。
如果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;
}