欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  Java

java编写五子棋

程序员文章站 2022-05-14 23:31:34
...
MyJFrame.java
package chi1.JFrame;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class MyJFrame extends JFrame implements MouseListener {
private static final long serialVersionUID = -46847020668994298L;
BufferedImage buff = null;   // 声明对象,为获取图片
     int x , y ;
     // 保存所有的点多的坐标
     int[][] allChess = new int[15][15] ;  // 用一个二维数组保存黑白子信息。
     String show1 = "" ;   // 默认黑方下棋
     //默认下黑子 , 判断下什么棋子。bool=true 下黑子    ; bool=false 下白子
    // boolean bool = true ;
      
      // 判断是否赢了比赛, 如果赢了比赛, 就不能再继续下棋了,
     boolean win =true ;
       public MyJFrame(){ 
    int width = Toolkit.getDefaultToolkit().getScreenSize().width ;       
        int height = Toolkit.getDefaultToolkit().getScreenSize().height ;   
          this.setTitle("五子棋");      
          this.setResizable(false);    
          this.setSize(500,550 );    
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
          this.setLocation((width-500)/2 , (height-550)/2);   
               this.repaint();
          this.addMouseListener(this);     
          try {
buff = ImageIO.read(new File("e:/wzq.jpg" ));  
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
          this.setVisible(true);         
  
       };
     //  boolean rekai = true ; 
     int select = 0 ;
     boolean bool2=true ;
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
        // System.out.println(e.getX());
        //  System.out.println(e.getY());
}
 
@Override
 
public void mousePressed(MouseEvent e) {
boolean bool = true ;
for(int x = 0 ; x=65&&x=70&&y=427&&e.getX()193&&e.getY()=301&&e.getX()466&&e.getY()=425&&e.getX()101&&e.getY()=426&&e.getX()382&&e.getY()=433&&e.getX()289&&e.getY()=0&&color == allChess[x-i][y]){
i++;
count++ ;
}
if(count>=5){
heng = true ;
}
        return heng ;
}
public boolean myCheck2() {             // 判断纵向是否有五子相连
int count = 1;
 boolean zhong = false ; 
 int color = allChess[x][y] ;
// TODO Auto-generated method stub
      int i = 1 ;
while((y+i)=0&&color == allChess[x][y-i]){
i++;
count++ ;
}
    
if(count>=5){
zhong = true ;  
} 
        return zhong ;
}
public boolean myCheck3() {            // 判断右斜向是否有五子相连
int count = 1;
 boolean zhong = false ; 
 int color = allChess[x][y] ;
// TODO Auto-generated method stub
       
      int i = 1 ;
while((x+i)=0&&color == allChess[x+i][y-i]){
i++ ;
count++ ;  
}
i = 1 ;
while((x-i)>=0&&(y+i)=5){
zhong = true ;
}
        return zhong ;
}
public boolean myCheck4() {              // 判左斜向是否有五子相连
int count = 1;
 boolean zhong = false ; 
 int color = allChess[x][y] ;
// TODO Auto-generated method stub
       
      int i = 1 ;
while((x-i)>=0&&(y-i)>=0&&color == allChess[x-i][y-i]){
i++ ;
count++ ;  
}
i = 1 ;
while((x+i)=5){
zhong = true ;
}
        return zhong ;
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
 
Text01.java
 
package chi1.Text;
import chi1.JFrame.MyJFrame;
public class Text01 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
         MyJFrame jf =  new MyJFrame() ;
        
       // String b1 = JOptionPane.showInputDialog("请输入你的名字:") ;
        //int b =   JOptionPane.showConfirmDialog(jf, "我的信息:" + b1);
} 
 
 
}

java编写五子棋