📄 squaremodel(backup).txt
字号:
import java.io.*;
import java.util.*;
public class SquareModel
{
private int activeS,//
activeR,//
activeY,//
activeX;//
private int score;//
private int addLineNum;//
private static int boardHeight,
boardWidth;
private boolean isEnd;
private int[][] board;//
private static int[][][][] squareModels;////种类、(转动)形态、块号、纵/横坐标
private static Random generator;
private void init(int boardWidth,int boardHeight)
throws FileNotFoundException,IOException
{
System.out.println("Begin to init 'SquareModel'...");
this.boardHeight=boardHeight;
this.boardWidth=boardWidth;
board=new int[boardWidth][boardHeight];
squareModels=new int[7][4][4][2];
generator=new Random(new Date().getTime());
System.out.println("Begin to load initial data...");
BufferedReader tmpIn=
new BufferedReader(new InputStreamReader(new FileInputStream("Set0.txt")));
for(int itr1=1;itr1<=7;itr1++)
for(int itr2=1;itr2<=4;itr2++)
{
int[][] map=new int[4][4];
for(int itr3=1;itr3<=4;itr3++)
{
int tmpX=Integer.valueOf(tmpIn.readLine().replaceAll(" ",""));
int tmpY=Integer.valueOf(tmpIn.readLine().replaceAll(" ",""));
squareModels[itr1-1][itr2-1][itr3-1][0]=tmpX;
squareModels[itr1-1][itr2-1][itr3-1][1]=tmpY;
map[tmpX+1][tmpY+1]=1;
}
for(int itr3=1;itr3<=4;itr3++)
{
for(int itr4=1;itr4<=4;itr4++)
System.out.print(map[itr3-1][itr4-1]);
System.out.print("\n");
}
System.out.print("\n");
}
tmpIn.close();
}
private boolean isValid()
{
int[][] tmp=getActiveSquare();
for(int itr1=1;itr1<=4;itr1++)
{
if(tmp[itr1-1][0]>boardWidth||tmp[itr1-1][0]<1||
tmp[itr1-1][1]>boardHeight||tmp[itr1-1][1]<1)
return false;
if(board[tmp[itr1-1][0]-1][tmp[itr1-1][1]-1]!=0)
return false;
}
return true;
}
private int scoreCalculate(int num)
{
return num;
}
public SquareModel(int boardWidth,int boardHeight)
{
try{
init(boardWidth,boardHeight);
reset();
}catch(Exception e)
{
e.printStackTrace();
}
}
public void reset()
{
System.out.println("Begin to reset 'SquareModel'...");
synchronized(this)
{
activeS=Math.abs(generator.nextInt())%7;//
activeR=0;
activeX=2;
activeY=2;
score=0;
addLineNum=0;
isEnd=false;
for(int itr1=1;itr1<=boardWidth;itr1++)
for(int itr2=1;itr2<=boardHeight;itr2++)
board[itr1-1][itr2-1]=0;
}
}
public int getBoardHeight()
{
return boardHeight;
}
public int getBoardWidth()
{
return boardWidth;
}
public int[][] getBoard()
{
int[][] boardCopy=new int[boardWidth][boardHeight];
synchronized(this)
{
for(int itr1=1;itr1<=boardWidth;itr1++)
for(int itr2=1;itr2<=boardHeight;itr2++)
boardCopy[itr1-1][itr2-1]=board[itr1-1][itr2-1];
}
return boardCopy;
}
public int[][] getActiveSquare()
{
int[][] tmp=new int[4][2];
synchronized(this)
{
int[][] square=squareModels[activeS][activeR];
for(int itr1=1;itr1<=4;itr1++)
{
tmp[itr1-1][0]=square[itr1-1][0]+activeX;
tmp[itr1-1][1]=square[itr1-1][1]+activeY;
}
}
return tmp;
}
public boolean[] getScoreLine()
{
boolean[] scoreLine=new boolean[boardHeight];
synchronized(this)
{
for(int itr1=1;itr1<=boardHeight;itr1++)
{
boolean tmp=true;
for(int itr2=1;itr2<=boardWidth;itr2++)
if(board[itr2-1][itr1-1]==0)
{
tmp=false;
break;
}
scoreLine[itr1-1]=tmp;
}
}
return scoreLine;
}
public boolean isEnd()
{
return isEnd;
}
public boolean rotateOper()
{
synchronized(this){
activeR=Math.abs(activeR+1)%4;}
if(isValid())
return true;
synchronized(this){
activeR=Math.abs(4+activeR-1)%4;}
return false;
}
public boolean downOper()
{
synchronized(this){
activeY+=1;}
if(isValid())
return true;
synchronized(this){
activeY-=1;}
return false;
}
public boolean leftOper()
{
synchronized(this){
activeX-=1;}
if(isValid())
return true;
synchronized(this){
activeX+=1;}
return false;
}
public boolean rightOper()
{
synchronized(this){
activeX+=1;}
if(isValid())
return true;
synchronized(this){
activeX-=1;}
return false;
}
public boolean rmLineOper()
{
int[] newPos=new int[boardHeight];
boolean[] scoreLine=getScoreLine();
int num=0;
for(int itr1=boardHeight,pos=boardHeight;itr1>=1;itr1--)
{
if(!scoreLine[itr1-1])
{
newPos[itr1-1]=pos;
pos--;
continue;
}
num++;
newPos[itr1-1]=-1;
}
if(num==0)
return false;
//System.out.println("it Works!!");
for(int itr1=1;itr1<=boardHeight;itr1++)
System.out.print(":"+itr1+","+newPos[itr1-1]);
System.out.println("");
synchronized(this)
{
for(int itr1=boardHeight;itr1>=1;itr1--)
if(newPos[itr1-1]!=-1)
for(int itr2=1;itr2<=boardWidth;itr2++)
board[itr2-1][newPos[itr1-1]-1]=board[itr2-1][itr1-1];
for(int itr1=1;itr1<=num;itr1++)
for(int itr2=1;itr2<=boardWidth;itr2++)
board[itr2-1][itr1-1]=0;
score+=scoreCalculate(num);
}
return true;
}
public void addLineOper(int addLineNum)
{
synchronized(this){
this.addLineNum+=addLineNum;}
}
public boolean newActiveSquare()
{
if(isEnd())
return !isEnd;
int[][] tmp=getActiveSquare();
synchronized(this)
{
for(int itr1=1;itr1<=4;itr1++)
board[tmp[itr1-1][0]-1][tmp[itr1-1][1]-1]=1;
for(int itr1=1;itr1<=boardHeight;itr1++)
{
if(boardHeight-itr1<addLineNum)
{
for(int itr2=1;itr2<=boardWidth;itr2++)
board[itr2-1][itr1-1]=1;
for(int itr3=1;itr3<=3;itr3++)
board[Math.abs(generator.nextInt())%boardWidth][itr1-1]=0;
continue;
}
for(int itr2=1;itr2<=boardWidth;itr2++)
board[itr2-1][itr1-1]=board[itr2-1][itr1+addLineNum-1];
}
addLineNum=0;
activeS=Math.abs(generator.nextInt())%7;//
activeR=1;
activeX=2;
activeY=2;
}
if(!isValid())
isEnd=true;
return !isEnd;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -