溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java swing五子棋的實(shí)現(xiàn)方法

發(fā)布時(shí)間:2020-08-25 15:00:12 來源:腳本之家 閱讀:151 作者:明禮馨德 欄目:編程語言

今天給大家介紹一下如何用Java swing實(shí)現(xiàn)五子棋的開發(fā)即用Java開發(fā)圖形界面程序五子棋,代碼由于太多,只貼部分,最下面會(huì)附上下載地址,廢話不多說,下面我們先看一下運(yùn)行結(jié)果: 

Java swing五子棋的實(shí)現(xiàn)方法

接下來我們看代碼:

首先是創(chuàng)建主frame框架界面:

package org.liky.game.frame;

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 FiveChessFrame extends JFrame implements MouseListener, Runnable {

 // 取得屏幕的寬度
 int width = Toolkit.getDefaultToolkit().getScreenSize().width;
 // 取得屏幕的高度
 int height = Toolkit.getDefaultToolkit().getScreenSize().height;
 // 背景圖片
 BufferedImage bgImage = null;
 // 保存棋子的坐標(biāo)
 int x = 0;
 int y = 0;
 // 保存之前下過的全部棋子的坐標(biāo)
 // 其中數(shù)據(jù)內(nèi)容 0: 表示這個(gè)點(diǎn)并沒有棋子, 1: 表示這個(gè)點(diǎn)是黑子, 2:表示這個(gè)點(diǎn)是白子
 int[][] allChess = new int[19][19];
 // 標(biāo)識(shí)當(dāng)前應(yīng)該黑棋還是白棋下下一步
 boolean isBlack = true;
 // 標(biāo)識(shí)當(dāng)前游戲是否可以繼續(xù)
 boolean canPlay = true;
 // 保存顯示的提示信息
 String message = "黑方先行";
 // 保存最多擁有多少時(shí)間(秒)
 int maxTime = 0;
 // 做倒計(jì)時(shí)的線程類
 Thread t = new Thread(this);
 // 保存黑方與白方的剩余時(shí)間
 int blackTime = 0;
 int whiteTime = 0;
 // 保存雙方剩余時(shí)間的顯示信息
 String blackMessage = "無限制";
 String whiteMessage = "無限制";

 public FiveChessFrame() {
 // 設(shè)置標(biāo)題
 this.setTitle("五子棋");
 // 設(shè)置窗體大小
 this.setSize(500, 500);
 // 設(shè)置窗體出現(xiàn)位置
 this.setLocation((width - 500) / 2, (height - 500) / 2);
 // 將窗體設(shè)置為大小不可改變
 this.setResizable(false);
 // 將窗體的關(guān)閉方式設(shè)置為默認(rèn)關(guān)閉后程序結(jié)束
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 // 為窗體加入監(jiān)聽器
 this.addMouseListener(this);
 // 將窗體顯示出來
 this.setVisible(true);

 t.start();
 t.suspend();

 // 刷新屏幕,防止開始游戲時(shí)出現(xiàn)無法顯示的情況.
 this.repaint();
 String imagePath = "" ;
 try {
  imagePath = System.getProperty("user.dir")+"/src/image/background.jpg" ;
  bgImage = ImageIO.read(new File(imagePath.replaceAll("\\\\", "/")));
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 }

 public void paint(Graphics g) {
 // 雙緩沖技術(shù)防止屏幕閃爍
 BufferedImage bi = new BufferedImage(500, 500,
  BufferedImage.TYPE_INT_RGB);
 Graphics g2 = bi.createGraphics();
 g2.setColor(Color.BLACK);
 // 繪制背景
 g2.drawImage(bgImage, 1, 20, this);
 // 輸出標(biāo)題信息
 g2.setFont(new Font("黑體", Font.BOLD, 20));
 g2.drawString("游戲信息:" + message, 130, 60);
 // 輸出時(shí)間信息
 g2.setFont(new Font("宋體", 0, 14));
 g2.drawString("黑方時(shí)間:" + blackMessage, 30, 470);
 g2.drawString("白方時(shí)間:" + whiteMessage, 260, 470);

 // 繪制棋盤
 for (int i = 0; i < 19; i++) {
  g2.drawLine(10, 70 + 20 * i, 370, 70 + 20 * i);
  g2.drawLine(10 + 20 * i, 70, 10 + 20 * i, 430);
 }

 // 標(biāo)注點(diǎn)位
 g2.fillOval(68, 128, 4, 4);
 g2.fillOval(308, 128, 4, 4);
 g2.fillOval(308, 368, 4, 4);
 g2.fillOval(68, 368, 4, 4);
 g2.fillOval(308, 248, 4, 4);
 g2.fillOval(188, 128, 4, 4);
 g2.fillOval(68, 248, 4, 4);
 g2.fillOval(188, 368, 4, 4);
 g2.fillOval(188, 248, 4, 4);

 /*
  * //繪制棋子 x = (x - 10) / 20 * 20 + 10 ; y = (y - 70) / 20 * 20 + 70 ;
  * //黑子 g.fillOval(x - 7, y - 7, 14, 14); //白子 g.setColor(Color.WHITE) ;
  * g.fillOval(x - 7, y - 7, 14, 14); g.setColor(Color.BLACK) ;
  * g.drawOval(x - 7, y - 7, 14, 14);
  */

 // 繪制全部棋子
 for (int i = 0; i < 19; i++) {
  for (int j = 0; j < 19; j++) {
  if (allChess[i][j] == 1) {
   // 黑子
   int tempX = i * 20 + 10;
   int tempY = j * 20 + 70;
   g2.fillOval(tempX - 7, tempY - 7, 14, 14);
  }
  if (allChess[i][j] == 2) {
   // 白子
   int tempX = i * 20 + 10;
   int tempY = j * 20 + 70;
   g2.setColor(Color.WHITE);
   g2.fillOval(tempX - 7, tempY - 7, 14, 14);
   g2.setColor(Color.BLACK);
   g2.drawOval(tempX - 7, tempY - 7, 14, 14);
  }
  }
 }
 g.drawImage(bi, 0, 0, this);
 }

 public void mouseClicked(MouseEvent e) {
 // TODO Auto-generated method stub

 }

 public void mouseEntered(MouseEvent e) {
 // TODO Auto-generated method stub

 }

 public void mouseExited(MouseEvent e) {
 // TODO Auto-generated method stub

 }

 public void mousePressed(MouseEvent e) {
 // TODO Auto-generated method stub
 /*
  * System.out.println("X:"+e.getX()); System.out.println("Y:"+e.getY());
  */
 if (canPlay == true) {
  x = e.getX();
  y = e.getY();
  if (x >= 10 && x <= 370 && y >= 70 && y <= 430) {
  x = (x - 10) / 20;
  y = (y - 70) / 20;
  if (allChess[x][y] == 0) {
   // 判斷當(dāng)前要下的是什么顏色的棋子
   if (isBlack == true) {
   allChess[x][y] = 1;
   isBlack = false;
   message = "輪到白方";
   } else {
   allChess[x][y] = 2;
   isBlack = true;
   message = "輪到黑方";
   }
   // 判斷這個(gè)棋子是否和其他的棋子連成5連,即判斷游戲是否結(jié)束
   boolean winFlag = this.checkWin();
   if (winFlag == true) {
   JOptionPane.showMessageDialog(this, "游戲結(jié)束,"
    + (allChess[x][y] == 1 ? "黑方" : "白方") + "獲勝!");
   canPlay = false;
   }
  } else {
   JOptionPane.showMessageDialog(this, "當(dāng)前位置已經(jīng)有棋子,請(qǐng)重新落子!");
  }
  this.repaint();
  }
 }
 /* System.out.println(e.getX() + " -- " + e.getY()); */
 // 點(diǎn)擊 開始游戲 按鈕
 if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 70
  && e.getY() <= 100) {
  int result = JOptionPane.showConfirmDialog(this, "是否重新開始游戲?");
  if (result == 0) {
  // 現(xiàn)在重新開始游戲
  // 重新開始所要做的操作: 1)把棋盤清空,allChess這個(gè)數(shù)組中全部數(shù)據(jù)歸0.
  // 2) 將 游戲信息: 的顯示改回到開始位置
  // 3) 將下一步下棋的改為黑方
  for (int i = 0; i < 19; i++) {
   for (int j = 0; j < 19; j++) {
   allChess[i][j] = 0;
   }
  }
  // 另一種方式 allChess = new int[19][19];
  message = "黑方先行";
  isBlack = true;
  blackTime = maxTime;
  whiteTime = maxTime;
  if (maxTime > 0) {
   blackMessage = maxTime / 3600 + ":"
    + (maxTime / 60 - maxTime / 3600 * 60) + ":"
    + (maxTime - maxTime / 60 * 60);
   whiteMessage = maxTime / 3600 + ":"
    + (maxTime / 60 - maxTime / 3600 * 60) + ":"
    + (maxTime - maxTime / 60 * 60);
   t.resume();
  } else {
   blackMessage = "無限制";
   whiteMessage = "無限制";
  }
  this.canPlay = true; 
  this.repaint();

  }
 }
 // 點(diǎn)擊 游戲設(shè)置 按鈕
 if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 120
  && e.getY() <= 150) {
  String input = JOptionPane
   .showInputDialog("請(qǐng)輸入游戲的最大時(shí)間(單位:分鐘),如果輸入0,表示沒有時(shí)間限制:");
  try {
  maxTime = Integer.parseInt(input) * 60;
  if (maxTime < 0) {
   JOptionPane.showMessageDialog(this, "請(qǐng)輸入正確信息,不允許輸入負(fù)數(shù)!");
  }
  if (maxTime == 0) {
   int result = JOptionPane.showConfirmDialog(this,
    "設(shè)置完成,是否重新開始游戲?");
   if (result == 0) {
   for (int i = 0; i < 19; i++) {
    for (int j = 0; j < 19; j++) {
    allChess[i][j] = 0;
    }
   }
   // 另一種方式 allChess = new int[19][19];
   message = "黑方先行";
   isBlack = true;
   blackTime = maxTime;
   whiteTime = maxTime;
   blackMessage = "無限制";
   whiteMessage = "無限制";
   this.canPlay = true; 
   this.repaint();
   }
  }
  if (maxTime > 0) {
   int result = JOptionPane.showConfirmDialog(this,
    "設(shè)置完成,是否重新開始游戲?");
   if (result == 0) {
   for (int i = 0; i < 19; i++) {
    for (int j = 0; j < 19; j++) {
    allChess[i][j] = 0;
    }
   }
   // 另一種方式 allChess = new int[19][19];
   message = "黑方先行";
   isBlack = true;
   blackTime = maxTime;
   whiteTime = maxTime;
   blackMessage = maxTime / 3600 + ":"
    + (maxTime / 60 - maxTime / 3600 * 60) + ":"
    + (maxTime - maxTime / 60 * 60);
   whiteMessage = maxTime / 3600 + ":"
    + (maxTime / 60 - maxTime / 3600 * 60) + ":"
    + (maxTime - maxTime / 60 * 60);
   t.resume();
   this.canPlay = true; 
   this.repaint();
   }
  }
  } catch (NumberFormatException e1) {
  // TODO Auto-generated catch block
  JOptionPane.showMessageDialog(this, "請(qǐng)正確輸入信息!");
  }
 }
 // 點(diǎn)擊 游戲說明 按鈕
 if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 170
  && e.getY() <= 200) {
  JOptionPane.showMessageDialog(this,
   "這個(gè)一個(gè)五子棋游戲程序,黑白雙方輪流下棋,當(dāng)某一方連到五子時(shí),游戲結(jié)束。");
 }
 // 點(diǎn)擊 認(rèn)輸 按鈕
 if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 270
  && e.getY() <= 300) {
  int result = JOptionPane.showConfirmDialog(this, "是否確認(rèn)認(rèn)輸?");
  if (result == 0) {
  if (isBlack) {
   JOptionPane.showMessageDialog(this, "黑方已經(jīng)認(rèn)輸,游戲結(jié)束!");
  } else {
   JOptionPane.showMessageDialog(this, "白方已經(jīng)認(rèn)輸,游戲結(jié)束!");
  }
  canPlay = false;
  }
 }
 // 點(diǎn)擊 關(guān)于 按鈕
 if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 320
  && e.getY() <= 350) {
  JOptionPane.showMessageDialog(this,
   "本游戲由MLDN制作,有相關(guān)問題可以訪問www.mldn.cn");
 }
 // 點(diǎn)擊 退出 按鈕
 if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 370
  && e.getY() <= 400) {
  JOptionPane.showMessageDialog(this, "游戲結(jié)束");
  System.exit(0);
 }
 }

 public void mouseReleased(MouseEvent e) {
 // TODO Auto-generated method stub

 }

 private boolean checkWin() {
 boolean flag = false;
 // 保存共有相同顏色多少棋子相連
 int count = 1;
 // 判斷橫向是否有5個(gè)棋子相連,特點(diǎn) 縱坐標(biāo) 是相同, 即allChess[x][y]中y值是相同
 int color = allChess[x][y];
 /*
  * if (color == allChess[x+1][y]) { count++; if (color ==
  * allChess[x+2][y]) { count++; if (color == allChess[x+3][y]) {
  * count++; } } }
  */
 // 通過循環(huán)來做棋子相連的判斷
 /*
  * int i = 1; while (color == allChess[x + i][y + 0]) { count++; i++; }
  * i = 1; while (color == allChess[x - i][y - 0]) { count++; i++; } if
  * (count >= 5) { flag = true; } // 縱向的判斷 int i2 = 1 ; int count2 = 1 ;
  * while (color == allChess[x + 0][y + i2]) { count2++; i2++; } i2 = 1;
  * while (color == allChess[x - 0][y - i2]) { count2++; i2++; } if
  * (count2 >= 5) { flag = true ; } // 斜方向的判斷(右上 + 左下) int i3 = 1 ; int
  * count3 = 1 ; while (color == allChess[x + i3][y - i3]) { count3++;
  * i3++; } i3 = 1; while (color == allChess[x - i3][y + i3]) { count3++;
  * i3++; } if (count3 >= 5) { flag = true ; } // 斜方向的判斷(右下 + 左上) int i4 =
  * 1 ; int count4 = 1 ; while (color == allChess[x + i4][y + i4]) {
  * count4++; i4++; } i4 = 1; while (color == allChess[x - i4][y - i4]) {
  * count4++; i4++; } if (count4 >= 5) { flag = true ; }
  */

 // 判斷橫向
 count = this.checkCount(1, 0, color);
 if (count >= 5) {
  flag = true;
 } else {
  // 判斷縱向
  count = this.checkCount(0, 1, color);
  if (count >= 5) {
  flag = true;
  } else {
  // 判斷右上、左下
  count = this.checkCount(1, -1, color);
  if (count >= 5) {
   flag = true;
  } else {
   // 判斷右下、左上
   count = this.checkCount(1, 1, color);
   if (count >= 5) {
   flag = true;
   }
  }
  }
 }

 return flag;
 }

 // 判斷棋子連接的數(shù)量
 private int checkCount(int xChange, int yChange, int color) {
 int count = 1;
 int tempX = xChange;
 int tempY = yChange;
 while (x + xChange >= 0 && x + xChange <= 18 && y + yChange >= 0
  && y + yChange <= 18
  && color == allChess[x + xChange][y + yChange]) {
  count++;
  if (xChange != 0)
  xChange++;
  if (yChange != 0) {
  if (yChange > 0)
   yChange++;
  else {
   yChange--;
  }
  }
 }
 xChange = tempX;
 yChange = tempY;
 while (x - xChange >= 0 && x - xChange <= 18 && y - yChange >= 0
  && y - yChange <= 18
  && color == allChess[x - xChange][y - yChange]) {
  count++;
  if (xChange != 0)
  xChange++;
  if (yChange != 0) {
  if (yChange > 0)
   yChange++;
  else {
   yChange--;
  }
  }
 }
 return count;
 }

 public void run() {
 // TODO Auto-generated method stub
 // 判斷是否有時(shí)間限制
 if (maxTime > 0) {
  while (true) {
  if (isBlack) {
   blackTime--;
   if (blackTime == 0) {
   JOptionPane.showMessageDialog(this, "黑方超時(shí),游戲結(jié)束!");
   }
  } else {
   whiteTime--;
   if (whiteTime == 0) {
   JOptionPane.showMessageDialog(this, "白方超時(shí),游戲結(jié)束!");
   }
  }
  blackMessage = blackTime / 3600 + ":"
   + (blackTime / 60 - blackTime / 3600 * 60) + ":"
   + (blackTime - blackTime / 60 * 60);
  whiteMessage = whiteTime / 3600 + ":"
   + (whiteTime / 60 - whiteTime / 3600 * 60) + ":"
   + (whiteTime - whiteTime / 60 * 60);
  this.repaint();
  try {
   Thread.sleep(1000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  System.out.println(blackTime + " -- " + whiteTime);
  }
 }
 }

}

然后再創(chuàng)建子框架界面:

package org.liky.game.frame;

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;

public class MyChessFrame extends JFrame implements MouseListener {
 public MyChessFrame() {
 this.setTitle("五子");
 this.setSize(1000, 700);
 this.setResizable(false) ;
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 int width = Toolkit.getDefaultToolkit().getScreenSize().width;
 int height = Toolkit.getDefaultToolkit().getScreenSize().height;
 /*System.out.println("寬度為: "+width);
 System.out.println("高度為: "+height);*/
 this.setLocation((width - 200)/2, (height-100)/2);
 
 this.addMouseListener(this);
 
 this.setVisible(true);
 }
 
 public void paint(Graphics g) {
 /*g.drawString("五子棋游戲", 20, 40);*/
 /*g.drawOval(20, 40, 40, 40);*/
 /*g.fillOval(20, 40, 40, 40);*/
 /*g.drawLine(20, 40, 80, 40);*/
 /* g.drawRect(20, 40, 40, 20);
 g.fillRect(80, 40, 40, 20);*/
 BufferedImage image = null;
 try {
  image = ImageIO.read(new File("E:/image/MLDN文字01.jpg"));
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 g.drawImage(image,0,0,this);
 g.drawOval(20, 40, 40, 40);
 g.setColor(Color.BLUE);
 g.fillRect(80, 40, 40, 20);
 g.setFont(new Font("宋體",40,40)); 
 g.drawString("五子棋游戲", 20, 100);
 }

 public void mouseClicked(MouseEvent arg0) {
 // TODO Auto-generated method stub
 
 }

 public void mouseEntered(MouseEvent arg0) {
 // TODO Auto-generated method stub
 
 }

 public void mouseExited(MouseEvent arg0) {
 // TODO Auto-generated method stub
 
 }

 public void mousePressed(MouseEvent arg0) {
 // TODO Auto-generated method stub
 
 }

 public void mouseReleased(MouseEvent arg0) {
 // TODO Auto-generated method stub
 
 }
 

}


以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI