?? gamecanvas.java
字號:
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) {// 炮的規(guī)則
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) {// 相的規(guī)則
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) {// 士的規(guī)則
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) {// 帥的規(guī)則
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) {// 卒的規(guī)則
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鍵我分成了兩種情況:一是選種棋子,
{ // 二是當(dāng)選擇了棋子后,讓棋子走到下面選擇的位置
guard = guard + 1;// 每按下FIRE一次,GUARD就加一,用來判斷FIRE是被選種還是選種后走下不棋
if (guard % 2 == 1) // 這時是當(dāng)選種某一個棋子時,調(diào)用choosChess函數(shù),選擇棋子
{
if (point[selectedY][selectedX] != 0) {
guard1 = selectedX;
guard2 = selectedY;
}
}
if (guard % 2 == 0)// 這種情況是當(dāng)棋子被選種后
{
if (point[selectedY][selectedX] != point[n][m])// 當(dāng)走的下一步不是自身,也就是玩家選過
{ // 一個棋子,又不想選了,這只需什么都不做
if ((point[n][m] == 1) | (point[n][m] == 9)
| (point[n][m] == 17) | (point[n][m] == 25))// 當(dāng)選定的棋子是車的時候
{ // repaint就OK了
if (point[selectedY][selectedX] == 0)// 當(dāng)下一步走的是空格,則改變選種的格子和下一步所
{ // 走的格子的point[][]和word[][]的植,然后repaint就OK
theRuleOfChe(m, n, selectedX, selectedY);
} else// 當(dāng)下一步是想吃對方的子的,則把下一步格子的值變?yōu)閯偛胚x定的格子的值,而
{ // 剛才選定的格子的值則便為零
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))// 當(dāng)然,想吃的子不能是自己的
{
theRuleOfChe(m, n, selectedX, selectedY);
}
}
}
if ((point[n][m] == 2) | (point[n][m] == 8)
| (point[n][m] == 18) | (point[n][m] == 24))// 當(dāng)選定的棋子是馬的時候
{
if (point[selectedY][selectedX] == 0) {
theRuleOfMa(m, n, selectedX, selectedY);
} else {
if ((point[selectedY][selectedX] / 17) != (point[n][m] / 17))// 當(dāng)然,想吃的子不能是自己的
{
theRuleOfMa(m, n, selectedX, selectedY);
}
}
}
if ((point[n][m] == 10) | (point[n][m] == 11)
| (point[n][m] == 26) | (point[n][m] == 27))// 當(dāng)選定的棋子是炮的時候
{
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))// 當(dāng)然,想吃的子不能是自己的
{
theRuleOfPao(m, n, selectedX, selectedY, g1);
}
}
}
if ((point[n][m] == 3) | (point[n][m] == 7)
| (point[n][m] == 19) | (point[n][m] == 23))// 當(dāng)選定的棋子是相的時候
{
if (point[selectedY][selectedX] == 0) {
theRuleOfXiang(m, n, selectedX, selectedY);
} else {
if ((point[selectedY][selectedX] / 17) != (point[n][m] / 17))// 當(dāng)然,想吃的子不能是自己的
{
theRuleOfXiang(m, n, selectedX, selectedY);
}
}
}
if ((point[n][m] == 4) | (point[n][m] == 6)
| (point[n][m] == 20) | (point[n][m] == 22))// 當(dāng)選定的棋子是士的時候
{
if (point[selectedY][selectedX] == 0) {
theRuleOfShi(m, n, selectedX, selectedY);
} else {
if ((point[selectedY][selectedX] / 17) != (point[n][m] / 17))// 當(dāng)然,想吃的子不能是自己的
{
theRuleOfShi(m, n, selectedX, selectedY);
}
}
}
if ((point[n][m] == 5) | (point[n][m] == 21))// 當(dāng)選定的棋子是帥的時候
{
if (point[selectedY][selectedX] == 0) {
theRuleOfShuai(m, n, selectedX, selectedY);
} else {
if ((point[selectedY][selectedX] / 17) != (point[n][m] / 17))// 當(dāng)然,想吃的子不能是自己的
{
theRuleOfShuai(m, n, selectedX, selectedY);
}
}
}
if ((point[n][m] > 11 & point[n][m] < 17))// 當(dāng)選定的棋子是紅方卒的時候
{
if (point[selectedY][selectedX] == 0) {
theRuleOfZu(m, n, selectedX, selectedY);
} else {
if ((point[selectedY][selectedX] / 17) != (point[n][m] / 17))// 當(dāng)然,想吃的子不能是自己的
{
theRuleOfZu(m, n, selectedX, selectedY);
}
}
}
if (point[n][m] > 27)// 當(dāng)選定的棋子是白方卒的時候
{
if (point[selectedY][selectedX] == 0) {
theRuleOfZu(m, n, selectedX, selectedY);
} else {
if ((point[selectedY][selectedX] / 17) != (point[n][m] / 17))// 當(dāng)然,想吃的子不能是自己的
{
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數(shù)組
{ 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數(shù)組
{ 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());
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -