📄 chesspad.java
字号:
break;
}
for (step = 1; step <= 4; step++) {
for (chessCompare = 0; chessCompare <= chessWhiteCount; chessCompare++) {
if (((a - step) * 20 == chessWhite_x[chessCompare]) && (b * 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 * 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;
}
}
//如果任何方向都没有连子超过5个,则尚不能分出胜负
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) {//白色棋子没有胜出
//将白色棋子添加到Panel,并设定显示位置
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 + -