资源描述:
《java基于swing实现的五子棋游戏代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、java基于swing实现的五子棋游戏代码这篇文章主要介绍了java基于swing实现的五子棋游戏代码,主要涉及图形界面与数组的用法,有不错的参考借鉴价值,需要的朋友可以参考下本文实例讲述了java基于swing实现的五子棋游戏代码。分享给大家供大家参考。主要功能代码如下:复制代码代码如下:importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassMainextendsJFrameimplementsActionListener{privatestaticfinallongserialVersionU
2、ID=1L;JButton[][]point=newJButton[40][40];int[][]state=newint[40][40];int[][]value=newint[40][40];int[]place=newint[2];publicMain(){this.setTitle("五子棋");this.setBounds(100,10,1000,1000);this.setLayout(newGridLayout(30,30));inti,j;for(i=0;i<=39;i++){for(j=0;j<=39;j++){state[i][j]=0;value[i][j]=0
3、;}}for(i=5;i<=34;i++){for(j=5;j<=34;j++){point[i][j]=newJButton("");this.add(point[i][j]);point[i][j].addActionListener(this);}}this.setVisible(true);}publicvoidactionPerformed(ActionEvente){inti,j;for(i=5;i<=34;i++){for(j=5;j<=34;j++){if(e.getSource()==point[i][j]){point[i][j].setBackground(Co
4、lor.RED);state[i][j]=1;point[i][j].setEnabled(false);value[i][j]=0;}}}for(i=5;i<=34;i++){for(j=5;j<=34;j++){value[i][j]=value(i,j);if(((state[i][j]==1)&&(state[i][j+1]==1)&&(state[i][j+2]==1)&&(state[i][j+3]==1)&&(state[i][j+4]==1))
5、
6、((state[i][j]==1)&&(state[i+1][j]==1)&&(state[i+2][j]==1)&&(s
7、tate[i+3][j]==1)&&(state[i+4][j]==1))
8、
9、((state[i][j]==1)&&(state[i-1][j+1]==1)&&(state[i-2][j+2]==1)&&(state[i-3][j+3]==1)&&(state[i-4][j+4]==1))
10、
11、((state[i][j]==1)&&(state[i+1][j+1]==1)&&(state[i+2][j+2]==1)&&(state[i+3][j+3]==1)&&(state[i+4][j+4]==1))){JOptionPane.showMessageDialog(null,"你太厉害
12、了吧,我打不过你");}}}place=largestValue(value);point[place[0]][place[1]].setBackground(Color.blue);state[place[0]][place[1]]=2;value[place[0]][place[1]]=0;for(i=5;i<=34;i++){for(j=5;j<=34;j++){if(((state[i][j]==2)&&(state[i][j+1]==2)&&(state[i][j+2]==2)&&(state[i][j+3]==2)&&(state[i][j+4]==2))
13、
14、((stat
15、e[i][j]==2)&&(state[i+1][j]==2)&&(state[i+2][j]==2)&&(state[i+3][j]==2)&&(state[i+4][j]==2))
16、
17、((state[i][j]==2)&&(state[i-1][j+1]==2)&&(state[i-2][j+2]==2)&&(state[i-3][j+3]==2)&&(state[i-4][j+4]==2))
18、
19、((state[i][j]==2)&&(state[i+