Commits

Meitian Huang  committed ba468d8

Float is tricky...

Signed-off-by: Meitian Huang <kid85783388@gmail.com>

  • Participants
  • Parent commits d9114b6

Comments (0)

Files changed (8)

File src/Game/Cell.java

  */
 
 public class Cell {
-/* Here x and y represent the location of the cell on the board of which the
- * is (0, 0)
- */
+    /* Here x and y represent the location of the cell on the board of which the
+    * is (0, 0)
+    */
     private float x, y;
     private float sideLength;
 

File src/Game/DFS.java

 import java.util.Random;
 
 /**
- *  DFS - This class uses Depth-first search to generate a random maze.
+ * DFS - This class uses Depth-first search to generate a random maze.
  */
 
 public class DFS {
 
     private Cell[][] grid;
 
-/**
- *  I will use bit operations.
- *  Note:
- *  N(north) is    0001_2,
- *  E(east) is     0010_2,
- *  S(south) is    0100_2,
- *  W(west) is     1000_2.
- */
+    /**
+     * I will use bit operations.
+     * Note:
+     * N(north) is    0001_2,
+     * E(east) is     0010_2,
+     * S(south) is    0100_2,
+     * W(west) is     1000_2.
+     */
     final int N = 1;
     final int E = 2;
     final int S = 4;
         directions.add(N);
     }
 
-/**
- *  Calculates the change in x if we want to more in the specified direction.
- */
+    /**
+     * Calculates the change in x if we want to more in the specified direction.
+     */
     private int DX(int direction) {
         if (E == direction) {
             return 1;
         }
     }
 
-/**
- *  Calculates the change in y if we want to more in the specified direction.
- */
+    /**
+     * Calculates the change in y if we want to more in the specified direction.
+     */
     private int DY(int direction) {
         if (N == direction) {
             return -1;
         }
     }
 
-/**
- *  Calculates the direction opposite to the specified direction.
- */
+    /**
+     * Calculates the direction opposite to the specified direction.
+     */
     private int Opposite(int direction) {
         if (E == direction) {
             return W;
         }
     }
 
-/**
- *  Generates a random maze.
- *  It randomly picks a direction and tries to move in
- *  that direction.
- *  It will move if:
- *      1. it will not be out of range after one move.
- *      2. the cell to which it moves has not been visited.
- *  It will mark the current cell and the next cell as "visited".
- */
+    /**
+     * Generates a random maze.
+     * It randomly picks a direction and tries to move in
+     * that direction.
+     * It will move if:
+     * 1. it will not be out of range after one move.
+     * 2. the cell to which it moves has not been visited.
+     * It will mark the current cell and the next cell as "visited".
+     */
     void carvePassagesFrom(int cx, int cy) {
         Collections.shuffle(directions);
         for (Integer direction : directions) {
         }
     }
 
-/**
- *  Adds the locations of horizontal walls and vertical walls in a random maze
- *  to the ArrayList.
- */
+    /**
+     * Adds the locations of horizontal walls and vertical walls in a random maze
+     * to the ArrayList.
+     */
     public void fill(ArrayList<Vector3f> vWallLocations,
                      ArrayList<Vector3f> hWallLocations) {
         for (int i = 0; i < height; i++) {
                 if (currentCell.hasNorthWall()) {
                     hWallLocations.add(
                             new Vector3f(currentCell.getX(),
-                                        0.2f,
-                                        currentCell.getY() - unit));
+                                    0.2f,
+                                    currentCell.getY() - unit));
                 }
 
                 if (currentCell.hasEastWall()) {
                     vWallLocations.add(
                             new Vector3f(currentCell.getX() + unit,
-                                        0.2f,
-                                        currentCell.getY()));
+                                    0.2f,
+                                    currentCell.getY()));
                 }
 
                 if (currentCell.hasSouthWall()) {
                     hWallLocations.add(
                             new Vector3f(currentCell.getX(),
-                                        0.2f,
-                                        currentCell.getY() + unit));
+                                    0.2f,
+                                    currentCell.getY() + unit));
                 }
                 if (currentCell.hasWestWall()) {
                     vWallLocations.add(
                             new Vector3f(currentCell.getX() - unit,
-                                        0.2f,
-                                        currentCell.getY()));
+                                    0.2f,
+                                    currentCell.getY()));
                 }
             }
         }
     }
 
-/**
- *  Checks if there is any cell that has a direction of '0'.
- *  This is a workaround function.
- */
+    /**
+     * Checks if there is any cell that has a direction of '0'.
+     * This is a workaround function.
+     */
     public boolean containErrors() {
         for (int i = 0; i < height; i++) {
             for (int j = 0; j < width; j++) {
         return false;
     }
 
-/**
- *  Returns the x coordinate of a random cell on the board.
- */
+    /**
+     * Returns the x coordinate of a random cell on the board.
+     */
     public float randomX() {
         Random random = new Random();
         return grid[random.nextInt(height)][random.nextInt(width)].getX();
     }
 
-/**
- *  Returns the y coordinate of a random cell on the board.
- */
+    /**
+     * Returns the y coordinate of a random cell on the board.
+     */
     public float randomY() {
         Random random = new Random();
         return grid[random.nextInt(height)][random.nextInt(width)].getY();

File src/Game/EndPoint.java

 import com.jme3.math.Vector3f;
 
 /**
- *  EndPoint - This class represents the end of a game.
- *  An EndPoint is similar to a Hole but it has a different behaviour on
- *  collision detection.
+ * EndPoint - This class represents the end of a game.
+ * An EndPoint is similar to a Hole but it has a different behaviour on
+ * collision detection.
  */
 public class EndPoint extends Hole {
     public EndPoint(Vector3f centre, Maze maze) {

File src/Game/Maze.java

 import com.jme3.font.BitmapText;
 import com.jme3.math.Vector3f;
 import com.jme3.scene.Node;
-import com.jme3.system.AppSettings;
-import com.jme3.util.SkyFactory;
 
 import java.util.ArrayList;
 
-
 public class Maze extends SimpleApplication {
 
     BulletAppState bulletAppState;
     ArrayList<Hole> holes;
     EndPoint end;
     ArrayList<Wall> walls;
+    BitmapText bitmapText;
+    int timeLeft;
+    boolean isLost;
 
     @Override
     public void simpleInitApp() {
 
         initWorld();
         setupKeys();
-        updateScore();
+
     }
 
     private void initWorld() {
         initBall();
         initHoles();
         initEnd();
+        initText();
         finalise();
     }
 
     }
 
     private void finalise() {
-        if (!this.getRenderer().equals(AppSettings.LWJGL_OPENGL1)) {
-            rootNode.attachChild(SkyFactory.createSky(
-                    assetManager, "Textures/Sky/Bright/BrightSky.dds", false));
-        }
+//        if (!this.getRenderer().equals(AppSettings.LWJGL_OPENGL1)) {
+//            rootNode.attachChild(SkyFactory.createSky(
+//                    assetManager, "Textures/Sky/Bright/BrightSky.dds", false));
+//        }
         this.setDisplayStatView(false);
         this.setDisplayFps(false);
     }
         cam.lookAt(board.getPhysicsLocation(), Vector3f.UNIT_Y);
 
         if (end.collideWith(ball)) {
-            score.increaseScore();
-            updateScore();
+            score.increaseScore(timeLeft);
+            score.reduceScore((int) time / 10);
             pref.increaseLevel();
             reset();
+            time = 0;
         }
 
         for (Hole hole : holes) {
             if (hole.collideWith(ball)) {
                 score.reduceScore();
-                updateScore();
                 reset();
             }
         }
-
-        if (10 == (int) time) {
-            time = 0;
-            score.reduceScore();
-            updateScore();
+        timeLeft = (pref.level * 30 - (int) time);
+        updateText();
+    }
+
+    private void updateText() {
+        timeLeft = (pref.level * 20 - (int) time);
+        if (timeLeft >= 0) {
+            bitmapText.setText("Level:  "
+                    + pref.level
+                    + "\t"
+                    + "Time Left:    "
+                    + timeLeft
+                    + "\t"
+                    + score.toString());
+        } else {
+            isLost = true;
+            bitmapText.setText("You lose! Press 'R' to restart. ");
         }
     }
 
-    public void updateScore() {
+    public void initText() {
         guiNode.detachAllChildren();
         guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
-        BitmapText bitmapText;
         bitmapText = new BitmapText(guiFont, false);
         bitmapText.setSize(guiFont.getCharSet().getRenderedSize());
-        bitmapText.setText(score.toString());
+        updateText();
         bitmapText.setLocalTranslation(0, bitmapText.getLineHeight(), 0);
         guiNode.attachChild(bitmapText);
     }
         bulletAppState.getPhysicsSpace().remove(board);
         boardNode.removeFromParent();
 
+        time = 0;
+        isLost = false;
+
         initBoard();
         initWall();
         initHoles();
     }
 
     public void rotate(float x, float y, float z) {
-        boardNode.rotate(x, y, z);
+        if (!isLost) {
+            boardNode.rotate(x, y, z);
+        }
     }
 
     public void attachNodeToRoot(Node node) {

File src/Game/Pref.java

 import java.util.ArrayList;
 
 /**
- *  Pref - This class contains every information the game needs to run.
+ * Pref - This class contains every information the game needs to run.
  */
 public class Pref {
     protected int level;
     public float ballX, ballY, ballZ;
     public float ballMass;
 
+    private float epsilon = 0.001f;
+
     protected ArrayList<Vector3f> vWallLocations, hWallLocations;
     protected BoxCollisionShape vWallShape, hWallShape;
 
         init();
     }
 
-/**
- *  Creates a new settings for the game.
- */
+    /**
+     * Creates a new settings for the game.
+     */
     public void init() {
         generateWalls();
         boardWidth = dfs.getHeight() / dfs.getWidth();
             do {
                 vector3f = new Vector3f(dfs.randomX(), 0, dfs.randomY());
             } while (
-                    vector3f.equals(end)
-                            || vector3f.equals(new Vector3f(ballX, 0, ballZ))
-                            || holeCentres.contains(vector3f));
+//                    vector3f.equals(end)
+//                            || vector3f.equals(new Vector3f(ballX, 0, ballZ))
+                    ((Math.abs(ballX - vector3f.getX()) < epsilon)
+                            && (Math.abs(ballZ - vector3f.getZ()) < epsilon))
+                            || ((Math.abs(vector3f.getX() - end.getX()) < epsilon)
+                            || (Math.abs(vector3f.getZ() - end.getZ()) < epsilon))
+                            || holeCentres.contains(vector3f)
+                    );
 
             holeCentres.add(vector3f);
         }
     }
 
-/**
- *  Randomly sets the location of the ball.
- */
+    /**
+     * Randomly sets the location of the ball.
+     */
     public void randomiseBallLocation() {
         do {
             ballX = dfs.randomX();
             ballZ = dfs.randomY();
-        } while (ballX == end.getX() && ballZ == end.getZ());
-        ballY = ballRadius;
+        }
+        while ((Math.abs(ballX - end.getX()) < epsilon) && (Math.abs(ballZ - end.getZ()) < epsilon));
+        ballY = ballRadius * 1.1f;
     }
 
-/**
- *  Generates a random maze and stores the information of walls.
- */
+    /**
+     * Generates a random maze and stores the information of walls.
+     */
     public void generateWalls() {
         vWallLocations = new ArrayList<Vector3f>();
         hWallLocations = new ArrayList<Vector3f>();

File src/Game/RigidBody.java

 package Game;
 
 /**
- *  RigidBody - This class represents every possible rigid body in the game.
+ * RigidBody - This class represents every possible rigid body in the game.
  */
 public abstract class RigidBody {
     protected Maze maze;
         this.maze = maze;
     }
 
-/**
- *  A rigid body should be able to attach to the board.
- */
+    /**
+     * A rigid body should be able to attach to the board.
+     */
     public abstract void attach();
 }

File src/Game/Score.java

 package Game;
 
 /**
- *  Score - represents the score of the game.
+ * Score - represents the score of the game.
  */
 
 public class Score {
         this.maze = maze;
     }
 
-/**
- *  Reduces the score by 1 and terminates the game if the score is less than 0.
- */
+    /**
+     * Reduces the score by 1 and terminates the game if the score is less than 0.
+     */
     public void reduceScore() {
         if ((--score) < 0) {
             maze.lose();
         }
     }
 
-    public void increaseScore() {
-        score += 10;
+    public void reduceScore(int n) {
+        if ((score -= n) < 0) {
+            maze.lose();
+        }
+    }
+
+    public void increaseScore(int n) {
+        score += n;
     }
 
     @Override

File src/Game/Starter.java

 
 
         AppSettings settings = new AppSettings(true);
+        settings.setTitle("Maze");
 //        Uses whatever version of OpenGL to render.
         settings.setRenderer(AppSettings.LWJGL_OPENGL_ANY);
         settings.setSettingsDialogImage("Game/java.gif");
 
         Maze app = new Maze();
         app.setSettings(settings);
+        System.out.println(0.1 + 0.2 == 0.3);
         app.start();
     }
 }