Java實(shí)現(xiàn)五子棋單機(jī)版
本文實(shí)例為大家分享了Java實(shí)現(xiàn)五子棋單機(jī)版的具體代碼,供大家參考,具體內(nèi)容如下
Java五子棋設(shè)計(jì)流程:
1.創(chuàng)建窗口和設(shè)計(jì)一個(gè)棋盤(pán)界面
2.實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊,棋子出現(xiàn),黑白棋輪流下
3.能夠判斷輸贏
4.添加按鈕功能
實(shí)現(xiàn)結(jié)果圖:
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; 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.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; ? public class Test { ? ?? ?public static void main(String[] args) { ?? ??? ?new MyFrame(); ?? ??? ? ?? ?} } ? class MyFrame extends JFrame implements MouseListener{ ?? ? ?? ?//保存坐標(biāo) ?? ?int x; ?? ?int y; ?? ?int x1; ?? ?int y1; ?? ?//黑子數(shù) ?? ?//白子數(shù) ?? ?//1是黑下,2是白下 ?? ?//默認(rèn)開(kāi)始是黑旗先下 ?? ?int flag=1; ?? ?//表示游戲是否結(jié)束 ?? ?//true游戲開(kāi)始,false游戲結(jié)束,不能再下 ?? ?boolean canPlay=true; ?? ?//保存之前下過(guò)的棋子的坐標(biāo) ?? ?//'0'代表沒(méi)有棋子,'1'代表黑棋,'2'代表白棋 ?? ?int [][]allChess=new int[19][19]; ?? ?//int [][]allChess=new int[25][25]; ?? ?//當(dāng)前棋子的總數(shù) ?? ?int chessSum=0; ?? ?BufferedImage bgImage =null; ?? ? ?? ?JButton withdraw=new JButton("悔棋"); ?? ?JButton restart=new JButton("重新開(kāi)始"); ?? ?JButton exit=new JButton("退出"); ?? ?JPanel south=new JPanel(); ?? ? ?? ?public MyFrame() { ?? ??? ?this.setTitle("五子棋"); ?? ??? ?setSize(630,700); ?? ??? ?setLayout(new BorderLayout());? ?? ??? ?setLocationRelativeTo(null);? ?? ??? ?setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ?? ??? ?try { ?? ??? ??? ?bgImage=ImageIO.read(new File("C:\\Users\\us\\Desktop\\1.jpg")); ?? ??? ?} catch (IOException e1) { ?? ??? ??? ? ?? ??? ??? ?e1.printStackTrace(); ?? ??? ?} ?? ??? ?addMouseListener(this);//將窗體加入監(jiān)聽(tīng) ?? ??? ? ?? ??? ?south.setLayout(new FlowLayout(FlowLayout.LEFT,60,30)); ?? ??? ? ?? ??? ?south.add(restart); ?? ??? ?south.add(withdraw); ?? ??? ?south.add(exit); ?? ??? ?//初始化按鈕事件監(jiān)聽(tīng)器內(nèi)部類(lèi)? ?? ??? ?MybuttonListener buttonListener =new MybuttonListener(); ?? ??? ?//將三個(gè)按鈕事件注冊(cè)監(jiān)聽(tīng)事件 ?? ??? ?restart.addActionListener(buttonListener); ?? ??? ?withdraw.addActionListener(buttonListener); ?? ??? ?exit.addActionListener(buttonListener); ?? ??? ?//將按鈕面板加到窗體的南部 ?? ??? ?this.add(south,BorderLayout.SOUTH); ?? ??? ? ?? ??? ?setVisible(true); ?? ?} ?? ? ?? ?public void paint(Graphics g) { ?? ??? ? ?? ??? ?int tempSum=chessSum; ?? ??? ?//棋盤(pán) ?? ??? ?g.drawImage(bgImage,8,30,this); ?? ??? ? ?? ??? ?for(int colum=58;colum<600 ;colum=colum+30){//行 ?? ??? ? g.drawLine(38,colum,578,colum); ?? ??? ?} ?? ??? ?for(int rand=38;rand<600;rand=rand+30){//列 ?? ??? ? g.drawLine(rand, 58,rand, 598); ?? ??? ?} ?? ??? ?//黑點(diǎn) ?? ??? ?g.fillOval(122, 143, 10, 10);? ?? ??? ?g.fillOval(484, 143, 10, 10); ?? ??? ?g.fillOval(122, 504, 10, 10); ?? ??? ?g.fillOval(303, 353, 10, 10); ?? ??? ?g.fillOval(484, 503, 10, 10); ?? ??? ?g.fillOval(122, 355, 10, 10); ?? ??? ?g.fillOval(484, 355, 10, 10); ?? ??? ?g.fillOval(303, 145, 10, 10); ?? ??? ?g.fillOval(303, 503, 10, 10); ?? ??? ? ?? ??? ?for(int i=0;i<allChess.length;i++) { ?? ??? ??? ?for(int j=0;j<allChess.length;++j) { ?? ??? ??? ? //下黑子 ?? ??? ??? ??? ?if(allChess[i][j]==1) { ?? ??? ??? ??? ??? ?int tempX=i*30+38;//左邊界到棋盤(pán)的距離 ?? ??? ??? ??? ??? ?int tempY=j*30+58;//上邊界到棋盤(pán)的距離 ?? ??? ??? ??? ??? ? g.setColor(Color.black); ?? ??? ??? ??? ??? ? g.fillOval(tempX-13,tempY-13,25,25); ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ?} ?? ??? ??? ??? ?? ?? ??? ??? ??? ?? ?? ??? ??? ??? ?//下白子 ?? ??? ??? ??? ?if(allChess[i][j]==2) { ?? ??? ??? ??? ??? ?int tempX=i*30+38; ?? ??? ??? ??? ??? ?int tempY=j*30+58; ?? ??? ??? ??? ??? ?g.setColor(Color.white); ?? ??? ??? ??? ??? ?g.fillOval(tempX-13,tempY-13,25,25); ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ??? ? ?? ??? ??? ??? ?? ?? ??? ??? ?} ?? ??? ?} ?? ??? ?//最后棋子用紅框表示 ?? ??? ?if(chessSum>0) { ?? ??? ? g.setColor(Color.red); ?? ??? ? g.drawRect(x*30+38-13, y*30+58-13, 25,25); ?? ??? ?} ?? ??? ?//g.setColor(Color.red); ?? ??? ?//g.drawRect(x1*30+38-13, y1*30+58-13, 25,25); ?? ??? ?chessSum++; ?? ??? ?System.out.println("總數(shù)為"+(chessSum-1)); ?? ?} ? ?? ? ?? ?public void mouseClicked(MouseEvent e) { ?? ??? ??? ? x=e.getX(); ?? ??? ? ? ? y=e.getY(); ?? ??? ? //System.out.println("x="+e.getX()+" "+"y="+e.getY()); ? ? ? ?if(canPlay) { ? ? ?? ? ?? ?? ??? ? if(x>=38&&x<=588&&y>=58&&y<=620) { ?? ??? ??? ?? ?? ??? ??? ?x=(x-38)/30;//38起點(diǎn),適應(yīng)19x19 ?? ??? ??? ?y=(y-58)/30; ?? ??? ??? ?if(allChess[x][y]==0){//此點(diǎn)沒(méi)有棋子,才可下 ?? ??? ??? ?//判斷該由哪方下棋 ?? ??? ??? ?if(flag==1) {//'1'代表由黑方下 ?? ??? ??? ??? ?allChess[x][y]=1;//'1'表示此處放黑棋 ?? ??? ??? ??? ?this.checkFive();//判斷黑棋是否五子相連 ?? ??? ??? ??? ? ?? ??? ??? ??? ?flag=2; ?? ??? ??? ?} ?? ??? ??? ?else { ?? ??? ??? ??? ?allChess[x][y]=2;//'2'表示此處放白棋 ?? ??? ??? ??? ?this.checkFive();//判斷白棋是否五子相連 ?? ??? ??? ??? ? ?? ??? ??? ??? ?flag=1;//'1'代表由黑方下 ?? ??? ??? ?} ?? ??? ? ?this.repaint(); ?? ??? ?} ?? ? ? } ? ? ? } ? ? ?? ? ? ? ? ? ?} ?? ? ?? ? ?? ?//判斷五子相連 ? ? public ?void checkFive(){ ? ? ?? ?//把要下的棋子顏色保存 ? ? ?? ?int color=allChess[x][y]; ? ? ?? ?//計(jì)算已連棋子個(gè)數(shù) ? ? ?? ?int count=1; ? ? ?? ? ? ? ?? ?//判斷橫向右邊是否五子 ? ? ?? ?for(int i=1;i<5;i++) { ? ? ?? ??? ?if(x>=15) ? ? ?? ??? ??? ?break; ? ? ?? ??? ?if(color==allChess[x+i][y]) { ? ? ?? ??? ??? ?count++; ? ? ?? ??? ?} ? ? ?? ??? ?checkWin(count); ? ? ?? ?} ? ? ?? ?count=1; ? ? ?? ? ? ? ?? ?//判斷橫向左邊是否五子 ? ? ?? ?for(int i=1;i<5;i++) { ? ? ?? ??? ?if(x<=3)//當(dāng)棋子左邊無(wú)法連成五子,直接退出 ? ? ?? ??? ??? ?break; ? ? ?? ??? ? ? ? ?? ??? ?if(color==allChess[x-i][y]) { ? ? ?? ??? ??? ?count++; ? ? ?? ??? ?} ? ? ?? ??? ?checkWin(count); ? ? ?? ?} ? ? ?? ?count=1; ? ? ?? ? ? ? ?? ?//判斷豎向下邊是否五子 ? ? ?? ?for(int i=1;i<5;i++) { ? ? ?? ??? ?if(y>=15)//當(dāng)棋子左邊無(wú)法連成五子,直接退出 ? ? ?? ??? ??? ?break; ? ? ?? ??? ?if(color==allChess[x][y+i]) { ? ? ?? ??? ??? ?count++; ? ? ?? ??? ?} ? ? ?? ??? ?checkWin(count); ? ? ?? ?} ? ? ?? ?count=1; ? ? ?? ? ? ? ?? ?//判斷豎向上邊是否五子 ? ? ?? ?for(int i=1;i<5;i++) { ? ? ?? ??? ?if(y<=3)//當(dāng)棋子豎向上邊無(wú)法連成五子,直接退出 ? ? ?? ??? ??? ?break; ? ? ?? ??? ?if(color==allChess[x][y-i]) { ? ? ?? ??? ??? ?count++; ? ? ?? ??? ?} ? ? ?? ??? ?checkWin(count); ? ? ?? ?} ? ? ?? ?count=1; ? ? ?? ? ? ? ?? ?//判斷右斜上邊是否五子 ? ? ?? ?for(int i=1;i<5;i++) { ? ? ?? ??? ?if(y<=3||x>=15)//當(dāng)棋子右斜上邊無(wú)法連成五子,直接退出 ? ? ?? ??? ??? ?break; ? ? ?? ??? ?if(color==allChess[x+i][y-i]) { ? ? ?? ??? ??? ?count++; ? ? ?? ??? ?} ? ? ?? ??? ?checkWin(count); ? ? ?? ?} ? ? ?? ?count=1; ? ? ?? ? ? ? ?? ?//判斷左斜向下邊是否五子 ? ? ?? ?for(int i=1;i<5;i++) { ? ? ?? ??? ?if(x<=3||y>=15)//當(dāng)棋子左斜向下邊無(wú)法連成五子,直接退出 ? ? ?? ??? ??? ?break; ? ? ?? ??? ? ? ? ?? ??? ?if(color==allChess[x-i][y+i]) { ? ? ?? ??? ??? ?count++; ? ? ?? ??? ?} ? ? ?? ??? ?checkWin(count); ? ? ?? ?} ? ? ?? ?count=1; ? ? ?? ? ? ? ?? ?//判斷左斜向上邊是否五子 ? ? ?? ?for(int i=1;i<5;i++) { ? ? ?? ??? ?if(x<=3||y<=3) ? ? ?? ??? ??? ?break; ? ? ?? ??? ?if(color==allChess[x-i][y-i]) { ? ? ?? ??? ??? ?count++; ? ? ?? ??? ?} ? ? ?? ??? ?checkWin(count); ? ? ?? ?} ? ? ?? ?count=1; ? ? ?? ? ? ? ?? ?//判斷右斜向下邊是否五子 ? ? ?? ?for(int i=1;i<5;i++) { ? ? ?? ??? ?if(y>=15||x>=15) ? ? ?? ??? ??? ?break; ? ? ?? ??? ?if(color==allChess[x+i][y+i]) { ? ? ?? ??? ??? ?count++; ? ? ?? ??? ?} ? ? ?? ??? ?checkWin(count); ? ??? ??? ??? ? ? ? ?? ??? ? ? ? ?? ?} ? ? ?? ?count=1; ? ? ?? ? ? ? } ?? ?public void mouseEntered(MouseEvent e) { ?? ??? ? x1=e.getX(); ?? ??? ? y1=e.getY(); ?? ? ? ? if(x1>=38&&x1<=588&&y1>=58&&y1<=620) { ?? ? ? ??? ?? ?? ? ? ??? ? setCursor(new Cursor(Cursor.HAND_CURSOR)); ?? ? ? ??? ? ?? ? ? ? } ?? ? ? ? ? ? } ?? ??? ? ?? ?public void mouseExited(MouseEvent arg0) { ?? ??? ?// TODO Auto-generated method stub ?? ??? ? ?? ?} ? ?? ?public void mousePressed(MouseEvent arg0) { ?? ??? ? ?? ??? ? ?? ?} ? ?? ?public void mouseReleased(MouseEvent e) { ?? ??? ? ?} ??? ? ?? ?public void checkWin(int count) { ?? ? ?? ?? ? ? if(count>=5) {//五子相連 ?? ??? ? ? ?? ??? ? if(allChess[x][y]==1) { ?? ??? ??? ?JOptionPane.showMessageDialog(this, "黑方勝出!!!!!!"); ?? ??? ? } ?? ??? ? if(allChess[x][y]==2) { ?? ??? ??? ?JOptionPane.showMessageDialog(this, "白方勝出!!!!!!"); ?? ??? ? } ?? ??? ? canPlay=false;//游戲結(jié)束 ?? ? ? } ?? ?} ?? ? ?? ?//重新開(kāi)始 ?? ?public void restartGame(){ ?? ??? ?for(int i=0;i<allChess.length;i++) { ?? ??? ??? ?for(int j=0;j<allChess.length;j++) { ?? ??? ??? ??? ?allChess[i][j]=0; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?flag=1;//默認(rèn)開(kāi)始是黑旗先下 ?? ??? ?canPlay=true; ?? ??? ?repaint(); ?? ?} ?? ? ?? ?//悔棋 ?? ?public void goback() { ?? ? ?if(allChess[x][y]!=0) {//當(dāng)棋盤(pán)有棋子,才能悔棋 ?? ??? ?allChess[x][y]=0; ?? ??? ?if(flag==1) { ?? ??? ??? ?flag=2; ?? ??? ??? ?repaint(); ?? ??? ?} ?? ??? ?else { ?? ??? ??? ?flag=1; ?? ??? ??? ?repaint(); ?? ??? ?} ?? ? ?} ?? ?} ?? ? ?? ?//按鈕事件監(jiān)聽(tīng)器內(nèi)部類(lèi)? ?? ?class MybuttonListener ?implements ActionListener{ ?? ??? ?public void actionPerformed(ActionEvent e) { ?? ??? ??? ?if(e.getSource()==restart) { ?? ??? ??? ??? ?restartGame(); ?? ??? ??? ?} ?? ??? ??? ?if(e.getSource()==withdraw) { ?? ??? ??? ??? ?goback(); ?? ??? ??? ?} ?? ??? ??? ?if(e.getSource()==exit) { ?? ??? ??? ??? ?System.exit(0); ? ?? ??? ??? ?} ?? ??? ?} ?? ?} }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)簡(jiǎn)易五子棋小游戲
- Java實(shí)現(xiàn)簡(jiǎn)單無(wú)界面五子棋
- java實(shí)現(xiàn)簡(jiǎn)單的五子棋游戲
- Java實(shí)現(xiàn)五子棋游戲(2.0)
- java實(shí)現(xiàn)五子棋小游戲
- java基于swing實(shí)現(xiàn)的五子棋游戲代碼
- Java實(shí)現(xiàn)五子棋網(wǎng)絡(luò)版
- Java實(shí)現(xiàn)兩人五子棋游戲(二) 畫(huà)出棋盤(pán)
- java實(shí)現(xiàn)單人版五子棋游戲
- java實(shí)現(xiàn)聯(lián)機(jī)五子棋
相關(guān)文章
SpringBoot環(huán)境搭建及第一個(gè)程序運(yùn)行(小白教程)
這篇文章主要介紹了SpringBoot環(huán)境搭建及第一個(gè)程序運(yùn)行,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Java 使用maven實(shí)現(xiàn)Jsoup簡(jiǎn)單爬蟲(chóng)案例詳解
這篇文章主要介紹了Java 使用maven實(shí)現(xiàn)Jsoup簡(jiǎn)單爬蟲(chóng)案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09Java中的DelayQueue實(shí)現(xiàn)原理及應(yīng)用場(chǎng)景詳解
這篇文章主要介紹了Java中的DelayQueue實(shí)現(xiàn)原理及應(yīng)用場(chǎng)景詳解,DelayQueue是一個(gè)沒(méi)有邊界BlockingQueue實(shí)現(xiàn),加入其中的元素必需實(shí)現(xiàn)Delayed接口,當(dāng)生產(chǎn)者線程調(diào)用put之類(lèi)的方法加入元素時(shí),會(huì)觸發(fā)Delayed接口中的compareTo方法進(jìn)行排序,需要的朋友可以參考下2023-12-12Spring應(yīng)用中使用acutator/refresh刷新屬性不生效的問(wèn)題分析及解決
在Spring應(yīng)用收到/actuator/refresh的POST請(qǐng)求后,標(biāo)注了@RefreshScope以及@ConfiguratioinProperties的bean會(huì)被Spring容器重新加載,但是,在實(shí)際應(yīng)用中,并沒(méi)有按照預(yù)期被Spring容器加載,本文將討論導(dǎo)致這種未按預(yù)期刷新的一種原因,感興趣的朋友可以參考下2024-01-01Java解決xss轉(zhuǎn)義導(dǎo)致轉(zhuǎn)碼的問(wèn)題
跨站腳本攻擊XSS是最普遍的Web應(yīng)用安全漏洞,本文主要介紹了Java解決xss轉(zhuǎn)義導(dǎo)致轉(zhuǎn)碼的問(wèn)題,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08Java多線程使用阻塞隊(duì)列實(shí)現(xiàn)生產(chǎn)者消費(fèi)者模型詳解
這篇文章主要介紹了Java多線程使用阻塞隊(duì)列實(shí)現(xiàn)生產(chǎn)者消費(fèi)者模型詳解,主要講解阻塞隊(duì)列的特性、實(shí)際開(kāi)發(fā)中常用的到的生產(chǎn)者消費(fèi)者模型,以及生產(chǎn)者消費(fèi)者模型解耦合、削峰填谷的好處,需要的朋友可以參考下2023-07-07Java 堆內(nèi)存與棧內(nèi)存詳細(xì)介紹
這篇文章主要介紹了Java 堆內(nèi)存與棧內(nèi)存詳細(xì)介紹的相關(guān)資料,這里對(duì)java 的堆內(nèi)存和棧內(nèi)存進(jìn)行了詳細(xì)的分析,需要的朋友可以參考下2016-11-11使用Spring?Boot?2.x構(gòu)建Web服務(wù)的詳細(xì)代碼
這篇文章主要介紹了使用Spring?Boot?2.x構(gòu)建Web服務(wù)的詳細(xì)代碼,主要基于JWT的身份認(rèn)證,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03