• 一个五子棋小游戏


    当初做练手的时候做的一个小游戏,突然想起来了。。。放在博客上就当做个纪念了。

    package fivechess;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.awt.*;
    /**
     * 简单的五子棋小游戏
     * @author*/
    public class FiveChessFrame extends JFrame implements MouseListener{
        //定义落子的横纵坐标(double型保证后面的就进取点原则)
        double x=0.0;
        double y=0.0;
        //定义黑白轮流的标记
        boolean isBlack=true;
        //定义游戏是否能继续
        boolean canPlay=true;
        //定义一个二维数组完成棋子位置的存储,二维数组值代表:0代表该点无子,1代表该点黑子,2代表白字
        int[][] chess=new int[15][15];//15表示棋盘的纵横线条数
        public FiveChessFrame(){
            int width=Toolkit.getDefaultToolkit().getScreenSize().width;
            int height=Toolkit.getDefaultToolkit().getScreenSize().height;
            setTitle("五子棋");
            setSize(765,580);
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            ButtonPane bpane=new ButtonPane();
            bpane.setSize(220,535);
            add(bpane);
            this.addMouseListener(this);//为窗体添加事件
            /*?(标签图像无法存在)URL url=this.getClass().getResource("/image/五子棋棋谱图.jpg");
            Icon icon=new ImageIcon(url);
            JLabel jl=new JLabel();
            jl.setIcon(icon);
            jl.setSize(535, 535);
            getContentPane().add(jl,BorderLayout.CENTER);*/
            //设置窗体的出现位置在屏幕中央
            setLocation((width-535)/2, (height-535)/2);
            setVisible(true);
        }
        public void paint(Graphics g){
            Graphics2D g2=(Graphics2D) g;
            //绘制五子棋图形界面
            BufferedImage img= null;
            
            try {
                img = ImageIO.read(new File("D:/我的JAVA之路/java项目-五子棋/Image/五子棋棋谱图.jpg"));
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            g2.drawImage(img,0,38,535,535,this);
            
            /*URL url2 =this.getClass().getResource("/image/东东头像-小.png");
            Image img2=Toolkit.getDefaultToolkit().getImage(resource);
            g2.drawImage(img2,(int)(x-12),(int)(y-12),24,24,this);*/
            //绘制棋子(遍历每一个落子点,保证所有下过的棋子存在棋盘中)
            for(int i=0;i<15;i++){
                for( int j=0;j<15;j++){
                    if(chess[i][j]==1){
                        int tempX=i*35+22;
                        int tempY=j*35+23;
                        //绘制黑棋子
                        g2.fillOval((tempX-12), (tempY-12), 24, 24);
                    /*    try {
                            img2=ImageIO.read(new File("D:/我的JAVA之路/java项目-五子棋/Image/东东头像-小.png"));
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        g2.drawImage(img2,(int)(x-12),(int)(y-12),24,24,null);*/
                    }if(chess[i][j]==2){
                        int tempX=i*35+22;
                        int tempY=j*35+23;
                        //绘制白棋子
                        g2.setColor(Color.WHITE);
                        g2.fillOval((tempX-12), (tempY-12), 24, 24);
                        g2.setColor(Color.BLACK);
                        g2.drawOval((tempX-12), (tempY-12), 24, 24);
                    }else{
                    }
                }
            }
        }
        //判断棋局是否结束
        private boolean isWin(){
            boolean flag=false;
            //取得需要判断胜负的棋子的颜色
            int color=chess[(int) x][(int) y];
            //判断x轴上的胜负
            int count=this.getCount(1, 0, color);
            if(count>=5)
                flag=true;
            //判断y轴上的胜负
            int count2=this.getCount(0, 1, color);
            if(count2>=5)
                flag=true;
            //判断斜轴(右下、左上)上的胜负
            int count3=this.getCount(1, 1, color);
            if(count3>=5)
                flag=true;
            //判断斜轴(右上、左下)上的胜负
            int count4=this.getCount(1, -1, color);
            if(count4>=5)
                flag=true;
            return flag;
        }
        //四个方向的判断总结为判断方法
        private int getCount(int xChange,int yChange,int color){
            int count = 1;//定义返回值count
            //保存xChange yChange的初始值
            int tempX=xChange;
            int tempY=yChange;
            //棋子一边方向的判断
            while(color==chess[(int) (x+xChange)][(int) (y+yChange)]){
                count++;
                if(xChange!=0){
                    if(xChange>0)
                        xChange++;
                    else
                        xChange--;
                }
                if(yChange!=0){
                    if(yChange>0)
                        yChange++;
                    else
                        yChange--;
                }
            }    
            //重新初始化xChange yChange
            xChange=tempX;
            yChange=tempY;
            //棋子另一边方向的判断
            while(color==chess[(int) (x-xChange)][(int) (y-yChange)]){
                count++;
                if(xChange!=0){
                    if(xChange>0)
                        xChange++;
                    else
                        xChange--;
                }
                if(yChange!=0){
                    if(yChange>0)
                        yChange++;
                    else
                        yChange--;
                }
            }    
            return count;
        }
        public static void main(String args[]){
            new FiveChessFrame();
        }
        @Override
        public void mouseClicked(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 mousePressed(MouseEvent e) {
            if(canPlay){
                System.out.println("("+e.getX()+","+e.getY()+")");
                //获取落子的横纵坐标
                x=e.getX();
                y=e.getY();
                //保证落子在棋盘内
                if(x>15&&x<519&&y>15&&y<516){
                    //对点位进行第x行、y列的转化,并四舍五入取点,保证就进落点原则
                    x=(int) Math.rint((x-22)/35);
                    y=(int) Math.rint((y-22)/35);
                    //保证该该落子点无子
                    if(chess[(int) x][(int) y]==0){
                        //落黑子
                        if(isBlack==true){
                            chess[(int) x][(int) y]=1;
                            isBlack=false;//标记转化
                        }else{//落白字
                            chess[(int) x][(int) y]=2;
                            isBlack=true;
                        }
                        this.repaint();
                        //判断黑棋是否满足获胜的条件
                        if(this.isWin()){
                            JOptionPane.showMessageDialog(this    ,chess[(int) x][(int) y]==1?"黑子获胜!":"白字获胜!");
                            canPlay=false;
                        }
                    }else{//如果落点位存在棋子,则提示重新落子
                        JOptionPane.showMessageDialog(this    , "请重新落子");
                    }
                }
                if(x>519&&x<765&&y>15&&y<516){
                    this.repaint();
                }
            }
        }
        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub
        }
        private class ButtonPane extends JPanel{
            BufferedImage img2= null;
            public void paint(Graphics g){
                try {
                    img2=ImageIO.read(new File("D:/我的JAVA之路/java项目-五子棋/Image/交大背景图.png"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                g.drawImage(img2, 536, 38,220,535,this);
            }
        }
    }
  • 相关阅读:
    wpf 文字动态动画效果
    c# 使用 Tchart控件之数据绑定
    Linq to sql学习之查询句法
    非常有用的查询所有SqlServer数据库字典的操作
    利用WPF建立自适应窗口大小布局的WinForm窗口
    SqlMethods
    wpf 炫彩动画效果简单实例
    SetBkMode与SetBkColor理解
    Windows的字体LOGFONT
    GetWindowRect和GetWindDC GetClientRect和GetDC 在标题栏输入文字
  • 原文地址:https://www.cnblogs.com/hushuai1314/p/6262502.html
Copyright © 2020-2023  润新知