五子棋课程设计

文章介绍了用于课程设计的一个简单的五子棋游戏项目,该项目使用JavaSwing库创建用户界面,包含完整报告和源码。代码中展示了JPanel的实现,用以处理鼠标事件,实现玩家交互,包括准备、认输、悔棋等功能。此外,还提供了关键代码片段,如按钮和标签的设置,以及游戏逻辑的初步实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在制作和整理这些课程设计我们花费了大量的时间、资金,希望能够给您带来更好的体验,觉得我们的内容还可以的话请 “👍点赞” “✍️评论” “💙收藏” 一键三连哦!

项目介绍

运行环境简单,提供完整的报告以及源码,技术相对较简单,非常适合课程设计提交,具体效果如下:

网站代码示例

package bean;

import View.Room;
import util.MSUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

public class MyJPanel extends JPanel implements MouseListener {
    public static int num;
    public static Room room;
//    public static JButton b1 = new JButton("准备");
//    public static JButton b2 = new JButton("认输");
//    public static JButton b3 = new JButton("悔棋");
//    public static JButton b4 = new JButton("关于本游戏");
//    public static JButton b5 = new JButton("退出");
    public static JLabel b1 = new JLabel("  准备");
    public static JLabel b2 = new JLabel("  认输");
    public static JLabel b3 = new JLabel("  悔棋");
    public static JLabel b4 = new JLabel("关于本游戏");
    public static JLabel b5 = new JLabel("  退出");
    public static Game game = new Game();
    public static JLabel l1 = new JLabel();
    public static JLabel l2 = new JLabel();
    public static JLabel l3 = new JLabel();
    public static JLabel l4 = new JLabel();
    public static JLabel l5 = new JLabel("");
    public static JLabel l6 = new JLabel("");
    public static JLabel l7 = new JLabel(""+num);
    public static JLabel l8 = new JLabel("房间名");
    public static JLabel l9 = new JLabel("我");
    public static JLabel l10 = new JLabel("对手");
    public static Boolean maker;
    public static Boolean flag = false;

    public JLabel getL4() {
        return l4;
    }

    public void setL4(JLabel l4) {
        this.l4 = l4;
    }


    public int x;
    public int y;
    public int chessX;
    public int chessY;
    public MyJPanel(Room room)
    {
        super();
        setLayout(null);
        this.room = room;
        this.addMouseListener(this);
        Font font1 = new Font("微软雅黑",Font.BOLD,20);
        b1.setFont(font1);
        b2.setFont(font1);
        b3.setFont(font1);
        b4.setFont(font1);
        b5.setFont(font1);
        b1.setBounds(0,0,100,50);
        b2.setBounds(200,0,100,50);
        b3.setBounds(400,0,100,50);
        b4.setBounds(600,0,100,50);
        b5.setBounds(800,0,100,50);
        l9.setBounds(0,350,100,50);
        l10.setBounds(900,350,100,50);
        l3.setBounds(0,400,100,50);
        l4.setBounds(900,400,100,50);
        l5.setBounds(0,500,100,50);
        l6.setBounds(900,500,100,50);
        l7.setBounds(0,200,100,50);
        l8.setBounds(0,150,100,50);
//        b1.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                if (l4.getText().equals("")){
//                    JOptionPane.showMessageDialog(form1,"还没有对手进入!");
//                }else {
//                    l5.setText("已准备!");
//                    try {
//                        MSUtil.oos.writeUTF("ready");
//                        MSUtil.oos.flush();
//                        MSUtil.oos.writeObject(l4.getText());
//                        MSUtil.oos.flush();
//                    } catch (IOException ioException) {
//                        ioException.printStackTrace();
//                    }
//                    if (l6.getText().equals("已准备!")){
//                        JOptionPane.showMessageDialog(form1,"游戏开始,房主先手!");
//                        l5.setText("");
//                        l6.setText("");
//                        if (maker){
//                            flag = true;
//                        }else {
//                            flag = false;
//                        }
//                    }
//                }
//            }
//        });
//        b2.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                int result = JOptionPane.showConfirmDialog(form1,"您确定要认输吗?","认输",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
//                if (result==0){
//                    try {
//                        MSUtil.oos.writeUTF("defeat");
//                        MSUtil.oos.flush();
//                        MSUtil.oos.writeObject(l4.getText());
//                        MSUtil.oos.flush();
//                    } catch (IOException ioException) {
//                        ioException.printStackTrace();
//                    }
//                    game.Clean();
//                    form1.repaint();
//                    game.Rush();
//                    JOptionPane.showMessageDialog(form1,"您已经认输!");
//                    l5.setText("未准备");
//                    l6.setText("未准备");
//                }
//            }
//        });
//        b3.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                if (flag==false){
//                    JOptionPane.showMessageDialog(form1,"等待对方同意悔棋");
//                    try {
//                        MSUtil.oos.writeUTF("regret");
//                        MSUtil.oos.flush();
//                        MSUtil.oos.writeObject(l4.getText());
//                        MSUtil.oos.flush();
//                        int[] chess = new int[]{x,y};
//                        MSUtil.oos.writeObject(chess);
//                        MSUtil.oos.flush();
//                    } catch (IOException ioException) {
//                        ioException.printStackTrace();
//                    }
//                }else {
//                    JOptionPane.showMessageDialog(form1,"现在是您的回合,不能悔棋");
//                }
//            }
//        });
//        b4.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                JOptionPane.showMessageDialog(form1,"本游戏为五子棋游戏,黑方和白方依次落子,若有一方凑齐5个棋子连在一起,则其获胜");
//            }
//        });
//        b5.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                int result = JOptionPane.showConfirmDialog(form1,"您确定要退出吗?","退出",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
//                if (result==0){
//                    if (!l4.getText().equals("")){
//                        try {
//                            MSUtil.oos.writeUTF("out");
//                            MSUtil.oos.flush();
//                            MSUtil.oos.writeObject(l4.getText());
//                            MSUtil.oos.flush();
//                        } catch (IOException ioException) {
//                            ioException.printStackTrace();
//                        }
//                    }
//                    open = false;
//                    MSUtil.out();
//                    Hall hall1 = new Hall(form1.username1);
//                    form1.dispose();
//                    hall1.setVisible(true);
//                }
//            }
//        });
        add(b1);
        add(b2);
        add(b3);
        add(b4);
        add(b5);
        add(l1);
        add(l2);
        add(l3);
        add(l4);
        add(l5);
        add(l6);
        add(l7);
        add(l8);
        add(l9);
        add(l10);
    }

    public void paint(Graphics g)
    {
        super.paint(g);
        game.Paint(g);
    }

    @Override
    public void mousePressed(MouseEvent e)
    {
        x = e.getX();
        y = e.getY();
        if (x>=100 && x<=900  && y>=100 && y<=900) {
            chessX = (x - 100) / 50;
            chessY = (y - 100) / 50;
            if (flag){
                makeChess(chessX,chessY);
                int[] chess = new int[]{chessX,chessY};
                try {
                    MSUtil.oos.writeUTF("down");
                    MSUtil.oos.flush();
                    MSUtil.oos.writeObject(l4.getText());
                    MSUtil.oos.flush();
                    MSUtil.oos.writeObject(chess);
                    MSUtil.oos.flush();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                flag = false;
            }
//            b1.setBounds(0,0,100,50);
//            b2.setBounds(200,0,100,50);
//            b3.setBounds(400,0,100,50);
//            b4.setBounds(600,0,100,50);
//            b5.setBounds(800,0,100,50);
        }else if (x>=0 && x<=100 && y>=0 && y<=50){
            if (l4.getText().equals("")){
                    JOptionPane.showMessageDialog(room,"还没有对手进入!");
                }else {
                    l5.setText("已准备!");
                    try {
                        MSUtil.oos.writeUTF("ready");
                        MSUtil.oos.flush();
                        MSUtil.oos.writeObject(l4.getText());
                        MSUtil.oos.flush();
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                    if (l6.getText().equals("已准备!")){
                        JOptionPane.showMessageDialog(room,"游戏开始,房主先手!");
                        l5.setText("");
                        l6.setText("");
                        if (maker){
                            flag = true;
                        }else {
                            flag = false;
                        }
                    }
                }
        }else if (x>=200 && x<=300 && y>=0 && y<=50){
            int result = JOptionPane.showConfirmDialog(room,"您确定要认输吗?","认输",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
                if (result==0){
                    try {
                        MSUtil.oos.writeUTF("defeat");
                        MSUtil.oos.flush();
                        MSUtil.oos.writeObject(l4.getText());
                        MSUtil.oos.flush();
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                    game.Clean();
                    room.repaint();
                    game.Rush();
                    JOptionPane.showMessageDialog(room,"您已经认输!");
                    l5.setText("未准备");
                    l6.setText("未准备");
                }
        }else if (x>=400 && x<=500 && y>=0 && y<=50){
            if (flag==false){
                    JOptionPane.showMessageDialog(room,"等待对方同意悔棋");
                    try {
                        MSUtil.oos.writeUTF("regret");
                        MSUtil.oos.flush();
                        MSUtil.oos.writeObject(l4.getText());
                        MSUtil.oos.flush();
                        int[] chess = new int[]{chessX,chessY};
                        MSUtil.oos.writeObject(chess);
                        MSUtil.oos.flush();
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                }else {
                    JOptionPane.showMessageDialog(room,"现在是您的回合,不能悔棋");
                }
        }else if (x>=600 && x<=700 && y>=0 && y<=50){
                JOptionPane.showMessageDialog(room,"本游戏为五子棋游戏,黑方和白方依次落子,若有一方凑齐5个棋子连在一起,则其获胜");
        }else if (x>=800 && x<=900 && y>=0 && y<=50){
            int result = JOptionPane.showConfirmDialog(room,"您确定要退出吗?","退出",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
                if (result==0){
                    if (!l4.getText().equals("")){
                        try {
                            MSUtil.oos.writeUTF("out");
                            MSUtil.oos.flush();
                            MSUtil.oos.writeObject(l4.getText());
                            MSUtil.oos.flush();
                        } catch (IOException ioException) {
                            ioException.printStackTrace();
                        }
                    }
                    game.Clean();
                    game.Rush();
                    MSUtil.out();
                    MSUtil.hall.setVisible(true);
                    room.dispose();
                }
        }
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
    }
    @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 mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    public void makeChess(int x,int y){
        int[] B = game.Judge(x, y);
        this.repaint();
        int b = game.P();
        if (b == 1)
        {
            JOptionPane.showMessageDialog(this,"游戏结束,黑棋获胜");
            game.Clean();
            game.Rush();
            flag = false;
            l5.setText("未准备");
            l6.setText("未准备");
        }
        else if (b == 2)
        {
            JOptionPane.showMessageDialog(this,"游戏结束,白棋获胜");
            game.Clean();
            game.Rush();
            flag = false;
            l5.setText("未准备");
            l6.setText("未准备");
        }
        Boolean Pe = game.Peace();
        if (Pe == true)
        {
            JOptionPane.showMessageDialog(this,"游戏结束,双方和棋");
            game.Clean();
            game.Rush();
            flag = false;
            l5.setText("未准备");
            l6.setText("未准备");
        }
        this.repaint();
    }

    public void Regret(int x,int y){
        int answer = game.Regret(x,y);
        room.repaint();
        if(answer ==1)
        {
            JOptionPane.showMessageDialog(room,"黑方悔棋");
        }
        else if(answer ==2)
        {
            JOptionPane.showMessageDialog(room,"白方悔棋");
        }
        else if(answer ==0)
        {
            JOptionPane.showMessageDialog(room,"棋局还未开始");
        }
    }
}

【源码获取】

我们分享了非常多的课程设计资料,以便让大家更好的学习,扫码关注下方公众号 大学生知识分享地 或直接搜索公众号名称在里面即可获取所有内容!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值