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

📄 piecesselect.java

📁 一个简单的Java Swing 游戏Blokus~方块圈地~Eclipse3.0 Project
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
					return false;
			}
			else if(i == 0 && j < 20 && j > 0 && 
					(boardT[i + 1][j - 1] == color || boardT[i + 1][j + 1] == color))
				return true;
			else if(i == 20 && j < 20 && j > 0 &&
					(boardT[i - 1][j - 1] == color || boardT[i - 1][j + 1] == color))
				return true;
			else if(j == 0 && i < 20 && i > 0 &&
					(boardT[i - 1][j + 1] == color || boardT[i + 1][j + 1] == color))
				return true;
			else if(j == 20 && i < 20 && i > 0 &&
					(boardT[i - 1][j - 1] == color || boardT[i + 1][j - 1] == color))
				return true;
			else if(j < 20 && j > 0 && i < 20 && i > 0 &&
					(boardT[i - 1][j - 1] == color || boardT[i + 1][j - 1] == color) ||
					boardT[i - 1][j + 1] == color || boardT[i + 1][j + 1] == color)
				return true;
			else
				return false;
		}catch(Exception e){
			return false;
		}
	}
	
	//自动情况下不能放下棋子的情况
	private boolean canNotBePutAuto(Pieces p, int[][] boardT, int i, int j){
		//boolean can = false;
		int color = p.getpColor();
		try{
			if(i == 0 && j < 20 && j > 0 && 
					(boardT[i][j - 1] == color || boardT[i][j + 1] == color ||
							boardT[i + 1][j] == color))
				return false;
			else if(i == 20 && j < 20 && j > 0 &&
					(boardT[i][j - 1] == color || boardT[i][j + 1] == color ||
							boardT[i - 1][j] == color))
				return false;
			else if(j == 0 && i < 20 && i > 0 &&
					(boardT[i - 1][j] == color || boardT[i + 1][j] == color ||
							boardT[i][j + 1] == color))
				return false;
			else if(j == 20 && i < 20 && i > 0 &&
					(boardT[i - 1][j] == color || boardT[i + 1][j] == color ||
							boardT[i][j + 1] == color))
				return false;
			else if(j < 20 && j > 0 && i < 20 && i > 0 &&
					(boardT[i][j + 1] == color || boardT[i - 1][j] == color ||
					boardT[i][j - 1] == color || boardT[i + 1][j] == color))
				return false;
			else
				return true;
		}catch(Exception e){
			return false;
		}
	}
	
	//生成缓存棋子矩阵
	private int[][] makeTempMatrix(Pieces p, int type){
		int[][] gridT;
		int[][] tempG; 
		int s,minR,minC;
		int color = 0;
		s = p.getScore();
		//System.out.println("S:" + s);
		gridT = new int[s][s];
		tempG = new int[s][s];
		switch(p.getpColor()){
			case 1: color = 1;
				break;
			case 2: color = 2;
				break;
			case 3: color = 3;
				break;
			case 4: color = 4;
				break;
			default: color = 0;
				break;
		}
		
		switch(type){
			case 0:{
				for(int i = 0; i < s; i ++)
					for(int j = 0; j < s; j ++){
						gridT[i][j] = p.getGrid()[i][j];
					}
			}
				break;
			case 1:{
				for(int i = 0; i < s; i ++)
					for(int j = 0; j < s; j ++){
						gridT[i][j] = 0;
						if(p.getGrid()[i][j] == 1)
							tempG[j][s - 1 - i] = 1;
						else
							tempG[j][s - 1 - i] = 0;
					}
				minR = minRow(tempG,1);
				minC = minColum(tempG,1);
				for(int i = 0; i < s; i ++)
					for(int j = 0; j < s; j ++){
						if(tempG[i][j] == 1)
							gridT[i - minR][j - minC] = 1;
					}
			}
				break;
			case 2:{
				for(int i = 0; i < s; i ++)
					for(int j = 0; j < s; j ++){
						gridT[i][j] = 0;
						if(p.getGrid()[i][j] == 1)
							tempG[s - 1 - i][s - 1 - j] = 1;
						else
							tempG[s - 1 - i][s - 1 - j] = 0;
					}
				minR = minRow(tempG,1);
				minC = minColum(tempG,1);
				for(int i = 0; i < s; i ++)
					for(int j = 0; j < s; j ++){
						if(tempG[i][j] == 1)
							gridT[i - minR][j - minC] = 1;
					}
			}
				break;
			case 3:{
				for(int i = 0; i < s; i ++)
					for(int j = 0; j < s; j ++){
						gridT[i][j] = 0;
						if(p.getGrid()[i][j] == 1)
							tempG[s - 1 - j][i] = 1;
						else
							tempG[s - 1 - j][i] = 0;
					}
				minR = minRow(tempG,1);
				minC = minColum(tempG,1);
				for(int i = 0; i < s; i ++)
					for(int j = 0; j < s; j ++){
						if(tempG[i][j] == 1)
							gridT[i - minR][j - minC] = 1;
					}
			}
				break;
			case 4:{
				for(int i = 0; i < s; i ++)
					for(int j = 0; j < s; j ++){
						gridT[i][j] = 0;
 						if(p.getGrid()[i][j] == 1)
							tempG[i][s - 1 - j] = 1;
						else
							tempG[i][s - 1 - j] = 0;
					}
				minR = minRow(tempG,1);
				minC = minColum(tempG,1);
				for(int i = 0; i < s; i ++)
					for(int j = 0; j < s; j ++){
						if(tempG[i][j] == 1)
							gridT[i - minR][j - minC] = 1;
					}
				break;
			}
			case 5:{
				for(int i = 0; i < s; i ++)
					for(int j = 0; j < s; j ++){
						gridT[i][j] = 0;
						if(p.getGrid()[i][j] == 1)
							tempG[s - 1 - i][j] = 1;
						else
							tempG[s - 1 - i][j] = 0;
					}
				minR = minRow(tempG,1);
				minC = minColum(tempG,1);
				for(int i = 0; i < s; i ++)
					for(int j = 0; j < s; j ++){
						if(tempG[i][j] == 1)
							gridT[i - minR][j - minC] = 1;
					}
			}
			default: for(int i = 0; i < s; i ++)
						 for(int j = 0; j < s; j ++)
							 gridT[i][j] = p.getGrid()[i][j];
				break;
		}
		
		return gridT;
	}
	
	//计算最小行数
	private int minRow(int[][] a, int color){
		for(int i = 0; i < a.length; i ++)
			for(int j = 0; j < a.length; j ++)
				if(a[i][j] == color){
					return i;
				}
		return a.length - 1;
	} 
	
	//计算最小列数
	private int minColum(int[][] a, int color){
		for(int i = 0; i < a.length; i ++)
			for(int j = 0; j < a.length; j ++)
				if(a[j][i] == color){
					return i;
				}
		return a.length - 1;
	}
	
	//计算最大行数
	private int maxRow(int[][] a, int color){
		int m = 0;
		for(int i = 0; i < a.length; i ++)
			for(int j = 0; j < a.length; j ++)
				if(a[i][j] == color){
					m = i;
				}
		return m;
	}
	
	//计算最大列数
	private int maxColum(int[][] a, int color){
		int m = 0;
		for(int i = 0; i < a.length; i ++)
			for(int j = 0; j < a.length; j ++)
				if(a[j][i] == color){
					m = i;
				}
		return m;
	}
	
	//创建矩阵
	private int[][] makeMatrix(int[][] a){
		int[][] m = new int[a.length][a.length];
		for(int i = 0; i < a.length; i ++)
			for(int j = 0; j < a.length; j ++)
				m[i][j] = 0;
		
		return m;
	}
	
	
	public void setBlueC(boolean b){
		this.blueC = b;
	}
	
	public void setYellowC(boolean b){
		this.yellowC = b;
	}
	
	public void setRedC(boolean b){
		this.redC = b;
	}
	
	public void setGreenC(boolean b){
		this.greenC = b;
	}
	
	//重写Run函数
	public void run(){
		//System.out.println("new thread");
		while(end == false){
			if(skipB == false){
				if(blueC == true)
					autoRun(1);
				else
					while(turn == 1){
						try{
							startGame.sleep(500);
							}
						catch(InterruptedException ie){
							
						}
					}
				game.repaint();
				
			}
			else{
				turn = 2;
				game.setYl(180);
				game.repaint();
			}
			
			if(skipY == false){
				if(yellowC == true)
					autoRun(2);
				else
					while(turn == 2)
						try{
							startGame.sleep(500);
							}
						catch(InterruptedException ie){
							
						}
				game.repaint();
				
			}
			else{
				turn = 3;
				game.setYl(360);
				game.repaint();
			}
			if(skipR == false){
				if(redC == true)
					autoRun(3);
				else
					while(turn == 3)
						try{
							startGame.sleep(500);
							}
						catch(InterruptedException ie){
							
						}
				game.repaint();
				
			}
			else{
				turn = 4;
				game.setYl(540);
				game.repaint();
			}
		
			if(skipG == false){
				if(greenC == true)
					autoRun(4);
				else
					while(turn == 4)
						try{
							startGame.sleep(500);
							}
						catch(InterruptedException ie){
							
						}
				game.repaint();
				
			}
			else{
				turn = 1;
				game.setYl(0);
				game.repaint();
			}
		}
		if(end == true){			
			start = false;
			game.setStartG(false);
			CreateDialog endD = new CreateDialog('d', game, this);
			game.repaint();
		}
	}
	
	public boolean getBlueC(){
		return blueC;
	}
	
	public boolean getYellowC(){
		return yellowC;
	}
	
	public boolean getRedC(){
		return redC;
	}
	
	public boolean getGreenC(){
		return greenC;
	}
	
	public void setGameType(int t){
		this.gametype = t;
	}
	
	public int getGameType(){
		return gametype;
	}
	
	public int getPlayMode(){
		return this.playMode;
	}
	
	public void setPlayMode(int pm){
		this.playMode = pm;
	}
	//初始化函数
	public void init(){
		turn = 1;
		start = false;
		onMouse = false;
		canPut = false;
		finishP = false;
		blueS = 0; yellowS = 0; redS = 0; greenS = 0; 
		skipB = false; skipY = false; skipR = false; skipG = false;
		blueC = false; yellowC = false; redC = false; greenC = false;
		end =false;
		a1 = 1;
		a2 = 2;
		a3[0] = 3; a3[1] = 4;
		a4[0] = 5; a4[1] = 6; a4[2] = 7; a4[3] = 8; a4[4] = 9;
		for(int i = 0; i < 12; i ++)
			a5[i] = 10 + i;
		c1[0] = 0; c1[1] = 1; c1[2] = 2; c1[3] = 3; c1[4] = 4; c1[5] = 5;
		gametype = 0;
		game.init();
	}
	
	public void keyPressed(KeyEvent e){
		int keyCode = e.getKeyCode();
		
		//System.out.println("Key Pressed");
		if(keyCode == KeyEvent.VK_SPACE && onMouse == true){
			Pieces t = this.temp;
			int[][] gr = t.getGrid();
			int s = t.getScore();
			int[][] tr = new int[s][s];
			for(int i = 0; i < s; i ++)
				for(int j = 0; j < s; j ++)
					tr[j][s - 1 - i] = gr[i][j];
			t.setGrid(tr);
			game.setPx(game.getKeyX());
			game.setPy(game.getKeyY());
			
			game.setTemp(t);
			game.setCir(1);
			game.repaint();
		}else if(keyCode == KeyEvent.VK_ENTER && onMouse == true){
			Pieces t = this.temp;
			int[][] gr = t.getGrid();
			int s = t.getScore();
			int[][] tr = new int[s][s];
			for(int i = 0; i < s; i ++)
				for(int j = 0; j < s; j ++)
					tr[i][j] = 0;
			for(int i = 0; i < s; i ++)
				for(int j = 0; j < s; j ++)
					if(gr[i][j] == 1) 
						tr[i][s - 1 - j] = gr [i][j];
			t.setGrid(tr);
				
			//System.out.println();
			game.setPx(game.getKeyX());
			game.setPy(game.getKeyY());
			game.setTemp(t);
			game.setCir(1);
			game.repaint();
		}else if(keyCode == KeyEvent.VK_ESCAPE && onMouse == false){
			CreateDialog exitD = new CreateDialog('e');
		}else if(keyCode == KeyEvent.VK_F1){
			CreateDialog helpD = new CreateDialog('h');
		}
	}
	
	public void keyTyped(KeyEvent e){
		
	}
	
	public void keyReleased(KeyEvent e){
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -