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

📄 gamecanvas.java

📁 手机版象棋游戏采用MIDP1.0开发
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				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("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("start");
			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();
	}

	public void receiveMessage(String str) { // TODO Auto-generated method
		// 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());
		}
	}
}

⌨️ 快捷键说明

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