2016-12-05 61 views
1

我想绘制选定的数独编号所属的方格。细胞Sudoku的绘制区域

该代码可用于打印的行和列:

   //Pintem la fila del nombre seleccionat 
      for (int i = 0; i < parent.getChildCount(); i++) { 
       TextView child = (TextView) parent.getChildAt(i); 
       if ((i/9)==x) { 
        //child.setBackgroundColor(Color.parseColor("#75FFEE")); 
        child.setBackground(getDrawable(R.drawable.contornfonsblau)); 
       } 
      } 

      //Pintem la columna del nombre seleccionat 
      for (int i = 0; i < parent.getChildCount(); i++) { 
       TextView child = (TextView) parent.getChildAt(i); 
       if ((i%9)==y) { 
        //child.setBackgroundColor(Color.parseColor("#75FFEE")); 
        child.setBackground(getDrawable(R.drawable.contornfonsblau)); 
       } 
      } 

我想相同的,但为方形。

EXAMPLE

回答

0

可以找到其平方大的数,通过使用整数除法分割的小方块的x和y坐标通过3属于中,由于大的正方形是3×3小方块。

因此,请与上面进行相同的比较,但事先将两边除以3。你还需要检查x和y:

for (int i = 0; i < parent.getChildCount(); i++) { 
    TextView child = (TextView) parent.getChildAt(i); 
    if ((((i/9)/3)==(x/3)) && (((i%9)/3)==(y/3))) { 
     child.setBackground(getDrawable(R.drawable.contornfonsblau)); 
    } 
} 

当然,(i/9)/3可以简化为i/27,但它的商榷是否是更具可读性。

可以将所有3个试验合并成一个for循环:

for (int i = 0; i < parent.getChildCount(); i++) { 
    TextView child = (TextView) parent.getChildAt(i); 
    if(((i/9)==x) || 
     ((i%9)==y) || 
     ((((i/9)/3)==(x/3)) && (((i%9)/3)==(y/3)))) { 
     child.setBackground(getDrawable(R.drawable.contornfonsblau)); 
    } 
}