From: gitknilch Date: Thu, 7 Mar 2013 16:51:05 +0000 (+0100) Subject: rename package to "freeshisen" X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/FreeShisen/commitdiff_plain/92b19250a620252d642c0abaf8dd3d733c299f3d rename package to "freeshisen" Change-Id: I2570d4c5bb555dd7e1381e3bf1f81f24a755b08f Signed-off-by: gitknilch --- diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 92f9f94..651a554 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,6 +1,6 @@ @@ -10,12 +10,12 @@ @@ -24,25 +24,25 @@ + android:value="de.cwde.freeshisen.ShisenShoActivity" /> - + + android:value="de.cwde.freeshisen.ShisenShoActivity" /> diff --git a/src/de/cwde/freeshisen/Board.java b/src/de/cwde/freeshisen/Board.java new file mode 100644 index 0000000..8e34402 --- /dev/null +++ b/src/de/cwde/freeshisen/Board.java @@ -0,0 +1,389 @@ +package de.cwde.freeshisen; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +public class Board { + private static String charpieces = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + + public int difficulty=1; // 1=Hard ... N=Easy + public boolean gravity=true; + public int [] boardSize; + public char[][] board; + public LinkedList history; + + // ---------------------- + // Public methods + // ---------------------- + + public Board() { + } + + // The board always has a 1-square width free rectangle that has + // to be taken into account when specifying the size + public void initialize(int sizeI, int sizeJ) { + boardSize = new int[2]; + boardSize[0]=sizeI; + boardSize[1]=sizeJ; + board = new char[boardSize[0]][boardSize[1]]; + for (int i=0;i(); + } + + public static String pieceToString(char piece) { + return charpieces.substring(piece,1); + } + + public static char StringToPiece(String piece) { + char upiece; + long charpiecesLen=charpieces.length(); + for(upiece=0;(upiece0) result+=" "; + result+=""+(j%10); + } + result+="\n "+StringRepeat("--",boardSize[1]); + for (int i=0;i0) result+=" "; + result+=charpieces.substring(board[i][j],board[i][j]+1); + } + result+=" |\n"; + if (i getPairs(int maxResults) { + List result=new ArrayList(); + List pieces=new ArrayList(); + List> piecePoints=new ArrayList>(); + for (int i=0;i points0=new ArrayList(); + points0.add(p); + pieces.add(piece); + piecePoints.add(points0); + + key=pieces.indexOf(piece); + piecePoints.get(key); + } else { + List points1=piecePoints.get(key); + points1.add(p); + } + } + + int nresults=0; + for (List points : piecePoints) { + int n=(int)points.size(); + for (int i=0;i path=getPath(a.copy(),b.copy()); + if (path!=null && path.size()>0) { + result.add(new Line(a,b)); + if (nresults++==maxResults) break; + } + } + if (nresults==maxResults) break; + } + if (nresults==maxResults) break; + } + return result; + } + + public int getNumPieces() { + int result=0; + for (int j=0;j getHorizontalLines(Point excludeA, Point excludeB) { + List result=new ArrayList(); + for (int i=0;i getVerticalLines(Point excludeA, Point excludeB) { + List result=new ArrayList(); + for (int j=0;j0;i--) board[i][p.j]=board[i-1][p.j]; + } + + private void undoGravity(Point p) { + if (gravity) for (int i=0;i=getMin().j && p.j<=getMax().j) + || (p.j==a.j && p.j==b.j && p.i>=getMin().i && p.i<=getMax().i); + } + + public Point cuts(Line l) { + if (isHorizontal() && l.isVertical() + && getMin().j<=l.a.j && getMax().j>=l.a.j + && l.getMin().i<=a.i && l.getMax().i>=a.i ) { + return new Point(a.i,l.a.j); + } else if (isVertical() && l.isHorizontal() + && getMin().i<=l.a.i && getMax().i>=l.a.i + && l.getMin().j<=a.j && l.getMax().j>=a.j ) { + return new Point(l.a.i,a.j); + } else return null; + } + + public Point getMin() { + if (a.ib.i || a.j>b.j) return a; + else return b; + } + + public String toString() { + return a+"-"+b; + } + + public Point a; + public Point b; +} diff --git a/src/de/cwde/freeshisen/Move.java b/src/de/cwde/freeshisen/Move.java new file mode 100644 index 0000000..9d3179d --- /dev/null +++ b/src/de/cwde/freeshisen/Move.java @@ -0,0 +1,17 @@ +package de.cwde.freeshisen; + +public class Move { + public Move(Point a, Point b, char piece) { + this.a=a; + this.b=b; + this.piece=piece; + } + + public String toString() { + return a+"-"+b+"("+Board.pieceToString(piece)+")"; + } + + public Point a; + public Point b; + public char piece; +} diff --git a/src/de/cwde/freeshisen/Point.java b/src/de/cwde/freeshisen/Point.java new file mode 100644 index 0000000..fc3e971 --- /dev/null +++ b/src/de/cwde/freeshisen/Point.java @@ -0,0 +1,30 @@ +package de.cwde.freeshisen; + +class Point { + public Point(int i, int j) { + this.i=i; + this.j=j; + } + + public boolean equals(Point p) { + return (i==p.i && j==p.j); + } + + public String toString() { + return "("+i+","+j+")"; + } + + public static Point fromString(String s) { + String[] ij=s.split(",",2); + int i=Integer.parseInt(ij[0]); + int j=Integer.parseInt(ij[1]); + return new Point(i,j); + } + + public int i; + public int j; + + public Point copy() { + return new Point(this.i,this.j); + } +} diff --git a/src/de/cwde/freeshisen/SettingsActivity.java b/src/de/cwde/freeshisen/SettingsActivity.java new file mode 100644 index 0000000..fbc87ae --- /dev/null +++ b/src/de/cwde/freeshisen/SettingsActivity.java @@ -0,0 +1,74 @@ +package de.cwde.freeshisen; + +import android.content.SharedPreferences; +import android.content.SharedPreferences.OnSharedPreferenceChangeListener; +import android.content.res.Resources; +import android.os.Bundle; +import android.preference.*; + +public class SettingsActivity extends PreferenceActivity +implements OnSharedPreferenceChangeListener { + + private ShisenSho app; + + private static final String KEY_PREF_DIFF = "pref_diff"; + private static final String KEY_PREF_SIZE = "pref_size"; + //private static final String KEY_PREF_GRAV = "pref_grav"; + //private static final String KEY_PREF_TIME = "pref_time"; + + @SuppressWarnings("deprecation") + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + app = ShisenSho.app(); + addPreferencesFromResource(R.xml.preferences); + SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences(); + + sharedPreferences.registerOnSharedPreferenceChangeListener(this); + updateSummary(sharedPreferences, KEY_PREF_DIFF, KEY_PREF_DIFF, R.array.difficulties); + updateSummary(sharedPreferences, KEY_PREF_SIZE, KEY_PREF_SIZE, R.array.sizes); + } + + @Override + public void onBackPressed() { + app.setOptions(); + super.onBackPressed(); + } + + @SuppressWarnings("deprecation") + @Override + protected void onResume() { + super.onResume(); + getPreferenceScreen().getSharedPreferences() + .registerOnSharedPreferenceChangeListener(this); + } + + @SuppressWarnings("deprecation") + @Override + protected void onPause() { + super.onPause(); + getPreferenceScreen().getSharedPreferences() + .unregisterOnSharedPreferenceChangeListener(this); + } + + + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + updateSummary(sharedPreferences, key, KEY_PREF_DIFF, R.array.difficulties); + updateSummary(sharedPreferences, key, KEY_PREF_SIZE, R.array.sizes); + } + + private void updateSummary(SharedPreferences sharedPreferences, String changedkey, String mykey, int myresource) { + if (changedkey.equals(mykey)) { + // FIXME: handle NumberFormatException here? + int i = Integer.parseInt(sharedPreferences.getString(changedkey, "1")); + + Resources res = getResources(); + String[] mystrings = res.getStringArray(myresource); + String name = mystrings[i-1]; + + @SuppressWarnings("deprecation") + Preference myPref = findPreference(changedkey); + myPref.setSummary("Currently: " + name); + } + } +} diff --git a/src/de/cwde/freeshisen/ShisenSho.java b/src/de/cwde/freeshisen/ShisenSho.java new file mode 100644 index 0000000..f4ee58e --- /dev/null +++ b/src/de/cwde/freeshisen/ShisenSho.java @@ -0,0 +1,114 @@ +package de.cwde.freeshisen; + +import android.app.Application; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import android.util.Log; + +public class ShisenSho extends Application { + private static ShisenSho instance = null; + private ShisenShoView view = null; + public ShisenShoActivity activity = null; + + public Board board; + public int[] boardSize=new int[2]; + public int difficulty=1; // 1=Easy, 2=Hard + public int size=3; // 1=Small, 2=Medium, 3=Big + public boolean gravity=true; + public boolean timeCounter=true; + + public static void log(String msg) { + Log.w("ShisenSho", msg); + } + + public void newPlay() { + board = new Board(); + board.buildRandomBoard(boardSize[0],boardSize[1],difficulty,gravity); + } + + public void setSize(int s) { + switch (s) { + case 1: + size=1; + boardSize[0]=6+2; + boardSize[1]=8+2; + break; + case 2: + size=2; + boardSize[0]=6+2; + boardSize[1]=12+2; + break; + case 3: + default: + size=3; + boardSize[0]=6+2; + boardSize[1]=16+2; + break; + } + } + + public void sleep(int deciSeconds) { + try { + Thread.sleep(deciSeconds*100); + } catch (InterruptedException e) { } + } + + public ShisenSho() { + instance = this; + setSize(size); + } + + public static synchronized ShisenSho app() { + return instance; + } + + public ShisenShoView getView() { + if (view == null) view = new ShisenShoView(this); + return view; + } + + /** Called when the activity is first created. */ + @Override + public void onCreate() { + super.onCreate(); + PreferenceManager.setDefaultValues(this, R.xml.preferences, false); + setOptions(); + } + + public void setOptions() { + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); + + // FIXME: handle NumberFormatException here? + int size = Integer.parseInt(sharedPref.getString("pref_size", "1")); + int difficulty = Integer.parseInt(sharedPref.getString("pref_diff", "1")); + boolean gravity = sharedPref.getBoolean("pref_grav", true); + boolean timeCounter = sharedPref.getBoolean("pref_time", true); + + boolean needsReset = false; + + if (size != this.size) { + setSize(size); + needsReset = true; + } + + if (difficulty != this.difficulty) { + this.difficulty = difficulty; + needsReset = true; + } + + if (gravity != this.gravity) { + this.gravity = gravity; + needsReset = true; + } + + if (timeCounter != this.timeCounter) { + this.timeCounter = timeCounter; + view.onTimeCounterActivate(); + } + + if (needsReset && (view != null)) { + view.reset(); + } + } + +} diff --git a/src/de/cwde/freeshisen/ShisenShoActivity.java b/src/de/cwde/freeshisen/ShisenShoActivity.java new file mode 100644 index 0000000..0478d07 --- /dev/null +++ b/src/de/cwde/freeshisen/ShisenShoActivity.java @@ -0,0 +1,121 @@ +package de.cwde.freeshisen; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.Intent; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.text.SpannableString; +import android.text.util.Linkify; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.ViewGroup; +import android.view.Window; +import android.widget.TextView; + +public class ShisenShoActivity extends Activity { + private ShisenShoView view; + + /** Called when the activity is first created. */ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + PreferenceManager.setDefaultValues(this, R.xml.preferences, false); + + requestWindowFeature(Window.FEATURE_NO_TITLE); + //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, + // WindowManager.LayoutParams.FLAG_FULLSCREEN); + + view = ShisenSho.app().getView(); + ShisenSho.app().activity = this; + setContentView(view); + } + + @Override + protected void onDestroy() { + ViewGroup vg = (ViewGroup)(view.getParent()); + vg.removeView(view); + ShisenSho.app().activity = null; + super.onDestroy(); + } + + @Override + protected void onPause() { + if (view!=null) { + view.pauseTime(); + } + super.onPause(); + } + + @Override + protected void onResume() { + super.onResume(); + if (view!=null) { + view.resumeTime(); + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.menu, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle item selection + switch (item.getItemId()) { + case R.id.hint: + case R.id.undo: + case R.id.clean: + return view.onOptionsItemSelected(item); + case R.id.options: + startActivityForResult(new Intent("de.cwde.freeshisen.SETTINGS", null), 0); + return true; + case R.id.about: + onAboutActivate(); + return true; + default: + return super.onOptionsItemSelected(item); + } + } + + private void onAboutActivate() { + // Try to load the a package matching the name of our own package + PackageInfo pInfo; + try { + pInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA); + String aboutTitle = String.format("About %s", getString(R.string.app_name)); + String versionString = String.format("Version: %s", pInfo.versionName); + String aboutText = getString(R.string.aboutText); + + // Set up the TextView + final TextView message = new TextView(this); + // We'll use a spannablestring to be able to make links clickable + final SpannableString s = new SpannableString(aboutText); + + // Set some padding + message.setPadding(5, 5, 5, 5); + // Set up the final string + message.setText(versionString + "\n" + s); + // Now linkify the text + Linkify.addLinks(message, Linkify.ALL); + + new AlertDialog.Builder(this) + .setTitle(aboutTitle) + .setCancelable(true) + .setIcon(R.drawable.icon) + .setPositiveButton(getString(android.R.string.ok), null) + .setView(message).create() + .show(); + } catch (NameNotFoundException e) { + e.printStackTrace(); + } + } +} diff --git a/src/de/cwde/freeshisen/ShisenShoView.java b/src/de/cwde/freeshisen/ShisenShoView.java new file mode 100644 index 0000000..a4280b7 --- /dev/null +++ b/src/de/cwde/freeshisen/ShisenShoView.java @@ -0,0 +1,682 @@ +package de.cwde.freeshisen; + +import java.util.List; +import java.util.Locale; +import java.util.Timer; +import java.util.TimerTask; + +import android.app.Activity; +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Matrix; +import android.graphics.Paint; +import android.graphics.Paint.Align; +import android.graphics.Paint.Cap; +import android.graphics.Paint.Join; +import android.graphics.Paint.Style; +import android.graphics.Rect; +import android.graphics.Typeface; +import android.os.Handler; +import android.os.Message; +import android.view.MenuItem; +import android.view.MotionEvent; +import android.view.SurfaceHolder; +import android.view.SurfaceView; + +class ShisenShoView extends SurfaceView implements SurfaceHolder.Callback { + + private enum StatePlay { UNINITIALIZED, IDLE, SELECTED1, SELECTED2, GAMEOVER }; + private enum StatePaint { BOARD, SELECTED1, SELECTED2, MATCHED, WIN, LOSE, HINT, TIME }; + + private int screenWidth; + private int screenHeight; + private int tilesetRows; + private int tilesetCols; + private int tileHeight; + private int tileWidth; + private Bitmap bg; + private Bitmap tile[]; + private int[] selection1=new int[2]; + private int[] selection2=new int[2]; + private List path=null; + private List pairs=null; + private long startTime; + private long playTime; + private long baseTime; + private Timer timer; + private static Handler timerHandler; + + private boolean timerRegistered=false; + private ShisenSho app; + private StatePlay cstate; + private StatePaint pstate; + private Canvas canvas = null; + private SurfaceHolder surfaceHolder = null; + public ShisenShoView(ShisenSho shishenSho) { + super((Context)shishenSho); + this.app = shishenSho; + cstate = StatePlay.UNINITIALIZED; + surfaceHolder = getHolder(); + surfaceHolder.addCallback(this); + } + + public ShisenShoView(Context ctx) { + super((Context)ctx); + // silence lint? + } + + private void paint(StatePaint pstate) { + this.pstate=pstate; + repaint(); + } + + private void control(StatePlay cstate) { + this.cstate=cstate; + } + + private void loadTileset() { + BitmapFactory.Options ops = new BitmapFactory.Options(); + ops.inScaled = false; + Bitmap tileset = BitmapFactory.decodeResource(getResources(), R.drawable.tileset, ops); + tileset.setDensity(Bitmap.DENSITY_NONE); + + // The tile set has 4 rows x 9 columns + tilesetRows = 4; + tilesetCols = 9; + int loadedtileWidth = tileset.getWidth()/tilesetCols; + int loadedtileHeight = tileset.getHeight()/tilesetRows; + tile = new Bitmap[tilesetRows*tilesetCols]; + + // align to screen: + // "large" is 16x6, and we want to have a nice border, so we use 17x7 and + // choose the lowest scale so everything fits + float scalex = ((float) screenWidth/17) / loadedtileWidth; + float scaley = ((float) screenHeight/7) / loadedtileHeight; + if (scaley < scalex) { + scalex = scaley; + } else { + scaley = scalex; + } + Matrix matrix = new Matrix(); + matrix.setScale(scalex, scaley); + + int k=0; + for (int i=0; i0) { + Line pair=pairs.get(0); + Point a=pair.a; + Point b=pair.b; + path=app.board.getPath(a,b); + paint.setColor(orange); + paint.setStyle(Style.STROKE); + paint.setStrokeCap(Cap.ROUND); + paint.setStrokeJoin(Join.ROUND); + paint.setStrokeWidth(3); + + cbuffer.drawRect(new Rect( + x0+a.j*tileWidth-2, + y0+a.i*tileHeight-2, + x0+a.j*tileWidth-2+tileWidth+2*2, + y0+a.i*tileHeight-2+tileHeight+2*2), + paint); + + if (path!=null) { + Point p0=null; + for (Point p1 : path) { + if (p0!=null) { + cbuffer.drawLine( + x0+p0.j*tileWidth-2+(tileWidth/2), + y0+p0.i*tileHeight-2+(tileHeight/2), + x0+p1.j*tileWidth-2+(tileWidth/2), + y0+p1.i*tileHeight-2+(tileHeight/2), + paint); + } + p0=p1; + } + path=null; + } + + cbuffer.drawRect(new Rect( + x0+b.j*tileWidth-2, + y0+b.i*tileHeight-2, + x0+b.j*tileWidth-2+tileWidth+2*2, + y0+b.i*tileHeight-2+tileHeight+2*2), + paint); + } + break; + } + + // Win & loose notifications + switch (pstate) { + case WIN: + drawMessage(cbuffer, screenWidth/2,screenHeight/2,true,"You Win!", "#FFFFFF", 100); + break; + case LOSE: + drawMessage(cbuffer, screenWidth/2,screenHeight/2,true,"Game Over", "#FFFFFF", 100); + break; + } + + if (app.timeCounter) switch (pstate) { + case BOARD: + case SELECTED1: + case SELECTED2: + case MATCHED: + case WIN: + case LOSE: + case HINT: + case TIME: + updateTime(); + int hours=(int)(playTime/(60*60)); + int minutes=(int)((playTime/60)%60); + int seconds=(int)(playTime%60); + String time=String.format(Locale.US, "%01d:%02d:%02d", hours, minutes, seconds); + + int timePosX=screenWidth-120; + int timePosY=screenHeight-10; + + drawMessage(cbuffer, timePosX+1,timePosY+1,false,time,"#000000",30); + drawMessage(cbuffer, timePosX,timePosY,false,time,"#FFFFFF",30); + break; + } + + // Debug messages + /* + debugMessage="StatePlay: "+cstate+"\n"+"StatePaint: "+pstate; + if (debugMessage!=null && debugMessage.length()>0) { + int l = 20; + String lines[] = debugMessage.split("\n"); + for (int i=0; i=0 && + i=0 && j=0 && i=0 && j0) { + app.board.play(a,b); + } + path=null; + paint(StatePaint.BOARD); + + pairs=app.board.getPairs(1); + if (pairs.size()==0) { + if (app.board.getNumPieces()==0) { + paint(StatePaint.WIN); + } else { + paint(StatePaint.LOSE); + } + control(StatePlay.GAMEOVER); + } else { + control(StatePlay.IDLE); + } + //undo.sensitive=app.board.getCanUndo(); + } + } + break; + case GAMEOVER: + reset(); + paint(StatePaint.BOARD); + break; + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void surfaceChanged(SurfaceHolder holder, int format, int width, + int height) { + surfaceHolder = holder; + if (cstate!=StatePlay.GAMEOVER && app.timeCounter && !timerRegistered) { + registerTimer(); + } + repaint(); + } + + public void surfaceCreated(SurfaceHolder holder) { + surfaceHolder = holder; + repaint(); + } + + public void surfaceDestroyed(SurfaceHolder holder) { + surfaceHolder = null; + if (timerRegistered) { + unregisterTimer(); + } + } + + /* + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + if (!initialized) initialize(); + + long currTime = System.currentTimeMillis(); + + a = (float)(currTime - startTime) / (float)duration; + if (a > (float)1.0) a = (float)1.0; + + x = Math.round(nextx*a + prevx*(1-a)); + y = Math.round(nexty*a + prevy*(1-a)); + + if (a == (float)1.0) computeNextTarget(); + + int bgWidth = bg.getWidth(); + int bgHeight = bg.getHeight(); + for (int i=0; i history; - - // ---------------------- - // Public methods - // ---------------------- - - public Board() { - } - - // The board always has a 1-square width free rectangle that has - // to be taken into account when specifying the size - public void initialize(int sizeI, int sizeJ) { - boardSize = new int[2]; - boardSize[0]=sizeI; - boardSize[1]=sizeJ; - board = new char[boardSize[0]][boardSize[1]]; - for (int i=0;i(); - } - - public static String pieceToString(char piece) { - return charpieces.substring(piece,1); - } - - public static char StringToPiece(String piece) { - char upiece; - long charpiecesLen=charpieces.length(); - for(upiece=0;(upiece0) result+=" "; - result+=""+(j%10); - } - result+="\n "+StringRepeat("--",boardSize[1]); - for (int i=0;i0) result+=" "; - result+=charpieces.substring(board[i][j],board[i][j]+1); - } - result+=" |\n"; - if (i getPairs(int maxResults) { - List result=new ArrayList(); - List pieces=new ArrayList(); - List> piecePoints=new ArrayList>(); - for (int i=0;i points0=new ArrayList(); - points0.add(p); - pieces.add(piece); - piecePoints.add(points0); - - key=pieces.indexOf(piece); - piecePoints.get(key); - } else { - List points1=piecePoints.get(key); - points1.add(p); - } - } - - int nresults=0; - for (List points : piecePoints) { - int n=(int)points.size(); - for (int i=0;i path=getPath(a.copy(),b.copy()); - if (path!=null && path.size()>0) { - result.add(new Line(a,b)); - if (nresults++==maxResults) break; - } - } - if (nresults==maxResults) break; - } - if (nresults==maxResults) break; - } - return result; - } - - public int getNumPieces() { - int result=0; - for (int j=0;j getHorizontalLines(Point excludeA, Point excludeB) { - List result=new ArrayList(); - for (int i=0;i getVerticalLines(Point excludeA, Point excludeB) { - List result=new ArrayList(); - for (int j=0;j0;i--) board[i][p.j]=board[i-1][p.j]; - } - - private void undoGravity(Point p) { - if (gravity) for (int i=0;i=getMin().j && p.j<=getMax().j) - || (p.j==a.j && p.j==b.j && p.i>=getMin().i && p.i<=getMax().i); - } - - public Point cuts(Line l) { - if (isHorizontal() && l.isVertical() - && getMin().j<=l.a.j && getMax().j>=l.a.j - && l.getMin().i<=a.i && l.getMax().i>=a.i ) { - return new Point(a.i,l.a.j); - } else if (isVertical() && l.isHorizontal() - && getMin().i<=l.a.i && getMax().i>=l.a.i - && l.getMin().j<=a.j && l.getMax().j>=a.j ) { - return new Point(l.a.i,a.j); - } else return null; - } - - public Point getMin() { - if (a.ib.i || a.j>b.j) return a; - else return b; - } - - public String toString() { - return a+"-"+b; - } - - public Point a; - public Point b; -} diff --git a/src/de/cwde/shisensho/Move.java b/src/de/cwde/shisensho/Move.java deleted file mode 100644 index 28bf071..0000000 --- a/src/de/cwde/shisensho/Move.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.cwde.shisensho; - -public class Move { - public Move(Point a, Point b, char piece) { - this.a=a; - this.b=b; - this.piece=piece; - } - - public String toString() { - return a+"-"+b+"("+Board.pieceToString(piece)+")"; - } - - public Point a; - public Point b; - public char piece; -} diff --git a/src/de/cwde/shisensho/Point.java b/src/de/cwde/shisensho/Point.java deleted file mode 100644 index 73879cd..0000000 --- a/src/de/cwde/shisensho/Point.java +++ /dev/null @@ -1,30 +0,0 @@ -package de.cwde.shisensho; - -class Point { - public Point(int i, int j) { - this.i=i; - this.j=j; - } - - public boolean equals(Point p) { - return (i==p.i && j==p.j); - } - - public String toString() { - return "("+i+","+j+")"; - } - - public static Point fromString(String s) { - String[] ij=s.split(",",2); - int i=Integer.parseInt(ij[0]); - int j=Integer.parseInt(ij[1]); - return new Point(i,j); - } - - public int i; - public int j; - - public Point copy() { - return new Point(this.i,this.j); - } -} diff --git a/src/de/cwde/shisensho/SettingsActivity.java b/src/de/cwde/shisensho/SettingsActivity.java deleted file mode 100644 index 5b75b70..0000000 --- a/src/de/cwde/shisensho/SettingsActivity.java +++ /dev/null @@ -1,74 +0,0 @@ -package de.cwde.shisensho; - -import android.content.SharedPreferences; -import android.content.SharedPreferences.OnSharedPreferenceChangeListener; -import android.content.res.Resources; -import android.os.Bundle; -import android.preference.*; - -public class SettingsActivity extends PreferenceActivity -implements OnSharedPreferenceChangeListener { - - private ShisenSho app; - - private static final String KEY_PREF_DIFF = "pref_diff"; - private static final String KEY_PREF_SIZE = "pref_size"; - //private static final String KEY_PREF_GRAV = "pref_grav"; - //private static final String KEY_PREF_TIME = "pref_time"; - - @SuppressWarnings("deprecation") - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - app = ShisenSho.app(); - addPreferencesFromResource(R.xml.preferences); - SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences(); - - sharedPreferences.registerOnSharedPreferenceChangeListener(this); - updateSummary(sharedPreferences, KEY_PREF_DIFF, KEY_PREF_DIFF, R.array.difficulties); - updateSummary(sharedPreferences, KEY_PREF_SIZE, KEY_PREF_SIZE, R.array.sizes); - } - - @Override - public void onBackPressed() { - app.setOptions(); - super.onBackPressed(); - } - - @SuppressWarnings("deprecation") - @Override - protected void onResume() { - super.onResume(); - getPreferenceScreen().getSharedPreferences() - .registerOnSharedPreferenceChangeListener(this); - } - - @SuppressWarnings("deprecation") - @Override - protected void onPause() { - super.onPause(); - getPreferenceScreen().getSharedPreferences() - .unregisterOnSharedPreferenceChangeListener(this); - } - - - public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - updateSummary(sharedPreferences, key, KEY_PREF_DIFF, R.array.difficulties); - updateSummary(sharedPreferences, key, KEY_PREF_SIZE, R.array.sizes); - } - - private void updateSummary(SharedPreferences sharedPreferences, String changedkey, String mykey, int myresource) { - if (changedkey.equals(mykey)) { - // FIXME: handle NumberFormatException here? - int i = Integer.parseInt(sharedPreferences.getString(changedkey, "1")); - - Resources res = getResources(); - String[] mystrings = res.getStringArray(myresource); - String name = mystrings[i-1]; - - @SuppressWarnings("deprecation") - Preference myPref = findPreference(changedkey); - myPref.setSummary("Currently: " + name); - } - } -} diff --git a/src/de/cwde/shisensho/ShisenSho.java b/src/de/cwde/shisensho/ShisenSho.java deleted file mode 100644 index 0ca4ede..0000000 --- a/src/de/cwde/shisensho/ShisenSho.java +++ /dev/null @@ -1,114 +0,0 @@ -package de.cwde.shisensho; - -import android.app.Application; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; -import android.util.Log; - -public class ShisenSho extends Application { - private static ShisenSho instance = null; - private ShisenShoView view = null; - public ShisenShoActivity activity = null; - - public Board board; - public int[] boardSize=new int[2]; - public int difficulty=1; // 1=Easy, 2=Hard - public int size=3; // 1=Small, 2=Medium, 3=Big - public boolean gravity=true; - public boolean timeCounter=true; - - public static void log(String msg) { - Log.w("ShisenSho", msg); - } - - public void newPlay() { - board = new Board(); - board.buildRandomBoard(boardSize[0],boardSize[1],difficulty,gravity); - } - - public void setSize(int s) { - switch (s) { - case 1: - size=1; - boardSize[0]=6+2; - boardSize[1]=8+2; - break; - case 2: - size=2; - boardSize[0]=6+2; - boardSize[1]=12+2; - break; - case 3: - default: - size=3; - boardSize[0]=6+2; - boardSize[1]=16+2; - break; - } - } - - public void sleep(int deciSeconds) { - try { - Thread.sleep(deciSeconds*100); - } catch (InterruptedException e) { } - } - - public ShisenSho() { - instance = this; - setSize(size); - } - - public static synchronized ShisenSho app() { - return instance; - } - - public ShisenShoView getView() { - if (view == null) view = new ShisenShoView(this); - return view; - } - - /** Called when the activity is first created. */ - @Override - public void onCreate() { - super.onCreate(); - PreferenceManager.setDefaultValues(this, R.xml.preferences, false); - setOptions(); - } - - public void setOptions() { - SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); - - // FIXME: handle NumberFormatException here? - int size = Integer.parseInt(sharedPref.getString("pref_size", "1")); - int difficulty = Integer.parseInt(sharedPref.getString("pref_diff", "1")); - boolean gravity = sharedPref.getBoolean("pref_grav", true); - boolean timeCounter = sharedPref.getBoolean("pref_time", true); - - boolean needsReset = false; - - if (size != this.size) { - setSize(size); - needsReset = true; - } - - if (difficulty != this.difficulty) { - this.difficulty = difficulty; - needsReset = true; - } - - if (gravity != this.gravity) { - this.gravity = gravity; - needsReset = true; - } - - if (timeCounter != this.timeCounter) { - this.timeCounter = timeCounter; - view.onTimeCounterActivate(); - } - - if (needsReset && (view != null)) { - view.reset(); - } - } - -} diff --git a/src/de/cwde/shisensho/ShisenShoActivity.java b/src/de/cwde/shisensho/ShisenShoActivity.java deleted file mode 100644 index 4375365..0000000 --- a/src/de/cwde/shisensho/ShisenShoActivity.java +++ /dev/null @@ -1,121 +0,0 @@ -package de.cwde.shisensho; - -import android.app.Activity; -import android.app.AlertDialog; -import android.content.Intent; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.content.pm.PackageManager.NameNotFoundException; -import android.os.Bundle; -import android.preference.PreferenceManager; -import android.text.SpannableString; -import android.text.util.Linkify; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.ViewGroup; -import android.view.Window; -import android.widget.TextView; - -public class ShisenShoActivity extends Activity { - private ShisenShoView view; - - /** Called when the activity is first created. */ - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - PreferenceManager.setDefaultValues(this, R.xml.preferences, false); - - requestWindowFeature(Window.FEATURE_NO_TITLE); - //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - // WindowManager.LayoutParams.FLAG_FULLSCREEN); - - view = ShisenSho.app().getView(); - ShisenSho.app().activity = this; - setContentView(view); - } - - @Override - protected void onDestroy() { - ViewGroup vg = (ViewGroup)(view.getParent()); - vg.removeView(view); - ShisenSho.app().activity = null; - super.onDestroy(); - } - - @Override - protected void onPause() { - if (view!=null) { - view.pauseTime(); - } - super.onPause(); - } - - @Override - protected void onResume() { - super.onResume(); - if (view!=null) { - view.resumeTime(); - } - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - MenuInflater inflater = getMenuInflater(); - inflater.inflate(R.menu.menu, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle item selection - switch (item.getItemId()) { - case R.id.hint: - case R.id.undo: - case R.id.clean: - return view.onOptionsItemSelected(item); - case R.id.options: - startActivityForResult(new Intent("de.cwde.shisensho.SETTINGS", null), 0); - return true; - case R.id.about: - onAboutActivate(); - return true; - default: - return super.onOptionsItemSelected(item); - } - } - - private void onAboutActivate() { - // Try to load the a package matching the name of our own package - PackageInfo pInfo; - try { - pInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA); - String aboutTitle = String.format("About %s", getString(R.string.app_name)); - String versionString = String.format("Version: %s", pInfo.versionName); - String aboutText = getString(R.string.aboutText); - - // Set up the TextView - final TextView message = new TextView(this); - // We'll use a spannablestring to be able to make links clickable - final SpannableString s = new SpannableString(aboutText); - - // Set some padding - message.setPadding(5, 5, 5, 5); - // Set up the final string - message.setText(versionString + "\n" + s); - // Now linkify the text - Linkify.addLinks(message, Linkify.ALL); - - new AlertDialog.Builder(this) - .setTitle(aboutTitle) - .setCancelable(true) - .setIcon(R.drawable.icon) - .setPositiveButton(getString(android.R.string.ok), null) - .setView(message).create() - .show(); - } catch (NameNotFoundException e) { - e.printStackTrace(); - } - } -} diff --git a/src/de/cwde/shisensho/ShisenShoView.java b/src/de/cwde/shisensho/ShisenShoView.java deleted file mode 100644 index 06162c1..0000000 --- a/src/de/cwde/shisensho/ShisenShoView.java +++ /dev/null @@ -1,682 +0,0 @@ -package de.cwde.shisensho; - -import java.util.List; -import java.util.Locale; -import java.util.Timer; -import java.util.TimerTask; - -import android.app.Activity; -import android.content.Context; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Matrix; -import android.graphics.Paint; -import android.graphics.Paint.Align; -import android.graphics.Paint.Cap; -import android.graphics.Paint.Join; -import android.graphics.Paint.Style; -import android.graphics.Rect; -import android.graphics.Typeface; -import android.os.Handler; -import android.os.Message; -import android.view.MenuItem; -import android.view.MotionEvent; -import android.view.SurfaceHolder; -import android.view.SurfaceView; - -class ShisenShoView extends SurfaceView implements SurfaceHolder.Callback { - - private enum StatePlay { UNINITIALIZED, IDLE, SELECTED1, SELECTED2, GAMEOVER }; - private enum StatePaint { BOARD, SELECTED1, SELECTED2, MATCHED, WIN, LOSE, HINT, TIME }; - - private int screenWidth; - private int screenHeight; - private int tilesetRows; - private int tilesetCols; - private int tileHeight; - private int tileWidth; - private Bitmap bg; - private Bitmap tile[]; - private int[] selection1=new int[2]; - private int[] selection2=new int[2]; - private List path=null; - private List pairs=null; - private long startTime; - private long playTime; - private long baseTime; - private Timer timer; - private static Handler timerHandler; - - private boolean timerRegistered=false; - private ShisenSho app; - private StatePlay cstate; - private StatePaint pstate; - private Canvas canvas = null; - private SurfaceHolder surfaceHolder = null; - public ShisenShoView(ShisenSho shishenSho) { - super((Context)shishenSho); - this.app = shishenSho; - cstate = StatePlay.UNINITIALIZED; - surfaceHolder = getHolder(); - surfaceHolder.addCallback(this); - } - - public ShisenShoView(Context ctx) { - super((Context)ctx); - // silence lint? - } - - private void paint(StatePaint pstate) { - this.pstate=pstate; - repaint(); - } - - private void control(StatePlay cstate) { - this.cstate=cstate; - } - - private void loadTileset() { - BitmapFactory.Options ops = new BitmapFactory.Options(); - ops.inScaled = false; - Bitmap tileset = BitmapFactory.decodeResource(getResources(), R.drawable.tileset, ops); - tileset.setDensity(Bitmap.DENSITY_NONE); - - // The tile set has 4 rows x 9 columns - tilesetRows = 4; - tilesetCols = 9; - int loadedtileWidth = tileset.getWidth()/tilesetCols; - int loadedtileHeight = tileset.getHeight()/tilesetRows; - tile = new Bitmap[tilesetRows*tilesetCols]; - - // align to screen: - // "large" is 16x6, and we want to have a nice border, so we use 17x7 and - // choose the lowest scale so everything fits - float scalex = ((float) screenWidth/17) / loadedtileWidth; - float scaley = ((float) screenHeight/7) / loadedtileHeight; - if (scaley < scalex) { - scalex = scaley; - } else { - scaley = scalex; - } - Matrix matrix = new Matrix(); - matrix.setScale(scalex, scaley); - - int k=0; - for (int i=0; i0) { - Line pair=pairs.get(0); - Point a=pair.a; - Point b=pair.b; - path=app.board.getPath(a,b); - paint.setColor(orange); - paint.setStyle(Style.STROKE); - paint.setStrokeCap(Cap.ROUND); - paint.setStrokeJoin(Join.ROUND); - paint.setStrokeWidth(3); - - cbuffer.drawRect(new Rect( - x0+a.j*tileWidth-2, - y0+a.i*tileHeight-2, - x0+a.j*tileWidth-2+tileWidth+2*2, - y0+a.i*tileHeight-2+tileHeight+2*2), - paint); - - if (path!=null) { - Point p0=null; - for (Point p1 : path) { - if (p0!=null) { - cbuffer.drawLine( - x0+p0.j*tileWidth-2+(tileWidth/2), - y0+p0.i*tileHeight-2+(tileHeight/2), - x0+p1.j*tileWidth-2+(tileWidth/2), - y0+p1.i*tileHeight-2+(tileHeight/2), - paint); - } - p0=p1; - } - path=null; - } - - cbuffer.drawRect(new Rect( - x0+b.j*tileWidth-2, - y0+b.i*tileHeight-2, - x0+b.j*tileWidth-2+tileWidth+2*2, - y0+b.i*tileHeight-2+tileHeight+2*2), - paint); - } - break; - } - - // Win & loose notifications - switch (pstate) { - case WIN: - drawMessage(cbuffer, screenWidth/2,screenHeight/2,true,"You Win!", "#FFFFFF", 100); - break; - case LOSE: - drawMessage(cbuffer, screenWidth/2,screenHeight/2,true,"Game Over", "#FFFFFF", 100); - break; - } - - if (app.timeCounter) switch (pstate) { - case BOARD: - case SELECTED1: - case SELECTED2: - case MATCHED: - case WIN: - case LOSE: - case HINT: - case TIME: - updateTime(); - int hours=(int)(playTime/(60*60)); - int minutes=(int)((playTime/60)%60); - int seconds=(int)(playTime%60); - String time=String.format(Locale.US, "%01d:%02d:%02d", hours, minutes, seconds); - - int timePosX=screenWidth-120; - int timePosY=screenHeight-10; - - drawMessage(cbuffer, timePosX+1,timePosY+1,false,time,"#000000",30); - drawMessage(cbuffer, timePosX,timePosY,false,time,"#FFFFFF",30); - break; - } - - // Debug messages - /* - debugMessage="StatePlay: "+cstate+"\n"+"StatePaint: "+pstate; - if (debugMessage!=null && debugMessage.length()>0) { - int l = 20; - String lines[] = debugMessage.split("\n"); - for (int i=0; i=0 && - i=0 && j=0 && i=0 && j0) { - app.board.play(a,b); - } - path=null; - paint(StatePaint.BOARD); - - pairs=app.board.getPairs(1); - if (pairs.size()==0) { - if (app.board.getNumPieces()==0) { - paint(StatePaint.WIN); - } else { - paint(StatePaint.LOSE); - } - control(StatePlay.GAMEOVER); - } else { - control(StatePlay.IDLE); - } - //undo.sensitive=app.board.getCanUndo(); - } - } - break; - case GAMEOVER: - reset(); - paint(StatePaint.BOARD); - break; - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - public void surfaceChanged(SurfaceHolder holder, int format, int width, - int height) { - surfaceHolder = holder; - if (cstate!=StatePlay.GAMEOVER && app.timeCounter && !timerRegistered) { - registerTimer(); - } - repaint(); - } - - public void surfaceCreated(SurfaceHolder holder) { - surfaceHolder = holder; - repaint(); - } - - public void surfaceDestroyed(SurfaceHolder holder) { - surfaceHolder = null; - if (timerRegistered) { - unregisterTimer(); - } - } - - /* - @Override - protected void onDraw(Canvas canvas) { - super.onDraw(canvas); - - if (!initialized) initialize(); - - long currTime = System.currentTimeMillis(); - - a = (float)(currTime - startTime) / (float)duration; - if (a > (float)1.0) a = (float)1.0; - - x = Math.round(nextx*a + prevx*(1-a)); - y = Math.round(nexty*a + prevy*(1-a)); - - if (a == (float)1.0) computeNextTarget(); - - int bgWidth = bg.getWidth(); - int bgHeight = bg.getHeight(); - for (int i=0; i