java實(shí)現(xiàn)五子棋小游戲
java實(shí)現(xiàn)五子棋小游戲
package Gomoku; import java.awt.Toolkit; import javax.swing.JFrame; public class GomokuFrame extends JFrame { //定義一個(gè)操作面板 OperatorPane op=null; public GomokuFrame() { //設(shè)置名稱 this.setTitle("五子棋"); //設(shè)置窗口大小 this.setSize(510,510); //設(shè)置窗口位置 //取得電腦屏幕大小 int computer_width=Toolkit.getDefaultToolkit().getScreenSize().width; int computer_height=Toolkit.getDefaultToolkit().getScreenSize().height; System.out.println("電腦屏幕的寬度:\n"+computer_width+"\n電腦屏幕的高度:\n"+computer_height); //居中 this.setLocation((computer_width-510)/2, (computer_height-510)/2); //實(shí)例化幕布 op=new OperatorPane(); //導(dǎo)入幕布 this.add(op); //添加鼠標(biāo)監(jiān)聽 this.addMouseListener(op); //設(shè)置窗口的顯示 this.setVisible(true); //設(shè)置窗口的正常關(guān)閉 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } //執(zhí)行測(cè)試 public static void main(String[] args) { new GomokuFrame(); } }
package Gomoku; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; import java.awt.image.BufferedImageFilter; import javax.swing.ImageIcon; import javax.swing.JOptionPane; import javax.swing.JPanel; public class OperatorPane extends JPanel implements MouseListener,Runnable { //定義背景圖片對(duì)象 Image imageBackground = null; //定義棋盤的格子的行數(shù) int boardrows=18; //定義棋盤的格子的列數(shù) int boardcols=18; //定義棋盤的格子的大小 int boardsize=20; //保存棋子坐標(biāo) int x=0,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)記下一步下黑棋還是白棋 boolean isBlack=true; //判斷游戲是否能夠繼續(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 = "無限制"; @SuppressWarnings("deprecation") public OperatorPane() { t.start(); t.suspend(); imageBackground=new ImageIcon("image/background.jpg").getImage(); } public void paint(Graphics g) { //雙緩沖技術(shù) BufferedImage b1=new BufferedImage(495,475,BufferedImage.TYPE_INT_ARGB); Graphics g2=b1.createGraphics(); // 畫出背景圖片 g2.drawImage(imageBackground, 0, 0,495,475,null); //畫出棋盤線 Color c=g2.getColor(); g2.setColor(Color.BLACK); for(int i=0;i<=boardrows;i++) { g2.drawLine(10,50+boardsize*i,10+boardsize*boardrows,50+boardsize*i); } for(int i=0;i<=boardcols;i++) { g2.drawLine(10+boardsize*i,50,10+boardsize*i,50+boardsize*boardcols); } //畫出三三位置 g2.fillOval(67, 107, 6, 6); g2.fillOval(67, 347, 6, 6); g2.fillOval(307, 107, 6, 6); g2.fillOval(307, 347, 6, 6); //畫出附點(diǎn) g2.fillOval(67, 227, 6, 6); g2.fillOval(307, 227, 6, 6); g2.fillOval(187, 107, 6, 6); g2.fillOval(187, 347, 6, 6); //畫出天元 g2.fillOval(187, 227, 6, 6); //畫出文字提示 /*Font f=new Font("黑體", Font.BOLD, 24); g.setFont(f);*/ g2.setFont(new Font("黑體", Font.BOLD, 20)); g2.setColor(Color.BLACK); g2.drawString("游戲信息:"+message, 130, 40); g2.setFont(new Font("宋體", Font.ITALIC, 15)); g2.drawString("黑方時(shí)間:"+blackMessage,25, 445); g2.drawString("白方時(shí)間:"+whiteMessage,245, 445); //繪制全部棋子 for(int i=0;i<=boardrows;i++) { for(int j=0;j<=boardcols;j++) { //存儲(chǔ)黑棋 if(allchess[i][j]==1) { int tempX=i*20-10; int tempY=j*20+30; //畫出黑棋 g2.setColor(Color.BLACK); g2.fillOval(tempX+12, tempY+13, 15, 15); } //存儲(chǔ)白棋 if(allchess[i][j]==2) { int tempX=i*20-10; int tempY=j*20+30; //畫出白棋 g2.setColor(Color.BLACK); g2.drawOval(tempX+12, tempY+13, 15, 15); g2.setColor(Color.WHITE); g2.fillOval(tempX+12, tempY+13, 15, 15); } } } g2.setColor(c); g.drawImage(b1,0,0,this); } private boolean checkWin() { boolean flag=false; int color = allchess[x][y]; /*// 保存共有相同顏色多少棋子相連 int count1=1; int count2=1; int count3=1; int count4=1; // 判斷橫向是否有5個(gè)棋子相連,特點(diǎn) 縱坐標(biāo) 是相同, 即allChess[x][y]中y值是相同 // 通過循環(huán)來做棋子相連的判斷 int i = 1; while (color == allchess[x+i][y]) { count1++; i++; } //將i值復(fù)位 i = 1; while (color == allchess[x-i][y]) { count1++; i++; } if(count1 >= 5) { flag = true; } //判斷縱向 ,即allChess[x][y]中x值是相同 int j = 1; while (color == allchess[x][y+j]) { count2++; j++; } //將j值復(fù)位 j = 1; while (color == allchess[x][y-j]) { count2++; j++; } if(count2>= 5) { flag = true; } //判斷斜向"\" int m1=1; int n1=1; while (color == allchess[x+m1][y+n1]) { count3++; m1++; n1++; } m1=1; n1=1; while (color == allchess[x-m1][y-n1]) { count3++; m1++; n1++; } if(count3>= 5) { flag = true; } //判斷斜向"/" int m2=1; int n2=1; while (color == allchess[x+m2][y-n2]) { count4++; m2++; n2++; } m2=1; n2=1; while (color == allchess[x-m2][y+n2]) { count4++; m2++; n2++; } if(count4>= 5) { flag = true; } */ int count; //橫向判斷 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; } private int checkCount(int xChange,int yChange,int color) { int count=1; int tempX=xChange; int tempY=yChange; while (color==allchess[x+xChange][y+yChange]) { count++; if(xChange!=0) { xChange++; } if(yChange!=0) { if(yChange<0) { yChange--; } else { yChange++; } } } //復(fù)位 xChange=tempX; yChange=tempY; while (color==allchess[x-xChange][y-yChange]) { count++; if(xChange!=0) { xChange++; } if(yChange!=0) { if(yChange<0) { yChange--; } else { yChange++; } } } return count; } public void mouseClicked(MouseEvent e) { System.out.println("x:"+e.getX()+"y:"+e.getY()); x=e.getX(); y=e.getY(); if(x>=10&&x<=(10+boardsize*boardrows+20)&&y>=50&&y<=(50+boardsize*boardcols+40)) { //System.out.println("點(diǎn)在棋盤內(nèi)。"); x=(x-10)/20; y=(y-50-20)/20; if(canPlay==true) { //判斷當(dāng)前要下什么顏色 if(allchess[x][y]==0) { 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(); } //點(diǎn)擊,游戲開始按鈕 //重新開始新的游戲 if(e.getX()>=400&&e.getX()<=470&&e.getY()>=80&&e.getY()<=110) { int result=JOptionPane.showConfirmDialog(this, "設(shè)置完成,是否重新開始游戲?"); if(result==0) { //重新開始的操作,allchess[][]數(shù)組中的信息全部為0 //清空棋盤 for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; j++) { allchess[i][j] = 0; } } //另一種方式 allChess = new int[19][19]; 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 = "無限制"; } message = "黑方先行"; isBlack = true; this.canPlay = true; this.repaint(); } } //點(diǎn)擊 游戲設(shè)置按鈕 if(e.getX()>=400&&e.getX()<=470&&e.getY()>=130&&e.getY()<=160) { 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()>=180&&e.getY()<=210) { JOptionPane.showMessageDialog(this,"這個(gè)一個(gè)五子棋游戲程序,黑白雙方輪流下棋,當(dāng)某一方連到五子時(shí),游戲結(jié)束。"); } //點(diǎn)擊 認(rèn)輸按鈕 if(e.getX()>=400&&e.getX()<=470&&e.getY()>=280&&e.getY()<=310) { int result=JOptionPane.showConfirmDialog(this,"是否確定認(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()>=330&&e.getY()<=360) { JOptionPane.showMessageDialog(this,"本游戲由南木工作室制作,有相關(guān)問題可以訪問www.yiyiinformation.com"); } //點(diǎn)擊 退出按鈕 if(e.getX()>=400&&e.getX()<=470&&e.getY()>=380&&e.getY()<=410) { JOptionPane.showMessageDialog(this, "游戲結(jié)束"); System.exit(0); } } //************************// @Override public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void run() { 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); } } } }
演示圖:
以上所述就是本文的全部?jī)?nèi)容了,希望能夠?qū)Υ蠹沂炀氄莆誮ava有所幫助。
相關(guān)文章
深入解析Java的Servlet過濾器的原理及其應(yīng)用
這篇文章主要介紹了深入解析Java的Servlet過濾器的原理及應(yīng)用,Java編寫的Servlet通常是一個(gè)與網(wǎng)頁一起作用于瀏覽器客戶端的程序,需要的朋友可以參考下2016-01-01JAVA使用TreeMap對(duì)字符串進(jìn)行排序
這篇文章主要介紹了JAVA使用TreeMap對(duì)字符串進(jìn)行排序,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02mybaits-plus?lambdaQuery()?和?lambdaUpdate()?常見的使用方法
MyBatis-Plus是一個(gè)?MyBatis?(opens?new?window)的增強(qiáng)工具,在?MyBatis?的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開發(fā)、提高效率而生,這篇文章主要介紹了mybaits-plus?lambdaQuery()?和?lambdaUpdate()?比較常見的使用方法,需要的朋友可以參考下2023-01-01Java 實(shí)現(xiàn)線程池任務(wù)編排的示例代碼
任務(wù)編排是將多個(gè)任務(wù)按照特定的依賴關(guān)系和執(zhí)行順序進(jìn)行組織和管理的過程,以確保任務(wù)能按預(yù)定邏輯順序高效執(zhí)行,本文就來介紹一下Java 實(shí)現(xiàn)線程池任務(wù)編排的示例代碼,感興趣的可以了解一下2024-10-10idea使用Maven Helper插件去掉無用的poom 依賴信息(詳細(xì)步驟)
這篇文章主要介紹了idea使用Maven Helper插件去掉無用的poom 依賴信息,本文分步驟給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04Java農(nóng)夫過河問題的繼承與多態(tài)實(shí)現(xiàn)詳解
這篇文章主要介紹了Java農(nóng)夫過河問題的繼承與多態(tài)實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01詳解 JAVA的回調(diào)機(jī)制CallBack
最近學(xué)習(xí)java,接觸到了回調(diào)機(jī)制(CallBack)。初識(shí)時(shí)感覺比較混亂,而且在網(wǎng)上搜索到的相關(guān)的講解,要么一言帶過,要么說的比較單純的像是給CallBack做了一個(gè)定義,本文給大家介紹JAVA的回調(diào)機(jī)制CallBack,感興趣的朋友一起學(xué)習(xí)2016-04-04使用Spring注解@EventListener實(shí)現(xiàn)監(jiān)聽原理
這篇文章主要介紹了使用Spring注解@EventListener實(shí)現(xiàn)監(jiān)聽原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08