五子棋
java写的,有注释,可以理解理解。
先贴张图
一.定义一个包:chess,包括三个class:StartChessJFrame,ChessBoard,Point
1.StartChessJFrame.java
主要包括主体界面,“Start”,"Exit","Back"三个按钮,显示主体界面。
2.ChessBoard.java
主要包括绘制棋盘,绘制棋子,落棋,判断是否有一方获胜,或者下满棋盘(平局)。
3.Point.java
主要的功能是定义棋子的颜色位置坐标,以及获取棋子的颜色,位置坐标(X,Y)。
二.基本代码
1.StartChessJFrame
package chess;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class StartChessJFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel toobar;
private JButton btnStart;
private JButton btnBack;
private JButton btnExit;
// private JMenuBar menuBar;
// private JMenu sysMenu;
private JMenuItem startMenuItem;
private JMenuItem backMenuItem;
private JMenuItem exitMenuItem;
ChessBoard chessBorad=new ChessBoard();
private class MyItemListener implements ActionListener { //按钮事件监听器
@Override
public void actionPerformed(ActionEvent e) {
Object obj=e.getSource();
if(obj==StartChessJFrame.this.startMenuItem||obj==btnStart) {
chessBorad.chessStart();
}else if(obj==exitMenuItem||obj==btnExit) {
System.exit(0);
}else if(obj==backMenuItem||obj==btnBack) {
chessBorad.chessBack();
}
}
}
public StartChessJFrame() {
MyItemListener ml=new MyItemListener();
setTitle("五子棋 - - - - - - 谨");
// 这是系统按钮,可以没有,相应的事件监听
// menuBar=new JMenuBar();
// sysMenu=new JMenu("系统");
// startMenuItem=new JMenuItem("重新开始");
// backMenuItem=new JMenuItem("悔棋");
// exitMenuItem=new JMenuItem("退出");
// sysMenu.add(startMenuItem);
// sysMenu.add(backMenuItem);
// sysMenu.add(exitMenuItem);
// this.startMenuItem.addActionListener(ml);
// backMenuItem.addActionListener(ml);
// exitMenuItem.addActionListener(ml);
// menuBar.add(sysMenu);
// setJMenuBar(menuBar);
toobar=new JPanel();
//按钮,Start,Back,Exit
btnStart=new JButton("Start");
btnBack=new JButton("Back");
btnExit=new JButton("Exit");
//设置按钮的颜色
btnStart.setBackground(new Color(255, 99, 71));
btnBack.setBackground(new Color(255, 99, 71));
btnExit.setBackground(new Color(255, 99, 71));
//设置对齐方式
toobar.setLayout(new FlowLayout(FlowLayout.CENTER,40,5));
//增添按钮
toobar.add(btnStart);
toobar.add(btnBack);
toobar.add(btnExit);
//增加事件监听器
btnStart.addActionListener(ml);
btnBack.addActionListener(ml);
btnExit.addActionListener(ml);
toobar.setBackground(new Color(250, 128, 114));
add(toobar,BorderLayout.SOUTH);
add(chessBorad,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(630, 710);
setLocation(600, 100);
//设置不能改变窗口大小
setResizable(false);
// pack();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
StartChessJFrame sc=new StartChessJFrame();
sc.setVisible(true);
}
}
2.ChessBoard
package chess;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ChessBoard extends JPanel implements MouseListener {
private static final long serialVersionUID = 1L;
public static final int MARGIN=30; //边距
public static final int GRID_SPAN=35; //网格间距
public static final int ROWS=16; //行数
public static final int COLS=16; //列数
private int flag=0;
int chessCount;
Point[] chessList=new Point[(ROWS+1)*(COLS+1)]; //棋子数组
int xIndex,yIndex;
boolean isBack=true;
public ChessBoard() {
setBackground(Color.ORANGE);
addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) { //设置手型鼠标
// int x1=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
// int y1=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
setCursor(new Cursor(Cursor.HAND_CURSOR));
}
@Override
public void mouseDragged(MouseEvent e) {
}
});
addMouseListener(this);
}
public Dimension getPerferredSize() {
return new Dimension(MARGIN*2+GRID_SPAN*COLS, MARGIN*2+GRID_SPAN*ROWS);
}
@Override
public void mousePressed(MouseEvent e) {
String colorName=isBack?"黑棋":"白棋";
xIndex=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
yIndex=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
if(xIndex<0||xIndex>ROWS||yIndex<0||yIndex>COLS) //落的棋子不在棋盘上
return;
if(findChess(xIndex, yIndex)) //已有棋子,不能再下
return;
Point ch=new Point(xIndex, yIndex, isBack?Color.black:Color.white);
chessList[chessCount++]=ch;
if(flag<1)
repaint();
if(isWin()==true) { //游戏结束,提示窗口
flag++;
JOptionPane.showMessageDialog(null, "恭喜,"+colorName+"获胜", "游戏结束", JOptionPane.OK_CANCEL_OPTION, null);
}
if(chessCount==(ROWS+1)*(COLS+1)) { //游戏平局,提示窗口
JOptionPane.showMessageDialog(null, "平局", "游戏结束", JOptionPane.OK_CANCEL_OPTION, null);
}
isBack=!isBack;
}
@Override
public void mouseClicked(MouseEvent arg0) { }
@Override
public void mouseEntered(MouseEvent arg0) { }
@Override
public void mouseExited(MouseEvent arg0) { }
@Override
public void mouseReleased(MouseEvent arg0) { }
private boolean findChess(int x,int y) { //判断是否有棋子
for(Point p:chessList) {
if(p!=null&&p.getX()==x&&p.getY()==y)
return true;
}
return false;
}
private boolean isWin() { //判断是否胜利
int count=1;
Color c=isBack?Color.black:Color.white;
for(int x=xIndex-1;x>=0;x--) {
if(getChess(x, yIndex, c)!=null)
count++;
else
break;
}
for(int x=xIndex+1;x<=ROWS;x++) {
if(getChess(x, yIndex, c)!=null)
count++;
else
break;
}
if(count>=5)
return true;
else
count=1;
for(int y=yIndex-1;y>=0;y--) {
if(getChess(xIndex, y, c)!=null)
count++;
else
break;
}
for(int y=yIndex+1;y<=COLS;y++) {
if(getChess(xIndex, y, c)!=null)
count++;
else
break;
}
if(count>=5)
return true;
else
count=1;
for(int x=xIndex+1,y=yIndex-1;x<=ROWS&&y>=0;x++,y--) {
if(getChess(x, y, c)!=null)
count++;
else
break;
}
for(int x=xIndex-1,y=yIndex+1;x>=0&&y<=COLS;x--,y++) {
if(getChess(x, y, c)!=null)
count++;
else
break;
}
if(count>=5)
return true;
else
count=1;
for(int x=xIndex-1,y=yIndex-1;x>=0&&y>=0;x--,y--) {
if(getChess(x, y, c)!=null)
count++;
else
break;
}
for(int x=xIndex+1,y=yIndex+1;x<=ROWS&&y<=COLS;x++,y++) {
if(getChess(x, y, c)!=null)
count++;
else
break;
}
if(count>=5)
return true;
else
count=1;
return false;
}
private Point getChess(int x,int y,Color c) { //获取棋子
for(Point p:chessList) {
if(p!=null&&p.getX()==x&&p.getY()==y&&p.getColor()==c)
return p;
}
return null;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
//绘制棋盘
for(int i=0;i<=ROWS;i++) {
g.drawLine(MARGIN, MARGIN+i*GRID_SPAN, MARGIN+COLS*GRID_SPAN, MARGIN+i*GRID_SPAN);
}
for(int i=0;i<=COLS;i++) {
g.drawLine(MARGIN+i*GRID_SPAN, MARGIN, MARGIN+i*GRID_SPAN,MARGIN+COLS*GRID_SPAN );
}
//绘制棋子
for(int i=0;i<chessCount;i++) {
int xPos=chessList[i].getX()*GRID_SPAN+MARGIN;
int yPos=chessList[i].getY()*GRID_SPAN+MARGIN;
g.setColor(chessList[i].getColor());
g.fillOval(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER);
//最后一个棋子加红色矩形框表示
if(i==chessCount-1) {
g.setColor(Color.red);
g.drawRect(xPos-Point.DIAMETER/2, yPos-Point.DIAMETER/2, Point.DIAMETER, Point.DIAMETER);
}
}
}
public void chessStart() { //重新开始
flag=0;
for(int i=chessCount;i>=0;i--)
chessList[i]=null;
chessCount=0;
isBack=true;
repaint();
}
public void chessBack() { //悔棋
flag=0;
if(chessCount<1)
return;
chessList[--chessCount]=null;
isBack=!isBack;
repaint();
}
}
3.Point
package chess;
import java.awt.Color;
public class Point {
//棋子的坐标
private int x;
private int y;
//棋子的颜色,黑色、白色
private Color color;
public static final int DIAMETER=30;
public Point(int x,int y,Color color) {
this.x=x;
this.y=y;
this.color=color;
}
//获取棋子的坐标,颜色
public int getX() {
return x;
}
public int getY() {
return y;
}
public Color getColor() {
return color;
}
}
都看到这了,点个赞呗,评论评论呗!