package inkball.model;
public class Map {
private Grid [][] grids;
private int row;
private int col;
private int[][] autoPathTime;
private Grid last;
private int mapSize;
private int imageSize;
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public Map(int row, int col,int mapSize,int imageSize) {
this.mapSize = mapSize;
this.imageSize = imageSize;
this.row = row;
this.col = col;
grids = new Grid[row][col];
autoPathTime = new int[row][col];
int i,j;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
grids[i][j]=new Grid(j*mapSize+mapSize/2,i*mapSize+mapSize/2,i,j);
autoPathTime[i][j]=0;
}
}
last=new Grid(mapSize,mapSize,row,col);
for (i = 1; i < col-5; i++) {
pathByStart(1,i);
pathByStart(row-5,i);
}
for (i = 1; i < row-5; i++) {
pathByStart(i,1);
pathByStart(i,col-5);
}
}
public boolean ifWall(int i, int j) {
if(i<0 || j<0 || i>=row || j>=col)
return false;
Grid g=grids[i][j];
return g.isWall();
}
public void setPath(int i, int j) {
grids[i][j].setWall(false);
autoPathTime[i][j]++;
}
public void setBefore(int i,int j) {
last.setxIndex(i);
last.setyIndex(j);
}
public boolean ifRange(int i, int j) {
if(i<=0||j<=0)
return false;
if(i>=row-1||j>=col-1)
return false;
return true;
}
public boolean ifautoPath(int i, int j) {
if(!grids[i][j].isWall())
return true;
return false;
}
public boolean ifLegal(int i, int j) {
if(ifautoPath(i,j-1)&&ifautoPath(i-1,j)&&ifautoPath(i-1,j))
return false;
if(ifautoPath(i,j+1)&&ifautoPath(i-1,j)&&ifautoPath(i-1,j+1))
return false;
if(ifautoPath(i,j-1)&&ifautoPath(i+1,j)&&ifautoPath(i+1,j-1))
return false;
if(ifautoPath(i,j+1)&&ifautoPath(i+1,j)&&ifautoPath(i+1,j+1))
return false;
return true;
}
public void pathByStart(int startXRow, int startYCol) {
setPath(startXRow,startYCol);
int num=200000;
int begin=0;
while (begin<num){
begin++;
int direction=RandomTool.randomNumber(1, 4);
if(direction==1){
if(ifRange(startXRow-1,startYCol)&&ifLegal(startXRow-1,startYCol)){
setBefore(startXRow,startYCol);
startXRow--;
setPath(startXRow,startYCol);
}
}
else if(direction==2){
if(ifRange(startXRow+1,startYCol)&&ifLegal(startXRow+1,startYCol)){
setBefore(startXRow,startYCol);
startXRow++;
setPath(startXRow,startYCol);
}
}
else if(direction==3){
if(ifRange(startXRow,startYCol-1)&&ifLegal(startXRow,startYCol-1)){
setBefore(startXRow,startYCol);
startYCol--;
setPath(startXRow,startYCol);
}
}
else if(direction==4){
if(ifRange(startXRow,startYCol+1)&&ifLegal(startXRow,startYCol+1)){
setBefore(startXRow,startYCol);
startYCol++;
setPath(startXRow,startYCol);
}
}
}
int c=2;
}
public void outT(int i, int j) {
System.out.println("i"+i+" j"+j);
}
public void autoPath(){
int startXRow=1;
int startYCol=1;
int num=200000;
int begin=0;
while (begin<num){
begin++;
int direction=RandomTool.randomNumber(1, 4);
if(direction==1){
if(ifRange(startXRow-1,startYCol)&&ifLegal(startXRow-1,startYCol)){
setBefore(startXRow,startYCol);
startXRow--;
setPath(startXRow,startYCol);
}
}
else if(direction==2){
if(ifRange(startXRow+1,startYCol)&&ifLegal(startXRow+1,startYCol)){
setBefore(startXRow,startYCol);
startXRow++;
setPath(startXRow,startYCol);
}
}
else if(direction==3){
if(ifRange(startXRow,startYCol-1)&&ifLegal(startXRow,startYCol-1)){
setBefore(startXRow,startYCol);
startYCol--;
setPath(startXRow,startYCol);
}
}
else if(direction==4){
if(ifRange(startXRow,startYCol+1)&&ifLegal(startXRow,startYCol+1)){
setBefore(startXRow,startYCol);
startYCol++;
setPath(startXRow,startYCol);
}
}
}
int c=2;
}
public boolean move(Role role){
int x=role.getX()+role.getMoveX();
int y=role.getY()+role.getMoveY();
int i,j;
for (i=0;i<row;i++){
for (j=0;j<col;j++){
Grid grid=grids[i][j];
if(grid.isWall()){
if(x+imageSize/2+4<=grid.getX()-mapSize/2){
}else if(grid.getX()+mapSize/2<=x-imageSize/2-4){
}else if(y+imageSize/2+4<=grid.getY()-mapSize/2){
}else if(grid.getY()+mapSize/2<=y-imageSize/2-4){
}else{
return false;
}
}
}
}
role.move();
return true;
}
public Grid[][] getGrids(){
return grids;
}
}