Back to project page donatello-y-raphael.
The source code is released under:
MIT License
If you think the Android project donatello-y-raphael listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.ATracePath; /*w w w .j a va 2s.c o m*/ import android.content.Context; import android.graphics.*; import android.os.Vibrator; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.Toast; import java.util.List; public class Board extends View { private Global global = Global.getInstance(); private Pack pack = global.mPacks.get(global.packID); private Challenge challenge = pack.getChallenge().get(global.challengeID); private Puzzle puzzle = challenge.getPuzzleList().get(global.puzzleID); private final int NUM_COLORS = puzzle.getSize(); private final int NUM_CELLS = puzzle.getSize(); private int m_cellWidth; private int m_cellHeight; private int currentColor = Color.TRANSPARENT; private boolean canDraw = true; private Coordinate firstPoint = null; private Vibrator vibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE); private final int BROWN = new Color().rgb(143, 88, 70); private final int BLUE = new Color().rgb(70, 88, 224); private final int RED = new Color().rgb(242, 53, 53); private final int PURPLE = new Color().rgb(124, 78, 145); private final int ORANGE = new Color().rgb(240, 162, 89); private final int LIGHTBLUE = new Color().rgb(134, 162, 207); private final int GREEN = new Color().rgb(106, 166, 23); private final int[] colors = new int[] {BROWN, BLUE, RED, PURPLE, LIGHTBLUE, GREEN}; private Rect m_rect = new Rect(); private Paint m_paintGrid = new Paint(); private Paint m_paintPath = new Paint(); private Paint paintPoints = new Paint(); private Path m_path = new Path(); private Cellpath[] cellPaths = new Cellpath[NUM_COLORS]; private Coordinate[] startingPoints = new Coordinate[NUM_CELLS * NUM_CELLS]; private int xToCol( int x ) { return (x - getPaddingLeft()) / m_cellWidth; } private int yToRow( int y ) { return (y - getPaddingTop()) / m_cellHeight; } private int colToX( int col ) { return col * m_cellWidth + getPaddingLeft() ; } private int rowToY( int row ) { return row * m_cellHeight + getPaddingTop() ; } public Board(Context context, AttributeSet attrs) { super(context, attrs); for(int i = 0; i < NUM_COLORS; i++) { cellPaths[i] = new Cellpath(); } for (int i = 0; i < (NUM_CELLS * NUM_CELLS); i++) { startingPoints[i] = null; } int tempColor = 0; for (int i = 0; i < puzzle.getCoordinate().size(); ++i) { int col = puzzle.getCoordinate().get(i).getCol(); int row = puzzle.getCoordinate().get(i).getRow(); int rowMajor1 = col * NUM_CELLS + row; startingPoints[rowMajor1] = new Coordinate(col, row, colors[tempColor]); tempColor = (i % 2 == 0 ? tempColor : tempColor+1); } m_paintGrid.setStyle( Paint.Style.STROKE ); m_paintGrid.setColor( Color.GRAY ); paintPoints.setStyle(Paint.Style.FILL); m_paintPath.setStyle(Paint.Style.STROKE); m_paintPath.setColor(Color.GREEN); m_paintPath.setStrokeWidth(32); m_paintPath.setStrokeCap(Paint.Cap.ROUND); m_paintPath.setStrokeJoin(Paint.Join.ROUND); m_paintPath.setAntiAlias(true); } @Override protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec ) { super.onMeasure( widthMeasureSpec, heightMeasureSpec ); int width = getMeasuredWidth() - getPaddingLeft() - getPaddingRight(); int height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom(); int size = Math.min(width, height); setMeasuredDimension( size + getPaddingLeft() + getPaddingRight(), size + getPaddingTop() + getPaddingBottom() ); } @Override protected void onSizeChanged( int xNew, int yNew, int xOld, int yOld ) { int sw = Math.max(1, (int) m_paintGrid.getStrokeWidth()); m_cellWidth = (xNew - getPaddingLeft() - getPaddingRight() - sw) / NUM_CELLS; m_cellHeight = (yNew - getPaddingTop() - getPaddingBottom() - sw) / NUM_CELLS; } @Override protected void onDraw( Canvas canvas ) { for (int i = 0; i < (NUM_CELLS * NUM_CELLS); i++) { if (startingPoints[i] != null) { int y = colToX(startingPoints[i].getCol()); int x = rowToY(startingPoints[i].getRow()); paintPoints.setColor(startingPoints[i].getColor()); canvas.drawCircle(x + (m_cellWidth / 2), y + (m_cellWidth / 2), m_cellHeight / 4, paintPoints); } } for (int r=0; r<NUM_CELLS; ++r ) { for (int c = 0; c<NUM_CELLS; ++c) { int x = colToX( c ); int y = rowToY( r ); m_rect.set(x, y, x + m_cellWidth, y + m_cellHeight); canvas.drawRect( m_rect, m_paintGrid ); } } for(int j = 0; j < NUM_COLORS; j++) { if ( !cellPaths[j].isEmpty() ) { List<Coordinate> colist = cellPaths[j].getCoordinates(); Coordinate co = colist.get(0); m_paintPath.setColor(co.getColor()); m_path.moveTo(colToX(co.getCol()) + m_cellWidth / 2, rowToY(co.getRow()) + m_cellHeight / 2); for ( int i=1; i<colist.size(); ++i ) { co = colist.get(i); m_path.lineTo(colToX(co.getCol()) + m_cellWidth / 2, rowToY(co.getRow()) + m_cellHeight / 2); } canvas.drawPath(m_path, m_paintPath); m_path.reset(); } } } private boolean areNeighbours( int c1, int r1, int c2, int r2 ) { return Math.abs(c1-c2) + Math.abs(r1-r2) == 1; } private int setPathColor(int rowMajor) { m_paintPath.setColor(startingPoints[rowMajor].getColor()); return m_paintPath.getColor(); } private int getTileColor(int rowMajor) { if (startingPoints[rowMajor] == null) return Color.TRANSPARENT; return startingPoints[rowMajor].getColor(); } private void validatePaths(Coordinate co) { for (int i = 0; i < NUM_COLORS; i++) { Cellpath path = cellPaths[i]; if (!path.isEmpty() && path.getColor() != co.getColor()) { List<Coordinate> coordinates = path.getCoordinates(); for (int j = 0; j < coordinates.size(); j++) { if (co.compareTo(coordinates.get(j))) { path.removeTail(j); break; } } } } } private void checkWinning() { int sum = 0; for (int i = 0; i < NUM_COLORS; i++) { sum += cellPaths[i].pathSize(); } if (sum == NUM_CELLS * NUM_CELLS) { if (this.global.vibrate) { vibrator.vibrate(2000); } Toast.makeText(this.getContext(), "COWABUNGA!", Toast.LENGTH_SHORT).show(); global.nextLevelBtn.setVisibility(VISIBLE); if (!global.levelFinished) { global.levelFinished = true; ProgressAdapter pa = new ProgressAdapter(getContext()); pa.insertBoard(global.puzzleID, global.packID, global.challengeID); } } } private int checkForPath(int c, int r) { Coordinate co = new Coordinate(c, r, currentColor); for (int i = 0; i < cellPaths.length; i++) { if (cellPaths[i].findCoordinate(co)) { return cellPaths[i].getColor(); } } return Color.TRANSPARENT; } @Override public boolean onTouchEvent( MotionEvent event ) { int x = (int) event.getX(); int y = (int) event.getY(); int c = xToCol(x); int r = yToRow(y); if (c >= NUM_CELLS || r >= NUM_CELLS) { return true; } if (event.getAction() == MotionEvent.ACTION_DOWN) { int rowMajor = r * NUM_CELLS + c; if (startingPoints[rowMajor] == null && checkForPath(c, r) == Color.TRANSPARENT) { return false; } if (startingPoints[rowMajor] != null) { currentColor = setPathColor(rowMajor); } firstPoint = startingPoints[rowMajor]; for (int i = 0; i < NUM_COLORS; i++) { Cellpath path = cellPaths[i]; //Only reset if pressing a starting point. Otherwise continuing path. if (!path.isEmpty() && path.getColor() == currentColor && startingPoints[rowMajor] != null) { path.reset(); invalidate(); } //Only append if pressing a starting point. Otherwise continuing path. if (path.isEmpty() && startingPoints[rowMajor] != null) { path.append(new Coordinate(c, r, currentColor)); break; } } } else if (event.getAction() == MotionEvent.ACTION_MOVE) { for (int j = 0; j < NUM_COLORS; j++) { if (!cellPaths[j].isEmpty() && cellPaths[j].getColor() == currentColor) { List<Coordinate> coordinateList = cellPaths[j].getCoordinates(); Coordinate last = coordinateList.get(coordinateList.size() - 1); if (areNeighbours(last.getCol(), last.getRow(), c, r)) { int rowMajor = r * NUM_CELLS + c; //Trying to reach a tile that isn't the right color if ((currentColor != getTileColor(rowMajor) && getTileColor(rowMajor) != Color.TRANSPARENT) || !canDraw) { return false; } //Path fully connected stop drawing if (currentColor == getTileColor(rowMajor) && firstPoint != startingPoints[rowMajor]) { canDraw = false; if(this.global.vibrate) { vibrator.vibrate(100); } } Coordinate temp = new Coordinate(c, r, currentColor); cellPaths[j].append(temp); validatePaths(temp); invalidate(); break; } } } } else if (event.getAction() == MotionEvent.ACTION_UP) { canDraw = true; } checkWinning(); return true; } }