Skip to content

Commit

Permalink
Fix gravity not applying after loading
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisIsPIRI committed Apr 10, 2021
1 parent fb8467a commit b573a17
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.thisispiri.mnk"
minSdkVersion 14
targetSdkVersion 29
versionCode 39
versionName "2.0.2"
versionCode 40
versionName "2.0.3"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
18 changes: 8 additions & 10 deletions app/src/main/java/com/thisispiri/mnk/BaseMnkGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class BaseMnkGame implements MnkGame { //TODO: Support renju
@Override public Shape[] getShapes() { return shapes; }
@Override public Stack<Move> getHistory() { return history; }

/**Returns the index of the next {@link Shape} to be placed in {@link MnkGame#shapes}.
/**Returns the index of the next {@link Shape} to be placed in {@link MnkGame#getShapes()}.
* @return The index of the next {@link Shape} to be placed.*/
@Override public int getNextIndex() {return nextIndex;}

Expand Down Expand Up @@ -105,7 +105,7 @@ public BaseMnkGame(final MnkGame original) {
else return false;
}

@Override public Point[] checkWin(final int x, final int y) {return checkWin(x, y, false);}
@Override public Point[] checkWin(final int x, final int y) { return checkWin(x, y, false); }
/**{@inheritDoc}
* The array will only contain {@link MnkGame#getWinStreak} Points even if the line is longer than that.
* The Points are stored from bottom to top if the line is vertical, right to left if horizontal,
Expand All @@ -117,26 +117,24 @@ public BaseMnkGame(final MnkGame original) {
new Point(Math.max(x - y, 0), Math.max(y - x, 0))};
for(int i = 0;i < xP.length;i++) {
int streak = 0;
Point p = starting[i];
while(inBoundary(getNextPoint(p, xP[i], yP[i]))) {
Point p = starting[i], n = new Point(p), temp;
while(inBoundary(p.y + yP[i], p.x + xP[i])) {
streak++;
Point n = getNextPoint(p, xP[i], yP[i]);
n.x = p.x + xP[i];
n.y = p.y + yP[i];
if(array[p.y][p.x] != array[n.y][n.x] || isEmpty(p.x, p.y)) {
if(streak == winStreak) //Exact matches not including the last cell
return getLinePoints(winStreak, p.x, p.y, -xP[i], -yP[i]);
streak = 0;
}
//All non-exact matches and exact matches including the last cell
if(streak == winStreak - 1 && (!exact || !inBoundary(getNextPoint(n, xP[i], yP[i]))))
if(streak == winStreak - 1 && (!exact || !inBoundary(n.y + yP[i], n.x + xP[i])))
return getLinePoints(winStreak, n.x, n.y, -xP[i], -yP[i]);
p = n;
temp = p; p = n; n = temp;
}
}
return null;
}
private Point getNextPoint(final Point p, final int xPlus, final int yPlus) {
return new Point(p.x + xPlus, p.y + yPlus);
}
/**Returns the {@code Point}s consisting a line.
* @param length The length of the line.
* @param xPlus The direction the X coordinate progresses in from startX on the line.
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/thisispiri/mnk/EmacsGomokuAi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ open class EmacsGomokuAi: MnkAi {
}
private fun forBackward(from: Point, mode: Mode, n: Int, action: (Int, Int) -> Unit) {
val p = Point(from)
for(i in 1..n) {
for(i in 0 until n) {
action(p.x, p.y)
backward(p, mode)
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/thisispiri/mnk/GravityMnkGame.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.thisispiri.mnk

/**An [MnkGame] decorator that adds gravity. */
/**An [MnkGame] decorator that adds gravity.
* This, or any other decorator that changes (x,y), should be the outermost decorator.*/
open class GravityMnkGame(private val game: MnkGame): MnkGame by game {
/**Returns the largest y that keeps (x, y) within boundary and empty. */
protected fun getFallenY(x: Int, y: Int): Int {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/thisispiri/mnk/LegalMnkGame.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.thisispiri.mnk

/**An [MnkGame] decorator through which you can only play valid moves.
* This should be the innermost decorator.*/
/**An [MnkGame] decorator through which you can only play valid moves.*/
open class LegalMnkGame(private val game: MnkGame): MnkGame by game {
/**Places a [Shape] on the position if and only if the tile is empty.
* @return Whether it succeeded in placing a stone.*/
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/thisispiri/mnk/MnkGame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ interface MnkGame {
val horSize: Int

/**Places a [Shape] at the supplied coordinate. Does not check if the move is illegal(the Shape may be placed on top of another, already placed, one).
* Changes [MnkGame.nextIndex].
* @return `true` if a [Shape] was successfully placed. `false` if it failed to place it(for example, because the coordinate was out of boundaries).*/
* Changes [MnkGame.nextIndex] if the placement was successful.
* @return `true` if a [Shape] was successfully placed. `false` if not(for example, because the coordinate was out of boundaries).*/
fun place(x: Int, y: Int): Boolean
/**Places the shape at the location.
* Does not change [MnkGame.nextIndex].
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/com/thisispiri/mnk/Move.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
public class Move {
public final Point coord;
public final Shape placed, prev;
Move(Point p, Shape placed, Shape previous) {
coord = new Point(p);
/**@param p The coordinate. Will be assigned directly to {@link Move#coord}, and not copied.*/
public Move(Point p, Shape placed, Shape previous) {
coord = p;
this.placed = placed;
prev = previous;
}
}
}
15 changes: 8 additions & 7 deletions app/src/main/java/com/thisispiri/mnk/andr/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,15 @@ public class MainActivity extends AppCompatActivity implements MnkManager, Timed
private int[] getPureRules() {
return new int[]{game.getHorSize(), game.getVerSize(), game.getWinStreak(), enableTimeLimit ? timeLimit : -1, gravity, exactOnly};
}
private void decorateAndSetGame(MnkGame base) {
game = new LegalMnkGame(base);
if(gravity == 1)
game = new GravityMnkGame(game);
}
/**{@inheritDoc}*/
@Override public void setRulesFrom(int[] array) {
game = new LegalMnkGame(new BaseMnkGame(array[0], array[1], array[2]));
gravity = array[4];
if(array[4] == 1)
game = new GravityMnkGame(game);
decorateAndSetGame(new BaseMnkGame(array[0], array[1], array[2]));
setTimeLimit(array[3]);
exactOnly = array[5];
myIndex = array[MnkManager.RULE_SIZE - 1];
Expand All @@ -168,9 +171,7 @@ private int[] getPureRules() {
/**Reads the rules from {@code pref}. Also initializes the game if the size has changed.*/
private void readRules(final SharedPreferences pref) { //TODO: Move initialization to readData?
gravity = pref.getBoolean("enableGravity", false) ? 1 : 0;
game = new LegalMnkGame(game == null ? new BaseMnkGame() : new BaseMnkGame(game));
if(gravity == 1)
game = new GravityMnkGame(game);
decorateAndSetGame(game == null ? new BaseMnkGame() : new BaseMnkGame(game));
if(game.setSize(pref.getInt("horSize", 15), pref.getInt("verSize", 15))) initialize(); //Initialize MainActivity fields too if the game was initialized.
game.setWinStreak(pref.getInt("winStreak", 5));
setTimeLimit(pref.getBoolean("enableTimeLimit", false) ? pref.getInt("timeLimit", 60000) : -1);
Expand Down Expand Up @@ -813,7 +814,7 @@ private void loadGame(final String fileName) {
else try {
MnkGame loaded = MnkSaveLoader.load(getFile(DIRECTORY_NAME, fileName + FILE_EXTENSION, false), game.getWinStreak());
initialize(); //Initialize after loading the game so that if loading fails, the previous game doesn't get initialized
game = new LegalMnkGame(loaded);
decorateAndSetGame(loaded);
board.setGame(game);
board.invalidate();
highlighter.updateValues(game.getHorSize(), game.getVerSize(), screenX);
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.4.31'
ext.kotlin_version = '1.4.32'
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.2'
classpath 'com.android.tools.build:gradle:4.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down

0 comments on commit b573a17

Please sign in to comment.