📄 gobanggame.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GobangGame extends JFrame implements ChessmanListener
{
Container contentPane;
Block[] blocks = new Block[8 * 8];
JPanel chessBoard = new JPanel();
JButton cmdNewGame = new JButton();
JLabel labHeiQi = new JLabel();
JLabel labBaiQi = new JLabel();
JButton cmdReDo = new JButton();
public static void main(String [] args)
{
GobangGame game=new GobangGame();
}
public GobangGame() {
setTitle("五子棋");
contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.setBackground(new Color(187,89,90));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Icon bq = new ImageIcon("baiqi.gif");
Icon hq = new ImageIcon("heiqi.gif");
chessBoard.setBackground(new Color(131, 82, 0));
chessBoard.setPreferredSize(new Dimension(250, 250));
chessBoard.setBounds(new Rectangle(20, 60, 250, 250));
chessBoard.setLayout(new GridLayout(8,8));
int n;
for (int i = 0; i < 8 * 8; i++) {
n = Block.ALL;
if (i % 8 == 0)
n = n & ~Block.LINELEFT;
if (i % 8 == 7)
n = n & ~Block.LINERIGHT;
if (i / 8 == 0)
n = n & ~Block.LINETOP;
if (i / 8 == 7)
n = n & ~Block.LINEBOTTOM;
blocks[i] = new Block(n, i / 8, i % 8);
blocks[i].addChessmanListener(this);
if ( (i - 1) % 4 == 3 && i / 8 % 4 == 3)
blocks[i].setStar(true);
chessBoard.add(blocks[i]);
}
labHeiQi.setFont(new java.awt.Font("Dialog", 1, 16));
labHeiQi.setIcon(hq);
labHeiQi.setIconTextGap(10);
labHeiQi.setBounds(new Rectangle(200, 8, 100, 40));
labBaiQi.setFont(new java.awt.Font("Dialog", 1, 16));
labBaiQi.setIcon(bq);
labBaiQi.setIconTextGap(10);
labBaiQi.setBounds(new Rectangle(330, 8, 100, 40));
cmdNewGame.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (Block.getPerson() != 0) {
for (int i = 0; i < 8 * 8; i++) {
if(blocks[i].getHas()!=0)
blocks[i].reSet();
}
}
Block.StartGame();
}
}
);
Block.setPersonLabel(labHeiQi, labBaiQi);
contentPane.add(cmdNewGame);
contentPane.add(chessBoard);
contentPane.add(labBaiQi);
contentPane.add(labHeiQi);
setSize(400,350);
setVisible(true);
}
public void chessmanPress(ChessmanEvent e) {
int up, down, left, right;
up = down = e.getRow();
left = right = e.getCollumn();
do {
up++;
}
while (up < 8 && blocks[up * 8 + e.getCollumn()].getHas() == e.getHas());
do {
down--;
}
while (down >= 0 && blocks[down * 8 + e.getCollumn()].getHas() == e.getHas());
if ( (up - down) > 5) {
for (int i = down + 1; i < up; i++)
blocks[i * 8 + e.getCollumn()].setWinChessman();
Block.end();
}
else {
do
{
right++;
}
while (right < 8 && blocks[right + e.getRow() * 8].getHas() == e.getHas());
do {
left--;
}
while (left >= 0 && blocks[left + e.getRow() * 8].getHas() == e.getHas());
if ( (right - left) > 5) {
for (int i = left + 1; i < right; i++)
blocks[i + e.getRow() * 8].setWinChessman();
Block.end();
}
else {
up = down = e.getRow();
left = right = e.getCollumn();
do {
right++;
down++;
}
while (right < 8 && down < 8 &&
blocks[right + down * 8].getHas() == e.getHas());
do {
left--;
up--;
}
while (left >= 0 && up >= 0 &&
blocks[left + up * 8].getHas() == e.getHas());
if ( (right - left) > 5) {
for (int i = left + 1; i < right; i++) {
up++;
blocks[i + up * 8].setWinChessman();
}
Block.end();
}
else {
up = down = e.getRow();
left = right = e.getCollumn();
do {
right++;
down--;
}
while (right < 8 && down >= 0 &&
blocks[right + down * 8].getHas() == e.getHas());
do {
left--;
up++;
}
while (left >= 0 && up < 8 &&
blocks[left + up * 8].getHas() == e.getHas());
if ( (right - left) > 5) {
for (int i = left + 1; i < right; i++) {
up--;
blocks[i + up * 8].setWinChessman();
}
Block.end();
}
}
}
}
if (Block.isEnd()) {
cmdReDo.setEnabled(false);
if (e.getHas() == 1) {
labHeiQi.setText("黑棋胜");
labBaiQi.setVisible(false);
labHeiQi.setVisible(true);
}
else {
labBaiQi.setText("白棋胜");
labHeiQi.setVisible(false);
labBaiQi.setVisible(true);
}
}
}
}
class Block extends JPanel {
static Block last,last2;
public static final int LINELEFT = 1 << 0; //左边线
public static final int LINERIGHT = 1 << 1; //右边线
public static final int LINETOP = 1 << 2; //上边线
public static final int LINEBOTTOM = 1 << 3; //下边线
public static final int ALL = LINELEFT + LINERIGHT + LINETOP + LINEBOTTOM;
public static int bWidth = 30;
private static int p1Count = 0, p2Count = 0;
private static JLabel labP1, labP2;
private static int person = 0;
private int type; //处于那个位置上
private boolean mouseIn;
private boolean winChessman = false;
private boolean star = false; //该点是否是星点
private static boolean start = true; //是否开始新游戏
static ImageIcon imgNormal, heiqi, baiqi, over, win, lastImg;
private ChessmanEvent chessmanEvent;
private ChessmanListener chessmanListener;
int has = 0;
int x = 0, y = 0;
Block(int bType, int row, int collumn) {
type = bType;
this.x = row;
this.y = collumn;
this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
imgNormal = new ImageIcon("back.gif");
heiqi = new ImageIcon("heiqi.gif");
baiqi = new ImageIcon("baiqi.gif");
over = new ImageIcon("over.gif");
win = new ImageIcon("win.gif");
lastImg = new ImageIcon("last.gif");
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (start && has == 0) {
person= 2 - ( person + 1 ) % 2 ;
has = person;
if (has == 2) {
p2Count++;
labP1.setVisible(true);
labP2.setVisible(false);
}
else if (has == 1) {
p1Count++;
labP2.setVisible(true);
labP1.setVisible(false);
}
chessmanEvent = new ChessmanEvent(has, x, y);
chessmanListener.chessmanPress(chessmanEvent);
last2=last;
last=Block.this;
if( last2!=null ) last2.repaint();
}
}
}
);
this.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
if(!start)
Block.this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
else if(has==0)
Block.this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
mouseIn = true;
repaint();
}
public void mouseExited(MouseEvent e) {
mouseIn = false;
if (has != 0)
Block.this.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
repaint();
}
}
);
this.setDoubleBuffered(false);
this.setPreferredSize(new Dimension(bWidth, bWidth));
}
//是否为星点
public boolean isStar()
{
return star;
}
//设为星点
public void setStar(boolean star)
{
this.star = false;
}
//添加监听器
public void addChessmanListener(ChessmanListener c)
{
chessmanListener = c;
}
//绘制图面
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int w;
w = bWidth;
g.drawImage(imgNormal.getImage(), 0, 0, bWidth, bWidth, this);
if ( (type & LINELEFT) == 0 || (type & LINERIGHT) == 0)
g2d.setStroke(new BasicStroke(2));
else
g2d.setStroke(new BasicStroke(1));
//上面的线
if ( (type & LINETOP) == LINETOP)
g2d.drawLine(w / 2, 0, w / 2, w / 2);
//下面的线
if ( (type & LINEBOTTOM) == LINEBOTTOM)
g2d.drawLine(w / 2, w / 2, w / 2, w);
if ( (type & LINETOP) == 0 || (type & LINEBOTTOM) == 0)
g2d.setStroke(new BasicStroke(2));
else
g2d.setStroke(new BasicStroke(1));
if ( (type & LINELEFT) == LINELEFT)
g2d.drawLine(0, w / 2, w / 2, w / 2);
//右边的线
if ( (type & LINERIGHT) == LINERIGHT)
g2d.drawLine(w / 2, w / 2, w, w / 2);
if (star)
g.fillRect(w / 2 - 2, w / 2 - 2, 5, 5);
if (has == 1)
{
g.setColor(Color.BLACK);
g.fillOval(3,3,bWidth - 6, bWidth - 6);
g.drawImage(heiqi.getImage(), 2, 2, bWidth - 3, bWidth - 3, this);
}
else if (has == 2)
{
g.setColor(Color.white);
g.fillOval(3,3,bWidth - 6, bWidth - 6);
g.drawImage(baiqi.getImage(), 2, 2, bWidth - 3, bWidth - 3, this);
}
else if (start && mouseIn)
{
if (person == 2|| person== 0 )
{
g.setColor(Color.BLACK);
g.fillOval(3,3,bWidth - 6, bWidth - 6);
g.drawImage(heiqi.getImage(), 2, 2, bWidth - 3, bWidth - 3, this);
}
else if (person == 1 )
{
g.setColor(Color.white);
g.fillOval(3,3,bWidth - 6, bWidth - 6);
g.drawImage(baiqi.getImage(), 2, 2, bWidth - 3, bWidth - 3, this);
}
}
g2d.setStroke(new BasicStroke(2));
g.setColor(Color.RED);
if (winChessman)
{
g.drawOval(9,9,bWidth - 17, bWidth - 17);
g.drawImage(win.getImage(), 7, 7, bWidth - 7, bWidth - 7, this);
}
else if ( last==Block.this )
{
g.drawLine(bWidth/2-5,bWidth/2,bWidth/2+5,bWidth/2);
g.drawLine(bWidth/2,bWidth/2-5,bWidth/2,bWidth/2+5);
g.drawImage(lastImg.getImage(), 8, 8, bWidth - 16, bWidth - 16, this);
}
}
//返回该格的棋子,黑为1,白为2
public int getHas()
{
return has;
}
//返回当前所下的棋子,黑为1,白为2
static public int getPerson()
{
return person;
}
//标记当前格子为胜棋,重画
public void setWinChessman()
{
winChessman = true;
this.repaint();
}
//重设当前格为空,重画
public void reSet()
{
has = 0;
winChessman=false;
repaint();
}
//开始游戏
static public void StartGame()
{
last=null;
p1Count = p2Count = 0;
person = 0;
start = true;
labP1.setVisible(true);
labP2.setVisible(false);
}
//结束游戏
static public void end()
{
start = false;
}
//是否结束
static public boolean isEnd()
{
return!start;
}
//设置交替显示的两个标签,用来指明当前轮到那个玩家
static public void setPersonLabel(JLabel p1, JLabel p2)
{
labP1 = p1;
labP2 = p2;
if (person == 0 || person == 2)
{
labP1.setVisible(true);
labP2.setVisible(false);
}
else
{
labP2.setVisible(true);
labP1.setVisible(false);
}
}
}
//棋子事件监听接口
interface ChessmanListener
{
public void chessmanPress(ChessmanEvent e);
}
//棋子事件
class ChessmanEvent
{
int has;
int row, collumn;
//构造函数
public ChessmanEvent(int has, int row, int collumn)
{
this.has = has;
this.row = row;
this.collumn = collumn;
}
//返回该格棋子,黑为1,白为2
public int getHas()
{
return has;
}
//返回该格所在的行
public int getRow()
{
return row;
}
//返回该格所在的列
public int getCollumn()
{
return collumn;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -