JAVA五子棋程序代码分析(1)

JAVA五子棋程序代码分析(1),第1张

楼主要是觉得看的不舒服可以拷到记事本里看~ import java.applet.* import java.awt.* import java.awt.event.* import java.applet.Applet import java.awt.Color//这一段import就不说了,下面要用到的就import进来 public class wuziqi extends Applet implements ActionListener,MouseListener,MouseMotionListener,ItemListener //继承Applet表明是个applet,后面的接口必须要实现每个接口的所有方法 { int color_Qizi=0//旗子的颜色标识 0:白子 1:黑子 int intGame_Start=0//游戏开始标志 0未开始 1游戏中 int intGame_Body[][]=new int[16][16]//设置棋盘棋子状态 0 无子 1 白子 2 黑子 Button b1=new Button("游戏开始") Button b2=new Button("重置游戏")//两个按钮 Label lblWin=new Label(" ")//这个label用来显示最后输赢信息的,先留空 Checkbox ckbHB[]=new Checkbox[2]//用来表明选择黑气或白棋先走的checkbox CheckboxGroup ckgHB=new CheckboxGroup()//两个checkbox必须放在同一个checkboxgroup里才能做到单选 public void init() //初始化,堆砌界面 { setLayout(null)//不设布局管理器 addMouseListener(this)//将本类作为鼠标事件的接口响应鼠标动作 add(b1)//将事先定义好的第一个按钮添加入界面 b1.setBounds(330,50,80,30)//设置第一个按钮左上角的位置和大小 b1.addActionListener(this)//将本类作为按钮事件的接口响应按钮动作 add(b2)//将事先定义好的第二个按钮添加进去 b2.setBounds(330,90,80,30)/设置第二个按钮左上角的位置和大小 b2.addActionListener(this)//将本类作为按钮事件的接口响应按钮动作 ckbHB[0]=new Checkbox("白子先",ckgHB,false)//new一个checkbox ckbHB[0].setBounds(320,20,60,30)//设置左上角位置和大小 ckbHB[1]=new Checkbox("黑子先",ckgHB,false)//new第二个checkbox ckbHB[1].setBounds(380,20,60,30)//设置左上角位置和大小 add(ckbHB[0])//将第一个checkbox加入界面 add(ckbHB[1])//将第二个checkbox加入界面 ckbHB[0].addItemListener(this)//将本类作为其事件接口来响应选中动作 ckbHB[1].addItemListener(this)//将本类作为其事件接口来响应选中动作 add(lblWin)//将标签加入界面 lblWin.setBounds(330,130,80,30)//设置标签的左上角位置和大小 Game_start_csh()//调用游戏初始化 } public void itemStateChanged(ItemEvent e) //ItemListener接口中的方法,必须要有 { if (ckbHB[0].getState()) //选择黑子先还是白子先 { color_Qizi=0//白棋先 } else { color_Qizi=1//黑棋先 } } public void actionPerformed(ActionEvent e) //ActionListener接口中的方法,也是必须的 { Graphics g=getGraphics()//这句话貌似可以去掉,g是用来画图或者画界面的 if (e.getSource()==b1) //如果动作的来源是第一个按钮 { Game_start()//游戏开始 } else //否则 { Game_re()//游戏重新开始 } } public void mousePressed(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseClicked(MouseEvent e) //鼠标单击时 { Graphics g=getGraphics()//获得画笔 int x1,y1 x1=e.getX()//单击处的x坐标 y1=e.getY()//单击处的y坐标 if (e.getX()<20 || e.getX()>300 || e.getY()<20 || e.getY()>300) //在棋盘范围之外 { return//则这是不能走棋的,直接返回 } //下面这两个if和两个赋值的作用是将x和y坐标根据舍入原则修改成棋盘上格子的坐标 if (x1%20>10) { x1+=20 } if(y1%20>10) { y1+=20 } x1=x1/20*20 y1=y1/20*20 set_Qizi(x1,y1)//在棋盘上画上一个棋子 } public void mouseEntered(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseExited(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseReleased(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseDragged(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseMoved(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void paint(Graphics g) //重绘和applet程序装载的时候会调用这个绘制的过程 { draw_qipan(g)//画棋盘 }

基于java的五子棋:

主要功能代码如下:

import java.awt.*

import javax.swing.*

import java.awt.event.*

public class Main extends JFrame implements ActionListener{

        private static final long serialVersionUID = 1L

        JButton[][] point=new JButton[40][40]

        int[][] state=new int[40][40]

        int[][] value=new int[40][40]

        int[] place=new int[2]

        public Main(){

                this.setTitle("五子棋")

                this.setBounds(100,10,1000,1000)

                this.setLayout(new GridLayout(30,30))

                int i,j

                for(i=0i<=39i++){

                        for(j=0j<=39j++){

                                state[i][j]=0

                                value[i][j]=0

                        }

                }

                for(i=5i<=34i++){

                        for (j=5j<=34j++){

                                point[i][j]=new JButton("")

                                this.add(point[i][j])

                                point[i][j].addActionListener(this)

                        }

                }

                this.setVisible(true)

        }

        public void actionPerformed(ActionEvent e)

    {

            int i,j

            for(i=5i<=34i++){

                    for(j=5j<=34j++){

                            if(e.getSource()==point[i][j]){

                                    point[i][j].setBackground(Color.RED)

                                    state[i][j]=1

                                    point[i][j].setEnabled(false)

                                    value[i][j]=0

                            }

                    }

            }

          

    public static void main(String[] args)throws NullPointerException{

            new Main()

    }

                else if(((state[i][j]==0)&&(state[i][j+1]==1)&&(state[i][j+2]==0))||

                                ((state[i][j]==0)&&(state[i][j-1]==1)&&(state[i][j-2]==0))||

                                ((state[i][j]==0)&&(state[i+1][j]==1)&&(state[i+2][j]==0))||

                                ((state[i][j]==0)&&(state[i-1][j]==1)&&(state[i-2][j]==0))||                               //空红空

                                ((state[i][j]==0)&&(state[i+1][j+1]==1)&&(state[i+2][j+2]==0))||

                                ((state[i][j]==0)&&(state[i+1][j-1]==1)&&(state[i+2][j-2]==0))||

                                ((state[i][j]==0)&&(state[i-1][j+1]==1)&&(state[i-2][j+2]==0))||

                                ((state[i][j]==0)&&(state[i-1][j-1]==1)&&(state[i-2][j-2]==0))||

                        ((state[i][j]==0)&&(state[i][j+1]==1)&&(state[i][j+2]==1)&&(state[i][j+3]==2))||

                                ((state[i][j]==0)&&(state[i][j-1]==1)&&(state[i][j-2]==1)&&(state[i][j-3]==2))||

                                ((state[i][j]==0)&&(state[i+1][j]==1)&&(state[i+2][j]==1)&&(state[i+3][j]==2))||

                                ((state[i][j]==0)&&(state[i-1][j]==1)&&(state[i-2][j]==1)&&(state[i-3][j]==2))||                            //空红红蓝型

                                ((state[i][j]==0)&&(state[i+1][j+1]==1)&&(state[i+2][j+2]==1)&&(state[i+3][j+3]==2))||

                                ((state[i][j]==0)&&(state[i+1][j-1]==1)&&(state[i+2][j-2]==1)&&(state[i+3][j-3]==2))||

                                ((state[i][j]==0)&&(state[i-1][j+1]==1)&&(state[i-2][j+2]==1)&&(state[i-3][j+3]==2))||

                                ((state[i][j]==0)&&(state[i-1][j-1]==1)&&(state[i-2][j-2]==1)&&(state[i-3][j-3]==2))){

                                value[i][j]=2

                                return value[i][j]

                }

                else if(((state[i][j]==0)&&(state[i][j+1]==2)&&(state[i][j+2]==0))||

                                ((state[i][j]==0)&&(state[i][j-1]==2)&&(state[i][j-2]==0))||

                                ((state[i][j]==0)&&(state[i+1][j]==2)&&(state[i+2][j]==0))||

                                ((state[i][j]==0)&&(state[i-1][j]==2)&&(state[i-2][j]==0))||

                                ((state[i][j]==0)&&(state[i+1][j+1]==2)&&(state[i+2][j+2]==0))||

                                ((state[i][j]==0)&&(state[i+1][j-1]==2)&&(state[i+2][j-2]==0))||

                                ((state[i][j]==0)&&(state[i-1][j+1]==2)&&(state[i-2][j+2]==0))||

                                ((state[i][j]==0)&&(state[i-1][j-1]==2)&&(state[i-2][j-2]==0))||

                        ((state[i][j]==0)&&(state[i][j+1]==2)&&(state[i][j+2]==2)&&(state[i][j+3]==1))||

                                ((state[i][j]==0)&&(state[i][j-1]==2)&&(state[i][j-2]==2)&&(state[i][j-3]==1))||

                                ((state[i][j]==0)&&(state[i+1][j]==2)&&(state[i+2][j]==2)&&(state[i+3][j]==1))||

                                ((state[i][j]==0)&&(state[i-1][j]==2)&&(state[i-2][j]==2)&&(state[i-3][j]==1))||

                                ((state[i][j]==0)&&(state[i+1][j+1]==2)&&(state[i+2][j+2]==2)&&(state[i+3][j+3]==1))||

                                ((state[i][j]==0)&&(state[i+1][j-1]==2)&&(state[i+2][j-2]==2)&&(state[i+3][j-3]==1))||

                                ((state[i][j]==0)&&(state[i-1][j+1]==2)&&(state[i-2][j+2]==2)&&(state[i-3][j+3]==1))||

                                ((state[i][j]==0)&&(state[i-1][j-1]==2)&&(state[i-2][j-2]==2)&&(state[i-3][j-3]==1))){

                                value[i][j]=1

                                return value[i][j]

                }

                else{

                        value[i][j]=0

                        return value[i][j]

                }

    }

    public int[] largestValue(int[][] value){

            int max,i,j,l,k

            l=0k=0

            max=0

            for(i=5i<34i++){

                    for(j=5j<34j++){

                            if(max<value[i][j]){

                                    max=value[i][j]

                                    place[0]=i

                                    place[1]=j

                            }

                    }

            }

            return place

    }

}

运行结果如下图所示:


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11504632.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存