2016-04-22 11 views
0

私は再帰的に行のアクションボードを作成しようとしています。私は既に多次元配列(8x8)を行っています。今、私が持っている唯一の問題は、ピースを正しい場所(特に黒いもの)に置く方法です。私の問題は何ですか?ピースの最初の行を印刷しようとしているとき、"-bbbbbb-"の代わりに"-b-b-b-b-b--"のようなものが印刷されます。最後の行についても同様です。行の再帰的なボード

public static void im (int[][]m, int r, int c){//Matriz 
    if (r==m.length-1 && c==m[0].length-1) { 
     System.out.print("-"); 
    } else { 
     if(r==0 && c>0 && c<m[0].length-1){ 
      System.out.print("b"); 
     } 
     if(c==0 && r>0 && r<m.length-1){ 
      System.out.print("w"); 
     } 
     if (c==m[0].length-1) { 
      if(r>0){ 
       System.out.print("w"); 
       System.out.println(""); 
       im(m,r+1,0); 
      }else{ 
       System.out.print("-"); 
       System.out.println(""); 
       im(m,r+1,0); 
      } 
     } else { 
      System.out.print("-"); 
      im(m,r,c+1); 
     } 
    } 
} 

答えて

0

あなたのif文のロジックに欠陥がある場合は、あなたが今まで知っていたすべてのブロックを再度試してコメントすることができます。このように:

if (a) { 
    // I know that a == true 
    if (b) { 
     // I know that a == true && b == true 
    } else { 
     // I know that a == true && b == false 
    } 
} 

このようにすれば、自分の間違いを簡単に見つけ出すことができます。これがコメントに役立たない場合は、正しいコードに返信します。 EDIT

:ここは正しいコードです:

if (r == 0 || r == m[0].length - 1) { 
    // top or bottom row 
    if (c == 0 || c == m.length - 1) { 
     // top or bottom row and leftmost column or rightmost column 
     System.out.print("-"); 
    } else { 
     // top or bottom row and not leftmost column or rightmost column 
     System.out.print("b"); 
    } 
} else { 
    // not top or bottom row 
    if (c == 0 || c == m.length - 1) { 
     // not top or bottom row and leftmost column or rightmost column 
     System.out.print("w"); 
    } else { 
     // not top or bottom row and not leftmost column or rightmost column 
     System.out.print("-"); 
    } 
} 

私は本当にそれは本当に私には意味がありませんので、あなたのオリジナルのコードを修正する方法がわからないので、私はちょうどそれを完全に書き直し忘れてはならないことは、すべての関数呼び出しが1文字だけを出力するようにして、少なくとも一度にprintステートメントの1つにしか到達できないようにすることです。

EDIT2:

public static void im(int[][] m, int r, int c) { 
    if (r == 0 || r == m.length - 1) { 
     // top or bottom row 
     if (c == 0 || c == m[0].length - 1) { 
      // top or bottom row and leftmost column or rightmost column 
      System.out.print("-"); 
     } else { 
      // top or bottom row and not leftmost column or rightmost column 
      System.out.print("b"); 
     } 
    } else { 
     // not top or bottom row 
     if (c == 0 || c == m[0].length - 1) { 
      // not top or bottom row and leftmost column or rightmost column 
      System.out.print("w"); 
     } else { 
      // not top or bottom row and not leftmost column or rightmost column 
      System.out.print("-"); 
     } 
    } 
    if (c == m[0].length - 1) { 
     // rightmost column 
     System.out.println(); 
     if (r != m.length - 1) { 
      // not bottom row 
      im(m, r + 1, 0); 
     } 
    } else { 
     // not rightmost column 
     im(m, r, c + 1); 
    } 
} 
+0

私は申し訳ありませんが、私は問題を見ることはできません。行の代わりに列(白い部分のみ)で始めたのは間違いでしたか? –

+0

作業コードが追加されました。 –

+0

私はまだコードを試してみませんでしたが、私はこれが再帰的なコードではないことを恐れています。それにもかかわらず、あなたのコードは私のものよりも「きれいに」見えます。私はそれを試して再帰的にするつもりです。私は後でそれがどうやって行ったか教えてくれるでしょう。ありがとうございました! –