?? chesspad.java
字號:
package chessface;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
/**
* 顯示棋盤的Panel。此Panel實現了鼠標監聽器
*/
public class chessPad extends Panel implements MouseListener {
public int chessPoint_x = -1, chessPoint_y = -1, chessColor = 1;
int chessBlack_x[] = new int[200];// 黑子的x坐標
int chessBlack_y[] = new int[200];// 黑子的y坐標
int chessWhite_x[] = new int[200];// 白子的x坐標
int chessWhite_y[] = new int[200];// 白子的y坐標
int chessBlackCount = 0, chessWhiteCount = 0;
int chessBlackWin = 0, chessWhiteWin = 0;
public boolean isMouseEnabled = false, isWin = false, isInGame = false;
public Label statusLabel = new Label("客戶端狀態");
public TextField statusText = new TextField("請先連接服務器");// 顯示客戶端狀態的文本框
public Socket chessSocket;
DataInputStream inData;
DataOutputStream outData;
public String chessSelfName = null;// 己方的名字
public String chessPeerName = null;// 對方的名字
public String host = null; //服務器ip地址
public int port = 4331; //連接的端口號
public chessThread chessthread = new chessThread(this);
/**
* 棋盤Panel的構造函數
*/
public chessPad() {
// 設置大小
setSize(440, 440);
// 設置布局
setLayout(null);
// 背景顏色
setBackground(new Color(204, 204, 204));
// 添加鼠標監聽器
addMouseListener(this);
// 添加狀態Label
add(statusLabel);
// 設置狀態Label大小
statusLabel.setBounds(30, 5, 70, 24);
// 顯示客戶端狀態的文本框
add(statusText);
statusText.setBounds(100, 5, 300, 24);
// 設置為不可編輯
statusText.setEditable(false);
}
/**
* 和服務器通信的函數
*/
public boolean connectServer(String ServerIP, int ServerPort) throws Exception {
try {
// 利用參數創建一個Socket的實例來完成和服務器之間的信息交換
// 連接服務器.
chessSocket = new Socket(ServerIP, ServerPort);
inData = new DataInputStream(chessSocket.getInputStream());
outData = new DataOutputStream(chessSocket.getOutputStream());
// 啟動一個用戶線程
chessthread.start();
return true;
} catch (IOException ex) {
statusText.setText("chessPad:connectServer:無法連接 \n");
}
return false;
}
/**
* 一方獲勝時的對棋局的處理
*/
public void chessVictory(int chessColorWin) {
// 清除所有的棋子
this.removeAll();
// 將保存所有黑棋和白棋的位置坐標的數組清空,為新一盤棋做準備。
for (int i = 0; i <= chessBlackCount; i++) {
chessBlack_x[i] = 0;
chessBlack_y[i] = 0;
}
for (int i = 0; i <= chessWhiteCount; i++) {
chessWhite_x[i] = 0;
chessWhite_y[i] = 0;
}
// 白棋、黑棋數量清空
chessBlackCount = 0;
chessWhiteCount = 0;
// 重新添加狀態信息
add(statusText);
statusText.setBounds(40, 5, 360, 24);
// 如果黑棋獲勝,計算雙方獲勝盤數,將雙方的戰績比在狀態文本框顯示出來。
if (chessColorWin == 1) {
chessBlackWin++;// 黑棋獲勝次數+1
statusText.setText("黑棋勝,黑:白為" + chessBlackWin + ":" + chessWhiteWin + ",重新開局,等待白棋下子...");
}
// 白棋獲勝,同上。
else if (chessColorWin == -1) {
chessWhiteWin++;
statusText.setText("白棋勝,黑:白為" + chessBlackWin + ":" + chessWhiteWin + ",重新開局,等待黑棋下子...");
}
}
/**
* 將各個棋子的坐標保存在數組里
*/
public void getLocation(int a, int b, int color) {
if (color == 1) {// 黑子
chessBlack_x[chessBlackCount] = a * 20;
chessBlack_y[chessBlackCount] = b * 20;
chessBlackCount++;
} else if (color == -1) {// 白子
chessWhite_x[chessWhiteCount] = a * 20;
chessWhite_y[chessWhiteCount] = b * 20;
chessWhiteCount++;
}
}
/**
* 依據五子棋的行棋規則判斷某方獲勝。具體就是判斷橫、豎、左斜、右斜方向特定顏色的棋子連接的總數是否超過5個,如果超過5個就為贏。
* 在每個方向的判斷又分為對正向、和反向分別統計計數。
*/
public boolean checkWin(int a, int b, int checkColor) {
int step = 1, chessLink = 1, chessLinkTest = 1, chessCompare = 0;
if (checkColor == 1) {// 黑棋方
chessLink = 1;// 連子個數
//////////////////////////////
// 以下兩個大的循環判斷豎直方向棋子連接總數
//////////////////////////////
// 向下的方向連子個數
for (step = 1; step <= 4; step++) {
// 下層循環判斷豎直向下第step個位置是否有白子
for (chessCompare = 0; chessCompare <= chessBlackCount; chessCompare++) {
// 如果有黑子
if (((a + step) * 20 == chessBlack_x[chessCompare]) && ((b * 20) == chessBlack_y[chessCompare])) {
chessLink = chessLink + 1;
if (chessLink == 5) {
return (true);
}
}
}
if (chessLink == (chessLinkTest + 1))// 豎直向下還有黑色棋子
chessLinkTest++;
else
break;
}
//向上的連子個數
for (step = 1; step <= 4; step++) {
//下層循環判斷豎直向上第step個位置是否有黑子
for (chessCompare = 0; chessCompare <= chessBlackCount; chessCompare++) {
//如果有黑子(其實這里可以break出來)
if (((a - step) * 20 == chessBlack_x[chessCompare]) && (b * 20 == chessBlack_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++) {
//右側第step個位置是否有同色棋子
for (chessCompare = 0; chessCompare <= chessBlackCount; chessCompare++) {
//如果有的話(讀者可以思考,這里可否break出來)
if ((a * 20 == chessBlack_x[chessCompare]) && ((b + step) * 20 == chessBlack_y[chessCompare])) {
chessLink++;
if (chessLink == 5) {
return (true);
}
}
}
if (chessLink == (chessLinkTest + 1))//下個位置是否找到棋子
chessLinkTest++;
else
break;
}
//橫向左側的連子數統計
for (step = 1; step <= 4; step++) {
//左側第step個位置是否有同色棋子
for (chessCompare = 0; chessCompare <= chessBlackCount; chessCompare++) {
//如果找到同色棋子
if ((a * 20 == chessBlack_x[chessCompare]) && ((b - step) * 20 == chessBlack_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 <= chessBlackCount; chessCompare++) {
if (((a - step) * 20 == chessBlack_x[chessCompare])
&& ((b + step) * 20 == chessBlack_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 <= chessBlackCount; chessCompare++) {
if (((a + step) * 20 == chessBlack_x[chessCompare])
&& ((b - step) * 20 == chessBlack_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 <= chessBlackCount; chessCompare++) {
if (((a + step) * 20 == chessBlack_x[chessCompare])
&& ((b + step) * 20 == chessBlack_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 <= chessBlackCount; chessCompare++) {
if (((a - step) * 20 == chessBlack_x[chessCompare])
&& ((b - step) * 20 == chessBlack_y[chessCompare])) {
chessLink++;
if (chessLink == 5) {
return (true);
}
}
}
if (chessLink == (chessLinkTest + 1))
chessLinkTest++;
else
break;
}
} else if (checkColor == -1) {
//如果是白色棋子,同黑色棋子一樣,對橫、豎、左斜、右斜
//幾個方向統計,如果有任何一個方向連子個數超過5個,則為贏。
//只不過檢查下個特定位置是否有特定顏色棋子時,要使用白棋對應坐標數組。
//其他同對黑棋的判斷一摸一樣:),注釋可以參考對黑棋判斷部分。
////////////////////////////////////
//豎向統計
////////////////////////////////////
chessLink = 1;
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
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -