miduo_server/gamecommon/src/main/java/util/CellUtil.java

218 lines
6.3 KiB
Java
Raw Normal View History

package util;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class CellUtil {
public static int xy2Pos(int x, int y) {
if (x < 0 || y < 0){
return 0;
}
return (x << 8) | y;
}
public static int[] pos2XY(int pos) {
int[] xy = new int[2];
xy[0] = (pos >> 8) & 0xff;
xy[1] = pos & 0xff;
return xy;
}
public static void main(String[] args) {
System.out.println(14*256+23);
List<Integer> path = new ArrayList<>();
path.add(3607);
path.add(3351);
for(Integer pos : path){
System.out.println(pos +"----------------");
System.out.print(pos2XY(pos)[0] + " ");
System.out.print(pos2XY(pos)[1] + " ");
}
}
/**
* width x height pos
* @param width
* @param height
* @param pos
* @return
*/
public static Set<Integer> getOneOpenCell(int width, int height, int pos){
int[] xy = pos2XY(pos);
int x = xy[0];
int y = xy[1];
Set<Integer> openCells = new HashSet<>();
for (int i = -2; i <= 2; i++) {
if (0 <= x+i && x+i < width && i!=0){
openCells.add(xy2Pos(x+i,y));
// System.out.println("=======> nX : "+(x+i)+" , nY : "+y);
}
if (0 <= y+i && y+i < height && i!=0){
openCells.add(xy2Pos(x,y+i));
// System.out.println("=======> nX : "+x+" , nY : "+(y+i));
}
}
if (x+1 < width && y+1 < height){
openCells.add(xy2Pos(x+1,y+1));
// System.out.println("===============> nX : "+(x+1)+" , nY : "+(y+1));
}
if (x+1 < width && y-1 >= 0){
openCells.add(xy2Pos(x+1,y-1));
// System.out.println("===============> nX : "+(x+1)+" , nY : "+(y-1));
}
if (x-1 >= 0 && y+1 < height){
openCells.add(xy2Pos(x-1,y+1));
// System.out.println("===============> nX : "+(x-1)+" , nY : "+(y+1));
}
if (x-1 >= 0 && y-1 >= 0){
openCells.add(xy2Pos(x-1,y-1));
// System.out.println("===============> nX : "+(x-1)+" , nY : "+(y-1));
}
System.out.println("=====================================");
return openCells;
}
public static Set<Integer> getSurroundPos(int width,int height ,int pos){
Set<Integer> poses = new HashSet<>();
int [] xy = pos2XY(pos);
int x = xy[0];
int y = xy[1];
int x1=x-1;
int y1=y-1;
for(;x1<x+2;x1++){
if(x1<1 || x1 > width){
continue;
}
for(;y1<y+2;y1++){
if(y1<1 || y1 > height){
continue;
}
if(x1 == x && y1 == y){
continue;
}
// System.out.println("x ->" + x1 + " y->" + y1);
poses.add(xy2Pos(x1,y1));
}
y1=y-1;
}
return poses;
}
/**
* -
* @param s1
* @param s2
* @return
*/
public static Set<Integer> union(Set<Integer> s1 , Set<Integer> s2){
for(Integer s : s2){
if(!s1.contains(s))
s1.add(s);
}
return s1 ;
}
/**
*
* @param pos1
* @param pos2
* @return
*/
public static boolean checkIsNextCell(int pos1 , int pos2){
int[] xy1 = pos2XY(pos1);
int[] xy2 = pos2XY(pos2);
if ((xy1[0]==xy2[0] && Math.abs(xy1[1]-xy2[1])==1) || (xy1[1]==xy2[1] && Math.abs(xy1[0]-xy2[0])==1)){
return true;
}
return false ;
}
public static boolean checkIsNextOrMineCell(int pos1 , int pos2){
int[] xy1 = pos2XY(pos1);
int[] xy2 = pos2XY(pos2);
if(pos1 == pos2){
return true;
}
if ((xy1[0]==xy2[0] && Math.abs(xy1[1]-xy2[1])==1) || (xy1[1]==xy2[1] && Math.abs(xy1[0]-xy2[0])==1)){
return true;
}
return false ;
}
public static boolean isContinuous(int lastXY, List<Integer> cells){
boolean first = false;
if (cells.size() > 0) {
first = checkIsNextOrMineCell(lastXY, cells.get(0));
if(!first){
return false;
}
}
for (int i = 1; i < cells.size() - 1; i++) {
boolean other = checkIsNextOrMineCell(cells.get(i), cells.get(i + 1));
if (!other) {
return false;
}
}
return true;
}
public static boolean isContinuous(int lastXY, List<Integer> cells,List<Integer> excludes){
boolean first = false;
if (cells.size() > 0) {
first = checkIsNextOrMineCell(lastXY, cells.get(0));
if(!first){
return false;
}
}
for (int i = 1; i < cells.size() - 1; i++) {
if(excludes.contains(cells.get(i)) || excludes.contains(cells.get(i +1 )) ){
return false;
}
boolean other = checkIsNextOrMineCell(cells.get(i), cells.get(i + 1));
if (!other) {
return false;
}
}
return true;
}
public static boolean calSurPos(int x,int y,int diff,int maxX,int maxY,Set<Integer> cachePos){
Set<Integer> possibalePos = new HashSet<>();
for(int i = 0 ; i <=diff ; i++){
for(int j = 0 ;j <=i;j++){
if(x-j>0 && x+i-j>0 && x+i-j <= maxY){
int xy2Pos = xy2Pos(x - j, y + i - j);
if(cachePos.contains(xy2Pos)){
return false;
}
possibalePos.add(xy2Pos(x-j,y+i-j));
}
if(x+j>0 && x+j <= maxX && y-i+j<= maxY && y-i+j>0){
int xy2Pos = xy2Pos(x+j,y-i+j);
if(cachePos.contains(xy2Pos)){
return false;
}
possibalePos.add(xy2Pos(x+j,y-i+j));
}
}
}
cachePos.addAll(possibalePos);
// System.out.println(possibalePos);
return true;
}
}