?? 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 + -