Android Open Source - iSlide Board






From Project

Back to project page iSlide.

License

The source code is released under:

MIT License

If you think the Android project iSlide 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 edu.csun.group2.islide.engine;
import java.util.ArrayList;
import java.util.Collections;
//from w  w w  . ja va  2 s .c  o  m

public class Board {
  
  public ArrayList<Byte> ary;//! the representation of the game board
  public byte empty;//! the index of the empty tile
  public byte width;// dimension of the board (N)
  public byte board_size;//! total number of tiles (NxN)
  
  /** /brief Constructor for the board class
   *   Creates an NxN board to play a sliding puzzle game with
   * @param size the width and height of the board, dimension N in an NxN board
   */
  public Board(int size)
  {
    width = (byte) size;
    board_size = (byte) (width*width);
    ary = new ArrayList<Byte>(board_size);
    
    for(byte i =0; i < board_size; ++i )
      ary.add(i);
/*
    java.util.Collections.shuffle(ary);
    empty = (byte) getIndex(0);

    
    while(!solvable())
    {
      int row =empty/width;
      int next = (row +1) * width;
      int safe_index = next%board_size;      
      Collections.swap(ary, (safe_index ), (safe_index +1) );
      empty = (byte) getIndex(0);
    }
    */
      
  }
  
  //! copy constructor
  @SuppressWarnings("unchecked")
  public Board(Board other)
  {
    width = other.width;
    board_size = other.board_size;
    empty = other.empty;
    //ary.addAll(other.ary);
    ary = (ArrayList<Byte>)other.ary.clone();
  }
  
  
    
  /** /brief Determines if the puzzle is solvable
   * @return boolean -true if the puzzle is solvable, otherwise false
   * 
  */
  public static boolean solvable(Board board){
    int width = board.width;
    int board_size = board.board_size;
    int empty = board.empty;
    @SuppressWarnings("unchecked")
    ArrayList<Byte> ary =(ArrayList<Byte>)board.ary.clone();
    
    //do we have an even number of inversions?
    boolean even_inversions = true;
    
    //is the width of the board even?
    int row = width - (empty/width);
    boolean even_width = (width & 0x1) == 0;
    /*System.out.println("Empty: " + empty);
    System.out.println("Empty Row from Bottom: "+ row);
    System.out.println("even width: " + even_width);*/
    
    // is the blank in an even row?
    boolean odd_blank = (row & 0x1) != 0;
    //System.out.println("Odd blank row from bottom: " + odd_blank);
    boolean even_blank = !odd_blank;
    
    int inversions = 0;
    
    for(int i = 0; i < board_size; ++i)
    {
      int x = ary.get(i);
      if(x == 0)
        continue;
      for(int j = i+1; j < board_size; ++j)
      {
        int y =ary.get(j);
        if( (y !=0) && (x > y) )
          inversions++;
      }
    }
    
    //if # of inversions is odd
    if((inversions & 0x1) != 0 )
      even_inversions = false;
    //System.out.println("odd inversions: " + !even_inversions);
    boolean odd_inversions = !even_inversions;
    //System.out.println("#inversions: " + inversions);
    //board.print();
    
    // odd width and even inv
    if(!even_width && even_inversions) 
        return true;
    
    // even width, odd m-i and odd inv
    if(even_width &&  odd_blank && odd_inversions)
      return true;
    
    // even width, even m-i and even inv
    if(even_width && even_blank && even_inversions)
      return true;
    
    return false;
  } 
  
  public boolean solvable(){
    return Board.solvable(this);
  }
  
  /**
   * /brief Makes a move for a sliding puzzle board
   * @param index
   * @return returns true if the move was made, else false
   */
  public boolean move(int index){
    
    if(index < board_size && index >= 0 && index != empty)
    {
      int diff = Math.abs(index - empty);
      
      if( ( (index%width == empty%width) && ( diff/width == 1 ) ) 
          || ( (diff ==1) && empty/width == index/width))//(Math.abs((empty%width) - (index%width)) != (width-1)) ) )
      {
        Collections.swap(ary, empty, index);
        empty = (byte) index;
        return true;
      }
      return false;      
    }
    //consider throwing exception
    return false;
  }
  
  public int hashCode(){
    return ary.hashCode();
  }
  
  boolean equals(Board other){
    
    if(ary.equals(other.ary))
      return true;
    
    return false;    
  }
  
  int getIndex(int num)
  {
    for(int i = 0; i < board_size; ++i)
    {
      if( ary.get(i) == num)
        return i;
    }
    
    return -1;
  }
  
  
  void print(){
    
    for(int i = 0; i < board_size; ++i)
    {
      System.out.print(ary.get(i) + " ");
      if((i+1)%width == 0)
        System.out.println();
    }
    System.out.println();
  }
  
  
}




Java Source Code List

edu.csun.group2.islide.CameraPreview.java
edu.csun.group2.islide.HighScore.java
edu.csun.group2.islide.MainActivity.java
edu.csun.group2.islide.MainMenuActivity.java
edu.csun.group2.islide.PhotoHandler.java
edu.csun.group2.islide.PlayMenu.java
edu.csun.group2.islide.SelectImageActivity.java
edu.csun.group2.islide.SettingsMenu.java
edu.csun.group2.islide.TakePictureActivity.java
edu.csun.group2.islide.UtilityInfo.java
edu.csun.group2.islide.Utility.java
edu.csun.group2.islide.engine.BoardTest.java
edu.csun.group2.islide.engine.Board.java
edu.csun.group2.islide.engine.GameBoardTest.java
edu.csun.group2.islide.engine.GameBoard.java
edu.csun.group2.islide.engine.GameManager.java
edu.csun.group2.islide.engine.InputHandler.java
edu.csun.group2.islide.engine.MoveComparator.java
edu.csun.group2.islide.engine.MoveTest.java
edu.csun.group2.islide.engine.Move.java
edu.csun.group2.islide.engine.Pair.java
edu.csun.group2.islide.engine.entity.SlideTile.java
edu.csun.group2.islide.engine.entity.TileManager.java
edu.csun.group2.islide.global.GameInfo.java
edu.csun.group2.islide.interfaces.IRenderable.java
edu.csun.group2.islide.iSlide.java