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

📄 translation.java

📁 用jav语言开发的基于J2SE平台的中国象棋.供大家参考和学习
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
					pawns[k]=i;
					k++;
				}
			}
			if (c2!='+' && c2!='-'){
				if (player==0){
					srcX = 8-(c2 - '0' -1);
				}else{
					srcX = c2 -'0' - 1;
				}
				for (i=0;i<k;i++){
					if(srcX == ActiveBoard.FILE[pawns[i]]){
						break;//srcY=在外部
					}
				}
				srcY = ActiveBoard.RANK[pawns[i]];
			}else{
				boolean found=false;
				for (i=0;i<k && !found;i++){
					for(j=i+1;j<k;j++){			
						if(ActiveBoard.FILE[pawns[i]]==ActiveBoard.FILE[pawns[j]]){
							found = true;
							srcX1 = ActiveBoard.FILE[pawns[i]];
							srcX2 = ActiveBoard.FILE[pawns[j]];
							srcY1 = ActiveBoard.RANK[pawns[i]];
							srcY2 = ActiveBoard.RANK[pawns[j]];
							break;//srcX1,srcX2在外
						}
					}
				}
				if (c2=='+' && player==0 || c2=='-' && player==1){
					srcX = srcX2;srcY = srcY2;
				}else{
					srcX = srcX1;srcY = srcY1;
				}
			}
			//if(!found){
			//	for(int i=0;i<k;i++){
			//		int x = ChessPosition.FILE[pawns[i]];
			//		int y = 8 - (c2-'0'-1);
			//		if((piece==6 && ChessPosition.FILE[pawns[i]]==8 - (c2-'0'-1)) ||
			//			(piece==13 && ChessPosition.FILE[pawns[i]]==c2-'0'-1)){
			//			srcX1 = srcX2 =  ChessPosition.FILE[pawns[i]];
			//			srcY1 = srcY2 = ChessPosition.RANK[pawns[i]];
			//			break;
			//		}
			//	}
			//}
		}else{
			if (c2=='+' && player==0 || c2=='-' && player==1){
				srcX = srcX2;srcY = srcY2;
			}else if (c2=='-' && player==0 || c2=='+' && player==1){
				srcX = srcX1;srcY = srcY1;
			}else{
				if (player==0){
					srcX = 8-(c2 - '0' -1);
				}else{
					srcX = c2 -'0' - 1;
				}
				if (srcX == srcX1){
					srcY = srcY1;
				}else{
					srcY = srcY2;
				}
			}
		}
		switch (piece) {
			case 0://King
			case 4://Rook
			case 5://Cannon
			case 6://Pawn
				if(c3=='+'){
					dstX = srcX;dstY = srcY+c4-'0';
				}else if(c3=='-'){
					dstX = srcX;dstY = srcY-(c4-'0');
				}else{
					dstX = 8 - (c4 - '0' - 1) ; dstY = srcY;
				}
				break;
			case 1://Advisor
				dstX = 8 -(c4 - '0' -1);
				if(c3=='+'){
					if (srcX1==srcX2 && srcY1!=srcY2){
						srcY = srcY1;dstY = srcY1 + 1;
						if (dstY>2) {srcY = srcY2; dstY = srcY2 + 1;} 
					}else{
						dstY = srcY + 1;
					}
				}else{
					if (srcX1==srcX2 && srcY1!=srcY2){
						srcY = srcY1; dstY = srcY1 - 1;
						if (dstY<0) {srcY = srcY2; dstY = srcY2 - 1;} 
					}else{
						dstY = srcY - 1;
					}
				}
				break;
			case 2://Bishop				
				dstX = 8 -(c4 - '0' -1);
				if (c3=='+'){
					if (srcX1==srcX2 && srcY1!=srcY2){
						dstY = srcY1 + 2;srcY = srcY1; 
						if (dstY>4) {dstY = srcY2 + 2;srcY = srcY2; } 
					}else{
						dstY = srcY + 2;
					}
				}else{
					if (srcX1==srcX2 && srcY1!=srcY2){
						dstY = srcY1 - 2;srcY = srcY1; 
						if (dstY<0) {dstY = srcY2 + 2;srcY = srcY2; } 
					}else{
						dstY = srcY - 2;
					}
					dstY = srcY - 2;
				}
				break;
			case 3://Knight
				dstX = 8 - (c4 - '0' -1);
				if(c3=='+'){
					dstY = srcY + ((Math.abs(dstX - srcX)==1)?2:1);
				}else{
					dstY = srcY - ((Math.abs(dstX - srcX)==1)?2:1);
				}
				break;
			case 7://King
			case 11://Rook
			case 12://Cannon
			case 13://Pawn
				if(c3=='+'){
					dstX = srcX;dstY = srcY-(c4-'0');
				}else if(c3=='-'){
					dstX = srcX;dstY = srcY+(c4-'0');
				}else{
					dstX = c4 - '0' - 1 ; dstY = srcY;
				}
				break;
			case 8://Advisor在一条直线上不用前后表示
				dstX = c4 - '0' -1;
				if(c3=='+'){
					if (srcX1==srcX2 && srcY1!=srcY2){
						dstY = srcY1 - 1;srcY = srcY1; 
						if (dstY<7) {dstY = srcY2 - 1;srcY = srcY2; } 
					}else{
						dstY = srcY - 1;
					}
				}else{
					if (srcX1==srcX2 && srcY1!=srcY2){
						dstY = srcY1 + 1;srcY = srcY1; 
						if (dstY>9) {dstY = srcY2 + 1;srcY = srcY2; } 
					}else{
						dstY = srcY + 1;
					}
				}
				break;
			case 9://Bishop				
				dstX = c4 - '0' -1;
				if (c3=='+'){
					if (srcX1==srcX2 && srcY1!=srcY2){
						dstY = srcY1 - 2;srcY = srcY1; 
						if (dstY < 5) {dstY = srcY2 - 2; srcY = srcY2;} 
					}else{
						dstY = srcY - 2;
					}
				}else{
					if (srcX1==srcX2 && srcY1!=srcY2){
						dstY = srcY1 + 2;srcY = srcY1; 
						if (dstY > 9 ) {dstY = srcY2 + 2;srcY = srcY2; } 
					}else{
						dstY = srcY + 2;
					}
				}
				break;
			case 10://black Knight
				dstX = c4 - '0' -1;
				if(c3=='+'){
					dstY = srcY - ((Math.abs(dstX - srcX)==1)?2:1);
				}else{
					dstY = srcY + ((Math.abs(dstX - srcX)==1)?2:1);
				}
				break;
			default: return null;
		}
		return new MoveNode(srcX*10+srcY,dstX*10+dstY);
	}
	public File[] getFile(File path){
		File[] flist = path.listFiles(new FileFilter(){
			public boolean accept(File pathname) {
				if (pathname.getName().endsWith("QP")
					|| pathname.isDirectory())
					return true;
				else return false;
			}
		});
		return flist;
	}
	public int writeToBook(File f) throws IOException{
		RandomAccessFile inFile = new RandomAccessFile(f, "rw");
		ActiveBoard position = new ActiveBoard();
		position.loadFen(fenStr);
		int index = 0,value = 0;
		int step = 30;
		String tmpF, tmpM;
		MoveNode move;
		inFile.seek(inFile.length());
		int tmp = this.size();
		while(index<this.size() && value<20){//前8个回合
			move = this.getMoveStruct(index);
			if(move==null) break;
			tmpM = String.copyValueOf(move.location());
			tmpF = position.getFenStr();				
			position.movePiece(move);
			inFile.writeBytes(tmpM+" "+tmpF+newLine);
			index++;
			value++;
		}
		value = 8;
		if (result.equals("0-1")){
			while(index<this.size() && value<step){
				move = this.getMoveStruct(index);
				if(move==null) break;
				position.movePiece(move);
				index++;
				move = this.getMoveStruct(index);
				if(move==null) break;
				tmpM = String.copyValueOf(move.location());
				tmpF = position.getFenStr();
				inFile.writeBytes(tmpM+" "+tmpF+newLine);	
				position.movePiece(move);
				index++;
				value++;
			}
		}else if(result.equals("1-0")){
			while(index<this.size() && value<step){
				move = this.getMoveStruct(index);
				if(move==null) break;
				tmpM = String.copyValueOf(move.location());
				tmpF = position.getFenStr();				
				inFile.writeBytes(tmpM+" "+tmpF+newLine);
				position.movePiece(move);
				index++;
				move = this.getMoveStruct(index);
				if(move==null) break;
				position.movePiece(move);
				index++;
				value++;
			}			
		}
		inFile.close();
		return value;
	}

	public int getPreBook(){
		File f1 = new File("E:/websphere/ChineseChess/data/book.pre");
		File dir = new File("F:/ChessDoc/book");
		File flist[] = getFile(dir);
		int value = 0;
		for (int i=0;i<flist.length;i++){
			try {
				File tmpFile = flist[i];
				loadFromFile(flist[i]);
				value+=writeToBook(f1);
				
				if (i%200==0){
					System.out.println("File "+i+":"+ tmpFile+",Line:"+value);
				}
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return value;
	}
	public int sortBook(File srcFile,File dstFile, int LineNum) throws IOException{
		BufferedReader in = new BufferedReader(new FileReader(srcFile));
		BufferedWriter out = new BufferedWriter(new FileWriter(dstFile));
		String lineStr;
		int i=0,j=0,index;
		//int outLoop = LineNum/divid + 1;
		String allLine[] = new String[LineNum];
		int WriteLine=0;
		j=0;
		for(j=0;j<LineNum;j++){
			allLine[j]="";
		}
		//System.out.println("OutLoop:"+i);
			
		j=0;
		while((lineStr = in.readLine())!=null && j<LineNum){
			allLine[j]=lineStr.substring(4) + "#" + lineStr.substring(0,4);
			j++;
		}
		Arrays.sort(allLine,0,j);
		for(i=j-1;i>=0;){
			lineStr = allLine[i];			
			index = lineStr.indexOf("#");
			if (index>0){
				out.write(lineStr.substring(index+1) + " " + lineStr.substring(0,index)+ newLine);
				WriteLine++;
				for (int k = i - 1;k>=0;k--){
					if(allLine[k].equals(lineStr)) {
						i--;
					}else{
						break;
					}
				}
				i--;
			}else{
				i--;
			}
		}
		in.close();
		out.close();
		System.out.println("Total Line:" + WriteLine);
		return WriteLine;	
	}
	public void getBook(File srcFile,File dstFile,int maxSize) throws IOException{
		BufferedReader in = new BufferedReader(new FileReader(srcFile));
		BufferedWriter out = new BufferedWriter(new FileWriter(dstFile));
		String lineStr;
		int i=0,j=0,index;
		String allLine[] = new String[maxSize];
		int WriteLine=0;
		j=0;
		String mStr,fStr,mStr0,fStr0;
		int value = 1;
//
  		lineStr = in.readLine();
		mStr0=lineStr.substring(0,4);
		index=4;
		char ch = lineStr.charAt(index);
		while(ch==' ') {
			index++;
			ch = lineStr.charAt(index);
		}
		fStr0=lineStr.substring(index);
		out.write(mStr0 + " " + value + " " + fStr0+newLine);
		j++;

		while((lineStr = in.readLine())!=null && j<maxSize){
			mStr=lineStr.substring(0,4);
			index=4;
			ch = lineStr.charAt(index);
			while(ch==' ') {
				index++;
				ch = lineStr.charAt(index);
			}
			fStr=lineStr.substring(index);
			if (fStr.equals(fStr0)){
				out.write(mStr + " " + value + " " + fStr+newLine);
			}else{
				mStr0=mStr;fStr0=fStr;value=(value+1);//%5111;
				out.write(mStr + " " + value + " " + fStr+newLine);
			}
			j++;
		}
		out.close();
		in.close();
	}
	
	private void transForm(File src, File dst) throws IOException{
		BufferedReader in = new BufferedReader(new FileReader(src));
		BufferedWriter out = new BufferedWriter(new FileWriter(dst));
		String lineStr,mStr,fStr;
		int index;
		while((lineStr=in.readLine())!=null){
			mStr=lineStr.substring(0,4);
			index=5;
			while(lineStr.charAt(index)>='0' && lineStr.charAt(index)<='9'){
				index++;
			}
			fStr=lineStr.substring(index+1);
			out.write(mStr+" "+fStr+newLine);
		}
		out.close();
		in.close();
	}
	public static void main(String args[]) throws IOException{
		Translation ts = new Translation();
		File f1 = new File("E:/websphere/ChineseChess/data/book.txt");
		File f2 = new File("E:/websphere/ChineseChess/data/book.pre");
		File f3 = new File("E:/websphere/ChineseChess/data/book1.pre");
		File f4 = new File("E:/websphere/ChineseChess/save/testsave3");
		File f5 = new File("E:/websphere/ChineseChess/data/BOOK2.pre");
		File f6 = new File("E:/websphere/ChineseChess/data/book3.pre");
		int maxLine;
		//maxLine = ts.getPreBook();
		//System.out.println("MaxLine1: "+ maxLine);
		maxLine = ts.sortBook(f2,f3,150000);
		System.out.println("MaxLine2: "+ maxLine);
		//ts.getBook(f3,f1,maxLine);
		//ts.loadFromFile(f4);
		//ts.writeToBook(f3);
		//ts.transForm(f5,f6);
		System.out.println("test");		
	}
	/**
	 * @return
	 */
	public String getBlackName() {
		return blackName;
	}

	/**
	 * @return
	 */
	public String getBlackTeam() {
		return blackTeam;
	}

	/**
	 * @return
	 */
	public String getFenStr() {
		return fenStr;
	}

	/**
	 * @return
	 */
	public int getFirstBout() {
		return firstBout;
	}

	/**
	 * @return
	 */
	public String getGameDate() {
		return gameDate;
	}

	/**
	 * @return
	 */
	public String getGameEvent() {
		return gameEvent;
	}

	/**
	 * @return
	 */
	public String getGameRound() {
		return gameRound;
	}

	/**
	 * @return
	 */
	public String getGameSite() {
		return gameSite;
	}

	/**
	 * @return
	 */
	public String getRedName() {
		return redName;
	}

	/**
	 * @return
	 */
	public String getRedTeam() {
		return redTeam;
	}

	/**
	 * @return
	 */
	public String getResult() {
		return result;
	}

	public int size() {
		return moveStructList.size();
	}

	/**
	 * @param string
	 */
	public void setBlackName(String string) {
		blackName = string;
	}

	/**
	 * @param string
	 */
	public void setBlackTeam(String string) {
		blackTeam = string;
	}

	/**
	 * @param i
	 */
	public void setFirstBout(int i) {
		firstBout = i;
	}

	/**
	 * @param string
	 */
	public void setGameDate(String string) {
		gameDate = string;
	}

	/**
	 * @param string
	 */
	public void setGameEvent(String string) {
		gameEvent = string;
	}

	/**
	 * @param string
	 */
	public void setGameRound(String string) {
		gameRound = string;
	}

	/**
	 * @param string
	 */
	public void setGameSite(String string) {
		gameSite = string;
	}

	/**
	 * @param string
	 */
	public void setRedName(String string) {
		redName = string;
	}

	/**
	 * @param string
	 */
	public void setRedTeam(String string) {
		redTeam = string;
	}

	/**
	 * @param string
	 */
	public void setResult(String string) {
		result = string;
	}

}

⌨️ 快捷键说明

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