java編寫五子棋

d4nd 9年前發布 | 9K 次閱讀 Java

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);
    

    }

}</pre>

 本文由用戶 d4nd 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!