import java.awt.Color;
import java.awt.Graphics;
public class Board {
private int[][] board = new int[15][15];
public Board() {
}
public void reset() {
for(int i = 0; i < board.length; i++) { // 行
for(int j = 0; j < board[i].length; j++) { // 列
board[i][j] = 0;
}
}
}
/**
* 判斷當前走棋放是否獲得勝利
* @param row 落子的行
* @param col 落子的列
* @param isBlack 是否黑棋走棋
* @return 返回true表示獲得勝利, 否則返回false
*/
public boolean judge(int row, int col, boolean isBlack) {
return checkHLine(row, col, isBlack) || checkVLine(row, col, isBlack)
|| checkX1Line(row, col, isBlack) || checkX2Line(row, col, isBlack);
}
// 檢查從右上到左下的對角線上是否有五顆棋子
private boolean checkX2Line(int row, int col, boolean isBlack) {
int currentPiece = isBlack ? 1 : 2;
int counter = 1;
int currentCol = col;
int currentRow = row;
while(currentRow < 15 && currentCol > 0 && (board[++currentRow][--currentCol] == currentPiece)) {
counter++;
}
currentCol = col;
currentRow = row;
while(currentRow > 0 && currentCol < 15 && (board[--currentRow][++currentCol] == currentPiece)) {
counter++;
}
return counter >= 5;
}
// 檢查從左上到右下的對角線上是否有五顆棋子
private boolean checkX1Line(int row, int col, boolean isBlack) {
int currentPiece = isBlack ? 1 : 2;
int counter = 1;
int currentCol = col;
int currentRow = row;
while(currentRow > 0 && currentCol > 0 && (board[--currentRow][--currentCol] == currentPiece)) {
counter++;
}
currentCol = col;
currentRow = row;
while(currentRow < 15 && currentCol < 15 && (board[++currentRow][++currentCol] == currentPiece)) {
counter++;
}
return counter >= 5;
}
// 檢查垂直方向上是否有五顆棋子
private boolean checkVLine(int row, int col, boolean isBlack) {
int currentPiece = isBlack ? 1 : 2;
int counter = 1;
int currentRow = row;
while(currentRow > 0 && (board[--currentRow][col] == currentPiece)) {
counter++;
}
currentRow = row;
while(currentRow < 15 && (board[++currentRow][col] == currentPiece)) {
counter++;
}
return counter >= 5;
}
// 檢查水平方向是否有五顆棋子
private boolean checkHLine(int row, int col, boolean isBlack) {
int currentPiece = isBlack ? 1 : 2;
int counter = 1;
int currentCol = col;
while(currentCol > 0 && (board[row][--currentCol] == currentPiece)) {
counter++;
}
currentCol = col;
while(currentCol < 15 && (board[row][++currentCol] == currentPiece)) {
counter++;
}
return counter >= 5;
}
/**
* 在棋盤上放置棋子
* @param row 行
* @param col 列
* @param isBlack 是否是黑棋
* @return 返回true表示落子有效, 否則落子無效
*/
public boolean addPiece(int row, int col, boolean isBlack) {
if(board[row][col] == 0) {
board[row][col] = isBlack? 1 : 2;
return true;
}
return false;
}
public void draw(Graphics g) {
for(int i = 0; i < board.length; i++) { // 行
for(int j = 0; j < board[i].length; j++) { // 列
if(board[i][j] != 0) {
if(board[i][j] == 1) { // 黑棋
g.setColor(Color.BLACK);
}
else if(board[i][j] == 2) { // 白棋
g.setColor(Color.WHITE);
}
g.fillOval(35 * (j + 1), 35 * (i + 1), 30, 30);
}
}
}
}
}
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
@SuppressWarnings("serial")
public class GameFrame extends JFrame {
private Board b = new Board();
private boolean isBlackTurn = true; // 是否輪到黑棋走棋
private boolean isWin = false;
private JButton resetButton = null;
public GameFrame() {
this.setTitle("五子棋");
this.setSize(600, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(null);
resetButton = new JButton("R");
resetButton.setFont(new Font("Times New Roman", Font.BOLD, 12));
resetButton.setBounds(540, 540, 50, 35);
resetButton.setEnabled(false);
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
b.reset();
isWin = false;
resetButton.setEnabled(false);
repaint();
}
});
this.add(resetButton);
// 添加鼠標事件監聽器
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (!isWin) {
int x = e.getX();
int y = e.getY();
// 判斷點擊位置是否在棋盤以內
if (x >= 50 && x <= 540 && y >= 50 && y <= 540) {
// 計算在第幾行第幾列點下了鼠標
int row = Math.round((y - 50) / 35.0f);
int col = Math.round((x - 50) / 35.0f);
if (b.addPiece(row, col, isBlackTurn)) { // 向棋盤添加棋子
repaint(); // 重繪窗口
if (b.judge(row, col, isBlackTurn)) {
JOptionPane.showMessageDialog(null,
isBlackTurn ? "黑棋勝" : "白棋勝");
isWin = true;
resetButton.setEnabled(true);
repaint();
} else {
isBlackTurn = !isBlackTurn; // 交換走棋
}
}
}
}
}
});
}
public void paint(Graphics g) {
super.paint(g);
// 將邊框加粗
g.fillRect(48, 48, 2, 492);
g.fillRect(48, 48, 492, 2);
g.fillRect(48, 540, 492, 2);
g.fillRect(540, 48, 2, 494);
// 繪製棋盤上的線條
for (int i = 0; i < 15; i++) {
g.drawLine(50, 50 + 35 * i, 540, 50 + 35 * i); // 橫線
g.drawLine(50 + 35 * i, 50, 50 + 35 * i, 540); // 縱線
}
// 標出天元
g.fillOval(290, 290, 10, 10);
b.draw(g);
}
public static void main(String[] args) {
new GameFrame().setVisible(true);
}
}