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

📄 chessboard.java

📁 一款JAVA款的跳棋
💻 JAVA
字号:
package org.yushang.jumpchess.pkg;

import java.util.Vector;

import org.yushang.jumpchess.pub.Director;
import org.yushang.jumpchess.pub.Map;
import org.yushang.jumpchess.pub.Node;
import org.yushang.jumpchess.pub.Position;
import org.yushang.jumpchess.strategy.Step;

public abstract class ChessBoard {
	public abstract Chess getChess(Position position);
	
	public abstract Chess getChess(int index);

	public abstract int getChessCount();
	
	public abstract Position getPosition(Chess chess);
	
	public abstract Position getPosition(int index);
	
	public abstract void Go(Chess chess, Position chessPosition);
	
	private Position CanJumpTo(Chess chess, Position chessPosition,
			Director direcotor) {
		Position re = new Position(chessPosition);
		int Empty = 0;
		do {
		    re = re.getJoint(direcotor);
			if (re == null) {
				return null;			
			}
			if ((this.getChess(re) != null) && (this.getChess(re) != chess)) {
				break;
			} else {
				Empty++;
			}
		} while (true);		
		
		do {
			re = re.getJoint(direcotor);
			if (re == null) {
				return null;			
			}
			if ((this.getChess(re) != null) && (this.getChess(re) != chess)) {
				return null;
			} else {
				Empty--;
			}			
		} while (Empty != -1);
	
		return re;		
	}
	
	private Position[] CanJumpTo(Chess chess, Position chessPosition) {
		Vector vector = new Vector();
		
		for (int j = 0; j < Director.directors.length; j++) {
			Position position = CanJumpTo(chess, chessPosition, Director.directors[j]);
			if (position == null) {
				continue;				
			}
			vector.add(position);
		}
	
		Position[] re = new Position[vector.size()];
		for (int i = 0; i < vector.size(); i++) {
			re[i] = (Position) vector.get(i);
		}
		return re;
	}
	
	private Position[] CanMoveTo(Chess chess, Position chessPosition) {
		Vector vector = new Vector();
				
		for (int j = 0; j < Director.directors.length; j++) {
			Position position = chessPosition.getJoint(Director.directors[j]);
			if (position == null) {
				continue;				
			}
			if ((this.getChess(position) != null) && (this.getChess(position) != chess)) {
				continue;
			}
			vector.add(position);
		}
				
		Position[] re = new Position[vector.size()];
		for (int i = 0; i < vector.size(); i++) {
			re[i] = (Position) vector.get(i);
		}
		return re;				
	}	
	
	public Map CanGo(Chess chess) {
		Map map = new Map();
		Position chessPosition = getPosition(chess);
		
		//加入初始位置的结点
		map.create(chessPosition, 1);		
		int nodeIndex = 0;
		
		//建立JumpTo的Map
		while (nodeIndex < map.size()) {
			Node node = map.get(nodeIndex);
			Position[] positions = CanJumpTo(chess, (Position) node.data);
			for (int i = 0, index = 0; i < positions.length; i++) {
				index = map.indexOfByData(positions[i]);
				Node subnode = null;
				if (index == -1) {
					subnode = map.create(positions[i], node.getStep() + 1);					
				} else {
					subnode = map.get(index);
				}
				
				node.JointTo(subnode);
			}
			nodeIndex++;
		}
		
       //建立Move的Map
		nodeIndex = 0;
		Node node = map.get(nodeIndex);
		Position[] positions = CanMoveTo(chess, chessPosition);
		for (int i = 0; i < positions.length; i++) {
			Node subnode = map.create(positions[i], node.getStep() + 1);
			node.JointTo(subnode);
			subnode.JointTo(node);
		}
		
		return map;
	}
	
	public Step[] getAllStep(Chess[] chesses) {
		Vector vector = new Vector();
		for (int i = 0; i < chesses.length; i++) {
			Map map = CanGo(chesses[i]);
			for (int j = 1; j < map.size(); j++) {
				vector.add(new Step(chesses[i], (Position) map.get(j).data));
			}			
		}

		Step[] steps = new Step[vector.size()];
		for (int i = 0; i < steps.length; i++) {
			steps[i] = (Step) vector.get(i);
		}
		
		return steps;
	}
	
}

⌨️ 快捷键说明

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