0

我有一个任务是制作一个处理草图,制作 4 x 4 数独游戏并使玩家将丢失的数字输入到游戏网格中,我现在面临的问题是制作一个循环或允许玩家输入所有缺失的数字?我已经成功地允许玩家使用 switch 语句和 if 语句输入前 4 个数字,但是程序只通过每个案例的第一个 if 语句,我想让它通过其余的?这是我做的鳕鱼。

void setup ()
{
  background (255);
  size (125,125);
  board();
}

void cell (int x , int y , int s )
{
  rect (x,y,s,s);
}

void triple (int x , int y , int s )
{
  for (int i = 0 ; i < 2 ; i ++)
  {
    cell ( x,y,s);
    x += s;
  }
}

void block (int x , int y , int s )
{
  for (int i = 0 ; i < 2 ; i ++)
  {
    triple ( x , y , s);
    y += s;
  }
}

void row (int x , int y , int s )
{
  for ( int i = 0 ; i < 2 ; i ++)
  {
    block ( x , y , s);
    x += (s*2);
  }
}

void cellArray ( int x , int y , int s)
{
  for (int i = 0 ; i < 2 ; i ++)
  {
    row ( x , y , s);
    y += (s*2);

  }
}

void darwLines ( int x , int y , int s)
{
  strokeWeight (3);

  for (int i = 0 ; i < 1 ; i ++)
  {
    x += (s*2);
    line ( x , 0 , x , (s*4));
  }

  for ( int j = 0 ; j < 1 ; j ++)
  {
    y += (s*2);
    line ( 0 , y , (s*4) , y );
  }

  strokeWeight (1);
}

void board ()
{
  cellArray (0,0,30);
  darwLines (0,0,30);
}

void draw ()
{
   int [][] fixedArray = new int [4][4];
   fill (0);
   textSize(28);
    for (int i = 0 ; i < 4 ; i ++)
    {
      for (int j = 0 ; j < 4 ; j ++)
      {
        fixedArray [0][2] = 3; 
        fixedArray [1][1] = 4; 
        fixedArray [2][2] = 4;
        fixedArray [3][3] = 3;

        text(fixedArray [0][2], 65,25);
        text(fixedArray [1][1],35,58);
        text(fixedArray [2][2],65,88);
        text(fixedArray [3][3],95,117);
      }//end of inner loop.
    }//end of outter loop.

  if (mousePressed)
  {
    mouseClicked ();
  }
}
/*------------------------------------------------------------------------------------------*/
void mouseClicked ()
{
  int s = 30 ; 
  int cellX = mouseX / s;
  int cellY = mouseY / s;


  int [][] userInputArray = new int [4][4];



  for (int m = 0 ; m < 4 ; m ++)
  {
    for (int n = 0 ; n < 4 ; n ++)
    {

      switch (key)
     {
        case '1':

          if (mouseX > userInputArray [0][0] -s && mouseX < userInputArray [0][0]+s && mouseY > userInputArray [0][0] -s && mouseY < userInputArray [0][0] +s )
          {
            fill (0,0,255);
            text (key , 10,25);

          }

          else  if (mouseX > userInputArray [1][2] -30 && mouseX < userInputArray [1][2]+30 && mouseY > userInputArray [1][2] -30 && mouseY < userInputArray [1][2] +30)
             {
               fill (0,0,255);
               text ('1' , 65,58);
             }




          else  if (mouseX > userInputArray [2][3] -30 && mouseX < userInputArray [2][3]+30 && mouseY > userInputArray [2][3] -30 && mouseY < userInputArray [2][3] +30)
            {
              fill (0,0,255);
              text('1' ,95,88);
            }



         else   if (mouseX > userInputArray [3][1] -30 && mouseX < userInputArray [3][1]+30 && mouseY > userInputArray [3][1] -30 && mouseY < userInputArray [3][1] +30)
            {
              fill (0,0,255);
              text('1' ,35,117);
            }

          break ; 


          case '2':

            if (mouseX > userInputArray [0][1] -(s-s) && mouseX < userInputArray [0][1]+(s*2) && mouseY > userInputArray [0][1] -s && mouseY < userInputArray [0][1] +s )
            {
              fill (0,0,255);
              text(key ,35,25);
            }
           else  if (mouseX > userInputArray [1][3] - (s-s) && mouseX < userInputArray [1][3] +(s*2) && mouseY > userInputArray [1][3] -(s*2) && mouseY < userInputArray [1][3] +(s*2))
            {
              fill(0,0,255);
              text(key,95,58);
            }
           else  if (mouseX > userInputArray [2][0] -(s-s) && mouseX < userInputArray [2][0] +(s*2) && mouseY > userInputArray [2][0] -(s*2) && mouseY < userInputArray [2][0] +(s*2))
            {
              fill(0,0,255);
              text(key,10,88);
            }
            else if (mouseX > userInputArray [3][2] -(s-s) && mouseX < userInputArray [3][2] +(s*2) && mouseY > userInputArray [3][2] -(s*2) && mouseY < userInputArray [3][2] +(s*2))
            {
              fill(0,0,255);
              text(key,65,117);
            }

          break;

          case '3':

            if ( mouseX > userInputArray [1][0] -s && mouseX < userInputArray [1][0]+s && mouseY > userInputArray [1][0] -(s-s) && mouseY < userInputArray [1][0] +(s*2))
            {
              fill(0,0,255);
              text(key,10,58);
            }
            else if (mouseX > userInputArray [2][1] -s && mouseX < userInputArray [2][1]+s && mouseY > userInputArray [2][1] -(s-s) && mouseY < userInputArray [2][1] +(s*2))
            {
              fill(0,0,255);
              text(key,35,88);
            }

          break ;

          case '4':

            if (mouseX > userInputArray [0][3] +60 && mouseX < userInputArray [0][3]+120 && mouseY > userInputArray [0][3] -30 && mouseY < userInputArray [0][3] +30 )
            {
              fill(0,0,255);
              text(key,95,25);
            }

           else if (mouseX > userInputArray [3][0] +60 && mouseX < userInputArray [3][0]+120 && mouseY > userInputArray [3][0] -30 && mouseY < userInputArray [3][0] +30 )
            {
              fill(0,0,255);
              text(key,10,117);
            }
          break;

          default :
          {
            fill (255);
            rect ((cellX * s),(cellY*s),s,s);
          }
      }//end of switch.

    }//end of the inner loop.
  }//end of the outter for loop.
}
4

1 回答 1

0

我浏览了您的代码,并观察到一些问题:

  • 首先,代码相当混乱。drawlines + cellArray + row + ... 可以更优雅地解决,而不需要如此复杂的函数嵌套。

  • 显然,您希望 fixedArray 始终保持不变。因此,您不需要在每个绘制循环中定义它并分配值。您可以将该声明移至代码的开头。

  • userInputArray 也是如此,值在任何地方都没有定义,因此等于 0。

  • 您在其自身之上绘制每个数字 16 次,这就是它看起来如此古怪的原因......绘制文本时不需要嵌套的 for 循环。

这就是说,请参阅附件中的增强代码提案。它解决了所有问题,如果需要更改数独大小和位置,则更加灵活,并且通常是更一致的代码。

尽管如此,如果你想让这变得真正有趣,试着弄清楚如何生成随机值,这样每个数独都与前一个不同!网上有很多数独生成算法。

祝你好运!

// GLOBAL VARS DECLARATION
int[][] resultsValue = {
  {1,2,3,4},
  {3,4,1,2},
  {2,3,4,1},
  {4,1,2,3}};
boolean[][] resultsSolved = new boolean[4][4];
int cellsX = 4;
int cellsY = 4;
float cellSize = 30; 
float xPos = 0;
float yPos = 0;

void setup ()
{
  background (255);
  size (130, 130);
  smooth();

  // GLOBAL VARS VALUE ASSIGNMENT            
  resultsSolved[0][2] = true;
  resultsSolved[1][1] = true;
  resultsSolved[2][2] = true;
  resultsSolved[3][3] = true;
}

void draw ()
{
  background(255);

  drawBoard(cellsX, cellsY, cellSize, xPos, yPos, 2);

  pushStyle();
  fill (0);
  textSize(28);

  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      if (resultsSolved[i][j] == true) {
         text(resultsValue[i][j], 5 + xPos + j * cellSize, -5 + yPos + (i + 1) * cellSize);
      }
    }
  }

  popStyle();

  if (mousePressed)
  {
    mouseClicked ();
  }

}

/*------------------------------------------------------------------------------------------*/

void drawBoard(int rows, int cols, float cellSize, float xPos, float yPos, int cellBlocks) {
  pushStyle();
  noFill();
  stroke(0);
  strokeWeight(1);

  // DRAW RECTANGLES
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      rect(xPos + i * cellSize, yPos + j * cellSize, cellSize, cellSize);
    }
  }

  strokeWeight(3);

  // DRAW LINES
  for (int i = 0; i < cols / cellBlocks; i++) {
      line(xPos + i * cellSize * cellBlocks, yPos, xPos + i * cellSize  * cellBlocks, yPos + rows * cellSize); 
  }
  for (int i = 0; i < rows / cellBlocks; i++) {
      line(xPos, yPos + i * cellSize * cellBlocks, xPos + cols * cellSize, yPos + i * cellSize * cellBlocks); 
  }

  popStyle();
} 

void mouseClicked ()
{
  int cellX = (int) ((mouseX - xPos) / cellSize); 
  if (cellX > cellsX - 1) cellX = cellsX - 1;
  else if (cellX < 0) cellX = 0;

  int cellY = (int) ((mouseY - yPos) / cellSize); 
  if (cellY > cellsY - 1) cellY = cellsY - 1;
  else if (cellY < 0) cellY = 0;

  int lastKeyInt = int(key - '0');

  if (resultsValue[cellY][cellX] == lastKeyInt) {
    resultsSolved[cellY][cellX] = true;
  }
}
于 2013-01-17T17:16:41.893 回答