搜索
首页Javajava教程java编写五子棋

java编写五子棋

Nov 11, 2016 am 10:58 AM

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<15 ; x++){
for(int y = 0 ; y < 15 ; y++){
 if(allChess[x][y]==0){
       if(select==2){
      bool = true ;
     }else if(select==1){
      bool = false;
     }
      }
}
}
if(win==true){     
// TODO Auto-generated method stub
            
              x = e.getX();     
              y = e.getY();     
           if(x>=65&&x<=418&&y>=70&&y<=430){
           
          x =  (x-60)/25;     //得到距离附近的交叉点X轴坐标
          y =  (y-70)/25;    
          if(allChess[x][y]==0){   
          if(bool==true){       //默认下黑棋子先
          allChess[x][y] =1;   
             show1 = "白方下棋";   // 提示下步棋下白色
             bool = false ;    // 修改标记, 下步棋应该由白方下
             select = 1 ;
          }else{
           allChess[x][y]= 2 ;   
           show1 = "黑方下棋";   
        bool = true;   
        select = 2 ;
          }
          this.repaint();    
          int zong = 0  ;
          for(int i = 0 ; i < 15 ; i++ ){
      for(int k = 0 ; k < 15 ; k++){
      if(allChess[i][k]!=0){
          zong++ ; 
       }
      }
      }
           if(zong==225){
          JOptionPane.showMessageDialog(this, "游戏平局,请点击“重新开始”重
 
 
新比过!"); 
            }
          boolean heng1 = this.myCheck1() ;  //横  false
          boolean heng2 = this.myCheck2() ; 
          boolean heng3 = this.myCheck3() ;  
          boolean heng4 = this.myCheck4() ;  
        if(heng1==true||heng2==true||heng3==true||heng4==true){
            JOptionPane.showMessageDialog(this,"游戏结束:" + 
                   (allChess[x][y]==1?"黑方":"白方")+"胜利");
                       
            win = false ; 
           }     
              }
           }
} 
 
if(e.getX()>=427&&e.getX()<=495&&e.getY()>193&&e.getY()<228){   // 关于设计
JOptionPane.showMessageDialog(this, "本程序用JAVA设计") ;
        }
if(e.getX()>=301&&e.getX()<=407&&e.getY()>466&&e.getY()<532){    // 重新开始
JOptionPane.showMessageDialog(this, "重新游戏") ;
for(int i = 0 ; i < 15 ; i++ ){
for(int k = 0 ; k < 15 ; k++){
       allChess[i][k] = 0 ; 
}
  
}   
show1 = "" ;
bool = true ;
win =true ;
select = 0 ;
bool2 = true ;
this.repaint() ; 
    }
 
if(e.getX()>=425&&e.getX()<=491&&e.getY()>101&&e.getY()<140){   //  设置设计 
for(int x = 0 ; x<15 ; x++){
for(int y = 0 ; y < 15 ; y++){
 if(allChess[x][y]==0&&select!=0){
   bool2 = false ;
             }
      }
  }
 if(bool2==true){
      String input = JOptionPane.showInputDialog( "选择谁先下棋:白方输入“1” ; 黑方输入
“2” ") ; 
        try{
        select = Integer.parseInt(input);
          if(select!=1&&select!=2){
            JOptionPane.showMessageDialog(this, "您输入的不是1或2,请重新设置!");
              select = 0; 
         }
           }catch(Exception e1){
                 JOptionPane.showMessageDialog(this, "输入有误,请重新输入!");
           }
        }else{
        JOptionPane.showMessageDialog(this, "游戏比赛中,不能设置此项目!!!");
        }
 }    
if(e.getX()>=426&&e.getX()<=495&&e.getY()>382&&e.getY()<425){   // 结束设计
int a = JOptionPane.showConfirmDialog(this, "游戏是否结束?") ;   
 if(a==0){
System.exit(0);
   }
 if(a==1){
 JOptionPane.showMessageDialog(this, "欢迎您回到游戏来!"); 
 }
 if(a==2){
 JOptionPane.showMessageDialog(this, "小样, 请别再耍我!");   
 }
 }
if(e.getX()>=433&&e.getX()<=495&&e.getY()>289&&e.getY()<334){    //游戏说明
JOptionPane.showMessageDialog(this, "五子棋游戏规则: 谁先把5颗棋子相连,谁
 
就是赢家") ;
    }
}
 public void paint(Graphics g){    
      BufferedImage B1 = new BufferedImage(500 , 550 ,BufferedImage.TYPE_INT_ARGB); 
       // 定义一个新的缓冲图片
       Graphics g2 = B1.createGraphics();
       
      g2.drawImage(buff, 0, 20, this);     // 将图片显示在窗体上
      g2.setColor(Color.BLACK);           // 设置画笔颜色为黑色
      g2.fillOval(135, 144, 6, 6);
      g2.fillOval(334, 144, 6, 6);      //  画几个定点
      g2.fillOval(138, 342, 6, 6);
      g2.fillOval(334, 342, 6, 6);
      g2.fillOval(236, 242, 6, 6); 
      g2.setFont(new Font("黑体" , Font.BOLD , 20));   
      g2.drawString("游戏信息:"+show1, 83, 49);   //显示当前由哪一方下棋
      g2.drawString("黑方:", 65, 473);
      g2.drawString("白方:", 62, 518);
      for(int i = 0 ; i < 15 ; i++ ){    // 循环判断
      for(int j = 0 ; j < 15 ; j++){
      if(allChess[i][j]==1){    
      //黑子
      int tempX = i*25 + 63 ;   
      int tempY = j*25 + 70 ;  
 
      g2.fillOval(tempX-8, tempY-8, 16,16);   
       
      }
      if(allChess[i][j]==2){     
      //白子
      int tempX = i*25 + 63 ;  
      int tempY = j*25 + 70 ;  
      g2.setColor(Color.WHITE);    
      g2.fillOval(tempX-8, tempY-8, 16,16);    
          g2.setColor(Color.BLACK);
          g2.drawOval(tempX-8, tempY-8, 16, 16); 
      }
       
       
       
      }
      }
       g.drawImage(B1, 0, 5, this);   // 把B1这张图片在窗体显示出来
      
      } 
public boolean myCheck1() {                // 判断横向是否有五子相连
int count = 1;  
 boolean heng = false ;  //
 int color = allChess[x][y] ;  // 判断颜色 
// TODO Auto-generated method stub
      int i = 1 ;
while(x+i<=14&&color == allChess[x+i][y]){
i++ ;
count++ ;
// System.out.println(count);
//System.out.println(i);
}
i = 1 ;
while(x-i>=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)<=14&&color == allChess[x][y+i]){
i++ ;
count++ ;  
}
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)<=14&&(y-i)>=0&&color == allChess[x+i][y-i]){
i++ ;
count++ ;  
}
i = 1 ;
while((x-i)>=0&&(y+i)<=14&&color == allChess[x-i][y+i]){
i++;
count++ ;
}
if(count>=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)<=14&&(y+i)<=14&&color == allChess[x+i][y+i]){
i++;
count++ ;
}
if(count>=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);
} 
 
 
}

14495610711.jpg

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
JVM如何处理操作系统API的差异?JVM如何处理操作系统API的差异?Apr 27, 2025 am 12:18 AM

JVM通过JavaNativeInterface(JNI)和Java标准库处理操作系统API差异:1.JNI允许Java代码调用本地代码,直接与操作系统API交互。2.Java标准库提供统一API,内部映射到不同操作系统API,确保代码跨平台运行。

Java 9影响平台独立性中引入的模块化如何?Java 9影响平台独立性中引入的模块化如何?Apr 27, 2025 am 12:15 AM

modularitydoesnotdirectlyaffectJava'splatformindependence.Java'splatformindependenceismaintainedbytheJVM,butmodularityinfluencesapplicationstructureandmanagement,indirectlyimpactingplatformindependence.1)Deploymentanddistributionbecomemoreefficientwi

什么是字节码,它与Java的平台独立性有何关系?什么是字节码,它与Java的平台独立性有何关系?Apr 27, 2025 am 12:06 AM

BytecodeinJavaistheintermediaterepresentationthatenablesplatformindependence.1)Javacodeiscompiledintobytecodestoredin.classfiles.2)TheJVMinterpretsorcompilesthisbytecodeintomachinecodeatruntime,allowingthesamebytecodetorunonanydevicewithaJVM,thusfulf

为什么Java被认为是一种独立于平台的语言?为什么Java被认为是一种独立于平台的语言?Apr 27, 2025 am 12:03 AM

javaachievesplatformIndependencEthroughThoJavavIrtualMachine(JVM),wodecutesbytecodeonyanydenanydevicewithajvm.1)javacodeiscompiledintobytecode.2)

图形用户界面(GUIS)如何提出Java平台独立性的挑战?图形用户界面(GUIS)如何提出Java平台独立性的挑战?Apr 27, 2025 am 12:02 AM

JavaGUI开发中的平台独立性面临挑战,但可以通过使用Swing、JavaFX,统一外观,性能优化,第三方库和跨平台测试来应对。JavaGUI开发依赖于AWT和Swing,Swing旨在提供跨平台一致性,但实际效果因操作系统不同而异。解决方案包括:1)使用Swing和JavaFX作为GUI工具包;2)通过UIManager.setLookAndFeel()统一外观;3)优化性能以适应不同平台;4)使用如ApachePivot或SWT的第三方库;5)进行跨平台测试以确保一致性。

Java开发的哪些方面取决于平台?Java开发的哪些方面取决于平台?Apr 26, 2025 am 12:19 AM

JavadevelovermentIrelyPlatForm-DeTueTososeVeralFactors.1)JVMVariationsAffectPerformanceNandBehaviorAcroSsdifferentos.2)Nativelibrariesviajnijniiniininiinniinindrododerplatefform.3)

在不同平台上运行Java代码时是否存在性能差异?为什么?在不同平台上运行Java代码时是否存在性能差异?为什么?Apr 26, 2025 am 12:15 AM

Java代码在不同平台上运行时会有性能差异。1)JVM的实现和优化策略不同,如OracleJDK和OpenJDK。2)操作系统的特性,如内存管理和线程调度,也会影响性能。3)可以通过选择合适的JVM、调整JVM参数和代码优化来提升性能。

Java平台独立性有什么局限性?Java平台独立性有什么局限性?Apr 26, 2025 am 12:10 AM

Java'splatFormentenceHaslimitations不包括PerformanceOverhead,versionCompatibilityIsissues,挑战WithnativelibraryIntegration,Platform-SpecificFeatures,andjvminstallation/jvminstallation/jvmintenance/jeartenance.therefactorscomplicatorscomplicatethe“ writeOnce”

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具