welcome: please sign in
location: "解のある配置を作る"の差分
5と6のリビジョン間の差分
2009-12-15 12:40:03時点のリビジョン5
サイズ: 712
編集者: masahiko
コメント:
2009-12-17 07:52:06時点のリビジョン6
サイズ: 453
編集者: masahiko
コメント:
削除された箇所はこのように表示されます。 追加された箇所はこのように表示されます。
行 3: 行 3:
ピースを1つ取り除いてできた空白部分を使って
移動するための処理を記述します。

{{{
 int spx, spy;
}}}

{{{
 public void paintComponent(Graphics g)
 {
  for(...)
   for(...)
   {
    ...
   }
  g.setColor(Color.lightGray);
  g.fillRect(spx*haba, spy*haba, haba, haba);
 }
}}}
行 26: 行 7:
  int x, y, x2, y2, w, cnt;
行 27: 行 9:
  spx = yoko-1;
  spy = tate-1;

  for (cnt = 0; cnt < (tate*yoko*2); ) // 偶数回置換
  ...
  for (cnt = 0; cnt < (tate*yoko*2); cnt++)
行 31: 行 12:
   x = (int)(Math.random() * yoko);
   y = (int)(Math.random() * tate);
   if(x != spx || y != spy)
   x = (int)(Math.random() * (yoko-1));
   y = (int)(Math.random() * (tate-1));
   if(Math.random() < 0.5)
行 35: 行 16:
    ban[spx][spy] = ban[x][y];
    spx = x;
    spy = y;
    cnt++;
    x2 = x;
    y2 = y + 1;
行 40: 行 19:
   else
   {
    x2 = x + 1;
    y2 = y;
   }
   w = ban[x][y];
   ban[x][y] = ban[x2][y2];
   ban[x2][y2] = w;

ばらばらにする

   1         void shokika()
   2         {
   3                 int x, y, x2, y2, w, cnt;
   4                 ...
   5                 ...
   6                 for (cnt = 0; cnt < (tate*yoko*2); cnt++)
   7                 {
   8                         x = (int)(Math.random() * (yoko-1));
   9                         y = (int)(Math.random() * (tate-1));
  10                         if(Math.random() < 0.5)
  11                         {
  12                                 x2 = x;
  13                                 y2 = y + 1;
  14                         }
  15                         else
  16                         {
  17                                 x2 = x + 1;
  18                                 y2 = y;
  19                         }
  20                         w = ban[x][y];
  21                         ban[x][y] = ban[x2][y2];
  22                         ban[x2][y2] = w;
  23                 }
  24         }

解のある配置を作る (最終更新日時 2012-01-25 00:55:25 更新者 masahiko)