【解決方法】戦艦ゲームのWPFで船をランダムに配置するにはどうすればよいですか?


コンピューター用に船をランダムに配置しようとしましたが、ランダム ボタンをクリックしても機能せず、グリッド全体に色が付けられます。 これはランダムなボタンクリックです

private void Button_Click_1(object sender, RoutedEventArgs e)
      {
          int[,] enemyArrayPlace = new int[row, col];
          for (int i = 0; i < row; i++)
          {
              for (int j = 0; j < col; j++)
              {
                  if (enemyArrayPlace[i,j] == 0)
                  {
                      enemyShips.RandomShip(gridEnemy);
                      gridEnemy.Background = Brushes.Red;
                  }
                  else
                  {
                      gridEnemy.Background = Brushes.Blue;
                  }
              }
          }
      }

これは配置を行う EnemyShips クラスですが、それらを 4,3,2 列にランダムに配置する方法を記述して、それらが重ならないようにし、すべてのセルが別のセルから離れているようにする方法を知りません。 また、このクラスの内容をランダムボタンに渡す方法がわかりません。これは、船をランダム化してグリッドに配置します

class EnemyShips
{
    Random rand = new Random();
    public int fourman = 4;
    public int threeman = 3;
    public int twoman = 2;
    public int oneman = 1;
    private readonly int row = 10;
    private readonly int col = 10;
    public int[,] ship;
    public int[,] enemyShipArray;
    int x;
    int y;
    public int[,] RandomShip(Grid grid)
    {
        enemyShipArray = new int[row, col];
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                for (int a = 0; a < 1; a++)
                {
                    ship = enemyShipArray;

                    if (x + 4 <= 10 || x + 4 <= 1 || y + 4 <= 10 || y - 4 >= 1)
                    {
                        x = rand.Next(0, 10);
                        y = rand.Next(0, 10);

                        fourman = enemyShipArray[x,y];
                        enemyShipArray[x, y] = 1;
                        return enemyShipArray;
                    }
                    for (int b = 0; b < 2; b++)
                    {
                        if (x + 3 <= 10 || x + 3 <= 1 || y + 3 <= 10 || y - 3 >= 1)
                        {
                            x = rand.Next(0, 10);
                            y = rand.Next(0, 10);
                            threeman = enemyShipArray[x,y];
                            enemyShipArray[x, y] = 1;
                            return enemyShipArray;
                        }
                    }
                    for (int c = 0; c < 3; c++)
                    {
                        if (x + 2 <= 10 || x + 2 <= 1 || y + 2 <= 10 || y - 2 >= 1)
                        {
                            x = rand.Next(0, 10);
                            y = rand.Next(0, 10);
                            twoman = enemyShipArray[x,y];
                            enemyShipArray[x, y] = 1;
                            return enemyShipArray;
                        }
                    }
                    for (int d = 0; d < 4; d++)
                    {
                        if (x + 1 <= 10 || x + 1 <= 1 || y + 1 <= 10 || y - 1 >= 1)
                        {
                            x = rand.Next(0, 10);
                            y = rand.Next(0, 10);
                            oneman = enemyShipArray[x,y];
                            enemyShipArray[x, y] = 1;
                            return enemyShipArray;
                        }
                    }
                }
            }
        }
        return enemyShipArray;
    }
}

私が試したこと:

私は、enemyShips クラスで 0 で満たされた空の配列を作成しようとしました。また、船をランダムなセルに配置するためにランダムな生成を行ってみましたが、私のコードでは、船全体ではなくランダムなセルを 1 つずつ選択して、行に配置したり、桁。

解決策 1

あなたは大きな間違いを犯していると思います: 船の中心を 1 つの正方形に置くだけでよいと思い込んでいるように見えますが、そうではありません。全然重なります。
このスレッドを試してください: C# 戦艦ゲームボード & ランダム割り当て – C# ディスカッション ボード[^] 彼は非常によく似た問題を抱えていました。

解決策 2

コメント

タイトルとURLをコピーしました