我正在研究战列舰,我正在使用二维数组并将数字放置在网格数组中的数字上(如图所示);战列舰数组界限检查
/// Places the ships in a sequence on the grid
/// </summary>
/// <param name="ship"></param>
public static void PlaceShips(Ship ship)
{
Random rnd1 = new Random();
Random rnd2 = new Random();
if (ship.Orientation == "h")
{
//int r = rnd1.Next(_grid.GetLength(0));
//int c = rnd2.Next(_grid.GetLength(1));
int r = 0;
int c = 2;
for (int i = 0; i < ship.Values.Length; i++)
{
_grid[r++, c] = ship.Values[i];
}
}
else if (ship.Orientation == "v")
{
//int r = rnd1.Next(_grid.GetLength(0));
//int c = rnd2.Next(_grid.GetLength(1));
int r = 3;
int c = 4;
for (int i = 0; i < ship.Values.Length; i++)
{
_grid[r, c++] = ship.Values[i];
}
}
我想用随机方法来设置坐标,但是我的数学是关闭的,当涉及到检查数组边界,并保持在阵列内的船只。请任何人提供任何帮助?非常感激。我想我需要根据船的长度进行修改,然后检查余下部分是否大于船长,但我正在努力将其放入代码中。
嗨,可以解释downvote吗?感谢:D – Patashu 2013-03-22 00:24:38