您的位置:首页 > 编程语言

史上最具核心内容的五子棋核心代码------verision1.0

2016-07-24 10:31 393 查看
import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

 public class 五子棋2 extends JPanel {

static int x=0,y=0,m,n,M,N,X,Y,JUDGE=1,sum=0;
boolean bool=false,huatu=true;
static int[] vector_red=new int[700];
 //记录自己所落的棋子坐标的1真0假
static int[] vector_black=new int[700];
 //记录自己所落的棋子坐标的1真0假
static int[] collection=new int[700];//记录双方所落的棋子坐标的1真0假
JFrame frame=new JFrame();

public 五子棋2(){

setBackground(Color.black);

this.setLayout(null);

frame.setLayout(null);

this.setBounds( 0, 0,750,750);

frame.setResizable(false);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setBounds(300,50,850,750);

frame.setVisible(true);

doing();

frame.add(this);

}

public void doing(){//执行坐标解析
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){

m=e.getX();
n=e.getY();
System.out.println(m+"---"+n);

X=m%30;Y=n%30;M=m/30;N=n/30;
if(X>20&&Y>20){       M++;N++;if(collection[M*26+N]==1)System.out.println("此处有棋子");else bool=true; }
if(X<10&&Y<10){       if(collection[M*26+N]==1)System.out.println("此处有棋子");else bool=true;
}
if(X>20&&Y<10){       M++; if(collection[M*26+N]==1)System.out.println("此处有棋子");else bool=true;
}
if(X<10&&Y>20){       N++; if(collection[M*26+N]==1)System.out.println("此处有棋子");else bool=true; }
if(bool==true)repaint();
} });
}

protected void paintComponent(Graphics g){//绘制面板

g.setColor(Color.BLACK);//g.drawLine(0,0,1,600);

for(int i=0;i<=750;i+=30){
g.drawLine(0,i,750,i);
g.drawLine(i,0,i,750);
}
g.drawLine(749,0,749,750);
if(JUDGE==1)g.setColor(Color.red);
else g.setColor(Color.black);

if(bool==true){
int dog=M*26+N;
if(M==0){}
g.drawArc(30*M-10,30*N-10,20,20,0,360);
g.fillArc(30*M-10,30*N-10,20,20,0,360);

collection[dog]=1;
if(JUDGE==2){vector_black[dog]=1;
if(check(vector_black)==true)System.out.println("你赢了");JUDGE--;}
else{vector_red[dog]=1;
if(check(vector_red)==true)System.out.println("你赢了");JUDGE++;}

bool=false;
}
}
public boolean check(int[] vector){//进行4个主要方向检测是否出现五棋连现象
boolean hori=true,
vert=true, und=true,
bel=true;
int horizon=M*26+N;
int vertical=M*26+N;
int under=M*26+N;
int below=M*26+N;

for(int i=1;i<=4;i++){

if(hori==true){
horizon++; if(vector[horizon]==0){hori=false;horizon--;
} }
if(vert==true){
vertical+=10; if(vector[vertical]==0)
{vert=false; vertical-=10; } }
if(und==true){
under+=11; if(vector[under]==0){und=false;
under-=11;} }
if(bel==true) {
below+=9; if(vector[below]==0)
{bel =false;
below-=9;} }

}
hori=true; 
vert=true;
 
und=true;
 
bel=true;
for(int i=1;i<=4;i++){
if(hori==true){
horizon--; if(horizon<0||vector[horizon]==0){hori=false;
} }
if(vert==true){
vertical-=10; if(vertical<0||vector[vertical]==0)
{vert=false;  }  }
if(und==true){
under-=11; if(under<0||vector[under]==0){und=false;
} }
if(bel==true) {
below-=9; if(below<0||vector[below]==0)
{bel =false;
} }

}
if(hori==true){
return true; }
if(vert==true){
return true;  }
if(und==true){
return true; }
if(bel==true) {
return true; }

return false;

}

public static void main(String[] args){
new 五子棋2();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息