基于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
}
}
运行结果如下图所示:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)