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

📄 jumpchesscontrol.java

📁 一款JAVA款的跳棋
💻 JAVA
字号:
package org.yushang.jumpchess.Interface;
// Download:http://www.codefans.net

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import org.yushang.jumpchess.audio.WavePlayer;
import org.yushang.jumpchess.pkg.*;
import org.yushang.jumpchess.pub.BoardArea;
import org.yushang.jumpchess.pub.Color;
import org.yushang.jumpchess.pub.Position;
import org.yushang.jumpchess.strategy.MainStrategy;
import org.yushang.jumpchess.strategy.Strategy;

public class JumpChessControl {
	
	private Drawer drawer = null;
	
	private JumpChess jumpchess = null;	
	private boolean debug = false;
	private Animation animation = null;
	private Chess GoChess = null;
	private boolean LockMouse = false;
	
	private Canvas canvas = null;
	private String Over = "";
	private Position mousePosition = null;
	private Player oldPlayer = null;	
	
	private final static int TIMER = 15;
	
	//Debug
	private Chess DebugChess = null;
	
	public JumpChessControl(Shell shell, Canvas canvas) {		
		drawer = new Drawer(shell.getDisplay());
		this.canvas = canvas;
		addListener();
		LockMouse = true;
		//Initialize();
	}	
	
	private PlayerInfo[] createPlayerInfos(String[] names, int[] types, int Count) {
		PlayerInfo[] playerInfos = new PlayerInfo[Count];
		BoardArea[] areas = BoardArea.AllocateArea(Count);
		
		for (int i = 0; i < Count; i++) {
			String n = names[i];
			Color c = Color.getColor(areas[i].Index());
			boolean is = (types[i] == 0);
			
			playerInfos[i] = new PlayerInfo(n, c, is, areas[i]);
		}
		           
		return playerInfos;
	}
	
	public void Initialize(String[] names, int[] types, int Count) {
		PlayerInfo[] playerInfos = createPlayerInfos(names, types, Count);
		jumpchess = new JumpChess(playerInfos);
		
		//设置敌对玩家
		for (int i = 0; i < playerInfos.length - 1; i++) {
			jumpchess.getPlayByIndex(i).setOppsite(jumpchess.getPlayByIndex(i + 1));
		}
		jumpchess.getPlayByIndex(playerInfos.length - 1).setOppsite(jumpchess.getPlayByIndex(0));
		
		Over = "";
		LockMouse = false;
		canvas.redraw();
		animation = null;
	}
	
	public void Debug() {
		debug = !debug;
		LockMouse = false;
		canvas.redraw();
	}
	
	private void addListener() {
		canvas.addListener(SWT.Paint, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event event) {				
				Rectangle rect = canvas.getClientArea();
				Image buffer = new Image(canvas.getDisplay(), rect);
				GC gc = new GC(buffer);
				
				Draw(gc);

				gc.dispose();
				event.gc.drawImage(buffer, 0, 0);		
				buffer.dispose();
			}
		});	
		
		canvas.addListener(SWT.MouseDown, new Listener() {
			public void handleEvent(Event event) {
				MouseEvent(event.button,
						event.x, event.y);
				
			}		
		});
		
		canvas.addListener(SWT.MouseMove, new Listener() {
			public void handleEvent(Event event) {
				mousePosition = drawer.MouseXYToPosition(new Point(event.x, event.y));
			}
		});
	}
	
	public void BeginAnimation() {		
		if (animation != null) {
			startAnimationTimer();
		}
	}
	
	public boolean RunAnimation() {		
		canvas.redraw();
		if (animation != null) { 
			return animation.Run();
		} else {
			return false;
		}
	}
	
	public void EndAnimation() {
		//动画完成了		
		animation = null;
		this.GoChess = null;					
		LockMouse = false;
		if (!Wined()) {		
			PlayerGO(Player.MouseNull, null);
		} else {
			WavePlayer wavePlayer = new WavePlayer();
			if (jumpchess.getCurentPlay().getClass() == Man.class) {
				wavePlayer.Play(WavePlayer.WAVEWIN);
			} else {
				wavePlayer.Play(WavePlayer.WAVELOST);
			}	
		}
	}
	
	void startAnimationTimer() {
		final Display display = Display.getCurrent();
		display.timerExec(TIMER, new Runnable() {
			public void run() {
				if (RunAnimation()) {
					display.timerExec(TIMER, this);
				} else {
					EndAnimation();
				}
					
			}
		});
	}	
	
	private void DrawDebug(GC gc) {		
		//画棋盘
		drawer.DrawChessBoard(gc);
		if (jumpchess == null) return;
		
		//画一般的棋子
		for (int i = 0; i < jumpchess.getChessBoard().getChessCount(); i++) {
			Chess chess = jumpchess.getChessBoard().getChess(i);
			drawer.DrawChess(gc, chess.GetColor(), 
					jumpchess.getChessBoard().getPosition(chess), - 1);
		}
		
		if (DebugChess != null) {
			drawer.DrawPosition(gc, jumpchess.getChessBoard().getPosition(DebugChess));
		}		
	}
	
	private void DrawCommonChess(GC gc) {		
		for (int i = 0; i < jumpchess.getChessBoard().getChessCount(); i++) {
			Chess chess = jumpchess.getChessBoard().getChess(i);
			if (chess != this.GoChess) {
				drawer.DrawChess(gc, chess.GetColor(), 
						jumpchess.getChessBoard().getPosition(chess), -1);
			}
		}
	}
	
	private void DrawCanGo(GC gc) {
		//画可下位置
		if (jumpchess.getCurentPlay().getClass() != Man.class) {
			return ;
		}
		
		Position[] CanGo = ((Man) jumpchess.getCurentPlay()).getCanGo();
		if (CanGo != null) {
			for (int i = 0; i < CanGo.length; i++) {
				drawer.DrawPosition(gc, CanGo[i]);	
			}
		}
	}
	
	private void DrawWinText(GC gc) {
		if (!Over.equals("")) {			
			final Font font = new Font(canvas.getDisplay(), "宋体", 72, SWT.BOLD);
			gc.setFont(font);
			gc.setForeground(canvas.getDisplay().getSystemColor(SWT.COLOR_DARK_GREEN));
			gc.drawText(Over, 200, 200, true);
			
			font.dispose();
		}
		
		
		Player player = null;
		if ((animation != null) && (animation.getClass() == AnimationGo.class)) {
			player = oldPlayer;
		} else {
			player = jumpchess.getCurentPlay();
		}
		if (player == null) return;
		//显示谁下棋
		drawer.DrawChess(gc, player.GetColor(), new Point(30, 50), -5);		
		final Font font = new Font(canvas.getDisplay(), "宋体", 12, SWT.BOLD);
		gc.setFont(font);
		gc.setForeground(canvas.getDisplay().getSystemColor(SWT.COLOR_WHITE));		
		gc.drawText(player.getName(), 45, 44, true);
		font.dispose();
	}
	
	public void Draw(GC gc) {
		if (debug) {
			DrawDebug(gc);
		} else {
			//画棋盘
			drawer.DrawChessBoard(gc);
			if (jumpchess == null) return;
				
			//画一般的棋子
			DrawCommonChess(gc);
			
			//
			DrawCanGo(gc);
			
			//画动画
			if(animation != null) {
				animation.Draw(gc);
			}
			
			DrawWinText(gc);
			//drawer.DrawCoordinate(gc, mousePosition);			
		}
	}
	
	private void MouseEventDebug(int Button, int x, int y) {
		Position position = drawer.MouseXYToPosition(new Point(x, y));
		if (DebugChess == null) {				
			DebugChess = jumpchess.getChessBoard().getChess(position);
		} else {
			if (jumpchess.getChessBoard().getChess(position) == null) {
				jumpchess.getChessBoard().Go(DebugChess, position);
				Strategy strategy = new MainStrategy(jumpchess.getChessBoard(), jumpchess.getPlayByIndex(1));
				System.out.println("Score:" + strategy.CalculateScore());
				DebugChess = null;
			}
		}		
	}
	
	public void MouseEvent(int Button, int x ,int y) {
		if (LockMouse) {
			return;
		}

		if (debug) {
			MouseEventDebug(Button, x , y);
			canvas.redraw();
		} else {
			int Mouse = (Button == 1) ? Player.MouseClick : Player.MouseRight;
			Position position = drawer.MouseXYToPosition(new Point(x, y));
			PlayerGO(Mouse, position);
			
			canvas.redraw();
		}
	}
	
	private boolean Wined () {
		if (jumpchess.getCurentPlay().Winned()) {
			Over = jumpchess.getCurentPlay().getName() + "赢了";
			LockMouse = true;
			canvas.redraw();
					
			return true;

		} else {
			Over = "";
			canvas.redraw();
			return false;
		}
	}
	
	private void PlayerGO(int Mouse, Position position) {
		boolean oldAnimationIsNull = (animation == null);

		if (jumpchess.getCurentPlay().Run(Mouse, position)) {			
			GoChess = jumpchess.getCurentPlay().getGoChess();
			animation = new AnimationGo(drawer, GoChess, 
					jumpchess.getCurentPlay().getPath());
			if (oldAnimationIsNull) {
				BeginAnimation();
			}
			LockMouse = true;
			if (!Wined()) {
				oldPlayer = jumpchess.getCurentPlay();
				jumpchess.getNextPlay();
			}
			return;
		}		

		if (GoChess == jumpchess.getCurentPlay().getGoChess()) {
			return;
		}		
		GoChess = jumpchess.getCurentPlay().getGoChess();
		
		if (GoChess == null) {
			animation = null;
		} else {
			Point point = drawer.PositionToMouseXY(jumpchess.getChessBoard().getPosition(GoChess));
			animation = new AnimationSelect(drawer, GoChess, point);
			if (oldAnimationIsNull) {
				BeginAnimation();
			}
		}
		
	}
	
	

}

⌨️ 快捷键说明

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