📄 gamecanvas.java
字号:
g.drawLine(x + d + 6 * cellWidth, x + 7 * cellWidth + b, x + d + 6
* cellWidth, x + 7 * cellWidth + a);
g.drawLine(x + c + 6 * cellWidth, x + 6 * cellWidth + d + 2 * b, x + d
+ 6 * cellWidth, x + 6 * cellWidth + d + 2 * b);
g.drawLine(x + d + 2 * b + 6 * cellWidth, x + 7 * cellWidth + b, x + d
+ 2 * b + 6 * cellWidth, x + 7 * cellWidth + a);
g.drawLine(x + cellWidth + b + 6 * cellWidth, x + 6 * cellWidth + d + 2
* b, x + cellWidth + a + 6 * cellWidth, x + 6 * cellWidth + d
+ 2 * b);
}
protected void paintAllChess(Graphics g)// 画出所有的棋子
{
// if (color.equals("white")) {
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(255, 0, 0);
} else {
g.setColor(255, 255, 255);
}
g.fillArc(x - chessR + j * cellWidth, x - chessR + i
* cellWidth, 2 * chessR, 2 * chessR, 0, 360);
g.setColor(0x00000000);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,
Font.STYLE_BOLD, Font.SIZE_LARGE));
g.drawString(word[i][j], x + j * cellWidth, x + chessR + i
* cellWidth, Graphics.HCENTER | Graphics.BOTTOM);
}
}
}
}
protected void chooseChess(Graphics g)// 选定棋子,实现的原理就是如果选择了就再按照指定的颜色
{
m = guard1;
n = guard2; // 再重新单独输出一个棋子
g.setColor(255, 255, 0);
g.fillArc(x - chessR + guard1 * cellWidth, x - chessR + guard2
* cellWidth, 2 * chessR, 2 * chessR, 0, 360);
g.setColor(0x00000000);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString(word[guard2][guard1], x + guard1 * cellWidth, x + chessR
+ guard2 * cellWidth, Graphics.HCENTER | Graphics.BOTTOM);
}
protected void whoIsGoing(Graphics g)// 判断该谁走了
{
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
Font.SIZE_LARGE));
if (!myTurn) {
g.setColor(255, 255, 255);
g.drawString("该对方走了", x, x + chessR + 10 * cellWidth, Graphics.LEFT
| Graphics.BOTTOM);
} else {
g.setColor(255, 0, 0);
g.drawString("到你走棋", x, x + chessR + 10 * cellWidth, Graphics.LEFT
| Graphics.BOTTOM);
}
}
protected void checkWin(String str)// 判断输赢
{
int index = str.indexOf(",");
String str2 = str.substring(index + 1);
if (str2.equals("you")) {
Alert alert = new Alert("Congradulations!", "You win!", null,
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("Sorry!", "You Lose!", null, AlertType.INFO);
ok = new Command("OK", Command.OK, 1);
alert.setTimeout(Alert.FOREVER);
alert.addCommand(ok);
alert.setCommandListener(this);
Game.display.setCurrent(alert);
// this.addCommand(start);
}
}
protected void paintSelected(Graphics g)// 画选择框
{
g.setColor(0, 0, 255);
g.drawRect(x - chessR + selectedX * cellWidth, x - chessR + selectedY
* cellWidth, 2 * chessR, 2 * chessR);
}
protected void paint(Graphics g) {
g.setColor(0x00000000);
g.fillRect(0, 0, getWidth(), getHeight());
paintMapa(g);
paintMapb(g);
paintMapc(g);
paintAllChess(g);
if (guard % 2 == 1) {
chooseChess(g);
}
paintSelected(g);
whoIsGoing(g);
}
protected void changTwoChessNum(int m, int n, int selectedX, int selectedY,
boolean send)// 改变两个格子的值
{
// g2++;
if (send) {
if ((color.equals("red") && point[n][m] < 17)
|| (color.equals("white") && point[n][m] >= 17)) {
if (!banker) {
client.sendMessage("move;" + seatPos + ":" + selectedY
+ "," + selectedX + "," + n + "," + m);
} else {
client.sendMessage("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 = word[selectedY][selectedX];
word[selectedY][selectedX] = word[n][m];
word[n][m] = "空";
}
} else {
myTurn = false;
p = point[selectedY][selectedX];
point[selectedY][selectedX] = point[n][m];
point[n][m] = 0;
q = word[selectedY][selectedX];
word[selectedY][selectedX] = word[n][m];
word[n][m] = "空";
}
}
protected void theRuleOfChe(int m, int n, int selectedX, int selectedY)// 车的规则
{
g = 0;
if (m == selectedX) {
if (n > selectedY) {
for (i = 1; i < n - selectedY; i++) {
if (point[selectedY + i][m] != 0) {
g++;
}
}
} else {
for (i = 1; i < selectedY - n; i++) {
if (point[n + i][m] != 0) {
g++;
}
}
}
if (g == 0) {
changTwoChessNum(m, n, selectedX, selectedY, true);
}
}
if (n == selectedY) {
if (m > selectedX) {
for (i = 1; i < m - selectedX; i++) {
if (point[n][i + selectedX] != 0) {
g++;
}
}
} else {
for (i = 1; i < selectedX - m; i++) {
if (point[n][m + i] != 0) {
g++;
}
}
}
if (g == 0) {
changTwoChessNum(m, n, selectedX, selectedY, true);
}
}
}
protected void theRuleOfMa(int m, int n, int selectedX, int selectedY)// 马的规则
{
if (n < 9) {
if (point[n + 1][m] == 0) {
if (selectedX - m == 1) {
if (selectedY - n == 2) {
changTwoChessNum(m, n, selectedX, selectedY, true);
}
}
}
}
if (n > 0) {
if (point[n - 1][m] == 0) {
if (m - selectedX == 1) {
if (n - selectedY == 2) {
changTwoChessNum(m, n, selectedX, selectedY, true);
}
}
}
}
if (n < 9) {
if (point[n + 1][m] == 0) {
if (selectedX - m == -1) {
if (selectedY - n == 2) {
changTwoChessNum(m, n, selectedX, selectedY, true);
}
}
}
}
if (n > 0) {
if (point[n - 1][m] == 0) {
if (m - selectedX == -1) {
if (n - selectedY == 2) {
changTwoChessNum(m, n, selectedX, selectedY, true);
}
}
}
}
if (m < 8) {
if (point[n][m + 1] == 0) {
if (selectedX - m == 2) {
if (selectedY - n == 1) {
changTwoChessNum(m, n, selectedX, selectedY, true);
}
}
}
}
if (m > 0) {
if (point[n][m - 1] == 0) {
if (m - selectedX == 2) {
if (n - selectedY == 1) {
changTwoChessNum(m, n, selectedX, selectedY, true);
}
}
}
}
if (m < 8) {
if (point[n][m + 1] == 0) {
if (selectedX - m == 2) {
if (selectedY - n == -1) {
changTwoChessNum(m, n, selectedX, selectedY, true);
}
}
}
}
if (m > 0) {
if (point[n][m - 1] == 0) {
if (m - selectedX == 2) {
if (n - selectedY == -1) {
changTwoChessNum(m, n, selectedX, selectedY, true);
}
}
}
}
}
protected void theRuleOfPao(int m, int n, int selectedX, int selectedY,// 炮的规则
int g1) {
g = 0;
if (m == selectedX) {
if (n > selectedY) {
for (i = 1; i < n - selectedY; i++) {
if (point[selectedY + i][m] != 0) {
g++;
}
}
} else {
for (i = 1; i < selectedY - n; i++) {
if (point[n + i][m] != 0) {
g++;
}
}
}
if (g == g1) {
changTwoChessNum(m, n, selectedX, selectedY, true);
}
}
if (n == selectedY) {
if (m > selectedX) {
for (i = 1; i < m - selectedX; i++) {
if (point[n][i + selectedX] != 0) {
g++;
}
}
} else {
for (i = 1; i < selectedX - m; i++) {
if (point[n][m + i] != 0) {
g++;
}
}
}
if (g == g1) {
changTwoChessNum(m, n, selectedX, selectedY, true);
}
}
}
protected void theRuleOfXiang(int m, int n, int selectedX, int selectedY)// 相的规则
{
if(color.equals("red"))
if(selectedY<5)
return;
else if(color.equals("while"))
if(selectedY>4)
return;
if (n < 9 & m < 8) {
if (point[n + 1][m + 1] == 0) {
if ((selectedX - m == 2) & (selectedY - n == 2)) {
changTwoChessNum(m, n, selectedX, selectedY, true);
}
}
}
if (n > 0 & m < 8) {
if (point[n - 1][m + 1] == 0) {
if ((selectedX - m == 2) & (selectedY - n == -2)) {
changTwoChessNum(m, n, selectedX, selectedY, true);
}
}
}
if (n < 9 & m > 0) {
if (point[n + 1][m - 1] == 0) {
if ((selectedX - m == -2) & (selectedY - n == 2)) {
changTwoChessNum(m, n, selectedX, selectedY, true);
}
}
}
if (n > 0 & m > 0) {
if (point[n - 1][m - 1] == 0) {
if ((selectedX - m == -2) & (selectedY - n == -2)) {
changTwoChessNum(m, n, selectedX, selectedY, true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -