2016-12-16 31 views
-1

我做了一個記憶遊戲應用程序,並且我想添加一個計時器,當所有卡片匹配時開始和停止活動,以及如何檢查所有卡片是否匹配?這裏是我的代碼:我做了一個記憶遊戲的應用程序,並希望添加所有卡匹配時停止計時器。這裏是我的代碼: -

public class game4x4new extends AppCompatActivity implements View.OnClickListener { 

TextView moves, score, time; 
Button reset, submit, start; 

private int numberOfElements; 
private MemoryButton[] buttons; 

private int[] buttonGraphicLocations; 
private int[] buttonGraphics; 

private MemoryButton selectedButton1; 
private MemoryButton selectedButton2; 

int flippedtimes = 0; 
int match = 0; 
int seconds = 0; 

AlertDialog.Builder adb; 
AlertDialog a; 

private boolean isBusy = false; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_game4x4new); 

    GridLayout gridlayout = (GridLayout) findViewById(R.id.grid_layout_4x4); 

    moves = (TextView) findViewById(R.id.tv1); 
    score = (TextView) findViewById(R.id.tv2); 
    time = (TextView) findViewById(R.id.tv3); 
    reset = (Button) findViewById(R.id.button3); 
    submit = (Button) findViewById(R.id.button4); 
    start = (Button) findViewById(R.id.button5); 

    int numColumns = gridlayout.getColumnCount(); 
    int numRows = gridlayout.getRowCount(); 

    numberOfElements = numColumns * numRows; 
    buttons = new MemoryButton[numberOfElements]; 

    buttonGraphics = new int[numberOfElements/2]; 

    buttonGraphics[0] = R.drawable.blue; 
    buttonGraphics[1] = R.drawable.cyan; 
    buttonGraphics[2] = R.drawable.green; 
    buttonGraphics[3] = R.drawable.grey; 
    buttonGraphics[4] = R.drawable.maroon; 
    buttonGraphics[5] = R.drawable.pink; 
    buttonGraphics[6] = R.drawable.red; 
    buttonGraphics[7] = R.drawable.yellow; 

    buttonGraphicLocations = new int[numberOfElements]; 
    shuffleButtonGraphics(); 

    for (int r = 0; r < numRows; r++) { 
     for (int c = 0; c < numColumns; c++) { 
      MemoryButton tempButton = new MemoryButton(this, r, c, buttonGraphics[buttonGraphicLocations[r * numColumns + c]]); 
      tempButton.setId(View.generateViewId()); 
      tempButton.setOnClickListener(this); 
      buttons[r * numColumns + c] = tempButton; 

      gridlayout.addView(tempButton); 
     } 
    }   

    reset.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      shuffleButtonGraphics(); 

      match = 0; 
      flippedtimes = 0; 
      seconds = 0; 
      score.setText("SCORE :"); 
      moves.setText("MOVES :"); 
      time.setText("TIME :"); 
     } 
    }); 
} 

    protected void shuffleButtonGraphics() 
    { 
     Random rand= new Random(); 

     for(int i=0; i<numberOfElements; i++) 
     { 
      buttonGraphicLocations[i]= i % (numberOfElements/2); 
     } 

     for(int i=0; i<numberOfElements;i++) 
     { 
      int temp= buttonGraphicLocations[i]; 
      int swapIndex = rand.nextInt(16); 

      buttonGraphicLocations[i]=buttonGraphicLocations[swapIndex]; 
      buttonGraphicLocations[swapIndex]=temp; 
     } 
    } 

    @Override 
    public void onClick(View v) 
    { 
     if(isBusy) 
     return; 

     MemoryButton button= (MemoryButton) v; 

     if(selectedButton1==null) 
     { 
      selectedButton1=button; 
      selectedButton1.flipped(); 
      return; 
     } 
     if(selectedButton1.getId()==button.getId()) 
     { 
      return; 
     } 
     if (selectedButton1.getFrontDrawableId()==button.getFrontDrawableId()) 
     { 
      button.flipped(); 

      button.setMatched(true); 
      selectedButton1.setMatched(true); 

      selectedButton1.setEnabled(false); 
      button.setEnabled(false); 

      selectedButton1=null; 

      flippedtimes++; 
      match++; 
      moves.setText("MOVES : " + flippedtimes); 
      score.setText("SCORE : " + match); 

      return; 
     } 

     else 
     { 
      selectedButton2=button; 
      selectedButton2.flipped(); 
      isBusy=true; 
      flippedtimes++; 
      moves.setText("MOVES : " + flippedtimes); 

      final Handler handler= new Handler(); 

      handler.postDelayed(new Runnable() 
      { 
       @Override 
       public void run() 
       { 
        selectedButton2.flipped(); 
        selectedButton1.flipped(); 
        selectedButton1=null; 
        selectedButton2=null; 
        isBusy=false; 
       } 
      }, 1000); 
     } 
    } 
+1

也許是負面投票的國家理由?不要只是將問題標記爲冷落,這絕不會改善質量問題或鼓勵提問者進行富有成效的討論。 – Talha

+1

請考慮製作[mcve]。還要更好地解釋你的問題。現在我必須閱讀你的代碼來理解'匹配'的含義。此外,您的代碼格式不正確。頂部的行不包含在代碼塊中。請將所有內容移至四個空格中。這在任何IDE中都很容易實現,例如通過選擇所有內容並點擊標籤。 –

回答

0

當遊戲開始時啓動一個計時器。兩張牌被揭示後,檢查它們是否匹配。如果他們這樣做,檢查是否有任何不匹配的卡片。如果是的話,不要停止計時器。如果否,停止定時器。

相關問題