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

📄 position.java

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

public class Position {
	private int x;
	private int y;
	
	final static private int[][] pos = {
		       {5,5}, //1
		       {5,6}, //2
		       {5,7}, //3
		       {5,8}, //4
		       {1,13}, //5
		       {2,13}, //6
		       {3,13}, //7
		       {4,13}, //8
		       {5,13}, //9
		       {5,14}, //10
		       {5,15}, //11
		       {5,16}, //12
		       {5,17}, //13
		       {10,13}, //14
		       {11,13}, //15
		       {12,13}, //16
		       {13,13}, //17
    };
	
	public static boolean IsLegalPosition(int x, int y) {
		if ((x < 1) || (x > 17)) {
			return false;
		}
		if ((y < pos[x - 1][0]) || (y > pos[x - 1][1])) {
			return false;
		}
		return true;		
	}
	
	public Position(Position p) {
		x = p.x;
		y = p.y;
	}
	
	public Position(int x, int y) {
		if (!IsLegalPosition(x, y)) {
			throw new RuntimeException("Position(" + x + "," + y + ") is not Leagal");
		}
		this.x = x;
		this.y = y;
	}
	
	public boolean equals(Object o) {
		if (o == null) {
			return false;
		}
		
		if (o.getClass() != Position.class) {
			return false;
		}
		
		return ((this.x == ((Position) o).x) && (this.y == ((Position) o).y));
	}
	
	public int getx() {
		return x;
	}
	
	public int gety() {
		return y;
	}
	
	public void Set(int x, int y) {
		
		if (!IsLegalPosition(x, y)) {
			throw new RuntimeException("Position(" + x + "," + y + ") is not Leagal");
		}
		this.x = x;
		this.y = y;
	}
	
	public Position getJoint(Director director) {
		int x = 0;
		int y = 0;
		if (director == Director.UpLeft) {
			x = this.x;
			y = this.y + 1;
		} else if (director == Director.UpRight) {
			x = this.x + 1;
			y = this.y + 1;
		} else if (director == Director.Left) {
			x = this.x - 1;
			y = this.y;
		} else if (director == Director.Right) {
			x = this.x + 1;
			y = this.y;
		} else if (director == Director.DownLeft) {
			x = this.x - 1;
			y = this.y - 1;
		} else if (director == Director.DownRight) {
			x = this.x;
			y = this.y - 1;
		}
		
		if (IsLegalPosition(x, y)) {
			return new Position(x, y);
		} else {
			return null;	
		}		
				
	}
	
	public int getDistance(Position position) {
		int D1 = Math.abs(this.x - position.x) + Math.abs(this.y - position.y);
		int D2 = Math.abs(this.x - position.x) + Math.abs(this.y - (this.x - position.x) - position.y);
		int D3 = Math.abs(this.y - position.y) + Math.abs(this.x - (this.y - position.y) - position.x);
		
		return Math.min(Math.min(D1, D2), D3);		
	}
	
	public String toString() {
		return "Position[" + x + "," + y + "]";
	}
}

⌨️ 快捷键说明

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