Skip to content

Instantly share code, notes, and snippets.

@NobukazuHanada
Created February 8, 2023 09:31
Show Gist options
  • Save NobukazuHanada/49c9cef1bba2fa94506e096e0e8f555e to your computer and use it in GitHub Desktop.
Save NobukazuHanada/49c9cef1bba2fa94506e096e0e8f555e to your computer and use it in GitHub Desktop.
import java.util.stream.IntStream;
import java.util.Arrays;
// Board
final int BoardMargin = 50;
final int BoardX = BoardMargin;
final int BoardY = BoardMargin;
final int BoardWidth = 400;
final int BoardHeight = 400;
// Piece
final int MaxRow = 4;
final int MaxColumn = 4;
final int PieceCount = MaxRow * MaxColumn;
final int PieceWidth = BoardWidth / MaxColumn;
final int PieceHeight = BoardHeight / MaxRow;
PImage[] Pieces;
// Puzzle Map
int[][] PieceMap;
// select
boolean selected = false;
int selectedRow = -1;
int selectedColumn = -1;
// (x,y) <---> (row, column) functions
int columnToX(int column){
return column * PieceWidth + BoardX;
}
int rowToY(int row){
return row * PieceWidth + BoardY;
}
int xToColumn(int x){
return (x - BoardX) / PieceWidth;
}
int yToRow(int y){
return (y - BoardY) / PieceHeight;
}
// check (x,y) is on Board.
boolean onBoard(int x, int y){
return x > BoardX
&& y > BoardY
&& x < BoardX + BoardWidth
&& y < BoardY + BoardHeight;
}
// PieceIndex --> (row, column) --> (imgX, imgY) functions
int indexToRow(int index){
return index/MaxColumn;
}
int indexToColumn(int index){
return index%MaxColumn;
}
int columnToImgX(int column){
return column * PieceWidth;
}
int rowToImgY(int row){
return row * PieceWidth;
}
int indexToImgX(int index){
int column = indexToColumn(index);
return columnToImgX(column);
}
int indexToImgY(int index){
int row = indexToRow(index);
return rowToImgY(row);
}
// initialize Pieces
void setupPieces(){
var img = loadImage("sample.PNG");
img.resize(BoardWidth, BoardHeight);
Pieces = IntStream.range(0, PieceCount).mapToObj(index->
img.get(indexToImgX(index),indexToImgY(index),PieceWidth, PieceHeight)
).toArray(PImage[]::new);
}
// initialize Piece Map
void setupPieceMap(){
int[] indexes = IntStream.range(0,PieceCount).toArray();
var shuffledIndexes = new IntList(indexes);
shuffledIndexes.shuffle();
PieceMap = IntStream.range(0,MaxRow).mapToObj(row->
IntStream.range(0, MaxColumn).map(column->
shuffledIndexes.get(column + row * MaxColumn)
).toArray()
).toArray(int[][]::new);
}
void drawPiece(int index, int row, int column){
int x = columnToX(column);
int y = rowToY(row);
image(Pieces[index],x,y,PieceWidth, PieceHeight);
if(selected && row == selectedRow && column == selectedColumn ){
fill(100,100,100,80);
}else{
noFill();
}
stroke(0);
rect(x,y,PieceWidth, PieceHeight);
}
void drawAllPieces(){
for(int i = 0; i < MaxRow; i++){
for(int j = 0; j < MaxColumn; j++){
drawPiece(PieceMap[i][j],i,j);
}
}
}
void resetSelection(){
selected = false;
selectedRow = -1;
selectedColumn = -1;
}
void setSelection(int row, int column){
selected = true;
selectedRow = row;
selectedColumn = column;
}
void swapPieceMap(int row, int column){
var tmp = PieceMap[row][column];
PieceMap[row][column] = PieceMap[selectedRow][selectedColumn];
PieceMap[selectedRow][selectedColumn] = tmp;
}
void updateSelection(int row, int column){
if(selected){
if(selectedRow != row || selectedColumn != column){
swapPieceMap(row, column);
}
resetSelection();
}else{
setSelection(row, column);
}
}
void settings(){
size(BoardWidth + 2 * BoardMargin, BoardHeight + 2 * BoardMargin);
}
void setup(){
setupPieces();
setupPieceMap();
}
void draw(){
drawAllPieces();
}
void mousePressed(){
if( !onBoard(mouseX, mouseY) ) {
return;
}
int column = xToColumn(mouseX);
int row = yToRow(mouseY);
updateSelection(row, column);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment