Android Open Source - miner Play Ground Miner






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;
//from  w ww  . ja va2s .c o  m
import java.util.ArrayList;

import android.os.Bundle;
import android.text.InputFilter.LengthFilter;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;

import com.kolomiyets.miner.R;
import com.kolomiyets.miner.bt.notification.BtListener;
import com.kolomiyets.miner.bt.protocol.CmdFieldState;
import com.kolomiyets.miner.bt.protocol.EGameState;
import com.kolomiyets.miner.view.GridCell;

public class PlayGroundMiner extends PlayGroundBase implements OnItemSelectedListener {
  
  String TAG = PlayGroundMiner.class.getSimpleName();
  
  Button finish;
  Spinner dimenSelect;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    root = inflater.inflate(R.layout.playground_miner, null);
    grid = (TableLayout)root.findViewById(R.id.grid_miner_field);
    
    dimenSelect = (Spinner)root.findViewById(R.id.select_field_size);
    finish = (Button)root.findViewById(R.id.btn_finish_set_mines);
    
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(activity,
            R.array.dimen_lbls, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    dimenSelect.setAdapter(adapter);
    dimenSelect.setSelection(1);
    dimenSelect.setOnItemSelectedListener(this);
    dimenSelect.setEnabled(false);
    
    finish.setOnClickListener(new OnClickListener() {
      
      @Override
      public void onClick(View v) {
        passFieldToSapper();
      }
    });
    
    return root;
  }
  
  public void onStart() {
    super.onStart();
    buildFieldBySelection(dimenSelect.getSelectedItemPosition());
  };
  
  private int getDimension(int labelIndex){
    return getResources().getIntArray(R.array.dimen_vals)[labelIndex];
  }

  private void buildFieldBySelection(int selectIndex){
    cells = generateCells(getDimension(selectIndex));
    generateGrid(grid, cells, getDimension(selectIndex));
  }
  
  @Override
  public void onItemSelected(AdapterView<?> data, View view, int position, long id) {
    buildFieldBySelection(position);
  }

  @Override
  public void onNothingSelected(AdapterView<?> arg0) {}

  @Override
  public void onClick(View v) {
    GridCell cell = (GridCell)v;
    if(cell.getIsMined()){
      minesCount--;
      cell.clear();
    } else {
      cell.setMine();
      if(++minesCount == cells.size()/2){
        passFieldToSapper();
        return;
      }
    }
    finish.setEnabled(minesCount>0);
  }

  @Override
  public boolean onLongClick(View v) { return true; }
  
  private void passFieldToSapper() {
    activity.setCurrentGameState(EGameState.SOLVE);
    activity.getConnectionManager().sendCmd(
        new CmdFieldState(generateStateMatrix()));
    finish.setEnabled(false);
    dimenSelect.setEnabled(false);
    setGridEnabled(false);
    Toast.makeText(activity, R.string.msg_watch_sapper_play, Toast.LENGTH_LONG).show();
  }

  @Override
  ArrayList<Integer> generateStateMatrix(){
    ArrayList<Integer> matrix = new ArrayList<Integer>(cells.size());
    for(GridCell cell: cells){
      if(cell.getIsMined()){
        matrix.add(CmdFieldState.CELL_STATE_MINE);
      } else {
        matrix.add(CmdFieldState.CELL_STATE_EMPTY);
      }
    }
    return matrix;
  }
  
  @Override
  public void processIncommingFieldState(ArrayList<Integer> stateMatrix) {
    if(activity.getCurrentGameState().equals(EGameState.FINISH)) return;
    int processedCount = 0;
    for(int i=0; i<stateMatrix.size(); i++){
      int state = stateMatrix.get(i);
      if(state == CmdFieldState.CELL_STATE_MARK){
        cells.get(i).highlight(R.color.blue);
      } else if(state == CmdFieldState.CELL_STATE_PROCESSED){
        if(cells.get(i).getIsMined()){
          cells.get(i).blowUp();
          minerWins();
          return;
        } else {
          cells.get(i).highlight(R.color.green);
          if(++processedCount+minesCount == cells.size()){
            sapperWins();
            return;
          }
        }
      } else if(state == CmdFieldState.CELL_STATE_MINE){
        cells.get(i).highlight(R.color.grey);
      }
    }
  }

  @Override
  public void minerWins() {
    finalizeConnection();
    anounceWinner(true);
  }

  @Override
  public void sapperWins() {
    finalizeConnection();
    anounceWinner(false);
  }
  
  @Override
  public void finalizeConnection() {
    activity.setCurrentGameState(EGameState.FINISH);
    activity.stopGameStatePoster();
    activity.getConnectionManager().terminateConnection();
    cmdReceiver.unregister(activity);
    setGridEnabled(false);
  }
}




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