package indi.peter.game.gobang;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
public class GameUI extends JFrame{
private static final long serialVersionUID = -1607013426641393481L;
/**
* 棋子的直径
*/
private static final int CHESS_PIECE_DIAMETER = 40;
/**
* 正方形棋盘的边长. 边长 = 棋子的直径长度 * ( 18 + 2)
*/
private static final int CHESS_BOARD_SIDE_LENGTH = CHESS_PIECE_DIAMETER * (18 + 2);
/**
* 功能区的高度
*/
private static final int FUNCTION_AREA_HEIGHT = 40;
/**
* 功能区与棋盘的间隔高度
*/
private static final int GAP_HEIGHT_BETWEEN_FUNCTION_AREA_AND_CHESS_BOARD = 10;
/**
* 棋盘离UI边界的宽度
*/
private static final int GAP_WIDTH_BETWEEN_UI_AND_CHESS_BOARD = 20;
/**
* UI的宽. 宽 = 棋盘离UI边界的宽度 * 2 + 棋盘的宽度
*/
private static final int UI_WIDTH = GAP_WIDTH_BETWEEN_UI_AND_CHESS_BOARD * 2 + CHESS_BOARD_SIDE_LENGTH;
/**
* UI的高, 高 = 功能区高度 + 功能区和棋盘的间隙 + 棋盘的边长 + 棋盘离UI边界的宽度 * 2
*/
private static final int UI_HEIGHT = FUNCTION_AREA_HEIGHT + GAP_HEIGHT_BETWEEN_FUNCTION_AREA_AND_CHESS_BOARD + CHESS_BOARD_SIDE_LENGTH + GAP_WIDTH_BETWEEN_UI_AND_CHESS_BOARD * 2;
/* 功能区, 放置游戏开始和信息提示的组件 */
private JPanel functionAreaPanel;
private JButton gameStartBtn;
private static JLabel infoLabel;
/* 棋盘 */
private JPanel chessBoard;
/* 落子区,共有19*19个区域 */
private static ChessPieceDropArea[][] chessPieceDropArea;
public void initUI() {
Container container = getContentPane();
// Frame设定
container.setLayout(null);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 将游戏面板放在显示器中间
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setBounds((dim.width - UI_WIDTH) / 2, (dim.height - UI_HEIGHT) / 2, UI_WIDTH + 10, UI_HEIGHT + 35);
// 画功能区
functionAreaPanel = new JPanel();
functionAreaPanel.setSize(UI_WIDTH, FUNCTION_AREA_HEIGHT);
functionAreaPanel.setBackground(Color.white);
gameStartBtn = new JButton("重新开始");
functionAreaPanel.add(gameStartBtn);
infoLabel = new JLabel();
infoLabel.setBorder(new LineBorder(Color.GRAY));
functionAreaPanel.add(infoLabel);
container.add(functionAreaPanel);
// 按钮添加点击事件
gameStartBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
GameUI.clearPieces();
}
});
showInfoOfDropBlackPiece();
// 画棋盘
chessBoard = new ChessBoardPanel(CHESS_PIECE_DIAMETER);
chessBoard.setLayout(null);
chessBoard.setBounds(GAP_WIDTH_BETWEEN_UI_AND_CHESS_BOARD
, FUNCTION_AREA_HEIGHT + GAP_HEIGHT_BETWEEN_FUNCTION_AREA_AND_CHESS_BOARD + GAP_WIDTH_BETWEEN_UI_AND_CHESS_BOARD
, CHESS_BOARD_SIDE_LENGTH
, CHESS_BOARD_SIDE_LENGTH);
chessBoard.setBackground(Color.white);
chessBoard.setBorder(new LineBorder(Color.gray, 1));
container.add(chessBoard);
// 画落子区
chessPieceDropArea = new ChessPieceDropArea[19][19];
for(int i = 0; i < 19; i++) {
for(int j = 0; j < 19; j++) {
ChessPieceDropArea chessPieceDropAreaObj = new ChessPieceDropArea(i, j, CHESS_PIECE_DIAMETER);
chessPieceDropArea[i][j] = chessPieceDropAreaObj;
chessPieceDropAreaObj.setBounds(CHESS_PIECE_DIAMETER/2+i*CHESS_PIECE_DIAMETER
, CHESS_PIECE_DIAMETER/2+j*CHESS_PIECE_DIAMETER
, CHESS_PIECE_DIAMETER
, CHESS_PIECE_DIAMETER);
chessPieceDropAreaObj.setOpaque(false);
// chessPieceDropAreaObj.setBorder(new LineBorder(Color.black, 1));
chessBoard.add(chessPieceDropAreaObj);
}
}
}
public static void showInfoOfDropWhitePiece() {
infoLabel.setText("请下白棋");
}
public static void showInfoOfDropBlackPiece() {
infoLabel.setText("请下黑棋");
}
public static void clearPieces() {
ChessPieceDropArea.chessPieceColor = 0;
for(int i = 0; i < 19; i++) {
for(int j = 0; j < 19; j++) {
ChessPieceDropArea chessPieceDropAreaObj = chessPieceDropArea[i][j];
chessPieceDropAreaObj.isDroped = false;
chessPieceDropAreaObj.isWhite = false;
chessPieceDropAreaObj.repaint();
showInfoOfDropBlackPiece();
}
}
}
// true表示已经判断出结果, 对奕结束
public static boolean judge(boolean isWhite) {
System.out.println("isWhite="+isWhite);
// 检查横线上是否有五个相同的棋子
int count = 0;
for(int i = 0; i < 19; i++) {
for(int j = 0; j < 19; j++) {
ChessPieceDropArea chessPieceDropAreaObj = chessPieceDropArea[i][j];
if(!chessPieceDropAreaObj.isDroped || chessPieceDropAreaObj.isWhite != isWhite) {
// 没有落子, 或者已经断开, 重新从0计数
if(count >=5) {
return true;
}
count = 0;
}else {
count++;
if(count >=5) {
return true;
}
}
}
// 新的一行
count = 0;
}
// 检查每一列是否有五个相同的棋子
count = 0;
for(int i = 0; i < 19; i++) {
for(int j = 0; j < 19; j++) {
ChessPieceDropArea chessPieceDropAreaObj = chessPieceDropArea[j][i];
if(!chessPieceDropAreaObj.isDroped || chessPieceDropAreaObj.isWhite != isWhite) {
// 没有落子, 或者已经断开, 重新从0计数
if(count >=5) {
return true;
}
count = 0;
}else {
count++;
if(count >=5) {
return true;
}
}
}
// 新的一行
count = 0;
}
// 检查斜线上是否有五个相同的棋子
count = 0;
for(int i = 18; i > 0; i--) {
int tmp = i;
for(int j = 0; j < 19 && tmp < 19; j++) {
ChessPieceDropArea chessPieceDropAreaObj = chessPieceDropArea[tmp][j];
if(!chessPieceDropAreaObj.isDroped || chessPieceDropAreaObj.isWhite != isWhite) {
// 没有落子, 或者已经断开, 重新从0计数
if(count >=5) {
return true;
}
count = 0;
}else {
count++;
if(count >=5) {
return true;
}
}
tmp++;
}
}
// 检查反斜线上是否有五个相同的棋子
count = 0;
for(int i = 0; i < 19; i++) {
int tmp = i;
for(int j = 0; j < 19 && tmp >= 0; j++) {
ChessPieceDropArea chessPieceDropAreaObj = chessPieceDropArea[tmp][j];
if(!chessPieceDropAreaObj.isDroped || chessPieceDropAreaObj.isWhite != isWhite) {
// 没有落子, 或者已经断开, 重新从0计数
if(count >=5) {
return true;
}
count = 0;
}else {
count++;
if(count >=5) {
return true;
}
}
tmp--;
}
}
return false;
}
}