import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Gomoku extends JFrame {
privatefinal int CHESSBOARD_SIZE = 15;
privateJLabel jblWhite = new JLabel("White: 0s");
privateJLabel jblBlack = new JLabel("Black: 0s");
privateJButton jbtIcon = new JButton("Start!");
privateChessBoard chessboard = new ChessBoard();
privateint[][] grid = new int[CHESSBOARD_SIZE][CHESSBOARD_SIZE]; // 1 forwhite, 2 for black
privateboolean gameStatue = false; // to show statue ofgame: true for continue, false for end.
privateboolean isWinned = false;
privateboolean player = true; // true for white, false for black
private intwhiteTime = 0; // record white player's playing time(unitesecond)
private intblackTime = 0; // record black player's playing time(unitesecond)
privateTimer time = new Timer(1000, new TimerListener());
publicGomoku() {
for (int i = 0; i < grid.length;i++)
for (int j = 0; j <grid[0].length; j++)
grid[i][j] = 0;
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1, 3, 20, 5));
p1.add(jblWhite);
p1.add(jbtIcon);
p1.add(jblBlack);
jbtIcon.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if (!gameStatue&& !isWinned) {
gameStatue = true;
time.start();
jbtIcon.setText("New game");
}
else {
setDefaultStatue();
chessboard.repaint();
}
}
});
add(p1, BorderLayout.NORTH);
add(chessboard, BorderLayout.CENTER);
}
privateclass TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e){
if (player) {
whiteTime++;
if (whiteTime < 60)
jblWhite.setText("White: " + String.valueOf(whiteTime) +"s");
else if (whiteTime < 3600)
jblWhite.setText("White: " +String.valueOf(whiteTime / 60) + "min" + String.valueOf(whiteTime %60) + "s");
else
jblWhite.setText("White: " +String.valueOf(whiteTime / 3600) + "h" + String.valueOf((whiteTime% 3600) / 60) + "min" + String.valueOf(whiteTime % 60) +"s");
}
else {
blackTime++;
if (blackTime < 60)
jblBlack.setText("Black: " + String.valueOf(blackTime) +"s");
else if (blackTime < 3600)
jblBlack.setText("Black: " +String.valueOf(blackTime / 60) + "min" + String.valueOf(blackTime %60) + "s");
else
jblBlack.setText("Black: " +String.valueOf(blackTime / 3600) + "h" + String.valueOf((blackTime% 3600) / 60) + "min" + String.valueOf(blackTime % 60) +"s");
}
}
}
public voidsetDefaultStatue() {
for (int i = 0; i < grid.length;i++)
for (int j = 0; j <grid[0].length; j++)
grid[i][j] = 0;
gameStatue = false;
isWinned = false;
player = true;
time.stop();
whiteTime = 0;
blackTime = 0;
jblWhite.setText("White: 0s");
jblBlack.setText("Black: 0s");
jbtIcon.setText("Start!");
}
publicstatic void main(String[] args) {
Gomoku frame = new Gomoku();
frame.setTitle("Gomoku Game");
frame.setSize(400, 440);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
// checkwhether any five chess in a line are same color
publicstatic boolean checkFiveSame(int[][] grid) {
for (int i = 0; i < grid.length;i++) {
for (int j = 0; j <grid[0].length; j++) {
if (i < (grid.length - 4))
if (grid[i][j] != 0
&& grid[i][j] ==grid[i + 1][j]
&& grid[i][j] ==grid[i + 2][j]
&& grid[i][j] ==grid[i + 3][j]
&& grid[i][j] ==grid[i + 4][j])
return true;
if (j < (grid[0].length -4))
if (grid[i][j] != 0
&& grid[i][j] ==grid[i][j + 1]
&& grid[i][j] ==grid[i][j + 2]
&& grid[i][j] ==grid[i][j + 3]
&& grid[i][j] ==grid[i][j + 4])
return true;
if (i < (grid.length -4) && j< (grid[0].length - 4))
if (grid[i][j] != 0
&& grid[i][j] ==grid[i + 1][j + 1]
&& grid[i][j] ==grid[i + 2][j + 2]
&& grid[i][j] ==grid[i + 3][j + 3]
&& grid[i][j] ==grid[i + 4][j + 4])
return true;
if (i < (grid.length - 4)&& j >= 4)
if (grid[i][j] != 0
&& grid[i][j] ==grid[i + 1][j - 1]
&& grid[i][j] ==grid[i + 2][j - 2]
&& grid[i][j] ==grid[i + 3][j - 3]
&& grid[i][j] ==grid[i + 4][j - 4])
return true;
}
}
return false;
}
classChessBoard extends JPanel {
private int width;
private int height;
private int widthStep;
private int heightStep;
private int xStart;
private int yStart;
private int xEnd;
private int yEnd;
public ChessBoard() {
setBackground(Color.GRAY);
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (gameStatue) {
int mouseX = 0;
int mouseY = 0;
int gridX = 0;
int gridY = 0;
mouseX = e.getX();
mouseY = e.getY();
if (mouseX >= xStart&& mouseX <= xEnd&& mouseY >= yStart&& mouseY <= yEnd){
gridX = (mouseX - xStart) / widthStep;
gridY = (mouseY - yStart) / heightStep;
if (player) {
if (grid[gridX][gridY] == 0) {
grid[gridX][gridY] = 1;
repaint();
if (checkFiveSame(grid)) {
jbtIcon.setText("White win!");
gameStatue = false;
isWinned = true;
time.stop();
}
else if (checkChessboardFull(grid)) {
jbtIcon.setText("Game end!");
gameStatue = false;
time.stop();
}
player = !player;
}
}
else {
if (grid[gridX][gridY] == 0) {
grid[gridX][gridY] = 2;
repaint();
if (checkFiveSame(grid)) {
jbtIcon.setText("Black win!");
gameStatue = false;
isWinned = true;
time.stop();
}
else if (checkChessboardFull(grid)) {
jbtIcon.setText("Game end!");
gameStatue = false;
time.stop();
}
player = !player;
}
}
}
}
}
});
}
private boolean checkChessboardFull(int[][]grids) {
for (int i = 0; i < grids.length;i++) {
for (int j = 0; j <grids[0].length; j++) {
if (grids[i][j] == 0)
return false;
}
}
return true;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
width = (int)(getWidth() * 0.98);
height = (int)(getHeight() * 0.98);
widthStep = width / CHESSBOARD_SIZE;
heightStep = height / CHESSBOARD_SIZE;
xStart = (int)(width * 0.01);
yStart = (int)(height * 0.01);
xEnd = xStart + CHESSBOARD_SIZE *widthStep;
yEnd = yStart + CHESSBOARD_SIZE *heightStep;
// draw chessboard
g.setColor(Color.BLACK);
g.drawLine(xStart, yStart, xStart, yEnd);
g.drawLine(xStart, yStart, xEnd, yStart);
g.drawLine(xEnd, yStart, xEnd, yEnd);
g.drawLine(xStart, yEnd, xEnd, yEnd);
for (int i = 1; i <CHESSBOARD_SIZE; i++) {
g.drawLine(xStart + i * widthStep, yStart, xStart+ i * widthStep, yEnd);
g.drawLine(xStart, yStart + i * heightStep, xEnd,yStart + i * heightStep);
}
// draw chess
int chessRadius = (int)(Math.min(widthStep,heightStep) * 0.9 *0.5);
for (int i = 0; i < grid.length;i++) {
for (int j = 0; j <grid[0].length; j++) {
if (grid[i][j] == 1) {
g.setColor(Color.WHITE);
int xCenter = (int)(xStart + (i + 0.5) * widthStep);
int yCenter = (int)(yStart + (j + 0.5) * heightStep);
g.fillOval(xCenter - chessRadius, yCenter - chessRadius, 2 *chessRadius, 2 * chessRadius);
}
else if (grid[i][j] == 2) {
g.setColor(Color.BLACK);
int xCenter = (int)(xStart + (i + 0.5) *widthStep);
int yCenter = (int)(yStart + (j + 0.5) * heightStep);
g.fillOval(xCenter - chessRadius, yCenter - chessRadius, 2 *chessRadius, 2 * chessRadius);
}
}
}
}
}
}
一个五子棋的java程序
最新推荐文章于 2024-04-20 22:23:53 发布