史上最具核心内容的五子棋核心代码------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();
}
}
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();
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树