中国象棋1

使用Java语言编写中国象棋

一:象棋规则类

public class ChessRule {
	int[][] data = new int[10][9];
	public boolean rules(int gi, int gj,int si, int sj){
		int x = data[gi][gj];
		int y = data[si][sj];
		int start, end;
		System.out.println(x);
		System.out.println(y);
		//判断为何种棋子
		//車:只能走直线
		if(x == 1||x == 10){
			
			if(gi != si&&gj != sj)	return false;
			else if(gi == si){
				start = Math.min(gj, sj);
				end = Math.max(gj, sj);
				for(int m = 1; m < end - start; m++){
					if(data[gi][start+m] != 0)	return false;
				}
				return true;
			}
			else if(gj == sj){
				start = Math.min(gi, si);
				end = Math.max(gi, si);
				for(int m = 1; m < end - start; m++){
					if(data[start+m][gj] != 0)	return false;
				}
				return true; 
			}
			else return true;
		}
		//马:走日,且某个位置不可以有棋子
		else if(x == 2||x == 11){
			//下
			if(si - gi == 2&&Math.abs(gj-sj) == 1&&data[gi+1][gj] == 0)	return true;
			//上
			else if(gi - si == 2&&Math.abs(gj-sj) == 1&&data[gi-1][gj] == 0)	return true;
			//右
			else if(sj - gj == 2&&Math.abs(gi-si) == 1&&data[gi][gj+1] == 0)	return true;
			//左
			else if(gj - sj == 2&&Math.abs(gi-si) == 1&&data[gi][gj-1] == 0)	return true;
			//否则不可以走
			else return false;
		}
		//相:走田,且不能过河
		else if(x == 3||x == 12){
			//左上
			if(gi - si == 2&&gj - sj == 2&&data[gi-1][gj-1] == 0){
				
				if((x == 5&&si >= 5)||(x == 12&&si < 5))	return true;
				else return false;
			}
			//右上
			else if(gi - si == 2&&sj - gj == 2&&data[gi-1][gj+1] == 0){
				
				if((x == 5&&si >= 5)||(x == 12&&si < 5))	return true;
				else return false;
			}
			//左下
			else if(si - gi == 2&&gj - sj == 2&&data[gi+1][gj-1] == 0){
				
				if((x == 5&&si >= 5)||(x == 12&&si < 5))	return true;
				else return false;
			}
			//右下
			else if(si - gi == 2&&sj - gj == 2&&data[gi+1][gj+1] == 0){
				
				if((x == 5&&si >= 5)||(x == 12&&si < 5))	return true;
				else return false;
			}
			else return false;
		}
		//士:斜着走不能出田字格
		else if(x == 4||x == 13){
			
			if(Math.abs(gj-sj)==1&&Math.abs(gi-si)==1){
				
				if(x == 6&&si >= 7&&sj >= 3&&sj <= 5)	return true;
				else if(x == 13&&si <= 2&&sj >= 3&&sj <= 5)	return true;
				else return false;
			}
			else return false;
		}
		//将:不能出田字格且不能会面
		else if(x == 5||x == 14){
			
			if((Math.abs(gj-sj)==1&&gi - si ==0)||(gj - sj ==0&&Math.abs(gi-si)==1)){
				
				if(x == 7&&si >= 7&&sj >= 3&&sj <= 5)	return true;
				else if(x == 14&&si <= 2&&sj >= 3&&sj <= 5)	return true;
				else return false;
			}
			else return false;
			
		}
		//炮:隔一个
		else if(x == 6||x == 9){
			
			//若要吃棋子,必须中间有且只有一枚棋子
			if(x*y!=0){
				int t = 0;
				if(gi == si){
					for(int m = Math.min(gj, sj)+1; m < Math.max(gj, sj); m++){
						if(data[gi][m] != 0)	t++;
					}
				}
				else if(gj == sj){
					for(int m = Math.min(gi, si)+1; m < Math.max(gi, si); m++){
						if(data[m][gj] != 0)	t++;
					}
				}
				if(t == 1)	return true;
				
			}	
				
			//若为不吃棋子的情况,中间不可以有其他棋子
			else {
				int t = 0;
				if(gi == si){
					for(int m = Math.min(gj, sj)+1; m < Math.max(gj, sj); m++){
						if(data[gi][m] != 0)	t++;
					}
				}
				else if(gj == sj){
					for(int m = Math.min(gi, si)+1; m < Math.max(gi, si); m++){
						if(data[m][gi] != 0)	t++;
					}
				}
				if(t == 0) return true;
				else return false;
			}
		}
		//兵:不能后退,且过了河才可以左右移动
		else if(x == 7||x == 8){
			//判断是否过河
			if(x == 1){
				if(gi >=5){
					if(gi - si == 1&&gj == sj)	return true;
					else return false;
				}
				else if((gi - si == 1&&sj - gj == 0)||(gi - si == 0&&Math.abs(sj-gj) == 1))	return true;
				else return false;
			}
			else if(x == 8){
				if(gi <5){
					if(si - gi == 1&&gj == sj)	return true;
					else return false;
				}
				else if(((si - gi == 1&&sj - gj == 0))||(gi - si == 0&&Math.abs(sj-gj) == 1))	return true;
				else return false;
			}
			else return false;
		}
		
		return false;
	}
	public boolean meet(){
		int jiangi=0, jiangj=0, shuaii=0, shuaij=0, temp=0;
		for(int i=0; i<10; i++){
			for(int j=0; j<9; j++){
				if(data[i][j]==7){
					shuaii = i;
					shuaij = j;
				}
				else if(data[i][j]==14){
					jiangi = i;
					jiangj = j;
				}
			}
		}
		if(shuaij == jiangj){
			for(int i=jiangi+1; i<shuaii; i++){
				if(data[i][shuaij] != 0)	temp++;
			}
		}
		else return false;//没会面
		if(temp != 0)	return false;//没会面
		else return true;//会面了
	}
	}
  
    

二、获胜类
1.红方胜

public class Result {
	public void showresult(){
		JFrame red=new JFrame();
		red.setSize(100, 100);
		red.setTitle("游戏结束");
	    red.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    red.setLocationRelativeTo(null);//窗体居中显示
		FlowLayout layout=new FlowLayout();
		red.setLayout(layout);
		JLabel win=new JLabel("红方胜");
		win.setFont(new Font("宋体",Font.BOLD,30));
		 
		red.add(win);
		red.setVisible(true);
		win.setForeground(Color.RED);
	}

2.黑方胜

public class Resultblack {
	public void showresult(){
		JFrame red=new JFrame();
		red.setSize(100, 100);
		red.setTitle("游戏结束");
	    red.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    red.setLocationRelativeTo(null);//窗体居中显示
		FlowLayout layout=new FlowLayout();
		red.setLayout(layout);
		JLabel win=new JLabel("黑方胜");
		win.setFont(new Font("宋体",Font.BOLD,30));
		 
		red.add(win);
		red.setVisible(true);
		win.setForeground(Color.black);
	}
	}
匿名

发表评论

匿名网友