From 72160f290ba5da3386382b897095a8580b2822cb Mon Sep 17 00:00:00 2001 From: Finiganis Date: Mon, 4 Apr 2016 14:53:29 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Am=C3=A9lioration/D=C3=A9placement=20de=20p?= =?UTF-8?q?lusieurs=20cases?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Modification de onTouchEvent dans GameView.java * Ajout de 2 méthodes undoPush et redoClear dans Model.java afin de push les pièces depuis onTouchEvent --- .../main/java/android/rushdroid/GameView.java | 47 +++++++++++++++++-- .../java/android/rushdroid/model/Model.java | 11 ++--- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/android/rushdroid/GameView.java b/app/src/main/java/android/rushdroid/GameView.java index bae642b..299c4b9 100644 --- a/app/src/main/java/android/rushdroid/GameView.java +++ b/app/src/main/java/android/rushdroid/GameView.java @@ -33,6 +33,7 @@ final public class GameView extends SurfaceView { private Integer current = null; private final Model m; private final Bitmap[] bitmaps = new Bitmap[4]; + private Piece down_p = null; private int down_x; private int down_y; @@ -162,10 +163,12 @@ private Position interpolation(int x, int y) { return new Position(x * this.game_width / this.surface_width, y * this.game_height / this.surface_height); } + /* // A lot of side effects. private boolean help_move(int prev, int now) { return prev <= now ? this.m.moveForward(this.current) : this.m.moveBackward(this.current); } + */ @Override public boolean onTouchEvent(@NonNull MotionEvent e) { @@ -173,14 +176,48 @@ public boolean onTouchEvent(@NonNull MotionEvent e) { int y = (int) e.getY(); switch (e.getAction()) { case MotionEvent.ACTION_DOWN: { - this.current = m.getIdByPos(interpolation(x, y)); - this.down_x = x; - this.down_y = y; + // System.out.println(p); + Position p = interpolation(x, y); + this.current = m.getIdByPos(p); + this.down_x = p.getCol(); + this.down_y = p.getLig(); + if (current != null) { + this.down_p = m.piece(current); + } + return true; + } + case MotionEvent.ACTION_MOVE: { + if (this.current != null) { + if (m.getOrientation(current) == Direction.VERTICAL) { + int test = interpolation(x, y).getLig(); + if (down_y < test) { + if (this.m.moveForward(this.current)) { down_y += 1; } + } else { + if (down_y > test) { + if (this.m.moveBackward(this.current)) { down_y -= 1; } + } + } + } else { + int test = interpolation(x, y).getCol(); + if (down_x < test) { + if (this.m.moveForward(this.current)) { down_x += 1; } + } else { + if (down_x > test) { + if (this.m.moveBackward(this.current)) { down_x -= 1; } + } + } + } + } return true; } case MotionEvent.ACTION_UP: { if (this.current != null) { - if (m.getOrientation(current) == Direction.VERTICAL) { help_move(down_y, y); } else { help_move(down_x, x); } + if (down_p.getPos().getCol() != m.piece(current).getPos().getCol() + || down_p.getPos().getLig() != m.piece(current).getPos().getLig()) { + this.m.undoPush(down_p); + this.m.redoClear(); + this.down_p = null; + } this.current = null; } return true; @@ -190,4 +227,4 @@ public boolean onTouchEvent(@NonNull MotionEvent e) { } } } -} +} \ No newline at end of file diff --git a/app/src/main/java/android/rushdroid/model/Model.java b/app/src/main/java/android/rushdroid/model/Model.java index 5145f25..4ccbd7e 100644 --- a/app/src/main/java/android/rushdroid/model/Model.java +++ b/app/src/main/java/android/rushdroid/model/Model.java @@ -15,7 +15,6 @@ public class Model implements IModel { final private Deque undo = new ArrayDeque<>(); final private Deque redo = new ArrayDeque<>(); - public Model(@NonNull List pieces) { this.pieces = pieces; this.setAllPieces(); @@ -121,6 +120,10 @@ public int getCol(int id) { return this.pieces.get(id).getPos().getCol(); } + public void undoPush (Piece p) { undo.push(p); } + + public void redoClear () { redo.clear(); } + // TODO: Using soft-wired end-of-game position. public boolean endOfGame() { Integer id = this.grid.get(new Position(5, 2)); @@ -132,9 +135,6 @@ public boolean moveForward(int id) { Position pos = p.getPos(); int size = p.getSize(); - this.undo.push(p); - while (!redo.isEmpty()) { redo.pop(); } - switch (p.getOrientation()) { case HORIZONTAL: { int x = pos.getCol() + size; @@ -164,9 +164,6 @@ public boolean moveBackward(int id) { Position pos = p.getPos(); int offset = p.getSize() - 1; - this.undo.push(p); - while (!redo.isEmpty()) { redo.pop(); } - switch (p.getOrientation()) { case HORIZONTAL: { int x = pos.getCol() - 1; From 61c0bd1ca3a404a1f52ee9ffd79e713f8d0c10e5 Mon Sep 17 00:00:00 2001 From: Axel Viala Date: Wed, 6 Apr 2016 17:55:01 +0200 Subject: [PATCH 2/2] Polish and littles changes. --- .../java/android/rushdroid/GameActivity.java | 1 - .../java/android/rushdroid/GameThread.java | 11 ++- .../main/java/android/rushdroid/GameView.java | 71 ++++++++----------- .../android/rushdroid/model/Position.java | 5 +- 4 files changed, 40 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/android/rushdroid/GameActivity.java b/app/src/main/java/android/rushdroid/GameActivity.java index a7330dd..b1117d5 100644 --- a/app/src/main/java/android/rushdroid/GameActivity.java +++ b/app/src/main/java/android/rushdroid/GameActivity.java @@ -14,7 +14,6 @@ public class GameActivity extends Activity { Model game; - @Override protected void onCreate(Bundle savedInstanceState) { this.game = ((GameApplication) this.getApplication()).game(); diff --git a/app/src/main/java/android/rushdroid/GameThread.java b/app/src/main/java/android/rushdroid/GameThread.java index c3dfacf..ec1518f 100644 --- a/app/src/main/java/android/rushdroid/GameThread.java +++ b/app/src/main/java/android/rushdroid/GameThread.java @@ -13,6 +13,7 @@ class GameThread extends Thread { final private GameView view; final private SurfaceHolder holder; private boolean running = false; + private boolean work = false; public GameThread(GameView view, SurfaceHolder holder) { this.view = view; @@ -20,8 +21,12 @@ public GameThread(GameView view, SurfaceHolder holder) { } // flag to hold game state - protected void setRunning(boolean running) { - this.running = running; + protected void setOff() { + this.running = false; + } + + protected void setOn() { + this.running = true; } /** @@ -46,7 +51,7 @@ public void run() { } } try { - Thread.sleep(600); + Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/app/src/main/java/android/rushdroid/GameView.java b/app/src/main/java/android/rushdroid/GameView.java index 299c4b9..196d0e1 100644 --- a/app/src/main/java/android/rushdroid/GameView.java +++ b/app/src/main/java/android/rushdroid/GameView.java @@ -64,7 +64,7 @@ public void surfaceCreated(@NonNull SurfaceHolder h) { surface_height = GameView.this.getHeight(); fillArray(xs, surface_width, game_width); fillArray(ys, surface_height, game_height); - this.th.setRunning(true); + this.th.setOn(); this.th.start(); } @@ -72,7 +72,7 @@ public void surfaceCreated(@NonNull SurfaceHolder h) { public void surfaceDestroyed(@NonNull SurfaceHolder h) { // tell the thread to shut down and wait for it to finish // this is a clean shutdown - this.th.setRunning(false); + this.th.setOff(); boolean retry = true; while (retry) { try { @@ -114,20 +114,19 @@ private void drawGrid(Canvas c) { private void drawGame(@NonNull Canvas c, Iterable pieces) { int ratioY = this.surface_height / this.game_height; int ratioX = this.surface_width / this.game_width; - for (Piece p : pieces) { - int xp = p.getPos().getCol(); - int yp = p.getPos().getLig(); - - if (p.getOrientation() == Direction.VERTICAL) { - int x2 = (xp + 1) * ratioX; - int y2 = (yp + p.getSize()) * ratioY; - Bitmap bitmap = (p.getSize() == 2) ? (this.bitmaps[0]) : (this.bitmaps[1]); - c.drawBitmap(bitmap, null, new RectF(xp * ratioX, yp * ratioY, x2, y2), null); + for (Piece piece : pieces) { + Position p = piece.getPos(); + int size = piece.getSize(); + if (piece.getOrientation() == Direction.VERTICAL) { + int x2 = (p.x + 1) * ratioX; + int y2 = (p.y + size) * ratioY; + Bitmap bitmap = (size == 2) ? (this.bitmaps[0]) : (this.bitmaps[1]); + c.drawBitmap(bitmap, null, new RectF(p.x * ratioX, p.y * ratioY, x2, y2), null); } else { - int x2 = (xp + p.getSize()) * ratioX; - int y2 = (yp + 1) * ratioY; - Bitmap bitmap = (p.getSize() == 2) ? (this.bitmaps[2]) : (this.bitmaps[3]); - c.drawBitmap(bitmap, null, new RectF(xp * ratioX, yp * ratioY, x2, y2), null); + int x2 = (p.x + size) * ratioX; + int y2 = (p.y + 1) * ratioY; + Bitmap bitmap = (size == 2) ? (this.bitmaps[2]) : (this.bitmaps[3]); + c.drawBitmap(bitmap, null, new RectF(p.x * ratioX, p.y * ratioY, x2, y2), null); } } drawGrid(c); @@ -170,6 +169,12 @@ private boolean help_move(int prev, int now) { } */ + int help_move(int current, int test) { + if (current < test && this.m.moveForward(this.current)) { return current + 1; } + else if (current > test && this.m.moveBackward(this.current)) { return current - 1; } + else { return current; } + } + @Override public boolean onTouchEvent(@NonNull MotionEvent e) { int x = (int) e.getX(); @@ -179,41 +184,25 @@ public boolean onTouchEvent(@NonNull MotionEvent e) { // System.out.println(p); Position p = interpolation(x, y); this.current = m.getIdByPos(p); - this.down_x = p.getCol(); - this.down_y = p.getLig(); - if (current != null) { - this.down_p = m.piece(current); + if (this.current != null) { + this.down_p = m.piece(this.current); + this.down_x = p.x; + this.down_y = p.y; } return true; - } - case MotionEvent.ACTION_MOVE: { + } case MotionEvent.ACTION_MOVE: { if (this.current != null) { + Position p = interpolation(x, y); if (m.getOrientation(current) == Direction.VERTICAL) { - int test = interpolation(x, y).getLig(); - if (down_y < test) { - if (this.m.moveForward(this.current)) { down_y += 1; } - } else { - if (down_y > test) { - if (this.m.moveBackward(this.current)) { down_y -= 1; } - } - } + this.down_y = this.help_move(down_y, p.y); } else { - int test = interpolation(x, y).getCol(); - if (down_x < test) { - if (this.m.moveForward(this.current)) { down_x += 1; } - } else { - if (down_x > test) { - if (this.m.moveBackward(this.current)) { down_x -= 1; } - } - } + this.down_x = this.help_move(down_x, p.x); } } return true; - } - case MotionEvent.ACTION_UP: { + } case MotionEvent.ACTION_UP: { if (this.current != null) { - if (down_p.getPos().getCol() != m.piece(current).getPos().getCol() - || down_p.getPos().getLig() != m.piece(current).getPos().getLig()) { + if (!down_p.getPos().equals(m.piece(current).getPos())) { this.m.undoPush(down_p); this.m.redoClear(); this.down_p = null; diff --git a/app/src/main/java/android/rushdroid/model/Position.java b/app/src/main/java/android/rushdroid/model/Position.java index 02fba63..b57f3b5 100644 --- a/app/src/main/java/android/rushdroid/model/Position.java +++ b/app/src/main/java/android/rushdroid/model/Position.java @@ -4,8 +4,8 @@ import android.support.annotation.Nullable; final public class Position implements IPosition { - private final int x; - private final int y; + public final int x; + public final int y; /** * @param x An integer @@ -63,7 +63,6 @@ public Position add(@NonNull Position p) { public boolean equals(@Nullable Object o) { if (this == o) { return true; } if (o == null || this.getClass() != o.getClass()) { return false; } - Position p = (Position) o; return this.x == p.x && this.y == p.y; }