Initial check in.
This commit is contained in:
37
src/org/dyndns/vahagn/sokoban/App.java
Normal file
37
src/org/dyndns/vahagn/sokoban/App.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package org.dyndns.vahagn.sokoban;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
public class App extends Application {
|
||||
|
||||
private static App mApp = null;
|
||||
protected PuzzleContainer pc;
|
||||
public int level;
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void onCreate()
|
||||
{
|
||||
super.onCreate();
|
||||
mApp = this;
|
||||
pc = new PuzzleContainer();
|
||||
level = 0;
|
||||
}
|
||||
|
||||
public static App theApp()
|
||||
{
|
||||
return mApp;
|
||||
}
|
||||
|
||||
public PuzzleContainer getPuzzleContainer()
|
||||
{
|
||||
return pc;
|
||||
}
|
||||
|
||||
public Puzzle getCurrentPuzzle()
|
||||
{
|
||||
return getPuzzleContainer().getPuzzle( level );
|
||||
}
|
||||
}
|
||||
82
src/org/dyndns/vahagn/sokoban/PlayActivity.java
Normal file
82
src/org/dyndns/vahagn/sokoban/PlayActivity.java
Normal file
@@ -0,0 +1,82 @@
|
||||
package org.dyndns.vahagn.sokoban;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
//import android.widget.TextView;
|
||||
|
||||
//import org.dyndns.vahagn.sokoban.R;
|
||||
import static org.dyndns.vahagn.sokoban.App.theApp;
|
||||
|
||||
public class PlayActivity extends Activity
|
||||
implements View.OnClickListener
|
||||
|
||||
{
|
||||
protected final String TAG = "PlayActivity";
|
||||
protected Puzzle puzzle = null;
|
||||
public PlayView board;
|
||||
protected int level;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState)
|
||||
{
|
||||
Log.d(TAG, "onCreate: " + savedInstanceState );
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
board = new PlayView( this );
|
||||
setContentView( board );
|
||||
board.setOnClickListener(this);
|
||||
|
||||
if (savedInstanceState == null)
|
||||
{
|
||||
level = 0;
|
||||
setPuzzle( level );
|
||||
// We were just launched -- set up a new game
|
||||
// mSnakeView.setMode(SnakeView.READY);
|
||||
}
|
||||
else
|
||||
{
|
||||
// We are being restored
|
||||
// Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
|
||||
// if (map != null) {
|
||||
// mSnakeView.restoreState(map);
|
||||
// } else {
|
||||
// mSnakeView.setMode(SnakeView.PAUSE);
|
||||
// }
|
||||
}
|
||||
//
|
||||
//
|
||||
//
|
||||
// super.onCreate(savedInstanceState);
|
||||
// setContentView(R.layout.menu);
|
||||
//
|
||||
// try {
|
||||
// theApp().getPuzzleContainer().getPuzzle( 0 );
|
||||
// } catch (Exception e) {
|
||||
// // TODO Auto-generated catch block
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onClick( View v )
|
||||
{
|
||||
Log.d(TAG, "onClick: " );
|
||||
++level;
|
||||
if ( level >= theApp().getPuzzleContainer().getCount() )
|
||||
level = 0;
|
||||
setPuzzle( level );
|
||||
board.invalidate();
|
||||
}
|
||||
|
||||
private void setPuzzle( int level )
|
||||
{
|
||||
String title = "Sokoban: level " + new Integer(level+1).toString();
|
||||
setTitle(title);
|
||||
puzzle = theApp().getPuzzleContainer().getPuzzle( level );
|
||||
board.setPuzzle(puzzle);
|
||||
}
|
||||
}
|
||||
231
src/org/dyndns/vahagn/sokoban/PlayView.java
Normal file
231
src/org/dyndns/vahagn/sokoban/PlayView.java
Normal file
@@ -0,0 +1,231 @@
|
||||
package org.dyndns.vahagn.sokoban;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.graphics.*;
|
||||
import android.view.*;
|
||||
import static org.dyndns.vahagn.sokoban.Puzzle.symbole.*;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class PlayView extends View
|
||||
{
|
||||
private final static String TAG = "PlayView";
|
||||
private Puzzle puzzle;
|
||||
|
||||
private Matrix col2scr = new Matrix();
|
||||
private Matrix scr2col = new Matrix();
|
||||
private Bitmap[] tile;
|
||||
private int tile_x_size;
|
||||
private int tile_y_size;
|
||||
|
||||
private int offset_x;
|
||||
private int offset_y;
|
||||
|
||||
private final Paint mPaint = new Paint();
|
||||
|
||||
public PlayView(Context context)
|
||||
{
|
||||
super(context);
|
||||
|
||||
setFocusable(true);
|
||||
//
|
||||
// Resources r = this.getContext().getResources();
|
||||
|
||||
// TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);
|
||||
//
|
||||
// mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
|
||||
//
|
||||
// a.recycle();
|
||||
}
|
||||
|
||||
public void setPuzzle( Puzzle p )
|
||||
{
|
||||
puzzle = p;
|
||||
initTiles();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up tiles and matrices based on puzzle and view sizes.
|
||||
*/
|
||||
private void initTiles()
|
||||
{
|
||||
//
|
||||
// Get the sizes of view. If it is zero then simplly do nothing.
|
||||
// Otherwise, make sure that the width is bigger the height.
|
||||
//
|
||||
int w = getWidth();
|
||||
int h = getHeight();
|
||||
if ( w==0 || h==0 )
|
||||
return;
|
||||
else if ( w < h )
|
||||
{
|
||||
col2scr.reset();
|
||||
col2scr.setRotate(90, w/2, w/2);
|
||||
col2scr.invert(scr2col);
|
||||
|
||||
int t = w;
|
||||
w = h;
|
||||
h = t;
|
||||
}
|
||||
else
|
||||
{
|
||||
col2scr.reset();
|
||||
}
|
||||
//
|
||||
// Calculate the tile sizes.
|
||||
// NOTE: since puzzle width is always bigger the height then we
|
||||
// rotate the puzzle.
|
||||
//
|
||||
tile_x_size = w / puzzle.getColumnCount();
|
||||
tile_y_size = h / puzzle.getRowCount();
|
||||
if ( tile_x_size < tile_y_size )
|
||||
tile_y_size = tile_x_size;
|
||||
else
|
||||
tile_x_size = tile_y_size;
|
||||
//
|
||||
// Calculate the offset of puzzle.
|
||||
//
|
||||
offset_x = (w-tile_x_size*puzzle.getColumnCount())/2;
|
||||
offset_y = (h-tile_y_size*puzzle.getRowCount())/2;
|
||||
//
|
||||
// Set up inverse matrix to get col,row from screen pixel.
|
||||
//
|
||||
col2scr.invert(scr2col);
|
||||
scr2col.postTranslate(-offset_x, -offset_y);
|
||||
scr2col.postScale((float)1./tile_x_size, (float)1./tile_y_size);
|
||||
//
|
||||
// Create tile bitmaps for given width and height.
|
||||
//
|
||||
tile = new Bitmap[Puzzle.getSymCount()];
|
||||
tile[FLOOR.ordinal()] = createFloorTile();
|
||||
tile[WALL.ordinal()] = createWallTile();
|
||||
tile[BOX.ordinal()] = createBoxTile();
|
||||
tile[HOLE.ordinal()] = createHoleTile();
|
||||
tile[BOX_IN_HOLE.ordinal()] = createBingoTile();
|
||||
tile[WORKER.ordinal()] = createWorkerTile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply screen to board mapping and if the points are in the
|
||||
* column,row range then return it. Otherwise return null.
|
||||
*/
|
||||
public Point getTile( float scr_x, float scr_y )
|
||||
{
|
||||
float [] scr_p = { scr_x, scr_y };
|
||||
scr2col.mapPoints( scr_p );
|
||||
|
||||
if ( 0 <= scr_p[0] && scr_p[0] < puzzle.getColumnCount()
|
||||
&& 0 <= scr_p[1] && scr_p[1] < puzzle.getRowCount())
|
||||
return new Point( (int)scr_p[0], (int)scr_p[1] );
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
private Bitmap createFloorTile()
|
||||
{
|
||||
Bitmap bitmap = Bitmap.createBitmap(tile_x_size, tile_y_size, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.drawARGB(0xff,0x40,0x40,0x40);
|
||||
return bitmap;
|
||||
}
|
||||
private Bitmap createWallTile()
|
||||
{
|
||||
Bitmap bitmap = Bitmap.createBitmap(tile_x_size, tile_y_size, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.drawARGB(0xff,0xF0,0x40,0x40);
|
||||
return bitmap;
|
||||
}
|
||||
private Bitmap createBoxTile()
|
||||
{
|
||||
Bitmap bitmap = Bitmap.createBitmap(tile_x_size, tile_y_size, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.drawARGB(0xff,0x40,0xF0,0x40);
|
||||
return bitmap;
|
||||
}
|
||||
private Bitmap createHoleTile()
|
||||
{
|
||||
Bitmap bitmap = Bitmap.createBitmap(tile_x_size, tile_y_size, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.drawARGB(0xff,0x40,0x40,0xF0);
|
||||
return bitmap;
|
||||
}
|
||||
private Bitmap createBingoTile()
|
||||
{
|
||||
Bitmap bitmap = Bitmap.createBitmap(tile_x_size, tile_y_size, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.drawARGB(0xff,0x40,0xF0,0xF0);
|
||||
return bitmap;
|
||||
}
|
||||
private Bitmap createWorkerTile()
|
||||
{
|
||||
Bitmap bitmap = createFloorTile();
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
Paint paint = new Paint();
|
||||
paint.setARGB(0xff,0xF0,0x40,0xF0);
|
||||
canvas.drawOval(new RectF(1,1,tile_x_size-2,tile_y_size-2), paint);
|
||||
|
||||
Paint p = new Paint();
|
||||
p.setColor( Color.YELLOW );
|
||||
canvas.drawLine(tile_x_size/2, tile_y_size/2, tile_x_size/2, 0, p );
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSizeChanged(int w, int h, int oldw, int oldh)
|
||||
{
|
||||
initTiles();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent (MotionEvent event)
|
||||
{
|
||||
int a = event.getAction();
|
||||
int ps = event.getPointerCount();
|
||||
int hs = event.getHistorySize();
|
||||
for ( int p = 0; p < ps; ++p )
|
||||
{
|
||||
String msg = String.format("Action %d Pointer %d History %d", a, event.getPointerId(p), hs);
|
||||
// for ( int h = 0; h < hs; ++h )
|
||||
// msg += String.format("(%f,%f)", event.getHistoricalX(p,h), event.getHistoricalY(p,h));
|
||||
msg += String.format("(%f,%f)", event.getX(p), event.getY(p));
|
||||
Log.d("Sokoban", msg);
|
||||
}
|
||||
Point p = getTile(event.getX(),event.getY());
|
||||
if ( p == null )
|
||||
{
|
||||
Log.d(TAG, "onTouchEvent: outside.");
|
||||
return super.onTouchEvent(event);
|
||||
}
|
||||
|
||||
// Log.d(TAG, "onTouchEvent: " + p.x + " " + p.y);
|
||||
return true; //super.onTouchEvent(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDraw(Canvas canvas)
|
||||
{
|
||||
super.onDraw(canvas);
|
||||
if ( puzzle != null )
|
||||
{
|
||||
int cols = puzzle.getColumnCount();
|
||||
int rows = puzzle.getRowCount();
|
||||
|
||||
canvas.concat(col2scr);
|
||||
for (int i = 0; i < rows; ++i)
|
||||
{
|
||||
for (int j = 0; j < cols; ++j)
|
||||
{
|
||||
canvas.drawBitmap(tile[puzzle.getSym(i,j).ordinal()],
|
||||
offset_x + j * tile_x_size,
|
||||
offset_y + i * tile_y_size,
|
||||
mPaint);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
94
src/org/dyndns/vahagn/sokoban/Puzzle.java
Normal file
94
src/org/dyndns/vahagn/sokoban/Puzzle.java
Normal file
@@ -0,0 +1,94 @@
|
||||
package org.dyndns.vahagn.sokoban;
|
||||
|
||||
//import java.lang.Exception;
|
||||
import java.io.*;
|
||||
import android.util.Log;
|
||||
|
||||
public class Puzzle
|
||||
{
|
||||
public enum symbole
|
||||
{
|
||||
FLOOR,
|
||||
WALL,
|
||||
BOX,
|
||||
HOLE,
|
||||
BOX_IN_HOLE,
|
||||
WORKER;
|
||||
|
||||
public static int MAX_COUNT()
|
||||
{
|
||||
return 6;
|
||||
}
|
||||
};
|
||||
|
||||
protected static final String TAG = "Puzzle";
|
||||
protected int columns;
|
||||
protected int rows;
|
||||
protected symbole [] board;
|
||||
|
||||
public Puzzle( InputStream is )
|
||||
{
|
||||
load( is );
|
||||
}
|
||||
|
||||
private void load( InputStream is )
|
||||
{try{
|
||||
columns = is.read();
|
||||
rows = is.read();
|
||||
byte [] b = new byte [columns*rows];
|
||||
board = new symbole[columns*rows];
|
||||
is.read( b, 0, columns*rows);
|
||||
for( int i = 0; i < b.length; ++i )
|
||||
{
|
||||
switch( b[i] )
|
||||
{
|
||||
case 0:
|
||||
board[i] = symbole.FLOOR;
|
||||
break;
|
||||
case 1:
|
||||
board[i] = symbole.WALL;
|
||||
break;
|
||||
case 2:
|
||||
board[i] = symbole.BOX;
|
||||
break;
|
||||
case 3:
|
||||
board[i] = symbole.HOLE;
|
||||
break;
|
||||
case 4:
|
||||
board[i] = symbole.BOX_IN_HOLE;
|
||||
break;
|
||||
case 5:
|
||||
board[i] = symbole.WORKER;
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
catch ( Exception e )
|
||||
{
|
||||
Log.d( TAG, "load()", e);
|
||||
}}
|
||||
|
||||
|
||||
public final int getColumnCount()
|
||||
{
|
||||
return columns;
|
||||
}
|
||||
public final int getRowCount()
|
||||
{
|
||||
return rows;
|
||||
}
|
||||
public static int getSymCount()
|
||||
{
|
||||
return symbole.MAX_COUNT();
|
||||
}
|
||||
|
||||
/*
|
||||
* r- row
|
||||
* c- column
|
||||
*/
|
||||
public final symbole getSym( int r, int c )
|
||||
{
|
||||
return board[r*columns+c];
|
||||
}
|
||||
}
|
||||
73
src/org/dyndns/vahagn/sokoban/PuzzleContainer.java
Normal file
73
src/org/dyndns/vahagn/sokoban/PuzzleContainer.java
Normal file
@@ -0,0 +1,73 @@
|
||||
package org.dyndns.vahagn.sokoban;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import static org.dyndns.vahagn.sokoban.App.theApp;
|
||||
import android.util.Log;
|
||||
|
||||
public class PuzzleContainer
|
||||
{
|
||||
protected final String TAG = "PuzzleContainer";
|
||||
|
||||
protected int count;
|
||||
|
||||
/*
|
||||
* Load puzzle from the puzzle.bin.
|
||||
*/
|
||||
public Puzzle getPuzzle( int i )
|
||||
{try{
|
||||
InputStream is = theApp().getResources().openRawResource(R.raw.puzzles);
|
||||
|
||||
//
|
||||
// Read amount of puzzles.
|
||||
//
|
||||
count = read_i32(is);
|
||||
if ( i >= count )
|
||||
return null;
|
||||
//
|
||||
// Read the offset.
|
||||
//
|
||||
is.skip( i*4 );
|
||||
int offset = read_i32(is);
|
||||
//
|
||||
// Jump to the offset and read the puzzle.
|
||||
//
|
||||
is.skip( offset - 4 - (i+1)*4);
|
||||
Puzzle p = new Puzzle( is );
|
||||
//
|
||||
// Finally return the puzzle and we are done.
|
||||
//
|
||||
return p;
|
||||
}
|
||||
catch ( java.lang.Exception e )
|
||||
{
|
||||
Log.d(TAG, "Exception: " + e.getMessage() );
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}}
|
||||
|
||||
public int getCount()
|
||||
{try{
|
||||
if ( count == 0 )
|
||||
{
|
||||
InputStream is = theApp().getResources().openRawResource(R.raw.puzzles);
|
||||
count = read_i32(is);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
catch ( java.lang.Exception e )
|
||||
{
|
||||
Log.d(TAG, "Exception: " + e.getMessage() );
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}}
|
||||
|
||||
protected int read_i32( InputStream is ) throws IOException
|
||||
{
|
||||
int i = is.read();
|
||||
i |= is.read() << 8;
|
||||
i |= is.read() << 16;
|
||||
i |= is.read() << 24;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
93
src/org/dyndns/vahagn/sokoban/SokobanMenu.java
Normal file
93
src/org/dyndns/vahagn/sokoban/SokobanMenu.java
Normal file
@@ -0,0 +1,93 @@
|
||||
package org.dyndns.vahagn.sokoban;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.view.View;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import static org.dyndns.vahagn.sokoban.App.theApp;
|
||||
|
||||
public class SokobanMenu extends Activity
|
||||
{
|
||||
protected final String TAG = "SokobanMenu";
|
||||
protected final String LEVEL= "org.dyndns.vahagn.sokoban.LEVEL";
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState)
|
||||
{
|
||||
Log.d(TAG, "onCreate: " + savedInstanceState );
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.menu);
|
||||
|
||||
try {
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart()
|
||||
{
|
||||
Log.d(TAG, "onStart: " );
|
||||
|
||||
super.onStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRestart()
|
||||
{
|
||||
Log.d(TAG, "onRestart: " );
|
||||
|
||||
super.onRestart();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume()
|
||||
{
|
||||
Log.d(TAG, "onResume: " );
|
||||
|
||||
super.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause()
|
||||
{
|
||||
Log.d(TAG, "onPause: " );
|
||||
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop()
|
||||
{
|
||||
Log.d(TAG, "onStop: " );
|
||||
|
||||
super.onStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy()
|
||||
{
|
||||
Log.d(TAG, "onDestroy: " );
|
||||
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
//@Override
|
||||
public void onConfigurationChanged()
|
||||
{
|
||||
// super.onConfigurationChanged();
|
||||
// setContentView(R.layout-l.menu);
|
||||
}
|
||||
|
||||
public void onStartCurrentPuzzle( View v)
|
||||
{
|
||||
Intent intent = new Intent(this, PlayActivity.class);
|
||||
|
||||
startActivity( intent );
|
||||
Log.d(TAG, "onStartCurrentPuzzle: " );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user