Java五子棋小游戏
前言:这是五子棋练手项目,相当于对先前所学的知识所进行的一个总结,其中参考了许多CSDN大佬做的项目,在这里谢过。
开发这个小游戏前我们首先想一想,我们需要什么。
棋盘、棋子、输赢规则、界面 ,这是我首先能想到的四个东西。
那么我们捋一捋思路,应该是
1、先画出我们的棋盘,棋盘横竖十五道。
2、然后再在棋盘上画出我们的棋子,棋子要分为黑棋白旗,按顺序出现。
3、按照我们的输赢规则判断米字型的八个方向上有没有连成五个的同颜色棋子,如果有,判断胜负。
4、完善我们的界面
接下来开始。
Chess包
首先建一个包,包名简单粗暴易懂,Chess
chessUI类
这个类是我们的界面类,上面有棋盘
public class chessUI extends JFrame{
//写一个界面,在上面画棋盘
public void chessFrame(){
// 设置窗体
setTitle("五子棋");
setSize(1400, 1000);
// this.setResizable(false);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
g.drawLine(280, 60, 1160,60);
g.drawLine(280, 60, 280,940);
g.drawLine(280, 940, 1160,940);
g.drawLine(1160, 60, 1160,940);
for (int i = 0; i < 15; i++) {
g.setColor(Color.BLACK);
g.drawLine(300, 80 + i * 60, 1140, 80 + i * 60); // 画棋盘横线
g.drawLine(300 + i * 60, 80, 300 + i * 60, 920); // 画棋盘竖线
g.fillOval(473, 253, 15, 15);
g.fillOval(473, 733, 15, 15);
g.fillOval(473, 493, 15, 15);
g.fillOval(953, 253, 15, 15);
g.fillOval(953, 733, 15, 15);
g.fillOval(953, 493, 15, 15);
g.fillOval(713, 253, 15,15);
g.fillOval(713, 733, 15,15);
g.fillOval(713, 493, 15,15);
}
}
public static void main(String[] args) {
ChessUI chess = new ChessUI();
chess.chessFrame();
}
}
这里我们把界面完善一下,添加上我们后面要用的按钮,其实也是我懒得分开来说了,简单直接
public class ChessUI extends JFrame {
int[][] che = new int[30][30];//存我们的棋子
JLabel txtLable1 = new JLabel("准备开始");
JLabel txtLable2 = new JLabel(" ");
JComboBox<String> box = new JComboBox<String>();
public void chessFrame() {
// 设置窗体
setTitle("五子棋");
setSize(1400, 1000);
// this.setResizable(false);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 添加边界
JPanel eastP = new JPanel();
JPanel westP = new JPanel();
JPanel centP = new JPanel();
eastP.setPreferredSize(new Dimension(200, 0));
westP.setPreferredSize(new Dimension(240, 0));
centP.setPreferredSize(new Dimension(240, 1200));
westP.setLayout(new BorderLayout());
centP.setBackground(Color.lightGray);
this.add(eastP, BorderLayout.EAST);
this.add(westP, BorderLayout.WEST);
this.add(centP, BorderLayout.CENTER);
//设置右边边界的边框
JPanel upP = new JPanel();
JPanel cenP = new JPanel();
Dimension size = new Dimension(190, 300);
upP.setPreferredSize(size);
cenP.setPreferredSize(size);
eastP.add(upP, BorderLayout.NORTH);
eastP.add(cenP, BorderLayout.CENTER);
//左边边界
JPanel up = new JPanel();
JPanel cen = new JPanel();
up.setPreferredSize(size);
cen.setPreferredSize(size);
JPanel txtP =new JPanel();
txtP.setBackground(Color.lightGray);
txtP.setPreferredSize(new Dimension(150,130));
cen.add(txtP);
westP.add(up, BorderLayout.NORTH);
westP.add(cen, BorderLayout.CENTER);
txtLable1.setFont(new Font("黑体",Font.PLAIN,24));
txtLable2.setFont(new Font("黑体",Font.PLAIN,24));
txtP.add(txtLable1);
txtP.add(txtLable2);
// 添加按钮
Dimension btnSize = new Dimension(160, 45);
String[] butname= {"游戏说明","悔棋","认输","开始游戏"};
JButton[] button=new JButton[4];
for(int i=0;i<butname.length;i++) {
button[i]=new JButton(butname[i]);
button[i].setPreferredSize(btnSize);
cenP.add(button[i]);
}
cenP.add(box);
box.setPreferredSize(new Dimension(140,45));
box.addItem("--游戏模式--");
box.addItem("人人对战");
box.addItem("人机对战");
repaint();
setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
g.drawLine(280, 60, 1160,60);
g.drawLine(280, 60, 280,940);
g.drawLine(280, 940, 1160,940);
g.drawLine(1160, 60, 1160,940);
for (int i = 0; i < 15; i++) {
g.setColor(Color.BLACK);
g.drawLine(300, 80 + i * 60, 1140, 80 + i * 60); // 画棋盘横线
g.drawLine(300 + i * 60, 80, 300 + i * 60, 920); // 画棋盘竖线
g.fillOval(473, 253, 15, 15);
g.fillOval(473, 733, 15, 15);
g.fillOval(473, 493, 15, 15);
g.fillOval(953, 253, 15, 15);
g.fillOval(953, 733, 15, 15);
g.fillOval(953, 493, 15, 15);
g.fillOval(713, 253, 15,15);
g.fillOval(713, 733, 15,15);
g.fillOval(713, 493, 15,15);
}
}
public static void main(String[] args) {
ChessUI chess = new ChessUI();
chess.chessFrame();
}
}
drawChess类
这个类是我们的画棋子类
这个类写我们的画棋子的方法以及判断输赢等等,其中一个精髓就是这个int turn的设置,它是相当于一个控制开关,turn = 1 ,黑方落子,落完turn+1,然后轮到白方落子,落完turn-1,turn在1和2之间循环,我们的落子就会有规律地进行黑白交替了,最后判断完输赢之后令turn = 0,既不属于1也不属于2,无法落子。
public class DrawChess {
int turn;
ChessUI ui;
Graphics g;
//创建一个构造器,把ui 和 turn 传进来
public void setG(ChessUI ui, int turn) {
this.ui = ui;
this.turn = turn;
}
public void chess(int x, int y) {
g = ui.getGraphics();
g.setFont(new Font("行楷", Font.BOLD, 20));
int h = (x + 30) / 60;// 行
int l = (y - 50) / 60;// 列
int xp = h * 60 - 25;
int yp = l * 60 + 60;
// 检查x,y
if (x >= 300 && x <= 1140 && y >= 80 && y <= 920) {
System.out.println(x + "++" + y);
// 画棋子
if (turn == 1) {
g.setColor(Color.BLACK);
ui.che[h][l] = 1;
g.fillOval(xp, yp, 50, 50);
} else {
g.setColor(Color.WHITE);
g.fillOval(xp, yp, 50, 50);
}
}
}
}
ChessListener类
有了上面两个类以后我们只需要再写上一个类,就能完成我们的基本落子了,那就是我们的监听器类,因为每点击一步鼠标都要用上我们的监听器。
在这个类里添加我们监听器方法,需要用到MouseListener和ActionListener两个监听器。
public class ChessListener implements MouseListener, ActionListener {
int x;//坐标x
int y;//坐标y
int turn = 0;//turn=0,游戏不能开始
Graphics g;
DrawChess D = new DrawChess();
ChessUI ui;
public ChessListener() {
}
//构造器,把ui对象传进来
public ChessListener(ChessUI ui) {
this.ui = ui;
System.out.println("Listener ready");
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
if (turn == 0) {
//如果turn=0,游戏不能开始
return;
}
if (ui.box.getSelectedItem().equals("人人对战")) {
x = e.getX();
y = e.getY();
int h = (x + 30) / 60;// 行
int l = (y - 50) / 60;// 列
if (ui.che[h][l] != 0) {
//判断我们的棋子数组里有没有棋子,如果有就提示不能落子
JOptionPane.showMessageDialog(null, "此处已有棋子,请下在别处");
return;
}
D.setG(ui, turn);
D.chess(x, y);
if (x >= 300 && x <= 1140 && y >= 80 && y <= 920) {
if (turn == 1) {
turn++;
ui.txtLable2.setText("白方下棋");
} else if (turn == 2) {
turn--;
ui.txtLable2.setText("黑方下棋");
}
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("开始游戏")) {
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
ui.che[i][j] = 0;
// ui.Hash[i][j]=0;
}
}
ui.repaint();
turn = 1;
}
else if (ui.box.getSelectedItem().equals("游戏模式")) {
if (cmd.equals("开始游戏")) {
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
ui.che[i][j] = 0;
}
}
ui.repaint();
turn = 1;
}
}
}
}
最后在界面类里添加我们的监听器即可
public class ChessUI extends JFrame {
int[][] che = new int[30][30];
JLabel txtLable1 = new JLabel("准备开始");
JLabel txtLable2 = new JLabel(" ");
JComboBox<String> box = new JComboBox<String>();
ChessListener ch = new ChessListener(this);
public void chessFrame() {
// 设置窗体
setTitle("五子棋");
setSize(1400, 1000);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 添加边界
JPanel eastP = new JPanel();
JPanel westP = new JPanel();
JPanel centP = new JPanel();
eastP.setPreferredSize(new Dimension(200, 0));
westP.setPreferredSize(new Dimension(240, 0));
centP.setPreferredSize(new Dimension(240, 1200));
westP.setLayout(new BorderLayout());
centP.setBackground(Color.lightGray);
this.add(eastP, BorderLayout.EAST);
this.add(westP, BorderLayout.WEST);
this.add(centP, BorderLayout.CENTER);
// 设置右边边界的边框
JPanel upP = new JPanel();
JPanel cenP = new JPanel();
Dimension size = new Dimension(190, 300);
upP.setPreferredSize(size);
cenP.setPreferredSize(size);
eastP.add(upP, BorderLayout.NORTH);
eastP.add(cenP, BorderLayout.CENTER);
// 左边边界
JPanel up = new JPanel();
JPanel cen = new JPanel();
up.setPreferredSize(size);
cen.setPreferredSize(size);
JPanel txtP = new JPanel();
txtP.setBackground(Color.lightGray);
txtP.setPreferredSize(new Dimension(150, 130));
cen.add(txtP);
westP.add(up, BorderLayout.NORTH);
westP.add(cen, BorderLayout.CENTER);
txtLable1.setFont(new Font("黑体", Font.PLAIN, 24));
txtLable2.setFont(new Font("黑体", Font.PLAIN, 24));
txtP.add(txtLable1);
txtP.add(txtLable2);
// 添加按钮
Dimension btnSize = new Dimension(160, 45);
String[] butname = { "游戏说明", "悔棋", "认输", "开始游戏" };
JButton[] button = new JButton[4];
for (int i = 0; i < butname.length; i++) {
button[i] = new JButton(butname[i]);
button[i].setPreferredSize(btnSize);
cenP.add(button[i]);
}
cenP.add(box);
box.setPreferredSize(new Dimension(140, 45));
box.addItem("--游戏模式--");
box.addItem("人人对战");
box.addItem("人机对战");
// 添加监听器
for (int i = 0; i < butname.length; i++) {
button[i].addActionListener(ch);// 添加发生操作的监听方法
}
box.addActionListener(ch);
setVisible(true);
addMouseListener(ch);
ch.g = this.getGraphics();
repaint();
}
public void paint(Graphics g) {
super.paint(g);
g.drawLine(280, 60, 1160, 60);
g.drawLine(280, 60, 280, 940);
g.drawLine(280, 940, 1160, 940);
g.drawLine(1160, 60, 1160, 940);
for (int i = 0; i < 15; i++) {
g.setColor(Color.BLACK);
g.drawLine(300, 80 + i * 60, 1140, 80 + i * 60); // 画棋盘横线
g.drawLine(300 + i * 60, 80, 300 + i * 60, 920); // 画棋盘竖线
g.fillOval(473, 253, 15, 15);
g.fillOval(473, 733, 15, 15);
g.fillOval(473, 493, 15, 15);
g.fillOval(953, 253, 15, 15);
g.fillOval(953, 733, 15, 15);
g.fillOval(953, 493, 15, 15);
g.fillOval(713, 253, 15, 15);
g.fillOval(713, 733, 15, 15);
g.fillOval(713, 493, 15, 15);
}
g.setColor(Color.BLACK);
g.drawRect(390, 945, 210, 40);
g.drawRect(650, 945, 210, 40);
g.setFont(new Font("楷体", Font.BOLD, 20));
g.drawString("黑方时间:", 400, 975);
g.drawString("白方时间:", 660, 975);
}
public static void main(String[] args) {
ChessUI chess = new ChessUI();
chess.chessFrame();
}
}
以上就是我们的最基础功能,画出棋盘和棋子,其他功能我将在(2)中完成
over~