type = box.getSelectedItem().toString(); // 獲取選擇的對戰模式
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("開始新遊戲")) {
gf.addMouseListener(this);
for (int i = 0; i < gf.isArrive.length; i++)
for (int j = 0; j < gf.isArrive[i].length; j++)
gf.isArrive[i][j] = 0; // 初始化存儲棋子的數組使其恢復到初始狀態
box.setEnabled(false); // 讓下拉可選框鎖定
gf.repaint();
}
else if (e.getActionCommand().equals("悔棋")) {
if (list.size() > 0) {
// 從list列表中獲取最後一顆棋子的位置
chess lastchess = list.remove(list.size() - 1); //得到上一步棋的位置
gf.isArrive[lastchess.r][lastchess.c] = 0; //讓該位置的數組置零
if (turn == 1)
turn++;
else
turn--;
gf.repaint();
}
}
else if (e.getActionCommand().equals("認輸")) {
if (turn == 1)
JOptionPane.showMessageDialog(gf, "黑棋認輸,白棋獲勝!");
else
JOptionPane.showMessageDialog(gf, "白棋認輸,黑棋獲勝!");
gf.removeMouseListener(this); //移除監聽 ( 按道理來說不應該在分出勝負以後棋盤上還能落子,所以應該移除監聽
box.setEnabled(true);
} else if (e.getSource() instanceof JComboBox) {
JComboBox<String> box = (JComboBox<String>) e.getSource();// 獲取事件源對象
type = box.getSelectedItem().toString(); // 獲取選擇的對戰模式
}
}
public void PERSONPLAY(int x, int y) { // 人下的方法 // 計算棋子要落的交叉點 int countx = (y - 20 + size / 2) / size; int county = (x - 20 + size / 2) / size; g = (Graphics2D) gf.getGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 抗鋸齒,讓棋子更加圓潤 int arrivex, arrivey; // 棋盤上的落點 arrivex = 20 + county * size; arrivey = 20 + countx * size; if (gf.isArrive[countx][county] != 0) // 有棋子,不可下棋 { JOptionPane.showMessageDialog(gf, "此處已有棋子,請換一個地方"); } else {// 當前位置可以下棋 if (turn == 1) { // 設置顏色 g.setColor(Color.black); // 下棋 g.fillOval(arrivex - size / 2, arrivey - size / 2, size, size); gf.isArrive[countx][county] = 1; turn++; } else { // 設置顏色 g.setColor(Color.white); // 下棋 g.fillOval(arrivex - size / 2, arrivey - size / 2, size, size); gf.isArrive[countx][county] = 2; turn--; } // list.add(new chess(countx, county));// 有序地存儲每個棋子的行列,爲悔棋做準備 // 判斷輸贏 if (Gobangwin.judge(gf.isArrive, countx, county)) { if (turn == 2) JOptionPane.showMessageDialog(gf, "黑棋勝利"); else JOptionPane.showMessageDialog(gf, "白棋勝利"); gf.removeMouseListener(this); //移除監聽 ( 按道理來說不應該在分出勝負以後棋盤上還能落子,所以應該移除監聽 } } }
package GoBang;
public class Gobangwin {
public static boolean judge(int[][]isArrive, int r, int c){ //如果五子相連就返回true,否則就返回false
if(countx(isArrive,r,c)>=5||county(isArrive,r,c)>=5||countxy1(isArrive,r,c)>=5||countxy2(isArrive,r,c)>=5)
return true;
else return false;
}
//計算豎直方向是否五子相連
private static int countx(int[][] isArrive, int r, int c) {
int count = 1;
for (int r1 = r - 1; r1 >= 0; r1--)
if (isArrive[r][c] == isArrive[r1][c])
count++;
else
break;
for (int r1 = r + 1; r1 < isArrive.length; r1++)
if (isArrive[r][c] == isArrive[r1][c])
count++;
else
break;
return count;
}
//計算水平方向是否五子相連
private static int county(int[][] isArrive, int r, int c) {
int count = 1;
for (int c1 = c - 1; c1 >= 0; c1--)
if (isArrive[r][c] == isArrive[r][c1])
count++;
else
break;
for (int c1 = c + 1; c1 < isArrive[r].length; c1++)
if (isArrive[r][c] == isArrive[r][c1])
count++;
else
break;
return count;
}
//計算左上至右下斜的方向是否五子相連
private static int countxy1(int[][] isArrive, int r, int c) {
int count = 1; //往左上角走
for (int r1 = r - 1,c1=c-1; r1 >= 0&&c1>=0; r1--,c1--)
if (isArrive[r][c] == isArrive[r1][c1])
count++;
else
break;
//往右下角走
for (int r1 = r + 1,c1=c+1; r1 < isArrive.length&&c1<isArrive[r].length; r1++,c1++)
if (isArrive[r][c] == isArrive[r1][c1])
count++;
else
break;
return count;
}
//計算右上至左下斜的方向是否五子相連
private static int countxy2(int[][] isArrive, int r, int c) {
int count = 1; //往右上角走
for (int r1 = r - 1,c1=c+1; r1 >= 0&&c1<isArrive[r].length; r1--,c1++)
if (isArrive[r][c] == isArrive[r1][c1])
count++;
else
break;
//往左下角走
for (int r1 = r + 1,c1=c-1; r1 < isArrive.length&&c1>=0; r1++,c1--)
if (isArrive[r][c] == isArrive[r1][c1])
count++;
else
break;
return count;
}
}
package GoBang;
public interface GoBangconfig {
public static int size=30,column=15,row=15,x=20,y=20;
}