Created
May 12, 2022 05:47
-
-
Save Nekodigi/8280cf9867ca74e14851abcefeb4c238 to your computer and use it in GitHub Desktop.
MatchThree
This file contains 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
class Cell{ | |
int i, j; | |
float x, y; | |
int type; | |
boolean dead; | |
Cell(int i, int j){ | |
this.type = (int)random(4); | |
this.i = i; | |
this.j = j; | |
} | |
void move(int di, int dj){ | |
map[i][j] = null; | |
i+=di; | |
j+=dj; | |
map[i][j] = this; | |
} | |
void calcPos(){ | |
x = offsetX+i*spacingX+spacingX/2; | |
y = offsetY+j*spacingY+spacingY/2; | |
} | |
void kill(){ | |
dead=true; | |
map[i][j] = null; | |
} | |
} |
This file contains 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
//config | |
int n=8;//map width | |
int m=8;//map height | |
//system variable | |
Cell[][] map = new Cell[n][m]; | |
int offsetX =50; | |
int offsetY = 50; | |
float spacingX = 0; | |
float spacingY = 0; | |
float cooltime = 0; | |
float cooltimeMax = 20; | |
void setup(){ | |
size(500, 500); | |
//make map | |
spacingX = (width-offsetX*2)/n; | |
spacingY = (height-offsetY*2)/n; | |
for(int i=0; i<n; i++){ | |
for(int j=0; j<m; j++){ | |
map[i][j] = new Cell(i, j); | |
} | |
} | |
} | |
void draw(){ | |
background(255); | |
if(cooltime <= 0)solve();//solve 3 match | |
if(frameCount%4==0){ | |
flow();//fall down | |
} | |
refill(); | |
show();//show cell | |
cooltime--; | |
} | |
int istart; | |
int jstart; | |
int xstart; | |
int ystart; | |
void mousePressed(){ | |
xstart = mouseX;//swap target | |
ystart = mouseY; | |
istart = (int)((mouseX-offsetX)/spacingX); | |
jstart = (int)(((height-mouseY)-offsetY)/spacingY); | |
} | |
void mouseReleased(){ | |
int dx = mouseX-xstart;//swap direction | |
int dy = mouseY-ystart; | |
if(dx == 0 && dy==0)return; | |
if(abs(dx) > abs(dy)){//move horizontal | |
if(dx>0){ | |
swap(istart, jstart, istart+1, jstart); | |
}else{ | |
swap(istart, jstart, istart-1, jstart); | |
} | |
}else{ | |
if(dy>0){ | |
swap(istart, jstart, istart, jstart-1); | |
}else{ | |
swap(istart, jstart, istart, jstart+1); | |
} | |
} | |
println("swap"); | |
} | |
void swap(int i1, int j1, int i2, int j2){//swap first cell and second cell | |
if(!(Iinrange(i1) && Iinrange(i2) && Jinrange(j1) && Jinrange(j2)))return; | |
if(map[i1][j1]==null || map[i2][j2] == null)return; | |
Cell temp = map[i1][j1]; | |
map[i2][j2].move(i1-i2, j1-j2); | |
temp.i = i2; | |
temp.j = j2; | |
map[i2][j2] = temp; | |
} | |
boolean Iinrange(int i){//check if i in in valid range | |
return 0<=i && i<n; | |
} | |
boolean Jinrange(int j){//check if j in valid range | |
return 0<=j && j<m; | |
} | |
void show(){//show cells | |
ellipseMode(CENTER); | |
noStroke(); | |
//stroke(100); | |
for(int i=0; i<n; i++){ | |
for(int j=0; j<m; j++){ | |
Cell c = map[i][j]; | |
if(c==null)continue; | |
switch(c.type){ | |
case 0://red | |
//fill(255, 0, 0); | |
fill(250, 60, 50); | |
break; | |
case 1://blue | |
//fill(0, 0, 255); | |
fill(60, 130, 254); | |
break;//green | |
case 2: | |
//fill(0, 255, 0); | |
fill(40, 207, 88); | |
break;//yellow | |
case 3: | |
//fill(255, 255, 0); | |
fill(250, 180, 80); | |
break; | |
} | |
c.calcPos(); | |
ellipse(c.x, height-c.y, spacingX*0.8, spacingY*0.8); | |
} | |
} | |
} | |
void refill(){//refill top of map | |
for(int i=0; i<n; i++){ | |
if(map[i][m-1] == null){ | |
map[i][m-1] = new Cell(i, m-1); | |
} | |
} | |
} | |
void flow(){//fall down cell | |
for(int j=1; j<m; j++){ | |
for(int i=0; i<n; i++){ | |
if(map[i][j-1]==null && map[i][j]!=null){ | |
map[i][j].move(0, -1); | |
return; | |
} | |
} | |
} | |
} | |
void solve(){//find 1 match pattern at once | |
if(solveXh(3))return; | |
if(solveXv(3))return; | |
} | |
boolean solveXh(int X){//check horizontal cell match | |
for(int j=0; j<m; j++){ | |
for(int i=0; i<=n-X; i++){ | |
Cell[] cells = new Cell[X]; | |
if(map[i][j]==null)continue; | |
int firstType = map[i][j].type; | |
boolean match=true; | |
for(int k=0; k<X; k++){ | |
cells[k] = map[i+k][j]; | |
if(cells[k] == null || cells[k].type != firstType) {match=false;break;} | |
} | |
if(match){ | |
println("match"); | |
for(int k=0; k<X; k++){ | |
cells[k].kill(); | |
} | |
cooltime = cooltimeMax; | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
boolean solveXv(int X){//check vertical cell match | |
for(int j=0; j<=m-X; j++){ | |
for(int i=0; i<n; i++){ | |
Cell[] cells = new Cell[X]; | |
if(map[i][j]==null)continue; | |
int firstType = map[i][j].type; | |
boolean match=true; | |
for(int k=0; k<X; k++){ | |
cells[k] = map[i][j+k]; | |
if(cells[k] == null || cells[k].type != firstType) {match=false;break;} | |
} | |
if(match){ | |
println("match"); | |
for(int k=0; k<X; k++){ | |
cells[k].kill(); | |
} | |
cooltime = cooltimeMax; | |
return true; | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment