-
Notifications
You must be signed in to change notification settings - Fork 76
Add infinite mode #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
llohanda
wants to merge
4
commits into
LonamiWebs:master
Choose a base branch
from
llohanda:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
539557e
By default, always generate fitting blocks
llohanda ca889bc
Make block validation as "infinite mode" option
llohanda ef1726b
Fix State.java based on reviews in #55
llohanda 871f665
Change `unfitBlock` in `State.java` from static to instance variable
llohanda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,282 @@ | ||
| package io.github.lonamiwebs.klooni.game; | ||
|
|
||
| import com.badlogic.gdx.Gdx; | ||
| import com.badlogic.gdx.math.MathUtils; | ||
| import com.badlogic.gdx.math.Vector2; | ||
|
|
||
| /* | ||
| State is an object designed to help validating randomly-generated blocks. Every time a block is | ||
| inserted into the board, the game's state changes. Different permutation of blocks and different | ||
| position creates new states. Thus to validate a set of generated blocks, we have to check all | ||
| possible states. Instead of checking them directly on the board, we create a special class for | ||
| validation. | ||
|
|
||
| This is used in infinite mode. | ||
| */ | ||
|
|
||
| public class State { | ||
Lonami marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // region Members | ||
|
|
||
| private boolean[][] state; | ||
| private final int cellCount; | ||
| private int emptySpace = 0; | ||
| private static int[] unfitBlock = new int[6]; | ||
Lonami marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private final static int [][] CHECKER = {{0,1,2},{0,2,1},{1,0,2},{1,2,0},{2,0,1},{2,1,0}}; | ||
| private final static Vector2 ORIGIN = new Vector2(0, -1); | ||
Lonami marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // endregion | ||
|
|
||
| //region Constructors | ||
|
|
||
| private State(Board board) { | ||
| this.cellCount = board.cellCount; | ||
| this.state = new boolean[cellCount][cellCount]; | ||
| for (int i = 0; i < board.cellCount; ++i) | ||
| for(int j = 0; j < board.cellCount; ++j) | ||
Lonami marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (board.isEmpty(i, j)) | ||
| emptySpace += 1; | ||
| else | ||
| this.state[i][j] = true; | ||
| clearComplete(); | ||
| } | ||
|
|
||
| private State(State toClone) { | ||
| this.cellCount = toClone.state.length; | ||
| this.state = new boolean[cellCount][cellCount]; | ||
| for (int i = 0; i < cellCount; ++i) | ||
| for(int j = 0; j < cellCount; ++j) | ||
| if (toClone.state[i][j]) | ||
| this.state[i][j] = true; | ||
| else | ||
| emptySpace += 1; | ||
| } | ||
|
|
||
| // endregion | ||
|
|
||
| //region Check piece | ||
|
|
||
| private static String getUnfitBlock() { | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (int i : unfitBlock) { | ||
| sb.append(i); | ||
| sb.append(" "); | ||
| } | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| // True if the given cell coordinates are inside the bounds of the board | ||
| private boolean inBounds(int x, int y) { | ||
| return x >= 0 && x < cellCount && y >= 0 && y < cellCount; | ||
| } | ||
|
|
||
| // True if the given piece at the given coordinates is not outside the bounds of the board | ||
| private boolean inBounds(Piece piece, int x, int y) { | ||
| return inBounds(x, y) && inBounds(x + piece.cellCols - 1, y + piece.cellRows - 1); | ||
| } | ||
|
|
||
| // Given coordinates as the starting point, return true if a piece fits in said coordinates | ||
| private boolean canPutPiece(Piece piece, int x, int y) { | ||
| if (!inBounds(piece, x, y)) | ||
| return false; | ||
|
|
||
| for (int i = 0; i < piece.cellRows; ++i) | ||
| for (int j = 0; j < piece.cellCols; ++j) | ||
| if (state[y + i][x + j] && piece.filled(i, j)) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| // Given Vector2 as last checked coordinates, return a new vector where the piece fits, | ||
| // else the same vector if there are no longer any place in board to fit the piece | ||
| private Vector2 canPutPiece(Piece piece, Vector2 origin) { | ||
| for (int j = (int) (origin.x + 1); j < cellCount; ++j) | ||
| if (canPutPiece(piece, j, (int) origin.y)) | ||
| return new Vector2(j, origin.y); | ||
|
|
||
| for (int i = (int) (origin.y + 1); i < cellCount; ++i) | ||
| for (int j = 0; j < cellCount; ++j) | ||
| if (canPutPiece(piece, j, i)) | ||
| return new Vector2(j, i); | ||
|
|
||
| return origin; | ||
| } | ||
|
|
||
| // Check every possible state from a set of blocks and an initial state. | ||
| // Immediately return true if found one fitting occurrence. | ||
| private static boolean checkPermute(Piece[] holder, State state) { | ||
| Vector2 temp1, temp2, temp3, pos1 = ORIGIN, pos2 = ORIGIN; | ||
| for (int i = 0; i < 6; i++) { | ||
Lonami marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| unfitBlock[i] = -1; | ||
| // TODO: Optimize the checking algorithm (use any pruning techniques?) | ||
| // TODO: Determine better criteria to change unfit block | ||
| while(true) { | ||
| State state1 = new State(state); | ||
Lonami marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| temp1 = state.canPutPiece(holder[CHECKER[i][0]], pos1); | ||
| if (temp1.epsilonEquals(pos1, MathUtils.FLOAT_ROUNDING_ERROR)) { | ||
| if (unfitBlock[i] < 0) | ||
| unfitBlock[i] = 0; | ||
| break; | ||
| } | ||
| state1.putPiece(holder[CHECKER[i][0]], temp1); | ||
| while (true) { | ||
| State state2 = new State(state1); | ||
Lonami marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| temp2 = state1.canPutPiece(holder[CHECKER[i][1]], pos2); | ||
| if (temp2.epsilonEquals(pos2, MathUtils.FLOAT_ROUNDING_ERROR)) { | ||
| if (unfitBlock[i] < 1) | ||
| unfitBlock[i] = 1; | ||
| break; | ||
| } | ||
| state2.putPiece(holder[CHECKER[i][1]], temp2); | ||
| temp3 = state2.canPutPiece(holder[CHECKER[i][2]], ORIGIN); | ||
| if (!temp3.epsilonEquals(ORIGIN, MathUtils.FLOAT_ROUNDING_ERROR)) { | ||
| state2.putPiece(holder[CHECKER[i][2]], temp3); | ||
| Gdx.app.log("Check permute", "Piece " + | ||
| holder[CHECKER[i][0]].colorIndex + " at " + temp1.toString()); | ||
| Gdx.app.log("Check permute", "Piece " + | ||
| holder[CHECKER[i][1]].colorIndex + " at " + temp2.toString()); | ||
| Gdx.app.log("Check permute", "Piece " + | ||
| holder[CHECKER[i][2]].colorIndex + " at" + temp3.toString()); | ||
| return true; | ||
| } | ||
| else | ||
| unfitBlock[i] = 2; | ||
| pos2 = new Vector2(temp2); | ||
| } | ||
| pos1 = new Vector2(temp1); | ||
| } | ||
| } | ||
| Gdx.app.log("Check permute", getUnfitBlock()); | ||
| return false; | ||
| } | ||
|
|
||
| // Change unfit block that cause most problem | ||
| private static Piece[] changeBlock(Piece[] holder, State state) { | ||
| int[] weight = new int[3]; | ||
| int max = 0; | ||
| for (int i = 0; i < 6; i++) { | ||
| int temp = CHECKER[i][unfitBlock[i]]; | ||
| weight[temp] += (unfitBlock[i] + 1) * 5; | ||
| } | ||
|
|
||
| for (int i : weight) { | ||
| if (i > max) | ||
| max = i; | ||
| } | ||
|
|
||
| Gdx.app.log("Change block", holder[0].colorIndex + ", " + | ||
| holder[1].colorIndex + ", " + holder[2].colorIndex); | ||
| Gdx.app.log("Change block", weight[0] + ", " + | ||
| weight[1] + ", " + weight[2]); | ||
| for (int i = 0; i < 3; i++) { | ||
| if (weight[i] == max) { | ||
| int previous = holder[i].colorIndex; | ||
| Gdx.app.log("Change block", "Changing block"); | ||
| holder[i] = Piece.random(); | ||
| if (checkPermute(holder, state)) { | ||
| Gdx.app.log("Change block", "Piece " + previous + | ||
| " to Piece " + holder[i].colorIndex); | ||
| return holder; | ||
| } | ||
| } | ||
| } | ||
| return changeBlock(holder, state); | ||
| } | ||
|
|
||
| public static Piece[] validateBlock(Piece[] holder, Board board) { | ||
| long invocationTime = System.nanoTime(); | ||
| State initialState = new State(board); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, we're rebuilding the entire state every time we need to validate a piece instead of keeping it in synchrony with the board itself. Though it's true this is only ran when more pieces are needed and that's not common, so I believe it's okay. |
||
| Gdx.app.log("Validation", "Board contains " + initialState.emptySpace + | ||
| " empty spaces."); | ||
| if (checkPermute(holder, initialState)) { | ||
| Gdx.app.log("Validation", "Validation ends, takes " + | ||
| (System.nanoTime() - invocationTime) + " ns!"); | ||
| return holder; | ||
| } | ||
| else { | ||
| Gdx.app.log("Validation", "Validation ends, takes " + | ||
| (System.nanoTime() - invocationTime) + " ns with changeBlock."); | ||
| return changeBlock(holder, initialState); | ||
| } | ||
| } | ||
|
|
||
| // endregion | ||
|
|
||
| // region Set piece | ||
|
|
||
| private void clearComplete() { | ||
| int clearCount = 0; | ||
| boolean[] clearedRows = new boolean[cellCount]; | ||
| boolean[] clearedCols = new boolean[cellCount]; | ||
|
|
||
| // Analyze rows and columns that will be cleared | ||
| for (int i = 0; i < cellCount; ++i) { | ||
| clearedRows[i] = true; | ||
| for (int j = 0; j < cellCount; ++j) { | ||
| if (!state[i][j]) { | ||
| clearedRows[i] = false; | ||
| break; | ||
| } | ||
| } | ||
| if (clearedRows[i]) | ||
| clearCount++; | ||
| } | ||
| for (int j = 0; j < cellCount; ++j) { | ||
| clearedCols[j] = true; | ||
| for (int i = 0; i < cellCount; ++i) { | ||
| if (!state[i][j]) { | ||
| clearedCols[j] = false; | ||
| break; | ||
| } | ||
| } | ||
| if (clearedCols[j]) | ||
| clearCount++; | ||
| } | ||
| if (clearCount > 0) { | ||
| // Do clear those rows and columns | ||
| for (int i = 0; i < cellCount; ++i) { | ||
| if (clearedRows[i]) { | ||
| for (int j = 0; j < cellCount; ++j) { | ||
| state[i][j] = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (int j = 0; j < cellCount; ++j) { | ||
| if (clearedCols[j]) { | ||
| for (int i = 0; i < cellCount; ++i) { | ||
| state[i][j] = false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void putPiece(Piece piece, Vector2 vec) { | ||
| for (int i = 0; i < piece.cellRows; ++i) | ||
| for (int j = 0; j < piece.cellCols; ++j) | ||
| if (piece.filled(i, j)) | ||
| state[(int) (vec.y + i)][(int) (vec.x + j)] = true; | ||
| clearComplete(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| StringBuilder sb = new StringBuilder("State:\n"); | ||
| for (int i = cellCount - 1 ; i > -1; --i) { | ||
| for (int j = 0; j < cellCount; ++j) { | ||
| if (state[i][j]) { | ||
| sb.append(1); | ||
| } else { | ||
| sb.append(0); | ||
| } | ||
| } | ||
| sb.append("\n"); | ||
| } | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| // endregion | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.