Advertisement
Conderoga

Untitled

Jan 16th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.50 KB | None | 0 0
  1. package com.DJR.MazeMaker;
  2.  
  3.  
  4. import android.graphics.Point;
  5.  
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.Iterator;
  9. import java.util.LinkedList;
  10.  
  11. import android.app.Activity;
  12. import android.app.AlertDialog;
  13. import android.content.DialogInterface;
  14. import android.content.pm.ActivityInfo;
  15. import android.graphics.Bitmap;
  16. import android.graphics.Bitmap.Config;
  17. import android.graphics.Canvas;
  18. import android.graphics.Color;
  19. import android.graphics.Paint;
  20. import android.graphics.Rect;
  21. import android.graphics.RectF;
  22. import android.hardware.Sensor;
  23. import android.hardware.SensorEvent;
  24. import android.hardware.SensorEventListener;
  25. import android.hardware.SensorManager;
  26. import android.os.Bundle;
  27. import android.util.Log;
  28. import android.view.MotionEvent;
  29. import android.view.View;
  30. import android.view.View.OnClickListener;
  31. import android.view.View.OnTouchListener;
  32.  
  33. public class MazeMaker extends Activity implements OnTouchListener
  34. {
  35.  
  36. //actual Maze object
  37. private Maze maze;
  38. //holds maze, so only have to calculate drawing of image once, and can then redraw maze from image
  39. private Bitmap mazeBitmap;
  40. private Canvas mazeCanvas;
  41. private MazeView mazeView;
  42. //the length of each side of a cell (in pixels)
  43. private float horizWallLength, vertWallLength;
  44. //controls animation
  45. private ThreadClass animation_;
  46. //in cells, not pixels!!
  47. private int mazeWidth, mazeHeight;
  48. //start is 0, most recent point is the last
  49. private ArrayList<Point> path;
  50. //stores points for the fancy path
  51. private Point pathPoint;
  52. private float thickness = -1;
  53. //volatile b/c modified in UI thread but used in main thread, want to make sure value is consistent
  54. private volatile int fingerX, fingerY;
  55. private volatile boolean fingerOn;
  56. private int startX, startY, endX,endY,minLength;
  57.  
  58.  
  59. @Override
  60. public void onCreate(Bundle savedInstanceState) {
  61. super.onCreate(savedInstanceState);
  62. setContentView(R.layout.main);
  63. this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  64. }
  65.  
  66. @Override
  67. public void onStart()
  68. {
  69. super.onStart();
  70. AlertDialog.Builder dialog = new AlertDialog.Builder(this);
  71. dialog.setMessage("Welcome to Maze Maker, where YOU can solve randomly generated mazes!");
  72. dialog.setPositiveButton("OK", new DialogInterface.OnClickListener()
  73. {
  74. @Override
  75. public void onClick(DialogInterface dialog, int which)
  76. {
  77. dialog.dismiss();
  78. startGame();
  79. }
  80. });
  81. dialog.show();
  82. mazeView = (MazeView)findViewById(R.id.maze_view);
  83. if(mazeView == null){
  84. Log.e("Maze View","Could not find");
  85. }
  86. else{
  87. mazeView.setOnTouchListener(this);
  88. Log.d("Screen View Size",mazeView.getWidth()+" x "+mazeView.getHeight());
  89. }
  90.  
  91. }
  92.  
  93. /**
  94. * Draws the maze image to the canvas and the current path over the canvas
  95. * @param canvas:the canvas to draw on
  96. */
  97. protected void drawGameContents(Canvas canvas)
  98. {
  99. if(mazeBitmap == null){
  100. resetMaze();
  101. }
  102. Paint p = new Paint();
  103. Bitmap tempBitmap = Bitmap.createBitmap(mazeView.getWidth(),mazeView.getHeight(),Bitmap.Config.ARGB_8888);
  104. Canvas tempCanvas = new Canvas(tempBitmap);
  105. tempCanvas.drawBitmap(mazeBitmap,new Rect(0,0,mazeBitmap.getWidth(),mazeBitmap.getHeight()), new RectF(0,0,mazeView.getWidth(),mazeView.getHeight()),p);
  106. //draw path and finger
  107. int drawColor = Color.RED;
  108. p.setColor(drawColor);
  109. float draw1X, draw2X, draw1Y, draw2Y;
  110.  
  111. thickness = Math.min(horizWallLength,vertWallLength)/3;
  112. p.setStrokeWidth(thickness);
  113. p.setColor(Color.RED);
  114. /*if(pathPoints!=null)
  115. for(int i = 0; i < pathPoints.size()-1; i++){
  116. tempCanvas.drawLine(pathPoints.get(i).x,pathPoints.get(i).y,
  117. pathPoints.get(i+1).x,pathPoints.get(i+1).y,p);
  118. tempCanvas.drawOval(new RectF(pathPoints.get(i).x-(thickness/2), pathPoints.get(i).y-(thickness/2),
  119. pathPoints.get(i).x+(thickness/2), pathPoints.get(i).y+(thickness/2)), p);
  120. if(i == pathPoints.size()-1){
  121. tempCanvas.drawOval(new RectF(pathPoints.get(i+1).x-(thickness/2), pathPoints.get(i+1).y-(thickness/2),
  122. pathPoints.get(i+1).x+(thickness/2), pathPoints.get(i+1).y+(thickness/2)), p);
  123. }
  124. }*/
  125.  
  126. if(path!=null && path.size()>1){
  127. //Log.e("Drawing path",""+path.size());
  128. //starts from end b/c used to try to do color gradient as path got older, but too much work/didn't work
  129. drawColor = Color.RED;
  130. p.setColor(drawColor);
  131. int numToAdd = 1;
  132. if(path.size()>=2)
  133. numToAdd = getNumToAdd();
  134. Log.d("Angle", ""+numToAdd);
  135. int i = path.size()-numToAdd;
  136. if(i>=0){
  137. Point cur = path.get(i);
  138. draw2X = (cur.x +0.5f)*horizWallLength;
  139. draw2Y = (cur.y +0.5f)*vertWallLength;
  140. thickness = Math.min(horizWallLength,vertWallLength)/3;
  141. p.setStrokeWidth(thickness);
  142. tempCanvas.drawLine(draw2X,draw2Y,pathPoint.x,pathPoint.y,p); //draws to current position of finger
  143. tempCanvas.drawOval(new RectF(pathPoint.x-(thickness/2), pathPoint.y-(thickness/2), //circle on joints in path
  144. pathPoint.x+(thickness/2),pathPoint.y+(thickness/2)), p);
  145. i--;
  146. while(i>=0){
  147. cur = path.get(i);
  148. draw1X = draw2X;
  149. draw1Y = draw2Y;
  150. draw2X = (cur.x +0.5f)*horizWallLength;
  151. draw2Y = (cur.y +0.5f)*vertWallLength;
  152. //drawColor -= 0x080000; Not entirely sure what this was trying to accomplish...
  153. // if(drawColor<0x800000){
  154. // drawColor = 0x800000;//after a while, all same color
  155. // }
  156. p.setColor(drawColor);
  157. //tempCanvas.drawRect(draw1X-.2f*horizWallLength,draw1Y-.2f*vertWallLength,draw2X+.2f*horizWallLength,draw2Y+.2f*vertWallLength, p);
  158. tempCanvas.drawOval(new RectF(draw1X-(thickness/2), draw1Y-(thickness/2), //circle on joints in path
  159. draw1X+(thickness/2),draw1Y+(thickness/2)), p);
  160. tempCanvas.drawLine(draw1X,draw1Y,draw2X,draw2Y,p);
  161. i--;
  162. }
  163. }
  164.  
  165. }
  166. /*p.setStrokeWidth(6);
  167. if(fingerOn && path!=null && path.size()>0){
  168. //Log.e("Drawing","Finger");
  169. Point last = path.get(path.size()-1);
  170. draw1X = (last.x +0.5f)*horizWallLength;
  171. draw1Y = (last.y +0.5f)*vertWallLength;
  172. draw2X = (fingerX +0.5f)*horizWallLength;
  173. draw2Y = (fingerY +0.5f)*vertWallLength;
  174. p.setColor(0x00FF00);
  175. tempCanvas.drawLine(draw1X,draw1Y,draw2X,draw2Y,p);
  176. }*/
  177. if(fingerOn){
  178. p.setColor(Color.YELLOW);
  179. //tempCanvas.drawCircle(fingerX,fingerY,Math.min(horizWallLength,vertWallLength)/2,p);
  180. //tempCanvas.drawCircle(path.getFirst().x, path.getFirst().y, Math.min(horizWallLength,vertWallLength)/3, p);
  181. }
  182. //copy drawing from temp bitmap over to final bitmap
  183. p.setColor(Color.WHITE);//not sure why this prevents whole screen from being black, but it does...
  184. canvas.drawBitmap(tempBitmap,new Rect(0,0,mazeView.getWidth(),mazeView.getHeight()), new RectF(0,0,mazeView.getWidth(),mazeView.getHeight()),p);//allows scaling of final thing to fit screen while having to do no scaling calculations throughout logic
  185. }
  186.  
  187. private void resetMaze(){
  188. int r, c;
  189. //prompt user for menu (probably small, medium, large, maybe XL choice from a list) TODO - use AlertDialog
  190. r = 5;
  191. c = 10;
  192. maze = new Maze(r,c);
  193. maze.generateMaze();
  194. Log.e("copy",maze.printMaze());
  195. horizWallLength = (1.0f*mazeView.getWidth())/c;
  196. vertWallLength = (1.0f*mazeView.getHeight())/r;
  197. if(path!=null){
  198. path.clear();
  199. }
  200. else{
  201. path = new ArrayList<Point>();
  202. }
  203. mazeBitmap = Bitmap.createBitmap(mazeView.getWidth(),mazeView.getHeight(),Config.ARGB_8888);
  204. mazeCanvas = new Canvas(mazeBitmap);
  205. Log.e("Maze Bitmap created: ",mazeCanvas.getWidth()+"x"+mazeCanvas.getHeight());
  206. Paint p = new Paint();
  207. p.setStrokeWidth(3);
  208. mazeCanvas.drawColor(Color.WHITE);
  209.  
  210. int[] endsStuff = maze.getFurthestEnds();
  211. //gets furthest start and end points, as well as the distance between them
  212. startX = endsStuff[0];
  213. startY = endsStuff[1];
  214. endX = endsStuff[2];
  215. endY = endsStuff[3];
  216. minLength = endsStuff[4];
  217.  
  218. p.setColor(Color.GREEN);
  219. mazeCanvas.drawRect(startX*horizWallLength, startY*vertWallLength, (startX+1)*horizWallLength, (startY+1)*vertWallLength, p);
  220. p.setColor(Color.CYAN);
  221. mazeCanvas.drawRect(endX*horizWallLength, endY*vertWallLength, (endX+1)*horizWallLength, (endY+1)*vertWallLength, p);
  222. p.setColor(Color.BLACK);
  223. mazeCanvas.drawLine(0,0,mazeCanvas.getWidth(),0,p);
  224. mazeCanvas.drawLine(0,0,0,mazeCanvas.getHeight(),p);
  225. //top right corner of square being drawn
  226. float drawX, drawY;
  227. drawY = 0.0f;
  228. for(int r2 = 0; r2<r;r2++){
  229. drawX = 0;
  230. for(int c2 = 0; c2<c;c2++){
  231. if(maze.hasSouthWall(r2, c2)){
  232. mazeCanvas.drawLine(drawX,drawY+vertWallLength,drawX+horizWallLength,drawY+vertWallLength,p);
  233. //Log.e("("+r2+","+c2+")","has south wall, drawing from ("+(drawX/horizWallLength)+","+((drawY+vertWallLength)/vertWallLength)+") to ("+((drawX+horizWallLength)/horizWallLength)+","+((drawY+vertWallLength)/vertWallLength)+")");
  234. }
  235. if(maze.hasEastWall(r2,c2)){
  236. mazeCanvas.drawLine(drawX+horizWallLength, drawY, drawX+horizWallLength, drawY+vertWallLength, p);
  237. //Log.e("("+r2+","+c2+")","has east wall, drawing from ("+((drawX+horizWallLength)/horizWallLength)+","+(drawY/vertWallLength)+") to ("+((drawX+horizWallLength)/horizWallLength)+","+((drawY+vertWallLength)/vertWallLength)+")");
  238. }
  239. /*p.setColor(Color.RED);
  240. mazeCanvas.drawCircle(drawX,drawY,12,p);
  241. p.setColor(Color.BLACK);*/
  242. drawX+=horizWallLength;
  243. }
  244. drawY+=vertWallLength;
  245. }
  246. /*//make up bs path for testing
  247. path = new LinkedList<Point>();
  248. path.addFirst(new Point(0,0));
  249. path.addFirst(new Point(0,1));
  250. path.addFirst(new Point(1,1));
  251. path.addFirst(new Point(2,1));
  252. for(int i = 0; i<walls.length;i++){
  253. Log.e("Walls",Arrays.toString(walls[i]));
  254. }*/
  255. //need to do the wall checking testing
  256. //Log.e("Clear between (0,0) and (1,1)",""+noWallsBetween(new Point(0,0), new Point(1,1)));
  257. //Log.e("Clear between (0,0) and (1,0)",""+noWallsBetween(new Point(0,0), new Point(1,0)));
  258. //Log.e("Clear between (0,0) and (0,1)",""+noWallsBetween(new Point(0,0), new Point(0,1)));
  259. //Log.e("Clear between (1,1) and (4,1)",""+noWallsBetween(new Point(1,1), new Point(4,1)));
  260.  
  261. }
  262.  
  263. private int getNumToAdd(){
  264. Point p1 = path.get(path.size()-2);
  265. Point p2 = path.get(path.size()-1);
  266. if(pathPoint==null)
  267. return 2;
  268. float p1x = (p1.x+.5f)*horizWallLength;
  269. float p1y = (p1.y+.5f)*vertWallLength;
  270. float p2x = (p2.x+.5f)*horizWallLength;
  271. float p2y = (p2.y+.5f)*vertWallLength;
  272.  
  273. float len = Math.abs(p2y-p1y)+Math.abs(p2x-p1x);
  274. float mag = len*(float)Math.sqrt(Math.pow(pathPoint.x-p2x,2)+Math.pow(pathPoint.y-p2y,2));
  275. float dot = (p1x-p2x)*(pathPoint.x-p2x) + (p1y-p2y)*(pathPoint.y - p2y);
  276.  
  277. float degree = (float)Math.acos(dot/mag);
  278. Log.d("Path Angle","Deg: "+degree);
  279. if(degree<=Math.PI/2.0f){
  280. return 2;
  281. }
  282. return 1;
  283. }
  284. private void handlePathPoint(int x, int y){
  285. Point p = new Point(x,y);
  286. if(checkPathPoint(p)){
  287. pathPoint = p;
  288. }
  289. }
  290. private boolean checkPathPoint(Point p){
  291. if(thickness == -1)
  292. return false;
  293. float remX = p.x%horizWallLength;
  294. float remY = p.y % vertWallLength;
  295. return remX>thickness/2 && remX < horizWallLength-(thickness/2) &&
  296. remY>thickness/2 && remY < vertWallLength-(thickness/2);
  297. }
  298. private boolean pointsEqual(Point p1, Point p2){
  299. return p1.x==p2.x && p1.y == p2.y;
  300. }
  301. /**
  302. * Starts the animation thread, which starts the game
  303. */
  304. public void startGame()
  305. {
  306. if(animation_==null)
  307. {
  308. animation_ = new ThreadClass(this);
  309. }
  310. animation_.start();
  311. if(mazeView == null){
  312. Log.e("Maze View","Retrying to set maze view");
  313. }
  314. if(mazeView == null){
  315. Log.e("Maze View","Retry failed");
  316. }
  317. else{
  318. Log.e("Maze View","Retry succesful");
  319. }
  320.  
  321. }
  322. /**
  323. * Stops the animation thread, which stops the game
  324. */
  325. public void stopGame()
  326. {
  327. if(animation_!=null)
  328. {
  329. animation_.stop();
  330. }
  331. animation_ = null;
  332. }
  333.  
  334. /*private void reset()
  335. {
  336. if(data_!=null)
  337. {
  338. data_.reset();
  339. }
  340. else
  341. {
  342. data_ = new GameData(this);
  343. }
  344. }*/
  345.  
  346.  
  347. @Override
  348. protected void onResume()
  349. {
  350. super.onResume();
  351. if(animation_!=null)
  352. {
  353. animation_.start();
  354. }
  355. }
  356.  
  357. @Override
  358. protected void onPause()
  359. {
  360. super.onPause();
  361. if(animation_!=null)
  362. {
  363. animation_.pause();
  364. }
  365. }
  366.  
  367. @Override
  368. protected void onStop()
  369. {
  370. super.onStop();
  371. stopGame();
  372. if(mazeView!=null)
  373. mazeView.setCanvas(null);
  374. mazeView = null;
  375. }
  376.  
  377. /**
  378. * Class that runs the animation thread
  379. */
  380. private class ThreadClass
  381. {
  382. private Thread thread_;
  383. private boolean running_;
  384. private MazeMaker parent_;
  385.  
  386. public ThreadClass(MazeMaker parent)
  387. {
  388. this.thread_ = null;
  389. this.running_ = false;
  390. parent_ = parent;
  391. }
  392.  
  393. public void start()
  394. {
  395. this.running_ = true;
  396.  
  397. if(this.thread_ == null)
  398. {
  399. this.thread_ = new Thread(new Runnable()
  400. {
  401. @Override
  402. public void run() {
  403. try {
  404. Thread.sleep(10);
  405. } catch (InterruptedException e1) {
  406. Log.e("Animation","Sleep at beginning failed");
  407. }
  408. while(running_)
  409. {
  410. updateScreen();
  411. try {
  412. Thread.sleep(20);//framerate of 50
  413. } catch (InterruptedException e) {
  414. e.printStackTrace();
  415. }//could add draw tracking to make sure sleeps exactly 20 seconds
  416. //and not just 20 seconds + the time it takes the frame to draw - TODO?
  417.  
  418. }
  419. }
  420. }, "Animation Thread");
  421. }
  422. try
  423. {
  424. this.thread_.start();
  425. }
  426. catch(Exception e) {}
  427. }
  428. private void updateScreen(){
  429. MazeView mv = parent_.mazeView;
  430. if(mv == null){
  431. //Log.e("Maze View","Maze View is still null, trying to reset before drawing");
  432. parent_.mazeView = (MazeView)parent_.findViewById(R.id.maze_view);
  433. if(parent_.mazeView == null){
  434. Log.e("Maze View","Maze View is eternally null, quitting this drawing cycle");
  435. return;
  436. }
  437. }
  438. try
  439. {
  440. mv.setCanvas( mv.getHolder().lockCanvas() );
  441. synchronized (mv.getHolder())//google said to >.>
  442. {
  443. parent_.drawGameContents(mv.getCanvas());
  444. }
  445. }
  446. catch (NullPointerException e)
  447. {
  448. Log.e("Drawing","Null Pointer when drawing: "+Arrays.toString(e.getStackTrace()));
  449. }
  450. catch(IndexOutOfBoundsException e){
  451. Log.e("Drawing","OBOE when drawing: "+Arrays.toString(e.getStackTrace()));
  452. }
  453. finally
  454. {
  455. if(mv.getCanvas()!=null)
  456. {
  457. mv.getHolder().unlockCanvasAndPost(mv.getCanvas());
  458. }
  459. }
  460. }
  461.  
  462. public void pause()
  463. {
  464. this.running_ = false;
  465. }
  466.  
  467. public void stop()
  468. {
  469. this.running_ = false;
  470. }
  471. }
  472.  
  473. @Override
  474. public boolean onTouch(View v, MotionEvent event) {
  475. //Log.e("Touch event",event.toString());
  476. int a = event.getAction();
  477. if(a==MotionEvent.ACTION_UP || a == MotionEvent.ACTION_CANCEL){
  478. if(path==null){
  479. path = new ArrayList<Point>();
  480. }
  481. else{
  482. path.clear();
  483. }
  484. pathPoint = new Point();
  485.  
  486. fingerOn = false;
  487. }
  488. else if(a==MotionEvent.ACTION_DOWN){
  489. if(path==null){
  490. path = new ArrayList<Point>();
  491. }
  492. else{
  493. path.clear();
  494. }
  495. pathPoint = new Point();
  496. fingerX = (int)event.getX();
  497. fingerY = (int)event.getY();
  498. handlePathPoint(fingerX,fingerY);
  499. Point temp = new Point((int)(fingerX/horizWallLength),(int)(fingerY/vertWallLength));
  500. if(temp.x == startX && temp.y == startY){
  501. Log.e("Path","Starting path at ("+temp.x+","+temp.y+")");
  502. path.add(temp);
  503. fingerOn = true;
  504. }
  505. }
  506. else if(a == MotionEvent.ACTION_MOVE){
  507. if(fingerOn){
  508. fingerX = (int)event.getX();
  509. fingerY = (int)event.getY();
  510. Point temp = new Point((int)(fingerX/horizWallLength),(int)(fingerY/vertWallLength));
  511. if(noWallsBetween(path.get(path.size()-1),temp)){
  512. handlePathPoint(fingerX,fingerY);
  513. if(!pointsEqual(path.get(path.size()-1),temp)){
  514. addAllBetween(path.get(path.size()-1),temp);
  515. Log.e("Path","Adding ("+temp.x+","+temp.y+")");
  516. path.add(temp);
  517. if(temp.x == endX && temp.y == endY){
  518. gameWon();
  519. }
  520. }
  521. }
  522. }
  523. else{
  524. Point temp = new Point((int)(fingerX/horizWallLength),(int)(fingerY/vertWallLength));
  525. if(temp.x == startX && temp.y == startY){
  526. handlePathPoint(fingerX,fingerY);
  527. path.add(temp);
  528. fingerOn = true;
  529. Log.e("Path","Starting path at ("+temp.x+","+temp.y+")");
  530. }
  531. }
  532. }
  533. return true;
  534. }
  535.  
  536. private boolean noWallsBetween(Point a, Point b){
  537. if(a.x!=b.x && a.y!=b.y){
  538. return false;
  539. }
  540. //return false in this special case because a = b, so no need to add more points to path
  541. /* I needed this for something with the fancy path
  542. else if(a.x == b.x && a.y == b.y){
  543. return false;
  544. }*/
  545.  
  546. else if(a.x == b.x){
  547. int min = Math.min(a.y,b.y);
  548. int max = Math.max(a.y,b.y);
  549. for(int i = min;i<max;i++){
  550. if(maze.hasSouthWall(i,a.x)){
  551. Log.e("Wall between","Failed: South wall at ("+a.x+","+i+")");
  552. return false;
  553. }
  554. }
  555. return true;
  556. }
  557. //a.y == b.y
  558. else{
  559. int min = Math.min(a.x,b.x);
  560. int max = Math.max(a.x,b.x);
  561. for(int i = min;i<max;i++){
  562. if(maze.hasEastWall(a.y,i)){
  563. Log.e("Wall between","Failed: East wall at ("+i+","+a.y+")");
  564. return false;
  565. }
  566. }
  567. return true;
  568. }
  569. }
  570. /**
  571. * Precondition: assumes in line, no walls between a and b
  572. * @param a
  573. * @param b
  574. */
  575. private void addAllBetween(Point a, Point b){
  576. //starts at a+1 because a is already in path
  577. //stops right before b, because b will get added after
  578. if(a.x == b.x){
  579. if(a.y>b.y){
  580. for(int i = a.y-1;i>b.y;i--){
  581. path.add(new Point(a.x,i));
  582. }
  583. }
  584. else{
  585. for(int i = a.y+1;i<b.y;i++){
  586. path.add(new Point(a.x,i));
  587. }
  588. }
  589. }
  590. //a.y == b.y
  591. else{
  592. if(a.y!=b.y){
  593. Log.e("AddAllBetween","Sanity check error, method was called when points were not in line");
  594. return;
  595. }
  596. if(a.x>b.x){
  597. for(int i = a.x-1;i>b.x;i--){
  598. path.add(new Point(i,a.y));
  599. }
  600. }
  601. else{
  602. for(int i = a.x+1;i<b.x;i++){
  603. path.add(new Point(i,a.y));
  604. }
  605. }
  606. }
  607. }
  608.  
  609. /**
  610. * Tells the user that they have successfully completed the game,
  611. * offers them multiple options for what to do next
  612. */
  613. private void gameWon(){
  614. Log.e("Game","You won!");
  615. AlertDialog.Builder dialog = new AlertDialog.Builder(this);
  616. dialog.setCancelable(false);
  617. dialog.setPositiveButton("Try a new maze", new DialogInterface.OnClickListener()
  618. {
  619. public void onClick(DialogInterface dialog, int id)
  620. {
  621. //restart
  622. resetMaze();
  623. dialog.dismiss();
  624. }
  625. });
  626. dialog.setNegativeButton("Quit", new DialogInterface.OnClickListener()
  627. {
  628. public void onClick(DialogInterface dialog, int id)
  629. {
  630. finish();//quit
  631. }
  632. });
  633. String s = "Congratulations! You solved the maze!\n";
  634. if(minLength >= path.size()-1){//maybe an oboe?.. who knows
  635. //if(minLength == 0){for testing
  636. s+="You made it from start to finish with the shortest path possible!";
  637. dialog.setMessage(s+"\nDo you want to quit or play a new maze?");
  638. dialog.show();
  639. }
  640. else{
  641. s+="However, there was a shorter path from start to end that was ";
  642. if(path.size()-minLength-1 ==1){
  643. s+= "1 space shorter.";
  644. }
  645. else{
  646. s+=(path.size()-minLength-1)+" spaces shorter.";
  647. }
  648.  
  649. dialog.setCancelable(true);
  650. dialog.setNeutralButton("Retry", new DialogInterface.OnClickListener() {
  651. @Override
  652. public void onClick(DialogInterface dialog, int id)
  653. {
  654. fingerOn = false;
  655. }
  656. });
  657. dialog.setMessage(s+"\nDo you want to play again, quit, or play a new maze?");
  658. dialog.show();
  659. }
  660.  
  661. }
  662. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement