`
nbtlxx
  • 浏览: 253643 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
社区版块
存档分类
最新评论

手机网络象棋开发(6):Mycanvas及共用类介绍

阅读更多
MyCancas类比较复杂
主要功能包括:
1. 游戏启动,
2. 画面绘制,包括界河、棋盘、棋子、
3. 响应按键:确认、移动等
4. 更新界面: 根据服务器的消息,解析具体内容,逻辑处理
5. 具体的棋子移动的逻辑

部分代码来自教程:java项目开发全程实录。

具体代码如下:
MyCanvas.java
package newclient;


import java.io.IOException;
import javax.microedition.lcdui.*;

/**
 * 
 * @author sean
 * 
 *
 */
public class MyGameCanvas extends Canvas implements CommandListener {
	protected Game game;

	String color = "";

	protected int rightSpace;// 屏幕右侧留的空间

	protected int x;// 棋盘输出的坐标

	private boolean myTurn = false;

	protected int gridWidth;// 每个棋格的边长

	protected int mapWidth, canvasW;// 棋盘的宽度和画布的宽度

	protected int a, b, c, d;

	protected int chessR;// 棋子的半径

	private int desknum = -1; // 桌子序号

	private int seatPos = -1; // 座位序号

	private boolean banker = false;

	protected int selectedX, selectedY;// 选择框在棋盘格局上的x,y位置

	protected static int i, j;

	protected int m, n, p;// 记住开始的selectedX,selectedY和point[selectedX][selectedY]

	protected String q;// 记住word[selectedX][selectedY]

	protected int guard, guard1, guard2, g, g1;// 标记FIRE被按了多少次,g是用来判断走直线时前后的棋子,中间是否有其他棋子的累加器

	protected static int turnWho;// 表示该谁走了

	protected static int isRedWin; // 红棋胜利

	protected static int isWhiteWin; // 白棋胜利

	private NetClient client;

	protected Command exitCmd, start, ok;

	private int point[][]; // 棋子位置数组

	protected String[][] chess; // 棋子名称数组

	private int chessSelColor; // 选择棋子的颜色

	private int backColor; // 棋盘背景色

	private int charColor; // 棋子汉字颜色

	private int lineColor; // 棋盘线的颜色

	private int borderColor;// 楚河汉界的颜色

	private int selBorderColor;// 选择棋子的边框色

	private int blackChees; // 黑棋棋颜色

	private int redChess;// 红棋颜色

	public MyGameCanvas() {
	};

	/**
	 * 游戏初始化动作
	 * 添加command
	 * 初始化棋子
	 * 庄家和轮次的设置
	 */
	public void init() {
		// desknum = -1;
		// seatPos = -1;
		addCommand(start);
		addCommand(exitCmd);
		initChess();
		banker = false;
		myTurn = false;
	}

	/**
	 * 游戏复位
	 * 包括添加command
	 * banker,turn设置为False
	 */
	public void reset() {
		initChess();
		addCommand(start);
		addCommand(exitCmd);
		banker = false;
		myTurn = false;
	}

	/**
	 * 构造方法
	 * @param game
	 * @param client
	 */
	public MyGameCanvas(Game game, NetClient client) {// 构造函数
		this.game = game;
		this.client = client;
		chessSelColor = 0x188312;// 初始化选择棋子的颜色
		backColor = 0xEECD05; // 初始化背景色
		charColor = 0xFFFFFF; // 初始化汉字颜色
		lineColor = 0x5A5743; // 初始化棋盘线的颜色
		borderColor = 0x5A5743; // 初始化楚河汉界的颜色
		selBorderColor = 0x50FAFC; // 初始化选择棋子的边框色
		blackChees = 0x000000; // 绿棋颜色
		redChess = 0xBF0404;// 红棋颜色
		rightSpace = getWidth() / 6;
		x = rightSpace * 1 / 3;
		canvasW = getWidth() - rightSpace;
		mapWidth = canvasW - canvasW % 8;
		gridWidth = mapWidth / 8;
		a = gridWidth * 2 / 5;
		b = gridWidth / 8;
		c = gridWidth - a;
		d = gridWidth - b;
		chessR = gridWidth * 2 / 5;
		selectedX = 4;
		selectedY = 7;
		guard = 0;
		guard1 = selectedX;
		guard2 = selectedY;
		m = guard1;
		n = guard2;
		chess = new String[10][9];
		turnWho = 1;
		initChess();
		exitCmd = new Command("退出", Command.EXIT, 0);
		start = new Command("开始", Command.OK, 1);
		addCommand(start);
		addCommand(exitCmd);
		setCommandListener(this);
	}

	/**
	 * 初始化棋子
	 * 具体算法需要进一步研究
	 */
	public void initChess() {
		point = new int[][] {
				{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },// 初始化INT数组
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 10, 0, 0, 0, 0, 0, 11, 0 },
				{ 12, 0, 13, 0, 14, 0, 15, 0, 16 },
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
				{ 28, 0, 29, 0, 30, 0, 31, 0, 32 },
				{ 0, 26, 0, 0, 0, 0, 0, 27, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
				{ 17, 18, 19, 20, 21, 22, 23, 24, 25 } };

		chess = new String[][] {
				{ "车", "马", "象", "士", "将", "士", "象", "马", "车", },
				{ "空", "空", "空", "空", "空", "空", "空", "空", "空", },
				{ "空", "炮", "空", "空", "空", "空", "空", "炮", "空", },
				{ "卒", "空", "卒", "空", "卒", "空", "卒", "空", "卒", },
				{ "空", "空", "空", "空", "空", "空", "空", "空", "空", },
				{ "空", "空", "空", "空", "空", "空", "空", "空", "空", },
				{ "卒", "空", "卒", "空", "卒", "空", "卒", "空", "卒", },
				{ "空", "炮", "空", "空", "空", "空", "空", "炮", "空", },
				{ "空", "空", "空", "空", "空", "空", "空", "空", "空", },
				{ "车", "马", "象", "士", "将", "士", "象", "马", "车", }, };
	}

	public int getDeskIndex() {
		return desknum;
	}

	public void setDeskIndex(int i) {
		desknum = i;
	}

	public void setSeatPos(int i) {
		seatPos = i;
	}

	public int getSeatPos() {
		return seatPos;
	}

	
	protected void paintChessboardUp(Graphics g) { // 画上半部棋盘
		for (int k = 0; k < 4; k++) {
			for (int l = 0; l < 8; l++) {
				g.setColor(lineColor);
				g.drawRect(x + l * gridWidth, x + k * gridWidth, gridWidth,
						gridWidth);
			}
		}
		g.setColor(lineColor);						// 设置画棋盘线的颜色
		g.drawLine(x + 3 * gridWidth, x, x + 5 * gridWidth, x + 2 * gridWidth);
		g.drawLine(x + 5 * gridWidth, x, x + 3 * gridWidth, x + 2 * gridWidth);
		// 画左上方的炮
		{
			g.drawLine(x + d, x + gridWidth + c, x + d, x + gridWidth + d);// 左上竖
			g.drawLine(x + c, x + gridWidth + d, x + d, x + gridWidth + d);// 左上横
			g.drawLine(x + d + 2 * b, x + gridWidth + c, x + d + 2 * b, x
					+ gridWidth + d);// 右上竖
			g.drawLine(x + gridWidth + b, x + gridWidth + d, x + gridWidth + a,
					x + gridWidth + d);// 右上横
			g.drawLine(x + d, x + 2 * gridWidth + b, x + d, x + 2 * gridWidth
					+ a);// 左下竖
			g.drawLine(x + c, x + gridWidth + d + 2 * b, x + d, x + gridWidth
					+ d + 2 * b);// 左下横
			g.drawLine(x + d + 2 * b, x + 2 * gridWidth + b, x + d + 2 * b, x
					+ 2 * gridWidth + a);// 右下竖
			g.drawLine(x + gridWidth + b, x + gridWidth + d + 2 * b, x
					+ gridWidth + a, x + gridWidth + d + 2 * b);// 右下横
		}
		// 画右上方的炮
		{
			g.drawLine(x + d + 6 * gridWidth, x + gridWidth + c, x + d + 6
					* gridWidth, x + gridWidth + d);
			g.drawLine(x + c + 6 * gridWidth, x + gridWidth + d, x + d + 6
					* gridWidth, x + gridWidth + d);
			g.drawLine(x + d + 2 * b + 6 * gridWidth, x + gridWidth + c, x + d
					+ 2 * b + 6 * gridWidth, x + gridWidth + 13 + 9);
			g.drawLine(x + gridWidth + b + 6 * gridWidth, x + gridWidth + d, x
					+ gridWidth + a + 6 * gridWidth, x + gridWidth + d);
			g.drawLine(x + d + 6 * gridWidth, x + 2 * gridWidth + b, x + d + 6
					* gridWidth, x + 2 * gridWidth + a);
			g.drawLine(x + c + 6 * gridWidth, x + gridWidth + d + 2 * b, x + d
					+ 6 * gridWidth, x + gridWidth + d + 2 * b);
			g.drawLine(x + d + 2 * b + 6 * gridWidth, x + 2 * gridWidth + b, x
					+ d + 2 * b + 6 * gridWidth, x + 2 * gridWidth + a);
			g.drawLine(x + gridWidth + b + 6 * gridWidth, x + gridWidth + d + 2
					* b, x + gridWidth + a + 6 * gridWidth, x + gridWidth + d
					+ 2 * b);
		}
	}

	protected void paintRiver(Graphics g) {// 画河
		g.setColor(lineColor);
		g.drawRect(x, x + 4 * gridWidth, mapWidth, gridWidth);
		g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
				Font.SIZE_LARGE));
		g.setColor(borderColor);
		g.drawString("楚河       汉界", getWidth() / 2, x + 4 * gridWidth
				+ gridWidth * 3 / 4, Graphics.HCENTER | Graphics.BASELINE);
	}

	protected void paintChessboardDown(Graphics g) {// 画下半部棋盘
		for (int q = 0; q < 4; q++) {
			for (int w = 0; w < 8; w++) {
				g.setColor(lineColor);
				g.drawRect(x + w * gridWidth, x + (q + 5) * gridWidth,
						gridWidth, gridWidth);
			}
		}
		g.setColor(lineColor);
		g.drawLine(x + 3 * gridWidth, x + 7 * gridWidth, x + 5 * gridWidth, x
				+ 9 * gridWidth);
		g.drawLine(x + 5 * gridWidth, x + 7 * gridWidth, x + 3 * gridWidth, x
				+ 9 * gridWidth);
		// 画左上方的炮
		{
			g.drawLine(x + d, x + 6 * gridWidth + c, x + d, x + 6 * gridWidth
					+ d);// 左上竖
			g.drawLine(x + c, x + 6 * gridWidth + d, x + d, x + 6 * gridWidth
					+ d);// 左上横

			g.drawLine(x + d + 2 * b, x + 6 * gridWidth + c, x + d + 2 * b, x
					+ 6 * gridWidth + d);// 右上竖
			g.drawLine(x + gridWidth + b, x + 6 * gridWidth + d, x + gridWidth
					+ a, x + 6 * gridWidth + d);// 右上横

			g.drawLine(x + d, x + 7 * gridWidth + b, x + d, x + 7 * gridWidth
					+ a);// 左下竖
			g.drawLine(x + c, x + 6 * gridWidth + d + 2 * b, x + d, x + 6
					* gridWidth + d + 2 * b);// 左下横

			g.drawLine(x + d + 2 * b, x + 7 * gridWidth + b, x + d + 2 * b, x
					+ 7 * gridWidth + a);// 右下竖
			g.drawLine(x + gridWidth + b, x + 6 * gridWidth + d + 2 * b, x
					+ gridWidth + a, x + 6 * gridWidth + d + 2 * b);// 右下横
		}
		// 画右上方的炮
		{
			g.drawLine(x + d + 6 * gridWidth, x + 6 * gridWidth + c, x + d + 6
					* gridWidth, x + 6 * gridWidth + d);
			g.drawLine(x + c + 6 * gridWidth, x + 6 * gridWidth + d, x + d + 6
					* gridWidth, x + 6 * gridWidth + d);

			g.drawLine(x + d + 2 * b + 6 * gridWidth, x + 6 * gridWidth + c, x
					+ d + 2 * b + 6 * gridWidth, x + 6 * gridWidth + d);
			g.drawLine(x + gridWidth + b + 6 * gridWidth,
					x + 6 * gridWidth + d, x + gridWidth + a + 6 * gridWidth, x
							+ 6 * gridWidth + d);

			g.drawLine(x + d + 6 * gridWidth, x + 7 * gridWidth + b, x + d + 6
					* gridWidth, x + 7 * gridWidth + a);
			g.drawLine(x + c + 6 * gridWidth, x + 6 * gridWidth + d + 2 * b, x
					+ d + 6 * gridWidth, x + 6 * gridWidth + d + 2 * b);

			g.drawLine(x + d + 2 * b + 6 * gridWidth, x + 7 * gridWidth + b, x
					+ d + 2 * b + 6 * gridWidth, x + 7 * gridWidth + a);
			g.drawLine(x + gridWidth + b + 6 * gridWidth, x + 6 * gridWidth + d
					+ 2 * b, x + gridWidth + a + 6 * gridWidth, x + 6
					* gridWidth + d + 2 * b);
		}
	}

	
	protected void paintAllChess(Graphics g) {// 画出所有棋子
		if (point == null)
			return;
		for (i = 0; i < 10; i++) {
			for (j = 0; j < 9; j++) {
				if (point[i][j] != 0) {
					if (point[i][j] < 17) {
						g.setColor(redChess);
					} else {
						g.setColor(blackChees);
					}
					g.fillArc(x - chessR + j * gridWidth, x - chessR + i
							* gridWidth, 2 * chessR, 2 * chessR, 0, 360);
					g.setColor(charColor);
					g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,
							Font.STYLE_BOLD, Font.SIZE_LARGE));
					g.drawString(chess[i][j], x + j * gridWidth, x + chessR + i
							* gridWidth, Graphics.HCENTER | Graphics.BOTTOM);
				}
			}
		}
	}

	protected void paintSelectedChess(Graphics g) {// 画选择的棋子
		m = guard1;
		n = guard2; // 再重新单独输出一个棋子
		g.setColor(chessSelColor);
		g.fillArc(x - chessR + guard1 * gridWidth, x - chessR + guard2
				* gridWidth, 2 * chessR, 2 * chessR, 0, 360);
		g.setColor(charColor);
		g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD,
				Font.SIZE_LARGE));
		g.drawString(chess[guard2][guard1], x + guard1 * gridWidth, x + chessR
				+ guard2 * gridWidth, Graphics.HCENTER | Graphics.BOTTOM);
	}

	protected void whoTurn(Graphics g) {// 判断该谁走了
		g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
				Font.SIZE_LARGE));
		if (!myTurn) {
			g.setColor(0xFE0000);
			g.drawString("轮到对家走棋", x, x + chessR + 10 * gridWidth,
					Graphics.LEFT | Graphics.BOTTOM);

		} else {
			g.setColor(0X9F3202);
			g.drawString("轮到自己走棋", x, x + chessR + 10 * gridWidth,
					Graphics.LEFT | Graphics.BOTTOM);
		}
	}

	/**
	 * 解析接收到数据
	 * 根据结果展示输赢界面
	 * 添加ok命令
	 * 设置新的画布
	 * @param str
	 */
	protected void checkWin(String str) {// 判断输赢
		System.out.println("checkWin " + str);
		int index = str.indexOf(",");
		String str2 = str.substring(index + 1);
		try {
			if (str2.equals("you")) {
				Alert alert;
				alert = new Alert("恭喜!", null, Image.createImage(this
						.getClass().getResourceAsStream("win.gif")),
						AlertType.INFO);
				ok = new Command("OK", Command.OK, 1);
				alert.setTimeout(Alert.FOREVER);
				alert.addCommand(ok);
				alert.setCommandListener(this);
				Game.display.setCurrent(alert);
			} else {
				Alert alert = new Alert("遗憾!", null, Image.createImage(this
						.getClass().getResourceAsStream("false.gif")),
						AlertType.INFO);
				ok = new Command("OK", Command.OK, 1);
				alert.setTimeout(Alert.FOREVER);
				alert.addCommand(ok);
				alert.setCommandListener(this);
				Game.display.setCurrent(alert);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected void paintSelected(Graphics g) {// 画选择框
		g.setColor(selBorderColor);
		g.drawRect(x - chessR + selectedX * gridWidth, x - chessR + selectedY
				* gridWidth, 2 * chessR, 2 * chessR);

	}

	/**
	 * 绘画的总开关
	 * 具体由各个方法实现
	 * 包括棋牌、棋子、界河等的绘画
	 */
	protected void paint(Graphics g) {
		g.setColor(backColor);
		g.fillRect(0, 0, getWidth(), getHeight());
		paintChessboardUp(g); // 调用画上半部棋盘的方法
		paintRiver(g); // 调用画河的方法
		paintChessboardDown(g); // 调用画下半部棋盘的方法
		paintAllChess(g); // 调用画棋子的方法
		if (guard % 2 == 1) {
			paintSelectedChess(g); // 调用画选择棋子的方法
		}
		paintSelected(g);
		whoTurn(g);
	}

	/**
	 * 
	 * @param m
	 * @param n
	 * @param selectedX
	 * @param selectedY
	 * @param send
	 */
	protected void changTwoChessNum(int m, int n, int selectedX, int selectedY,
			boolean send) {// 改变两个格子的值
		if (send) {
			if ((color.equals("red") && point[n][m] < 17)
					|| (color.equals("white") && point[n][m] >= 17)) {
				if (!banker) {
					client.sendMessage(Message.MOVE_REQ, "move;" + seatPos + ":" + selectedY
							+ "," + selectedX + "," + n + "," + m);
				} else {
					client.sendMessage(Message.MOVE_REQ,"move;" + seatPos + ":"
							+ (9 - selectedY) + "," + (8 - selectedX) + ","
							+ (9 - n) + "," + (8 - m));
				}

				myTurn = false;
				p = point[selectedY][selectedX];
				point[selectedY][selectedX] = point[n][m];

				point[n][m] = 0;
				q = chess[selectedY][selectedX];
				chess[selectedY][selectedX] = chess[n][m];
				chess[n][m] = "空";
			}
		} else {
			myTurn = false;
			p = point[selectedY][selectedX];
			point[selectedY][selectedX] = point[n][m];

			point[n][m] = 0;
			q = chess[selectedY][selectedX];
			chess[selectedY][selectedX] = chess[n][m];
			chess[n][m] = "空";
		}
	}

	private void theRuleOfChe(int m, int n, int sx, int sy) {// 车的规则
		g = 0;
		if (m == sx) {
			if (n > sy) {
				for (i = 1; i < n - sy; i++) {
					if (point[sy + i][m] != 0) {
						g++;
					}
				}
			} else {
				for (i = 1; i < sy - n; i++) {
					if (point[n + i][m] != 0) {
						g++;
					}
				}
			}
			if (g == 0) {
				changTwoChessNum(m, n, sx, sy, true);
			}

		}
		if (n == sy) {
			if (m > sx) {
				for (i = 1; i < m - sx; i++) {
					if (point[n][i + sx] != 0) {
						g++;
					}
				}
			} else {
				for (i = 1; i < sx - m; i++) {
					if (point[n][m + i] != 0) {
						g++;
					}
				}
			}
			if (g == 0) {
				changTwoChessNum(m, n, sx, sy, true);
			}

		}
	}

	private void theRuleOfMa(int m, int n, int sx, int sy) {// 马的规则
		if (n < 9) {
			if (point[n + 1][m] == 0) {
				if (sx - m == 1) {
					if (sy - n == 2) {
						changTwoChessNum(m, n, sx, sy, true);
					}
				}
			}
		}
		if (n > 0) {
			if (point[n - 1][m] == 0) {
				if (m - sx == 1) {
					if (n - sy == 2) {
						changTwoChessNum(m, n, sx, sy, true);
					}
				}
			}
		}
		if (n < 9) {
			if (point[n + 1][m] == 0) {
				if (sx - m == -1) {
					if (sy - n == 2) {
						changTwoChessNum(m, n, sx, sy, true);
					}
				}
			}
		}
		if (n > 0) {
			if (point[n - 1][m] == 0) {
				if (m - sx == -1) {
					if (n - sy == 2) {
						changTwoChessNum(m, n, sx, sy, true);
					}
				}
			}
		}
		if (m < 8) {
			if (point[n][m + 1] == 0) {
				if (sx - m == 2) {
					if (sy - n == 1) {
						changTwoChessNum(m, n, sx, sy, true);
					}
				}
			}
		}
		if (m > 0) {
			if (point[n][m - 1] == 0) {
				if (m - sx == 2) {
					if (n - sy == 1) {
						changTwoChessNum(m, n, sx, sy, true);
					}
				}
			}
		}
		if (m < 8) {
			if (point[n][m + 1] == 0) {
				if (sx - m == 2) {
					if (sy - n == -1) {
						changTwoChessNum(m, n, sx, sy, true);
					}
				}
			}
		}
		if (m > 0) {
			if (point[n][m - 1] == 0) {
				if (m - sx == 2) {
					if (n - sy == -1) {
						changTwoChessNum(m, n, sx, sy, true);
					}
				}
			}
		}
	}

	private void theRuleOfPao(int m, int n, int sx, int sy, int g1) {// 炮的规则
		g = 0;
		if (m == sx) {
			if (n > sy) {
				for (i = 1; i < n - sy; i++) {
					if (point[sy + i][m] != 0) {
						g++;
					}
				}
			} else {
				for (i = 1; i < sy - n; i++) {
					if (point[n + i][m] != 0) {
						g++;
					}
				}
			}
			if (g == g1) {
				changTwoChessNum(m, n, sx, sy, true);
			}

		}
		if (n == sy) {
			if (m > sx) {
				for (i = 1; i < m - sx; i++) {
					if (point[n][i + sx] != 0) {
						g++;
					}
				}
			} else {
				for (i = 1; i < sx - m; i++) {
					if (point[n][m + i] != 0) {
						g++;
					}
				}
			}
			if (g == g1) {
				changTwoChessNum(m, n, sx, sy, true);
			}

		}
	}

	private void theRuleOfXiang(int m, int n, int sx, int sy) {// 相的规则
		if (color.equals("red"))
			if (sy < 5)
				return;
			else if (color.equals("while"))
				if (sy > 4)
					return;
		if (n < 9 & m < 8) {
			if (point[n + 1][m + 1] == 0) {
				if ((sx - m == 2) & (sy - n == 2)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
			}
		}
		if (n > 0 & m < 8) {
			if (point[n - 1][m + 1] == 0) {
				if ((sx - m == 2) & (sy - n == -2)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
			}
		}
		if (n < 9 & m > 0) {
			if (point[n + 1][m - 1] == 0) {
				if ((sx - m == -2) & (sy - n == 2)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
			}
		}
		if (n > 0 & m > 0) {
			if (point[n - 1][m - 1] == 0) {
				if ((sx - m == -2) & (sy - n == -2)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
			}
		}
	}

	private void theRuleOfShi(int m, int n, int sx, int sy) {// 士的规则
		if ((m > 2 & m < 6) & (sx > 2 & sx < 6) & (n >= 7 & n <= 9)
				& (sy >= 7 & sy <= 9)) {

			if ((sx - m == 1) & (sy - n == 1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}

			if ((sx - m == 1) & (sy - n == -1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == -1) & (sy - n == 1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == -1) & (sy - n == -1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
		}
		if ((m > 2 & m < 6) & (sx > 2 & sx < 6) & (n >= 0 & n < 3)
				& (sy >= 0 & sy < 3)) {
			if ((sx - m == 1) & (sy - n == 1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == 1) & (sy - n == -1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == -1) & (sy - n == 1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == -1) & (sy - n == -1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
		}
	}

	private void theRuleOfShuai(int m, int n, int sx, int sy) {// 帅的规则
		if ((m > 2 & m < 6) & (sx > 2 & sx < 6) & (n >= 7 & n <= 9)
				& (sy >= 7 & sy <= 9)) {
			if ((sx - m == 1) & (sy - n == 0)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == -1) & (sy - n == 0)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == 0) & (sy - n == 1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == 0) & (sy - n == -1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
		}
		if ((m > 2 & m < 6) & (sx > 2 & sx < 6) & (n >= 0 & n < 3)
				& (sy >= 0 & sy < 3)) {
			if ((sx - m == 1) & (sy - n == 0)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == -1) & (sy - n == 0)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == 0) & (sy - n == 1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
			if ((sx - m == 0) & (sy - n == -1)) {
				changTwoChessNum(m, n, sx, sy, true);
			}
		}
	}

	private void theRuleOfZu(int m, int n, int sx, int sy) {// 卒的规则
		if (sy <= n) {
			if (n > 4) {
				if ((sy - n == -1) & (sx - m == 0)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
			} else {
				if ((sy - n == -1) & (sx - m == 0)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
				if ((sy - n == 0) & (sx - m == 1)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
				if ((sy - n == 0) & (sx - m == -1)) {
					changTwoChessNum(m, n, sx, sy, true);
				}
			}
		}
	}

	/**
	 * 
	 */
	public void commandAction(Command c, Displayable d) { // 软键按钮事件处理
		if (c == exitCmd) {
			(new Thread() {
				public void run() {
					client.sendMessage(Message.EXITGAME_REQ, "exitgame");
				}
			}).start();
			seatPos = -1;
			desknum = -1;
			Game.display.setCurrent(game.getPlayerList());
		} else if (c == ok) {
			reset();
			Game.display.setCurrent(this);

		} else if (c == start) {
			banker = false;
			client.sendMessage(Message.START_REQ, "start");
			System.out.println("client: start request");
			this.removeCommand(start);
		}
	}

	/**
	 * 按键处理
	 */
	protected void keyPressed(int keyCode) { // 处理按键
		int action = getGameAction(keyCode);
		if (myTurn) {
			if (action == Canvas.LEFT) {
				selectedX = (--selectedX + 8 + 1) % (8 + 1);
			} else if (action == Canvas.RIGHT) {
				selectedX = (++selectedX) % (8 + 1);
			} else if (action == Canvas.UP) {
				selectedY = (--selectedY + 9 + 1) % (9 + 1);
			} else if (action == Canvas.DOWN) {
				selectedY = (++selectedY) % (9 + 1);
			} else if (action == Canvas.FIRE)// 这里的FIRE键我分成了两种情况:一是选种棋子,
			{ // 二是当选择了棋子后,让棋子走到下面选择的位置

				guard = guard + 1;// 每按下FIRE一次,GUARD就加一,用来判断FIRE是被选种还是选种后走下不棋
				if (guard % 2 == 1) // 这时是当选种某一个棋子时,调用choosChess函数,选择棋子
				{
					if (point[selectedY][selectedX] != 0) {
						guard1 = selectedX;
						guard2 = selectedY;
					}
				}

				if (guard % 2 == 0)// 这种情况是当棋子被选种后
				{
					if (point[selectedY][selectedX] != point[n][m])// 当走的下一步不是自身,也就是玩家选过
					{ // 一个棋子,又不想选了,这只需什么都不做
						if ((point[n][m] == 1) | (point[n][m] == 9)
								| (point[n][m] == 17) | (point[n][m] == 25))// 当选定的棋子是车的时候
						{ // repaint就OK了
							if (point[selectedY][selectedX] == 0)// 当下一步走的是空格,则改变选种的格子和下一步所
							{ // 走的格子的point[][]和word[][]的植,然后repaint就OK
								theRuleOfChe(m, n, selectedX, selectedY);
							} else// 当下一步是想吃对方的子的,则把下一步格子的值变为刚才选定的格子的值,而
							{ // 刚才选定的格子的值则便为零
								System.out
										.println("point[selectedY][selectedX] / 17)="
												+ (point[selectedY][selectedX] / 17)
												+ "(point[n][m] / 17)="
												+ (point[n][m] / 17));
								if ((point[selectedY][selectedX] / 17) != (point[n][m] / 17))// 当然,想吃的子不能是自己的
								{
									theRuleOfChe(m, n, selectedX, selectedY);

								}
							}
						}
						if ((point[n][m] == 2) | (point[n][m] == 8)
								| (point[n][m] == 18) | (point[n][m] == 24))// 当选定的棋子是马的时候
						{
							if (point[selectedY][selectedX] == 0) {
								theRuleOfMa(m, n, selectedX, selectedY);
							} else {
								if ((point[selectedY][selectedX] / 17) != (point[n][m] / 17))// 当然,想吃的子不能是自己的
								{
									theRuleOfMa(m, n, selectedX, selectedY);

								}
							}
						}
						if ((point[n][m] == 10) | (point[n][m] == 11)
								| (point[n][m] == 26) | (point[n][m] == 27))// 当选定的棋子是炮的时候
						{
							if (point[selectedY][selectedX] == 0) {
								g1 = 0;
								theRuleOfPao(m, n, selectedX, selectedY, g1);
							} else {
								g1 = 1;
								if ((point[selectedY][selectedX] / 17) != (point[n][m] / 17))// 当然,想吃的子不能是自己的
								{
									theRuleOfPao(m, n, selectedX, selectedY, g1);

								}
							}
						}
						if ((point[n][m] == 3) | (point[n][m] == 7)
								| (point[n][m] == 19) | (point[n][m] == 23))// 当选定的棋子是相的时候
						{
							if (point[selectedY][selectedX] == 0) {
								theRuleOfXiang(m, n, selectedX, selectedY);
							} else {
								if ((point[selectedY][selectedX] / 17) != (point[n][m] / 17))// 当然,想吃的子不能是自己的
								{
									theRuleOfXiang(m, n, selectedX, selectedY);

								}
							}
						}
						if ((point[n][m] == 4) | (point[n][m] == 6)
								| (point[n][m] == 20) | (point[n][m] == 22))// 当选定的棋子是士的时候
						{
							if (point[selectedY][selectedX] == 0) {
								theRuleOfShi(m, n, selectedX, selectedY);
							} else {
								if ((point[selectedY][selectedX] / 17) != (point[n][m] / 17))// 当然,想吃的子不能是自己的
								{
									theRuleOfShi(m, n, selectedX, selectedY);

								}
							}
						}
						if ((point[n][m] == 5) | (point[n][m] == 21))// 当选定的棋子是帅的时候
						{
							if (point[selectedY][selectedX] == 0) {
								theRuleOfShuai(m, n, selectedX, selectedY);
							} else {
								if ((point[selectedY][selectedX] / 17) != (point[n][m] / 17))// 当然,想吃的子不能是自己的
								{
									theRuleOfShuai(m, n, selectedX, selectedY);

								}
							}
						}
						if ((point[n][m] > 11 & point[n][m] < 17))// 当选定的棋子是红方卒的时候
						{
							if (point[selectedY][selectedX] == 0) {
								theRuleOfZu(m, n, selectedX, selectedY);
							} else {
								if ((point[selectedY][selectedX] / 17) != (point[n][m] / 17))// 当然,想吃的子不能是自己的
								{
									theRuleOfZu(m, n, selectedX, selectedY);

								}
							}
						}
						if (point[n][m] > 27)// 当选定的棋子是白方卒的时候
						{
							if (point[selectedY][selectedX] == 0) {
								theRuleOfZu(m, n, selectedX, selectedY);
							} else {
								if ((point[selectedY][selectedX] / 17) != (point[n][m] / 17))// 当然,想吃的子不能是自己的
								{
									theRuleOfZu(m, n, selectedX, selectedY);

								}
							}
						}

					}
				}
			}
		}
		repaint();
	}

	/**
	 * 关键方法
	 * 接收信息的总开关
	 * 分析消息的命令字符串
	 * 分发给不同的方法处理
	 * 比如:移动棋子的方法、轮次、颜色、输赢等
	 * @param str
	 */
	public void receiveMessage(String str) { // TODO Auto-generated method
		System.out.println("client recieve game msg: " + str +":" + System.currentTimeMillis());
		// stub
		if (str.startsWith("move")) { // 别人移动信息
			int index0 = str.indexOf(";");
			int index1 = str.indexOf(":");
			int index2 = str.indexOf(",", index1 + 1);
			int index3 = str.indexOf(",", index2 + 1);
			int index4 = str.indexOf(",", index3 + 1);

			int seat = Integer.parseInt(str.substring(index0 + 1, index1));
			int selectedY = Integer.parseInt(str.substring(index1 + 1, index2));
			int selectedX = Integer.parseInt(str.substring(index2 + 1, index3));
			int n = Integer.parseInt(str.substring(index3 + 1, index4));
			int m = Integer.parseInt(str.substring(index4 + 1));
			if (seat != seatPos) {
				if (banker) {
					changTwoChessNum(8 - m, 9 - n, 8 - selectedX,
							9 - selectedY, false);
					myTurn = true;
				} else {
					changTwoChessNum(m, n, selectedX, selectedY, false);
					myTurn = true;
				}
				repaint();
			}
		} else if (str.startsWith("color")) {
			int index = str.indexOf(":");
			color = str.substring(index + 1);
			if (color.equals("white")) {
				point = new int[][] {
						{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },// 初始化INT数组
						{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
						{ 0, 10, 0, 0, 0, 0, 0, 11, 0 },
						{ 12, 0, 13, 0, 14, 0, 15, 0, 16 },
						{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
						{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
						{ 28, 0, 29, 0, 30, 0, 31, 0, 32 },
						{ 0, 26, 0, 0, 0, 0, 0, 27, 0 },
						{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
						{ 17, 18, 19, 20, 21, 22, 23, 24, 25 } };

			} else if (color.equals("red")) {
				point = new int[][] {
						{ 17, 18, 19, 20, 21, 22, 23, 24, 25 },// 初始化INT数组
						{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
						{ 0, 26, 0, 0, 0, 0, 0, 27, 0 },
						{ 28, 0, 29, 0, 30, 0, 31, 0, 32 },
						{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
						{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
						{ 12, 0, 13, 0, 14, 0, 15, 0, 16 },
						{ 0, 10, 0, 0, 0, 0, 0, 11, 0 },
						{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
						{ 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
			}
			repaint();
		} else if (str.startsWith("turn")) {
			myTurn = true;
			banker = true;
			repaint();
		} else if (str.startsWith("win")) {
			checkWin(str);
		} else if (str.startsWith("exitgame")) {
			game.initialize();
			Game.display.setCurrent(game.getPlayerList());
		}
	}
}




其他类比如:Message, Queue都是共用类,请参考系列文章3进行了解。    
分享到:
评论

相关推荐

    中国象棋源码

    【中国象棋源码】是一个基于Java编程语言开发的项目,主要目的是实现一个单机版的中国象棋游戏。这个项目对于学习Java编程,特别是图形用户界面(GUI)设计和事件处理具有很好的参考价值。开发者在描述中提到,他...

    MyCanvas:C++画布

    MyCanvas *ctx = 新的 MyCanvas; 填充矩形 ctx-&gt;beginPath(); ctx-&gt;rect(1,2,3,4); ctx-&gt;fillStyle("rgba(0,0,0,0.255)"); ctx-&gt;填充(); 绘图文本 ctx-&gt;beginPath(); ctx-&gt;font("粗体 3px Arial"); ctx-&gt;...

    mycanvas:点击查看→

    在"mycanvas-main"这个文件或目录中,可能包含了使用canvas进行图形编程的相关代码和资源。 在HTML5 canvas中,我们首先需要在HTML文档中定义一个`&lt;canvas&gt;`元素,并为其指定一个id以便于JavaScript引用: ```html...

    微信小程序之通过Canvas生成图片保存到手机相册

    本篇将详细介绍如何在微信小程序中利用Canvas实现这一功能。 首先,我们需要了解Canvas的基本用法。Canvas是一个画布元素,它允许我们使用JavaScript进行绘图操作,包括线条、形状、文字、图像等。在微信小程序中,...

    J2ME 跳动的小球

    J2ME是Java平台的一个子集,专为资源有限的嵌入式设备如手机和掌上电脑设计,它在移动游戏开发领域尤其流行。 在这款游戏中,"跳动的小球"是主角,通过屏幕上的运动模拟了小球的跳跃动作。这个项目包含了两个关键的...

    开发html5 2d 赛车游戏以及打包发布为手机APP 第三话 拖动我们的主角车子

    在本教程中,我们将深入探讨如何使用HTML5技术开发2D赛车游戏,并将其打包发布为手机应用程序。HTML5因其跨平台兼容性和丰富的图形处理能力,成为制作网页游戏的理想选择。在第三话中,我们将专注于如何实现游戏的...

    手机五子棋(J2ME)

    【手机五子棋(J2ME)】是一款基于Java Micro Edition (J2ME) 平台开发的五子棋游戏。J2ME是Java的一种轻量级应用框架,主要用于移动设备和嵌入式设备上的应用程序开发,如手机和智能手表。这款五子棋游戏实现了基本的...

    根据艾宾浩斯遗忘曲线背单词的手机软件

    标题中的“根据艾宾浩斯遗忘曲线背单词的手机软件”指的是一个利用心理学家赫尔曼·艾宾浩斯提出的遗忘曲线理论设计的移动应用程序。艾宾浩斯遗忘曲线揭示了人们在学习新信息后,记忆会随着时间的推移而衰退,但通过...

    javascript点击保存图片到手机本地文件

    在JavaScript开发中,将网页上的图片保存到用户手机本地是一个常见的需求,特别是在前端开发和交互设计中。这个功能通常涉及到JavaScript、ECMAScript、Canvas以及CSS等技术的结合使用。以下将详细介绍如何实现这一...

    拼图游戏_java课设_含可运行程序.doc

    9. **序列化(serialVersionUID)**:MyCanvas类中的serialVersionUID与Java的序列化机制有关,确保类的不同版本之间兼容。 10. **程序流程**:游戏启动时,主类MyMainFrame创建窗口,用户选择图片后,MyCanvas初始...

    数字小游戏 J2me

    游戏的主要逻辑在`Demo2`类和`MyCanvas`类中实现。 ##### `Demo2`类解析 `Demo2`类继承自`MIDlet`,这是J2ME中所有应用程序的基类。`MIDlet`负责处理应用程序的生命周期,包括启动、暂停和销毁。在`Demo2`类中: ...

    游戏中碰撞事件代码程序

    6. **NPC类的状态管理**: NPC类有一个`live`布尔变量来记录其存活状态,配合`isLive()`和`setLive()`方法来检查和改变这个状态。当NPC被击中时,`live`设为false,之后在`draw()`方法中先检查`live`是否为true,...

    java 跑步移动的小人

    在给定的代码中,Midlet 类主要负责设置主界面为 MyCanvas 类的一个实例。`startApp()` 方法调用时,将 MyCanvas 类实例设为当前显示的内容。 ```java protected void startApp() throws ...

    蓝牙五子棋源码

    根据给定文件的信息,我们可以提炼出以下IT领域的关键知识点,主要围绕J2ME(Java 2 Micro Edition)和蓝牙通信技术在游戏开发中的应用。 ### J2ME与蓝牙游戏开发 #### J2ME概述 J2ME是Java平台的一个版本,专为...

    一个经典的_JAVA_ME_手机程序入门级源码

    本文将详细介绍一个经典的Java ME手机程序入门级源码,该程序由Carol Hamer编写,旨在帮助初学者快速掌握Java ME(J2ME)开发的基本原理与实践技巧。通过分析`Hello.java`与`HelloCanvas.java`两个核心类的源码,...

    JAVA+图形界面开发

    综上所述,通过以上介绍的基础组件、用户界面组件以及图像处理类,可以有效地构建出复杂且美观的Java图形界面,特别是在资源受限的环境下。这种方案不仅适用于桌面应用开发,也适用于移动设备等资源受限环境下的开发...

    关于vscode开发WebGL没有提示的解决办法1

    "关于vscode开发WebGL没有提示的解决办法1" 在开发WebGL项目时,使用vscode编辑器但是没有提示,这是一个令人沮丧的问题。幸运的是,这个问题有一个简单的解决办法:使用JSDOC注释。 什么是JSDOC? JSDOC是一个...

    JAVA图形界面开发探索

    JAVA作为一种强大的编程语言,自1995年由Sun Microsystems推出以来,以其独特的特性如安全性、跨平台能力、面向对象设计及适用于网络开发等,迅速在IT业界占据了一席之地。JAVA最初的目标是为消费电子产品如PDA、机...

    一个简单的设置J2ME背景

    J2ME是一个轻量级的Java平台,主要用于移动设备和嵌入式系统,如手机、游戏机等。在J2ME应用程序中,设置背景可以提升用户体验,使界面更加吸引人。 首先,我们关注提供的两个文件:`MyCanvas.java`和`...

    Java手机程序设计入门11

    ### Java手机程序设计入门:MIDP图形处理详解 #### 引言 MIDP(Mobile Information Device Profile)是Java ME平台的一部分,专为移动设备如手机和平板电脑设计。对于初学者而言,掌握MIDP图形处理技术是至关重要...

Global site tag (gtag.js) - Google Analytics