Android Open Source - FindtheMines Main Activity






From Project

Back to project page FindtheMines.

License

The source code is released under:

MIT License

If you think the Android project FindtheMines 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.stealthness.findthemines;
//from  ww w.j av a 2s  . c om
import com.stealthness.findthemines.R;

import findtheminecore.BoardSize;
import findtheminecore.Game;
import findtheminecore.Position;
import findtheminecore.RuleSet;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener, OnItemLongClickListener {

  Game game;
  RuleSet rules;
  BoardSize size;
    private GridView gridView;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
      size = BoardSize.SMALL;
        rules = RuleSet.STANDARD;
        
        restartBoard(rules,size); 
        
        gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(new ImageAdapter(this,game));

        gridView.setOnItemClickListener(this);
        gridView.setOnItemLongClickListener(this);
      
    }


    
    private void restartBoard(RuleSet rules, BoardSize size){
        this.rules = rules;
        this.size = size;
    game = new Game(rules,size);
    game.randomiseBoard();
    }
    
  private Position convertPos(int position) {
    int i,j;
    i = position/game.getBoard().getNoOfCols();
    j = position - i*game.getBoard().getNoOfCols();
    Position pos = new Position(i,j);
    return pos;
  }
    
    public void startGame(View v){
      switchButtons(1);
      game.startGame();
        updateGrid();
    }
    
    public void restartGame(View v){
      switchButtons(0);
      restartBoard(rules,size);
        updateGrid();
    }
    
    public void restartGame(){
      switchButtons(0);
      restartBoard(rules,size);
        updateGrid();
    }
    
    public void endGame(View v){
      switchButtons(2);
      game.endGame();
      game.revealMines();
      updateGrid();   
    }
    
    private void switchButtons(int mode){
      switch (mode){
      case 0:
        setButtonVisabilty(Button.GONE,Button.VISIBLE,Button.GONE,Button.GONE);
        break;
      case 1:
        setButtonVisabilty(Button.VISIBLE,Button.GONE,Button.GONE,Button.VISIBLE);
        break;
      case 2:
        setButtonVisabilty(Button.GONE,Button.GONE,Button.VISIBLE,Button.VISIBLE);
        break;
      default:
        setButtonVisabilty(Button.VISIBLE,Button.VISIBLE,Button.VISIBLE,Button.VISIBLE);
        break;
      }  
    }
    
    public void setButtonVisabilty(int endVisibility, int startVisibility, int restartVisibility, int gridVisibility){
      Button startButton = (Button)findViewById(R.id.buttonStartGame);
      Button endButton = (Button)findViewById(R.id.buttonEndGame);
      Button restartButton = (Button)findViewById(R.id.buttonRestartGame);

    endButton.setVisibility(endVisibility);
    startButton.setVisibility(startVisibility);
    restartButton.setVisibility(restartVisibility);
    gridView.setVisibility(gridVisibility);
    }
    
    private void updateGrid(){    
      gridView.setNumColumns(size.noOfCols());
      gridView.setAdapter(new ImageAdapter(this,game));
    }


  @Override
  public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
      Position pos = convertPos(position);
      if (!(game.selectPos(pos))){
        Toast.makeText(getApplicationContext(),"Move fail :"+position,Toast.LENGTH_LONG).show();
      }
      ImageView imageView=(ImageView) parent.getChildAt(position);
        imageView.setImageResource(ImageAdapter.getImageResource(pos, game));
        testEndGame(v);
  }

  private void testEndGame(View v) {
    if(game.isEnded()){
      endGame(v);
      if (game.isWon()){
        Toast.makeText(getApplicationContext(),"Won",Toast.LENGTH_LONG).show();
      }
      switchButtons(2);
    }else if (game.getNoMovesLeft()==0){
      if (game.isWon()){
        Toast.makeText(getApplicationContext(),"Won",Toast.LENGTH_LONG).show();
      }
      endGame(v);
      switchButtons(2);
    }
    
  }

  @Override
  public boolean onItemLongClick(AdapterView<?> parent, View v, int position,  long id) {
    Position pos = convertPos(position);
    ImageView imageView=(ImageView) parent.getChildAt(position);
    if (!game.getPos(pos).isSelected()){    
      if (game.getPos(pos).isMarked()){      
        game.unMarkPos(pos);
        imageView.setImageResource(R.drawable.empty_blue);  
      }else{
        game.markPos(pos);
        imageView.setImageResource(R.drawable.marked_blue);  
        testEndGame(v);
      }    
    }    
        return true;
  }
  
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    if(game.isStarted){
      return false;
    }else{
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.game_menu, menu);
        DisplayMetrics metrics = getBaseContext().getResources().getDisplayMetrics();
          int widthSize = metrics.widthPixels/game.getBoard().getNoOfCols();
        menu.findItem(R.id.standard_rules).setChecked(true);
        menu.findItem(R.id.standard_size).setChecked(true);
          if (widthSize >60){
            menu.findItem(R.id.large_size).setVisible(true);
            menu.findItem(R.id.huge_size).setVisible(false);
          }
          if (widthSize >120){
            menu.findItem(R.id.large_size).setVisible(true);
            menu.findItem(R.id.huge_size).setVisible(false);
          } 
          if (widthSize >200){
            menu.findItem(R.id.huge_size).setVisible(true);
          }  
          return true;    
    }

      
      
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle presses on the action bar items
      switch (item.getItemId()) {
          case R.id.standard_size:
              changeSize(BoardSize.STANDARD);
              restartGame();
              return true;
          case R.id.large_size:
              changeSize(BoardSize.LARGE);
              restartGame();
              return true;
          case R.id.huge_size:
            item.setCheckable(false);
            restartGame();
            return false;
          case R.id.one_size:
              changeSize(BoardSize.ONE);
              restartGame();
            return false;
          case R.id.small_size:
              changeSize(BoardSize.SMALL);
              restartGame();
            return true;
          /// rules ///
          case R.id.standard_rules:
            changeRules(RuleSet.STANDARD);  
            restartGame();
            return true;
          case R.id.trafficlights_rules:
            changeRules(RuleSet.TRAFFICLIGHTS);  
            return true;
          case R.id.hard_rules:
            changeRules(RuleSet.HARD);  
            restartGame();
            return true;
          default:
              return super.onOptionsItemSelected(item);
      }
  }



  private void changeSize(BoardSize boardSize) {
    Log.d("test", "changeSize():"+boardSize.toString());  
    if (boardSize == BoardSize.ONE){
       restartBoard(rules, boardSize);
    }else if(boardSize ==BoardSize.STANDARD){
       restartBoard(rules, boardSize);    
    }else if (boardSize ==BoardSize.SMALL){
       restartBoard(rules, boardSize);  
    }else if (boardSize ==BoardSize.HUGE && !(Configuration.SCREENLAYOUT_SIZE_LARGE==getBaseContext().getResources().getConfiguration().screenLayout)){
       restartBoard(rules, boardSize);  
    }else if (boardSize ==BoardSize.LARGE && !(Configuration.SCREENLAYOUT_SIZE_LARGE==getBaseContext().getResources().getConfiguration().screenLayout)){
       restartBoard(rules, boardSize);  
    }
    updateGrid();
  }



  private void changeRules(RuleSet ruleChoice) {
    Log.d("test","changeRules():"+ruleChoice.toString());
    restartBoard(ruleChoice, size);
    updateGrid();
    
  }
  
}




Java Source Code List

com.stealthness.findthemines.ImageAdapter.java
com.stealthness.findthemines.MainActivity.java
com.stealthness.findthemines.SplashScreen.java
findtheminecore.BoardSize.java
findtheminecore.Board.java
findtheminecore.Game.java
findtheminecore.Position.java
findtheminecore.RuleSet.java
findtheminecore.TileType.java
findtheminecore.Tile.java