2013-01-12 2 views
0

스도쿠 게임을 4 x 4로 만드는 처리 스케치를 만들고 플레이어가 플레이 그리드에 누락 된 숫자를 입력하도록하는 과제가 있습니다. 현재 직면하고있는 문제는 루프 또는 플레이어가 누락 된 숫자를 모두 입력 할 수있게하려면 어떻게해야합니까? 플레이어가 switch 문과 if 문을 사용하여 처음 4 자리 숫자를 입력 할 수 있도록 허용했으나 프로그램은 각 case의 첫 번째 if 문을 통해서만 수행하고 나머지는 통과하도록하고 싶습니다. 여기 제가 만든 대구입니다.4 x 4 스도쿠 게임

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. 
} 
+0

어떤 프로그래밍 언어입니까? – JJJ

+0

@ 주 : 처리 중입니다. –

+0

아, 내 실수. – JJJ

답변

0

내가 몇 가지 문제가 코드를 통과하고, 관찰 : 모든

  • 먼저, 코드가 상당히 지저분하다. drawlines + cellArray + row + ...는 복잡한 함수 중첩이 필요없이 훨씬 더 우아하게 해결 될 수 있습니다.

  • 분명히 fixedArray를 항상 일정하게 유지하려고합니다. 따라서이를 정의하고 모든 그리기 루프에서 값을 할당 할 필요가 없습니다. 해당 선언을 코드 시작 부분으로 이동할 수 있습니다.

  • 같은이 값은 어디에도 정의하지 않으며, 당신은 각 숫자 자체의 상단에 16 배를 그리는 0

  • 때문에 동일은 필요가 없습니다 ... 그게 그렇게 crancky 보이는 이유, userInputArray으로 발생 텍스트를 그릴 때 중첩 된 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; 
    } 
} 
+0

도움을 많이 주셔서 감사합니다. 정말 감사합니다. – Lula

+0

안녕하세요 룰라, 피드백에 감사드립니다. 답이 유용하다고 생각되면 수령하여 홍보하십시오. 베스트. – GarciadelCastillo