Android Open Source - miner Play Ground Sapper






From Project

Back to project page miner.

License

The source code is released under:

Apache License

If you think the Android project miner listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.kolomiyets.miner.screen;
// w  ww. java2 s .  c  om
import java.util.ArrayList;

import com.kolomiyets.miner.R;
import com.kolomiyets.miner.bt.ConnectionBase;
import com.kolomiyets.miner.bt.protocol.CmdFieldState;
import com.kolomiyets.miner.bt.protocol.CmdGameTeam;
import com.kolomiyets.miner.bt.protocol.EGameState;
import com.kolomiyets.miner.dialog.DialogProvider;
import com.kolomiyets.miner.dialog.EDialogType;
import com.kolomiyets.miner.view.GridCell;

import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;

public class PlayGroundSapper extends PlayGroundBase {
  String TAG = PlayGroundSapper.class.getSimpleName();
  
  ProgressIndicationPoster progressIndicationPoster;
  int dimention;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    root = inflater.inflate(R.layout.playground_sapper, null);
    grid = (TableLayout)root.findViewById(R.id.grid_miner_field);
//    ArrayList<Integer> testMatrix = getTestMatrix();
//    processIncommingFieldState(testMatrix);
    return root;
  }
  
  ArrayList<Integer> getTestMatrix(){
    int[] testInt = new int[]{
        0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  -1,  -1,  -1,  0,  -1,  0,
        0,  0,  -1,  0,  -1,  0,  0,  0,
        0,  0,  -1,  -1,  -1,  0,  0,  0,
        0,  0,  0,  -1,  0,  0,  0,  0,
        0,  -1,  0,  0,  0,  -1,  0,  0,
        0,  0,  0,  -1,  -1,  -1,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0
    };
    ArrayList<Integer> testMatrix = new ArrayList<Integer>();
    for(int i: testInt){
      testMatrix.add(i);
    }
    return testMatrix;
  }
  
  private void autoRevealCells(int startIndex) {
    if(cells.get(startIndex).getIsMarked() 
        || !cells.get(startIndex).getIsMystery() 
        || !activity.getCurrentGameState().equals(EGameState.SOLVE)) {
      return;
    }
    
    ArrayList<Integer> indecesToCheck = new ArrayList<Integer>(8);
    int minesCount = 0;
    
    int y = startIndex/dimention;
    int x = startIndex-y*dimention;
    
    for(int i=-1; i<2; i++){
      for(int j=-1; j<2; j++){
        int _x = x + j;
        int _y = y + i;
        
        if(_x>=0&&_x<dimention&&_y>=0&&_y<cells.size()) {
          int indexToCheck = startIndex+(i*dimention)+j;
          
          if(indexToCheck!=startIndex 
              && indexToCheck>=0
              && indexToCheck<cells.size()) {
            
            GridCell cellToCheck = cells.get(indexToCheck);
            if(cellToCheck.getIsMined()) { 
              minesCount++; 
            } else if(cellToCheck.getIsMystery()) { 
              indecesToCheck.add(indexToCheck);
            }
          }
        }
      }
    }
    
    if(minesCount>0) {
      cells.get(startIndex).setIndication(minesCount);
    } else {
      cells.get(startIndex).clear();
    }
    
    if(++processedCount+this.minesCount == cells.size()){
      sapperWins();
      return;
    }
    
    if(cells.get(startIndex).getIsClear()){
      for(Integer i: indecesToCheck){
        autoRevealCells(i);
      }
    }
  }
  
  @Override
  public void onClick(View v) {
    GridCell cell = (GridCell)v;
    if(cell.getIsMarked()){
      cell.removeMark();
    } else if (cell.getIsMined()) {
      cell.blowUp();
      minerWins();
    } else {
      autoRevealCells(cells.indexOf(cell));
    }
  }

  @Override
  public boolean onLongClick(View v) {
    GridCell cell = (GridCell)v;
    if(cell.getIsMystery()){
      ((Vibrator)activity.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(300);
      cell.setMark();
    }
    return true;
  }

  private void revealMines(){
    for(GridCell cell: cells){
      if(cell.getIsMined()){
        cell.blowUp();
      }
    }
  }
  
  @Override
  public void processIncommingFieldState(ArrayList<Integer> stateMatrix) {
    activity.setCurrentGameState(EGameState.SOLVE);
    
    dimention = (int)Math.sqrt(stateMatrix.size());
    
    cells = generateCells(stateMatrix);
    generateGrid(grid, cells, dimention);
    
    root.findViewById(R.id.wait_layout).setVisibility(View.INVISIBLE);
    root.findViewById(R.id.field_layout).setVisibility(View.VISIBLE);
    
    progressIndicationPoster = new ProgressIndicationPoster();
    progressIndicationPoster.start();
  }
  
  @Override
  public void minerWins() {
    finalizeConnection();
    revealMines();
    anounceWinner(true);
  }
  
  @Override
  public void sapperWins() {
    finalizeConnection();
    anounceWinner(false);
  }
  
  @Override
  public void finalizeConnection() {
    activity.setCurrentGameState(EGameState.FINISH);
    activity.stopGameStatePoster();
    setGridEnabled(false);
    ConnectionBase.killThread(progressIndicationPoster);
    activity.getConnectionManager().sendCmd(
        new CmdFieldState(generateStateMatrix()));
    activity.getConnectionManager().terminateConnection();
  }
  
  @Override
  ArrayList<Integer> generateStateMatrix(){
    ArrayList<Integer> matrix = new ArrayList<Integer>(cells.size());
    for(GridCell cell: cells){
      if(cell.getIsMarked()){
        matrix.add(CmdFieldState.CELL_STATE_MARK);
      } else if(!cell.getIsMystery()){
        matrix.add(CmdFieldState.CELL_STATE_PROCESSED);
      } else {
        matrix.add(CmdFieldState.CELL_STATE_EMPTY);
      }
    }
    return matrix;
  }
  
  private class ProgressIndicationPoster extends Thread {
    
    @Override
    public void run() {
      while(!this.isInterrupted()
          && !activity.getCurrentGameState().equals(EGameState.IDLE)
          && !activity.getCurrentGameState().equals(EGameState.FINISH)
          && !activity.getCurrentGameState().equals(EGameState.TERMINATE)){
        
        try {
          activity.getConnectionManager().sendCmd(
              new CmdFieldState(generateStateMatrix()));
          Thread.sleep(200);
        } catch (Exception e) {
          e.printStackTrace();
          break;
        }
      }
    }
    
  }
}




Java Source Code List

com.kolomiyets.miner.MinerActivity.java
com.kolomiyets.miner.Miner.java
com.kolomiyets.miner.bt.BtStateMonitor.java
com.kolomiyets.miner.bt.ConnectionBase.java
com.kolomiyets.miner.bt.ConnectionManager.java
com.kolomiyets.miner.bt.ConnectionMaster.java
com.kolomiyets.miner.bt.ConnectionProcessor.java
com.kolomiyets.miner.bt.ConnectionSlave.java
com.kolomiyets.miner.bt.EConnectMethod.java
com.kolomiyets.miner.bt.IBtEnableCallback.java
com.kolomiyets.miner.bt.IRequestBtResult.java
com.kolomiyets.miner.bt.notification.BtListener.java
com.kolomiyets.miner.bt.notification.BtNotificationCmd.java
com.kolomiyets.miner.bt.notification.BtNotificationConnect.java
com.kolomiyets.miner.bt.notification.BtNotificationDiscoveryDevice.java
com.kolomiyets.miner.bt.notification.BtNotificationDiscoveryState.java
com.kolomiyets.miner.bt.notification.BtNotificationDiscovery.java
com.kolomiyets.miner.bt.notification.BtNotificationHandshake.java
com.kolomiyets.miner.bt.notification.BtNotificationPower.java
com.kolomiyets.miner.bt.notification.BtNotificationState.java
com.kolomiyets.miner.bt.notification.BtNotification.java
com.kolomiyets.miner.bt.notification.EConnectionSate.java
com.kolomiyets.miner.bt.notification.NotificationManager.java
com.kolomiyets.miner.bt.protocol.CmdBase.java
com.kolomiyets.miner.bt.protocol.CmdFactory.java
com.kolomiyets.miner.bt.protocol.CmdFieldState.java
com.kolomiyets.miner.bt.protocol.CmdGameState.java
com.kolomiyets.miner.bt.protocol.CmdGameTeam.java
com.kolomiyets.miner.bt.protocol.CmdHandshake.java
com.kolomiyets.miner.bt.protocol.CmdPing.java
com.kolomiyets.miner.bt.protocol.CmdResponse.java
com.kolomiyets.miner.bt.protocol.ECmdName.java
com.kolomiyets.miner.bt.protocol.EGameState.java
com.kolomiyets.miner.dialog.DialogProvider.java
com.kolomiyets.miner.dialog.EDialogType.java
com.kolomiyets.miner.dialog.IDialogResult.java
com.kolomiyets.miner.screen.PlayGroundBase.java
com.kolomiyets.miner.screen.PlayGroundMiner.java
com.kolomiyets.miner.screen.PlayGroundSapper.java
com.kolomiyets.miner.screen.ScreenBase.java
com.kolomiyets.miner.screen.ScreenConnectingGame.java
com.kolomiyets.miner.screen.ScreenCreateGame.java
com.kolomiyets.miner.screen.ScreenDevices.java
com.kolomiyets.miner.screen.ScreenManager.java
com.kolomiyets.miner.screen.ScreenSelectPlayer.java
com.kolomiyets.miner.view.GridCell.java