📄 chesspad.java
字号:
break;
}
chessLink = 1;
chessLinkTest = 1;
for (step = 1; step <= 4; step++) {
for (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare++) {
if ((a * 20 == chessWhite_x[chessCompare])
&& ((b + step) * 20 == chessWhite_y[chessCompare])) {
chessLink++;
if (chessLink == 5) {
return (true);
}
}
}
if (chessLink == (chessLinkTest + 1))
chessLinkTest++;
else
break;
}
for (step = 1; step <= 4; step++) {
for (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare++) {
if ((a * 20 == chessWhite_x[chessCompare])
&& ((b - step) * 20 == chessWhite_y[chessCompare])) {
chessLink++;
if (chessLink == 5) {
return (true);
}
}
}
if (chessLink == (chessLinkTest + 1))
chessLinkTest++;
else
break;
}
chessLink = 1;
chessLinkTest = 1;
for (step = 1; step <= 4; step++) {
for (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare++) {
if (((a - step) * 20 == chessWhite_x[chessCompare])
&& ((b + step) * 20 == chessWhite_y[chessCompare])) {
chessLink++;
if (chessLink == 5) {
return (true);
}
}
}
if (chessLink == (chessLinkTest + 1))
chessLinkTest++;
else
break;
}
for (step = 1; step <= 4; step++) {
for (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare++) {
if (((a + step) * 20 == chessWhite_x[chessCompare])
&& ((b - step) * 20 == chessWhite_y[chessCompare])) {
chessLink++;
if (chessLink == 5) {
return (true);
}
}
}
if (chessLink == (chessLinkTest + 1))
chessLinkTest++;
else
break;
}
chessLink = 1;
chessLinkTest = 1;
for (step = 1; step <= 4; step++) {
for (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare++) {
if (((a + step) * 20 == chessWhite_x[chessCompare])
&& ((b + step) * 20 == chessWhite_y[chessCompare])) {
chessLink++;
if (chessLink == 5) {
return (true);
}
}
}
if (chessLink == (chessLinkTest + 1))
chessLinkTest++;
else
break;
}
for (step = 1; step <= 4; step++) {
for (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare++) {
if (((a - step) * 20 == chessWhite_x[chessCompare])
&& ((b - step) * 20 == chessWhite_y[chessCompare])) {
chessLink++;
if (chessLink == 5) {
return (true);
}
}
}
if (chessLink == (chessLinkTest + 1))
chessLinkTest++;
else
break;
}
}
return (false);
}
/**
* 绘制棋盘,将棋盘绘制成19*19的个子并将棋盘上应有的五个点绘制上去。
*/
public void paint(Graphics g) {
for (int i = 40; i <= 380; i = i + 20) {
g.drawLine(40, i, 400, i);
}
g.drawLine(40, 400, 400, 400);
for (int j = 40; j <= 380; j = j + 20) {
g.drawLine(j, 40, j, 400);
}
g.drawLine(400, 40, 400, 400);
g.fillOval(97, 97, 6, 6);
g.fillOval(337, 97, 6, 6);
g.fillOval(97, 337, 6, 6);
g.fillOval(337, 337, 6, 6);
g.fillOval(217, 217, 6, 6);
}
/**
* 落子的时候绘制棋子
*/
public void chessPaint(int chessPoint_a, int chessPoint_b, int color) {
chessPoint_black chesspoint_black = new chessPoint_black(this);
chessPoint_white chesspoint_white = new chessPoint_white(this);
if (color == 1 && isMouseEnabled) {
//当黑子落子时,记下此子的位置
getLocation(chessPoint_a, chessPoint_b, color);
//判断是否获胜
isWin = checkWin(chessPoint_a, chessPoint_b, color);
if (isWin == false) {
//如果没有获胜,向对方发送落子信息,并绘制棋子
chessthread.sendMessage("/" + chessPeerName + " /chess "
+ chessPoint_a + " " + chessPoint_b + " " + color);
this.add(chesspoint_black);
chesspoint_black.setBounds(chessPoint_a * 20 - 7,
chessPoint_b * 20 - 7, 16, 16);
//在状态文本框显示行棋信息
statusText.setText("黑(第" + chessBlackCount + "步)"
+ chessPoint_a + " " + chessPoint_b + ",请白棋下子");
isMouseEnabled = false;
} else {
//如果获胜,直接调用chessVictory完成后续工作
chessthread.sendMessage("/" + chessPeerName + " /chess "
+ chessPoint_a + " " + chessPoint_b + " " + color);
this.add(chesspoint_black);
chesspoint_black.setBounds(chessPoint_a * 20 - 7,
chessPoint_b * 20 - 7, 16, 16);
chessVictory(1);
isMouseEnabled = false;
}
}
//白棋落子,同黑棋类似处理
else if (color == -1 && isMouseEnabled) {
getLocation(chessPoint_a, chessPoint_b, color);
isWin = checkWin(chessPoint_a, chessPoint_b, color);
if (isWin == false) {
chessthread.sendMessage("/" + chessPeerName + " /chess "
+ chessPoint_a + " " + chessPoint_b + " " + color);
this.add(chesspoint_white);
chesspoint_white.setBounds(chessPoint_a * 20 - 7,
chessPoint_b * 20 - 7, 16, 16);
statusText.setText("白(第" + chessWhiteCount + "步)"
+ chessPoint_a + " " + chessPoint_b + ",请黑棋下子");
isMouseEnabled = false;
} else {
chessthread.sendMessage("/" + chessPeerName + " /chess "
+ chessPoint_a + " " + chessPoint_b + " " + color);
this.add(chesspoint_white);
chesspoint_white.setBounds(chessPoint_a * 20 - 7,
chessPoint_b * 20 - 7, 16, 16);
chessVictory(-1);
isMouseEnabled = false;
}
}
}
/**
* 落子时在对方客户端绘制棋子。 对方接受到发送来的落子信息,调用此函数绘制棋子,显示落子状态等等
*/
public void netChessPaint(int chessPoint_a, int chessPoint_b, int color) {
chessPoint_black chesspoint_black = new chessPoint_black(this);
chessPoint_white chesspoint_white = new chessPoint_white(this);
getLocation(chessPoint_a, chessPoint_b, color);
if (color == 1) {
isWin = checkWin(chessPoint_a, chessPoint_b, color);
if (isWin == false) {
this.add(chesspoint_black);
chesspoint_black.setBounds(chessPoint_a * 20 - 7,
chessPoint_b * 20 - 7, 16, 16);
statusText.setText("黑(第" + chessBlackCount + "步)"
+ chessPoint_a + " " + chessPoint_b + ",请白棋下子");
isMouseEnabled = true;
} else {
this.add(chesspoint_black);
chesspoint_black.setBounds(chessPoint_a * 20 - 7,
chessPoint_b * 20 - 7, 16, 16);
chessVictory(1);
isMouseEnabled = true;
}
} else if (color == -1) {
isWin = checkWin(chessPoint_a, chessPoint_b, color);
if (isWin == false) {
this.add(chesspoint_white);
chesspoint_white.setBounds(chessPoint_a * 20 - 7,
chessPoint_b * 20 - 7, 16, 16);
statusText.setText("白(第" + chessWhiteCount + "步)"
+ chessPoint_a + " " + chessPoint_b + ",请黑棋下子");
isMouseEnabled = true;
} else {
chessthread.sendMessage("/" + chessPeerName + " /victory "
+ color);
this.add(chesspoint_white);
chesspoint_white.setBounds(chessPoint_a * 20 - 7,
chessPoint_b * 20 - 7, 16, 16);
chessVictory(-1);
isMouseEnabled = true;
}
}
}
/**
* 当鼠标按下时响应的动作。记下当前鼠标点击的位置,即当前落子的位置。
*/
public void mousePressed(MouseEvent e) {
if (e.getModifiers() == InputEvent.BUTTON1_MASK) {
chessPoint_x = (int) e.getX();
chessPoint_y = (int) e.getY();
int a = (chessPoint_x + 10) / 20, b = (chessPoint_y + 10) / 20;
if (chessPoint_x / 20 < 2 || chessPoint_y / 20 < 2
|| chessPoint_x / 20 > 19 || chessPoint_y / 20 > 19) {
} else {
chessPaint(a, b, chessColor);
}
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
}
/**
* 表示黑子的类
*/
class chessPoint_black extends Canvas {
chessPad chesspad = null;
chessPoint_black(chessPad p) {
setSize(20, 20);
chesspad = p;
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillOval(0, 0, 14, 14);
}
}
/**
* 表示白子的类
*/
class chessPoint_white extends Canvas {
chessPad chesspad = null;
chessPoint_white(chessPad p) {
setSize(20, 20);
chesspad = p;
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillOval(0, 0, 14, 14);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -