2016-04-29 45 views
0

我試圖重新創建pushori益智遊戲,我的主要問題在於下一個問題。每當用戶需要輸入一個令牌時,我們就會把它稱爲「1」,就像下面的例子那樣,它需要以某種方式與當時放置的矩陣行「合併」。Pushori Java編程矩陣

考慮這一點,該令牌是在矩陣意味着用戶需要按下右箭頭鍵做「行合併」爲左側:

0 0 0 0  the 0s are the main game matrix, 0 0 0 0 after you place 
    0 0 0 0  the number 1 is the element  0 0 0 0 it when the user 
    0 0 0 0  that's going to enter in the  0 0 0 0 press the right 
1 0 0 0 0  row (3) from the left direction ->0 0 0 1 arrow key 

這會是基本的例子現在真正的問題在於,當你有下以下:

0 3 0 3  there are many tokens around  0 3 0 3 after you place 
2 1 0 2 0  the matrix and when the user -> 0 2 1 2 it when the user 
    1 0 2 0  wants to input the next token  1 0 2 0 press the right 
    0 1 0 1  now it needs to behave like  0 1 0 1 arrow key 

1 0 2 0行(1)和下一個標記將被輸入的數字2,它也需要它的所有元素合併到新的用戶輸入(數字2)作爲新的結果0 2 1 2行(1),我很困惑,在這部分,因爲我有下一個功能,從目前的矩陣表類左表輸入LeftDirection但我卡住試圖想辦法實際實現此功能

public void enterFromLeftDirection(int index,int actualRow){ 


    where index would be element from the Left Direction that needs to merge 
    and actualRow would be actualRow from the matrix that the user is located 
    however I don't get how I would be able to merge everything as I've already said 


} 

任何提示/建議將不勝感激!提前致謝!

Pushori < - 你可以從我從遊戲中收集來看看遊戲從這裏

回答

0

因此,該行轉移最簡單的方法是......嗯,這很難簡單介紹一下 - 也許一些代碼將有所幫助:

// We will assume that grid is the name of the matrix in the Table object 

public void enterFromLeft(int numToEnter, int row){ 

    for(int i = grid[row].length - 1; i > 0; i--){ 

     // Now we check if that spot is a 0 - if it is, 
     // we need to shift another number over to fill the space 

     if(grid[row][i] == 0){ 

      // Shifts the rightmost number to this spot by 
      // iterating from right to left over the row to find a non-0 number 

      for(int j = i; j > 0; j--){ 

       // Checks if the number is non-0 - and if it is, sets the 
       // empty space to said number and sets the number to 0 - 
       // This essentially moves the rightmost non-0 tile to the right. 

       if(grid[row][j] != 0){ 

        grid[row][i] = grid[row][j]; 
        grid[row][j] = 0; 

        j = -1; // Ends the for loop 

       } 

      } 

      // If grid[row][i] is still 0, it means that there were no non-0 
      // numbers in the regular grid, we will need to fill that space 
      // with numToEnter. If we do that, there will be no other numbers 
      // we need to shift right, so we can end the loop. 

      if(grid[row][i] == 0){ 

       grid[row][i] = numToEnter; 

       i = -1; // To end the loop 

      } 

     } 

    } 

} 

我還沒有測試過這個,但我相信它會工作。要從右側而不是左側添加數字,請將for循環更改爲以其他方式迭代(以及數字以退出循環 - 用grid[row].length + 1替換-1)。

如果您有任何疑問,請在下面評論。但現在我需要去玩一些pushori。

+0

哈哈呀!這真棒,我有點想到類似的東西,但我正在創建一個新的數組,並將其分配給currentRow,但這似乎更合適!感謝人真的很感激! – NeptaliD

+0

是的 - 你可能會創建另一個數組,但是一般來說,如果你可以在數組內部完成它,你應該 - 它可以節省內存。這可能不是一個問題,但它的好習慣無論如何。 –