Home Java javaTutorial Java writing backgammon

Java writing backgammon

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);
} 
 
 
}
Copy after login

Java writing backgammon

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

See all articles