2012-04-25 86 views
0

嘿,大家我正在創建一個使用4x4 2D陣列的記憶遊戲。記憶遊戲隨機顏色

我有4x4用0-7的一對整數填充,它們是隨機分散的。我想爲每個對分配一個顏色,所以當鼠標點擊該方塊時,分配的顏色將基於該整數出現,您將不得不根據其相應的匹配顏色找到另一個整數。 我一直在遇到這個setColor方法的一些問題。我將包括我的整個代碼,以防其他地方混亂,這就是爲什麼。目前,如果我點擊每個廣場,一旦我使用了我分配了兩次的所有8種顏色,但其中一些顏色與另一個區塊上的相同整數所在的位置不匹配。同樣,當我多次點擊同一個瓷磚時,它會在2-3種顏色之間變化,我不確定我做錯了什麼。

我需要建議的部分是我已經分配的setColor方法和我的邏輯。

/*Sets the background of your memory board to black*/ 
public void init() 
{ 
    setSize(400,400); 
    setBackground(Color.BLACK); 
    buildBoard(4); 

} 
/*This is main in java applets 
    You may need to add (not 
    change) a couple things in this method 
*/ 
public void paint(Graphics canvas) 
{ 
    if(firstRun) //for the first run we need to build our random board 
    { 

     print2DArray(board); 
     buildBoard(4); 
     firstRun = false; 
    } 
    else // once our board is built we will display the game 
    { 
     displayGame(canvas); 
     if (mouseClicked) // if the mouse has been clicked 
     { 
      displayHit(canvas);//find which box the user clicked 
      mouseClicked = false; 
     } 
    } 
} 

/* 
    DO NOT change this method 
    determines if the mouse has been pressed 
    sets x and y Mouse to the location of the mouse arrow 
    redraws the image 
*/ 
public boolean mouseDown(Event e, int x, int y) 
{ 
    mouseClicked = true; 
    xMouse = x; 
    yMouse = y; 
    repaint(); 
    return true; 
} 

/*DO NOT change this method 
    redraws the scene 
*/ 

public void update (Graphics g) 
{ 
    paint(g); 
} 

/* 
    pre: none 
    post: build an array that holds the memory values for a board of size x size 
    the board will hold two of each int from 0 to size randomly placed in the array 
*/ 

public static void fillRect(Graphics g, int x, int y, int width, int length) 
{ 
    g.setColor(Color.BLACK); 
    width = 100; 
    length = 100; 
    x = (int)xMouse/100; 
    y = (int)yMouse/100;   
} 

public void buildBoard(int s) 

{ 
    int a = 4; 
    for (int row = 0; row < a; row++) 
     for (int column = 0; column < a; column++) 
     { 
      board[row][column] = count++ % 8; 
     } 
    for(int row = 0; row < 4; row++) 

     for(int column = 0; column < 4; column ++) 
     { 
      int x = (int)Math.floor(Math.random()*4); 
      int y = (int)Math.floor(Math.random()*4); 
      temp = board[row][column]; 
      board[row][column] = board[x][y]; 
      board[x][y] = temp; 


     } 
} 
public static void print2DArray(int[][] arr) 
{ 
    for (int row = 0; row < arr.length; row++) 
    { 
     for (int col = 0; col < arr[row].length; col++) 
     { 
      System.out.print(arr[row][col] + " "); 
     } 
     System.out.println(); 
    } 
} 





public void displayGame(Graphics canvas) 
{ 
    canvas.setColor(Color.WHITE); 

    for(int i =0; i < 400; i+= WIDTH) 
     for(int j = 0; j < 400; j+= WIDTH) 
      canvas.drawRect(i, j, WIDTH, WIDTH); 
} 

/* 
    Pre: xMouse and yMouse have been initialized 
    Post: A circle is displayed in the correct box on the screen 
    Currently the circle is displayed at the mouse location 
*/ 
public void displayHit(Graphics g) 
{ 

    /*int xGuess = (int)xMouse/100; 
    int yGuess = (int)yMouse/100; 
    board[xGuess][yGuess] = guess1; 
    int xGuess2 = (int)xMouse/100; 
    int yGuess2 = (int)yMouse/100; 
    board[xGuess2][yGuess2] = guess2; 

    if (guess1 == guess2) 
    { 
     setColor(g); 
     centerHit(xMouse, yMouse); 
     g.fillOval(xMouse, yMouse, 40, 40); 
    } 
    else 
    g.fillRect(guess1, guess2, width, length); 
    g.setColor(Color.BLACK);*/ 
    setColor(g); 
    centerHit(xMouse, yMouse); 
    g.fillOval(xMouse, yMouse, 40, 40); 
} 

public void setColor(Graphics g) 
{ 

    centerClick(x1,y1);  
    //int x = xMouse; 
    //int y = yMouse; 
    colorIndex = board[row][column]; 
    board[row][column] = board[x1][y1]; 
    board[x1][y1] = colorIndex; 


    switch(colorIndex) 
    { 
    case 0: g.setColor(Color.RED); 
    break; 
    case 1: g.setColor(Color.GREEN); 
    break; 
    case 2: g.setColor(Color.BLUE); 
    break; 
    case 3: g.setColor(Color.ORANGE); 
    break; 
    case 4: g.setColor(Color.CYAN); 
    break; 
    case 5: g.setColor(Color.MAGENTA); 
    break; 
    case 6: g.setColor(Color.PINK); 
    break; 
    case 7: g.setColor(Color.YELLOW); 
    break; 
    } 

} 
public void centerHit(int centerX, int centerY) 
{ 
    { 
     if ((xMouse > 0) && (xMouse <=100)) 
      xMouse = 33; 
     else if ((xMouse > 100) && (xMouse <=200)) 
      xMouse = 133; 
     else if ((xMouse > 200) && (xMouse <=300)) 
      xMouse = 233; 
     else if ((xMouse > 300) && (xMouse <=400)) 
      xMouse = 333; 
    } 
    { 
     if ((yMouse > 0) && (yMouse <=100)) 
      yMouse = 33; 
     else if ((yMouse > 100) && (yMouse <=200)) 
      yMouse = 133; 
     else if ((yMouse > 200) && (yMouse <=300)) 
      yMouse = 233; 
     else if ((yMouse > 300) && (yMouse <=400)) 
      yMouse = 333; 
    } 
} 
public void centerClick(int centerX, int centerY) 
{ 
    { 
     if ((xMouse > 0) && (xMouse <=100)) 
      x1 = 0; 
     else if ((xMouse > 100) && (xMouse <=200)) 
      x1 = 1; 
     else if ((xMouse > 200) && (xMouse <=300)) 
      x1 = 2; 
     else if ((xMouse > 300) && (xMouse <=400)) 
      x1 = 3; 
    } 
    { 
     if ((yMouse > 0) && (yMouse <=100)) 
      y1 = 0; 
     else if ((yMouse > 100) && (yMouse <=200)) 
      y1 = 1; 
     else if ((yMouse > 200) && (yMouse <=300)) 
      y1 = 2; 
     else if ((yMouse > 300) && (yMouse <=400)) 
      y1 = 3; 
    } 
} 

}

+0

林不知道什麼youre試圖用三行代碼,繼承人什麼,我想'colorIndex =板[行] [列]做;'您選擇閱讀一些細胞的顏色,不知道哪個單元格cuz我沒有看到'列'或'行'價值的變化。 'board [row] [column] = board [x1] [y1];'在我看來,像你在改變原版,我不認爲這是你想要的,而board [x1] [y1] = colorIndex;'assign最後單擊單元格顏色點擊單元格... mmmh,請解釋這三行_clearly_ – jambriz 2012-04-25 20:30:19

+0

@jambriz板最初讀取0-7並重復0-7爲最後兩行。我混合了行和列來隨機化數字。這三行代碼是我嘗試使用鼠標點擊器在4x4 2D陣列上選擇一個塊。然後使用xindex和yindex,並在板[row] [column]的那一點找到相應的數組值。然後,我將在剛纔通過switch語句點​​擊的索引處設置該值,以便根據它的整數值 – user1215307 2012-04-25 20:39:11

回答

2

這個代碼

colorIndex = board[row][column]; 
    board[row][column] = board[x1][y1]; 
    board[x1][y1] = colorIndex; 

不斷從改變顏色,改變

colorIndex = board[x1][y1]; 

色彩匹配問題是因爲您選擇建設板兩次:第一次在paint方法的第一次運行中,您可以在其中打印陣列,然後在init方法,其中板被覆蓋,讓你看到不同的價值觀

解決這個問題,你應該在init方法搭板只有一次,並呼籲print2DArray事後檢查,所以

buildBoard(4); 
print2DArray(board); //<-- add this line to verify colors 

,你可以省略firstRun標誌和所有關聯的代碼。

評論或刪除此代碼:

/*if(firstRun) //for the first run we need to build our random board 
    { 
     print2DArray(board); 
     buildBoard(4); 


     firstRun = false; 
    } 
    else // once our board is built we will display the game 
    {*/ 
+0

確定任何顏色,以便幫助解決我的一個問題。它可以防止每次點擊多個顏色變化,但我的整數和指定的顏色不匹配,我不知道爲什麼 – user1215307 2012-04-25 20:57:01

+0

你也可以解釋board [x1] [y1] = colorIndex vs colorIndex = board [x1 ] [y1] = colorIndex; – user1215307 2012-04-25 20:57:38

+0

你如何檢查它們是否匹配?他們似乎匹配我,無論如何嘗試改變'colorIndex = board [x1] [y1];''colorIndex = board [y1] [x1];'看看你想要什麼 – jambriz 2012-04-25 21:06:39