⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 squaremodel.java

📁 javahomework(source).rar
💻 JAVA
字号:
import java.io.*;
import java.util.*;

public  class SquareModel
{
	private static final int MAX_LIMITE=5;
	private static final int MIN_LIMITE=-5;
	public static final int EMPTY_ATTRIBUTE=0;
	public static final int PLAIN_ATTRIBUTE=1;
	public static final int PUNISH_ATTRIBUTE=2;
	public static final int PROTECT_ATTRIBUTE=3;
	private int activeS,//
			activeR,//
			activeY,//
			activeX;//
	private int score;
	private int punishLineNum;//
	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 void adjustBoard()
	{
		if(addLineNum<=0)
			return;
		for(int itr1=1;itr1<=boardHeight;itr1++)
		{
			if(boardHeight-itr1<addLineNum)
			{
				for(int itr2=1;itr2<=boardWidth;itr2++)
					board[itr2-1][itr1-1]=PLAIN_ATTRIBUTE;
				for(int itr3=1;itr3<=3;itr3++)
					board[Math.abs(generator.nextInt())%boardWidth][itr1-1]=EMPTY_ATTRIBUTE;
				continue;
			}
			for(int itr2=1;itr2<=boardWidth;itr2++)
				board[itr2-1][itr1-1]=board[itr2-1][itr1+addLineNum-1];
		}
		addLineNum=0;
	}
		
	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]!=EMPTY_ATTRIBUTE)
				return false;
		}
		return true;
	}
	
	private int scoreCalculate(int num)
	{
		if(num>=2)
			punishLineNum++;
		if(num==4)
			punishLineNum++;
		punishLineNum=punishLineNum<=MAX_LIMITE?punishLineNum:MAX_LIMITE;
		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;
			punishLineNum=0;
			addLineNum=0;
			isEnd=false;
			for(int itr1=1;itr1<=boardWidth;itr1++)
				for(int itr2=1;itr2<=boardHeight;itr2++)
					board[itr1-1][itr2-1]=EMPTY_ATTRIBUTE;
		}
	}
	
	public int getScore()
	{
		int tmp;
		synchronized(this)
		{
			tmp=score;
		}
		return tmp;
	}
	
	public int getPunishLineNum()
	{
		int tmp;
		synchronized(this)
		{
			tmp=punishLineNum;
			punishLineNum=0;
		}
		return tmp;
	}
	
	public int getProtectLineNum()
	{
		int tmp;
		synchronized(this)
		{
			tmp=addLineNum>0?0:-addLineNum;
		}
		return tmp;
	}
	
	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 int[][] getShadowSquare()
	{
		int[][] tmp=new int[4][2];
		int tmpI=0;
		boolean tmpB;
		synchronized(this)
		{
			int[][] square=squareModels[activeS][activeR];
			while(true)
			{
				activeY+=tmpI+1;
				tmpB=isValid();
				activeY-=tmpI+1;
				if(!tmpB)
					break;
				tmpI++;
			}
			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+tmpI;
			}
		}
		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]==EMPTY_ATTRIBUTE)
					{
						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;
			activeR=Math.abs(4+activeR-1)%4;
		}
		return false;
	}
	
	public boolean downOper()
	{
		synchronized(this)
		{
			activeY+=1;
			if(isValid())
				return true;
			activeY-=1;
		}
		return false;
	}
		
	public boolean leftOper()
	{
		synchronized(this)
		{
			activeX-=1;
			if(isValid())
				return true;
			activeX+=1;
		}
		return false;
	}
	
	public boolean rightOper()
	{
		synchronized(this)
		{
			activeX+=1;
			if(isValid())
				return true;
			activeX-=1;
		}
		return false;
	}
	
	public void spaceOper()
	{
		synchronized(this)
		{
			while(downOper());
			newActiveSquare();
		}
		return;
	}
	
	public boolean rmLineOper()
	{
		int[] newPos=new int[boardHeight];
		boolean[] scoreLine=getScoreLine();
		int num=0;
		synchronized(this)
		{
			for(int itr1=boardHeight,pos=boardHeight;itr1>=1;itr1--)
			{
				if(!scoreLine[itr1-1])
				{
					newPos[itr1-1]=pos;
					pos--;
					continue;
				}
				num++;
				newPos[itr1-1]=-1;
					for(int itr2=1;itr2<=boardWidth;itr2++)
						switch(board[itr2-1][itr1-1])
						{
							case PUNISH_ATTRIBUTE:
								punishLineNum++;
								punishLineNum=punishLineNum<MAX_LIMITE?punishLineNum:MAX_LIMITE;
								break;
							case PROTECT_ATTRIBUTE:
								addLineNum--;
								addLineNum=addLineNum>MIN_LIMITE?addLineNum:MIN_LIMITE;
								break;
						}
						
			}
		}
		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]=EMPTY_ATTRIBUTE;
			score+=scoreCalculate(num);
		}
		return true;
	}
	
	public void addLineOper(int addLineNum)
	{
		synchronized(this)
		{
			this.addLineNum+=addLineNum;
			int tmpI=this.addLineNum;
			boolean tmpB;
			activeY+=tmpI;
			tmpB=isValid();
			activeY-=tmpI;
			if(tmpB)
				adjustBoard();
		}
		
	}
		
	public boolean newActiveSquare()
	{
		if(isEnd())
			return !isEnd;
		int[][] tmp=getActiveSquare();
		int attribute=Math.abs(generator.nextInt())%3+1;
		synchronized(this)
		{
			for(int itr1=1;itr1<=4;itr1++)
				board[tmp[itr1-1][0]-1][tmp[itr1-1][1]-1]=attribute;
			adjustBoard();
			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 + -