java编写五子棋程序,不要图形界面的那种

importjava.awt.Button;

importjava.awt.Color;

importjava.awt.Graphics;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.KeyAdapter;

importjava.awt.event.KeyEvent;

importjava.awt.event.MouseEvent;

importjava.awt.event.MouseListener;

importjava.util.ArrayList;

importjava.util.Random;

importjavax.swing.BoxLayout;

importjavax.swing.JFrame;

importjavax.swing.JOptionPane;

importjavax.swing.JPanel;

importjavax.swing.Timer;

publicclassChessGameextendsJFrameimplementsMouseListener{

privateintblackcount=0;

privateintwhitecount=0;

privatebooleanflag=true;

privatebooleanwhitewin;

privatebooleanblackwin;

privatebooleanflash=false;

publicTimertime;

privateArrayListblacklist;

privateArrayListwhitelist;

publicChessGame(){

setSize(450,500);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

blacklist=newArrayList();

whitelist=newArrayList();

this.addMouseListener(this);

setVisible(true);

}

@Override

publicvoidpaint(Graphicsg){

if(flash==true){

super.paint(g);

}

flash=false;

g.setColor(Color.WHITE);

g.fillRect(0,0,420,420);

g.setColor(Color.DARK_GRAY);

g.drawLine(5,5,420,5);

g.drawLine(5,5,5,420);

g.drawLine(420,5,420,420);

g.drawLine(5,420,420,420);

for(inti=1;i<=21;i++){

g.drawLine(5,20*i,420,20*i);

g.drawLine(20*i,5,20*i,420);

}

g.setColor(Color.BLACK);

for(inti=0;i<blacklist.size();i++){

g.fillOval(blacklist.get(i).getX()*20+12,blacklist.get(i).getY()*20+32,16,16);

}

g.setColor(Color.RED);

for(inti=0;i<whitelist.size();i++){

g.fillOval(whitelist.get(i).getX()*20+12,whitelist.get(i).getY()*20+32,16,16);

}

this.checkwin(blacklist,0,0,0,0);

this.checkwin(whitelist,0,0,0,1);

g.setColor(Color.black);

Stringstr1=黑方胜利局数为:+blackcount;

g.drawString(str1,10,450);

Stringstr2=红方胜利局数为:+whitecount;

g.drawString(str2,150,450);

}

publicstaticvoidmain(String[]args){

ChessGamesnake=newChessGame();

}

publicbooleancheckExist(mypointa){

for(inti=0;i<whitelist.size();i++){

if(whitelist.get(i).getX()==a.getX()&&whitelist.get(i).getY()==a.getY()){

returntrue;

}

}

for(inti=0;i<blacklist.size();i++){

if(blacklist.get(i).getX()==a.getX()&&blacklist.get(i).getY()==a.getY()){

returntrue;

}

}

returnfalse;

}

@Override

publicvoidmouseClicked(MouseEvente){

//TODOAuto-generatedmethodstub

doublex=e.getX();

doubley=e.getY();

if(x>=5&&x=5&&y<=410){

doublek=20;

x=x/k-1;

y=y/k-2;

intax=(int)Math.round(x);

intay=(int)Math.round(y);

if(checkExist(newmypoint(ax,ay))){

JOptionPane.showMessageDialog(null,该位置已有棋子!);

repaint();

}else{

if(flag==true){

blacklist.add(newmypoint(ax,ay));

}

if(flag==false){

whitelist.add(newmypoint(ax,ay));

}

flag=!flag;

repaint();

}

}

}

publicvoidcheckwin(ArrayListlist,intpos,intdirect,intcount,inttype){

if(count==0){

for(inti=0;i<list.size();i++){

for(intj=0;j<list.size();j++){

if(list.get(i).getY()==list.get(j).getY()&&(list.get(j).getX()-list.get(i).getX()==-1)){

direct=2;

count++;

checkwin(list,j,2,count,type);

count=0;

}

if(list.get(i).getX()==list.get(j).getX()&&(list.get(j).getY()-list.get(i).getY()==-1)){

direct=0;

count++;

checkwin(list,j,0,count,type);

count=0;

}

if(list.get(j).getY()==list.get(i).getY()-1&&(list.get(j).getX()-list.get(i).getX()==1)){

direct=4;

count++;

checkwin(list,j,4,count,type);

count=0;

}

if(list.get(j).getY()==list.get(i).getY()+1&&(list.get(j).getX()-list.get(i).getX()==1)){

direct=5;

count++;

checkwin(list,j,5,count,type);

count=0;

}

}

}

}

else{

if(direct==4){

for(inti=0;i<list.size();i++){

if(i!=pos){

if(list.get(i).getX()==list.get(pos).getX()+1&&(list.get(i).getY()-list.get(pos).getY()==-1)){

count=count+1;

if(count==4){

if(type==0){

System.out.println(blackwin!);

blackcount++;

JOptionPane.showMessageDialog(null,黑方赢了!);

restart();

}if(type==1){

System.out.println(whitewin!);

whitecount++;

JOptionPane.showMessageDialog(null,红方赢了!);

restart();

}

}else{

checkwin(list,i,direct,count,type);

}

}

}

}

}

if(direct==5){

for(inti=0;i<list.size();i++){

if(i!=pos){

if(list.get(i).getX()==list.get(pos).getX()+1&&(list.get(i).getY()-list.get(pos).getY()==1)){

count=count+1;

if(count==4){

if(type==0){

System.out.println(blackwin!);

blackcount++;

JOptionPane.showMessageDialog(null,黑方赢了!);

restart();

}if(type==1){

System.out.println(whitewin!);

whitecount++;

JOptionPane.showMessageDialog(null,红方赢了!);

restart();

}

}else{

checkwin(list,i,direct,count,type);

}

}

}

}

}

if(direct==0){

for(inti=0;i<list.size();i++){

if(i!=pos){

if(list.get(pos).getX()==list.get(i).getX()&&(list.get(i).getY()-list.get(pos).getY()==-1)){

count=count+1;

if(count==4){

if(type==0){

System.out.println(blackwin!);

blackcount++;

JOptionPane.showMessageDialog(null,黑方赢了!);

restart();

}if(type==1){

System.out.println(whitewin!);

whitecount++;

JOptionPane.showMessageDialog(null,红方赢了!);

restart();

}

}else{

checkwin(list,i,direct,count,type);

}

}

}

}

}

if(direct==2){

for(inti=0;i<list.size();i++){

if(i!=pos){

if(list.get(pos).getY()==list.get(i).getY()&&(list.get(i).getX()-list.get(pos).getX()==-1)){

count=count+1;

if(count==4){

if(type==0){

System.out.println(blackwin!);

blackcount++;

JOptionPane.showMessageDialog(null,黑方赢了!);

restart();

}if(type==1){

System.out.println(whitewin!);

whitecount++;

JOptionPane.showMessageDialog(null,红方赢了!);

restart();

}

}else{

checkwin(list,i,direct,count,type);

}

}

}

}

}

}

}

publicvoidrestart(){

whitelist=newArrayList();

blacklist=newArrayList();

flash=true;

repaint();

}

@Override

publicvoidmousePressed(MouseEvente){

//TODOAuto-generatedmethodstub

}

@Override

publicvoidmouseReleased(MouseEvente){

//TODOAuto-generatedmethodstub

}

@Override

publicvoidmouseEntered(MouseEvente){

//TODOAuto-generatedmethodstub

}

@Override

publicvoidmouseExited(MouseEvente){

//TODOAuto-generatedmethodstub

}

}

classmypoint{

intx;

inty;

publicmypoint(inta,intb){

this.x=a;

this.y=b;

}

publicintgetX(){

returnthis.x;

}

publicintgetY(){

returnthis.y;

}

}

免责声明:本站发布的游戏攻略(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场。
如果本文侵犯了您的权益,请联系站长邮箱进行举报反馈,一经查实,我们将在第一时间处理,感谢您对本站的关注!