还没有判断胜负的功能,现阶段就这样了。
package com.lovoinfo;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
/**
* 棋盘
* @author 谷振东
*
*/
public class Board {
private static final int CELL_SIZE = 40;
private CellState[][] board = new CellState[15][15];
private int lastRow = -1, lastCol = -1;
public Board() {
reset();
}
/**
* 走棋
* @param row 落子的行
* @param col 落子的列
* @param isBlack 是不是黑棋
* @return 如果落子成功返回true否则返回false
*/
public boolean move(int row, int col, boolean isBlack) {
if(board[row][col] == CellState.Empty) {
board[row][col] = isBlack? CellState.Black : CellState.White;
lastRow = row;
lastCol = col;
return true;
}
return false;
}
/**
* 绘制棋盘和棋子
* @param g 画笔
*/
public void draw(Graphics g) {
for(int i = 0; i < board.length; i++) {
g.drawLine(30, 30 + CELL_SIZE * i, 590, 30 + CELL_SIZE * i);
g.drawLine(30 + CELL_SIZE * i, 30, 30 + CELL_SIZE * i, 590);
}
g.fillOval(305, 305, 10, 10);
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(3));
g.drawRect(25, 25, 570, 570);
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length; j++) {
if(board[i][j] != CellState.Empty) {
Color color = board[i][j] == CellState.Black? Color.BLACK : Color.WHITE;
g.setColor(color);
g.fillOval(10 + CELL_SIZE * j, 10 + CELL_SIZE * i,
CELL_SIZE, CELL_SIZE);
}
}
}
if(lastRow != -1 && lastCol != -1) {
g.setColor(Color.RED);
g.drawLine(25 + CELL_SIZE * lastCol, 30 + CELL_SIZE * lastRow,
35 + CELL_SIZE * lastCol, 30 + CELL_SIZE * lastRow);
g.drawLine(30 + CELL_SIZE * lastCol, 25 + CELL_SIZE * lastRow,
30 + CELL_SIZE * lastCol, 35 + CELL_SIZE * lastRow);
}
}
/**
* 重置棋盘
*/
public void reset() {
lastRow = -1;
lastCol = -1;
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length; j++) {
board[i][j] = CellState.Empty;
}
}
}
}
package com.lovoinfo;
/**
* 枚举
* @author 谷振东
*
*/
public enum CellState {
Empty, Black, White
}
package com.lovoinfo;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
@SuppressWarnings("serial")
public class RenjuDialog extends JDialog {
private RenjuPanel panel = new RenjuPanel();
public RenjuDialog() {
this.setTitle("五子棋");
this.setSize(625, 645);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setIconImage(new ImageIcon("renju.png").getImage());
this.add(panel);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_F2) {
panel.reset();
repaint();
}
}
});
}
public static void main(String[] args) {
new RenjuDialog().setVisible(true);
}
}
package com.lovoinfo;
import java.awt.Color;
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.JPanel;
import javax.swing.Timer;
/**
* 五子棋的面板
* @author 谷振东
*
*/
@SuppressWarnings("serial")
public class RenjuPanel extends JPanel {
private Timer timer = null;
private Board b = new Board();
private boolean isBlack = true;
private boolean gameBegin = false;
public RenjuPanel() {
this.setSize(620, 620);
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if(gameBegin) {
int x = e.getX();
int y = e.getY();
if(x >= 20 && x <= 600 && y >= 20 && y <= 600) {
int row = Math.round((y - 30) / 40.0f);
int col = Math.round((x - 30) / 40.0f);
if(b.move(row, col, isBlack)) {
isBlack = !isBlack;
repaint();
}
}
}
}
});
timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int row = (int) (Math.random() * 15);
int col = (int) (Math.random() * 15);
if(b.move(row, col, isBlack)) {
isBlack = !isBlack;
repaint();
}
}
});
timer.start();
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.ORANGE);
g.fillRect(0, 0, 620, 620);
g.setColor(Color.BLACK);
b.draw(g);
}
/**
* 重置五子棋面板
*/
public void reset() {
timer.stop();
b.reset();
isBlack = true;
gameBegin = true;
}
}