日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

简单的五子棋操作用两种方法实现

發布時間:2025/3/20 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 简单的五子棋操作用两种方法实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近有五子棋的大作業,周折半天才弄明白其中的原理,查閱了許多資料,然后網上的代碼只有幾篇原創并且注釋很少,感覺不好理解。所以感覺有必要分享一下自己的心得
本人使用兩種方法:
1:(傳統方法)鼠標點擊事件。大致流程為 定義窗口——從寫JPanel中的paint函數(畫圖由paint實現)——畫棋盤——設置數組儲存坐標以及是否有棋子——添加鼠標點擊事件畫棋子(判斷鼠標點擊的位置離此點最近的那個店的坐標并畫棋子)——判斷是否有勝利
1)這里說一下paint函數,paint函數定義在那個界面里他會自動執行畫圖不需要調用,所以你只需要寫好約束的事件讓他畫完棋盤后該什么時候在哪里畫棋子。
2)repaint 函數是起到重畫作用,你點擊過后再最近的那個店需要畫棋子,repaint就起到從畫的作用
3)本人判斷成行的方法是分別定義四個變量代碼五子棋個數,當》=5時停止。彈出新的窗口。具體是如果此點左右都有相同顏色的棋子,就吧這個點向左找到一直顏色不同為止,然后從這點向下(右)直接計數。
4)本人的棋子是畫出來的,如果追求美觀可以用image方法使用下載下來的圖片棋子(但是思想一致)。
附上代碼和注釋:

import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.*; public class wuziqi extends JFrame {void judgle(int a[][],int i,int j){//需要定義兩個變量(作用相同)一個判斷黑棋子,一個判斷白棋int k=1; int kk=1;//判斷橫方向int l=1;int kl=1;//判斷豎方向int k1=1;int kk1=1;//判斷左上和右下方向int k2=1; int kk2=1;//判斷左下和右上方向//橫方向的判斷int m=i;int n=j;int m1=i;int n1=j;int m2=i;int n2=j;while(m-1>=0&&a[m-1][j]==1) {m--;}//上下判斷while(m<18&&a[m 1][j]==1) {k ;m ;} //左右判斷//while(n-1>=0&&a[i][n-1]==1) {n--;}while(n<18&&a[i][n 1]==1) {l ;n ;} //左上右下while(m1-1>=0&&n1-1>=0&&a[m1-1][n1-1]==1) {m1--;n1--;}while(m1<18&&n1<18&&a[m1 1][n1 1]==1) {k1 ;m1 ;n1 ;} //左下右上方向 while(m2-1>=0&&n2 1<19&&a[m2-1][n2 1]==1) {m2--;n2 ;}while(m2<18&&n2-1>=0&&a[m2 1][n2-1]==1) {k2 ;m2 ;n2--;} m=i; n=j; m1=i; n1=j;m2=i; n2=j;while(m-1>=0&&a[m-1][j]==2) {m--;}//上下判斷while(m<18&&a[m 1][j]==2) {kk ;m ;} //左右判斷//while(n-1>=0&&a[i][n-1]==2) {n--;}while(n<18&&a[i][n 1]==2) {kl ;n ;} //左上右下while(m1-1>=0&&n1-1>=0&&a[m1-1][n1-1]==2) {m1--;n1--;}while(m1<18&&n1<18&&a[m1 1][n1 1]==2) {kk1 ;m1 ;n1 ;} //左下右上方向 while(m2-1>=0&&n2 1<19&&a[m2-1][n2 1]==2) {m2--;n2 ;}while(m2<18&&n2-1>=0&&a[m2 1][n2-1]==2) {kk2 ;m2 ;n2--;}if(k>=5||l>=5||k1>=5||k2>=5||kk>=5||kl>=5||kk1>=5||kk2>=5) { setTitle("游戲已結束");JFrame frame=new JFrame ("恭喜獲勝");Container c=frame.getContentPane();c.setLayout(new GridLayout(4,1,0,0) );if(a[i][j]==1) {JLabel label=new JLabel("恭喜黑色贏了",JLabel.CENTER);label.setFont(new Font("宋體",0,35));c.add(label);}if(a[i][j]==2) {JLabel label=new JLabel("恭喜白色贏了",JLabel.CENTER);label.setFont(new Font("宋體",0,35));c.add(label);}JButton b1=new JButton("再來一把");JButton b2=new JButton("結 束");b1.setFocusPainted(false); b2.setFocusPainted(false); c.add(new JLabel("")); c.add(b1);b1.setBackground(Color.YELLOW);c.add(b2);c.setBackground(Color.red);frame.setSize(300, 300);frame.setLocationRelativeTo(null);frame.setVisible(true);//順序問題frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); b1.addActionListener(new ActionListener() {//建立監聽事件public void actionPerformed(ActionEvent e) { if(b1==e.getSource()) for(int i=0;i<19;i )for(int j=0;j<19;j ){a[i][j]=0;}frame.dispose();repaint(); } });//關閉當前窗口b2.addActionListener(new ActionListener() {//建立監聽事件public void actionPerformed(ActionEvent e) {if(b2==e.getSource()) System.exit(0);//關閉所有} }); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } } public wuziqi() {setTitle("這是一個五子棋游戲");setVisible(true);Container c = getContentPane();DrawPanel1 jp1 = new DrawPanel1();c.add(jp1);setSize(1000, 1010);jp1.setBackground(new Color(60,150,200));//隨便賦值一個背景顏色this.setResizable(false);this.setLocationRelativeTo(null);//劇中放置,要在setsize后面放置setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);c.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {{int x=0,y=0;for (int i = 0; i < 19; i ) {if (jp1.b4[i][2] >e.getX()){if(i>0) { if(e.getX()-jp1.b4[i-1][2] e.getY())//找到點擊的點右側的最近那個坐標值{if(i>0) { if(e.getY()-jp1.b3[2][i-1]=0&&i<=18) {//橫方向的判斷int m=i;int n=j;int m1=i;int n1=j;int m2=i;int n2=j;while(m-1>=0&&a[m-1][j]==true) {m--;}//上下判斷while(m<18&&a[m 1][j]==true) {k ;m ;} //左右判斷//while(n-1>=0&&a[i][n-1]==true) {n--;}while(n<18&&a[i][n 1]==true) {l ;n ;} //左上右下while(m1-1>=0&&n1-1>=0&&a[m1-1][n1-1]==true) {m1--;n1--;}while(m1<18&&n1<18&&a[m1 1][n1 1]==true) {k1 ;m1 ;n1 ;} //左下右上方向 while(m2-1>=0&&n2 1<19&&a[m2-1][n2 1]==true) {m2--;n2 ;}while(m2<18&&n2-1>=0&&a[m2 1][n2-1]==true) {k2 ;m2 ;n2--;} if(k>=5||l>=5||k1>=5||k2>5) { setTitle("游戲已結束");JFrame frame=new JFrame ("恭喜獲勝");Container c=frame.getContentPane();c.setLayout(new GridLayout(4,1,0,0) );JLabel label=new JLabel("恭喜你贏了",JLabel.CENTER);label.setFont(new Font("宋體",0,35));JButton b1=new JButton("再來一把");JButton b2=new JButton("結 束");b1.setFocusPainted(false); b2.setFocusPainted(false); c.add(new JLabel(""));c.add(label);c.add(b1);b1.setBackground(Color.YELLOW);c.add(b2);c.setBackground(Color.red);//c.add(b1);//c.add(b2);frame.setSize(300, 300);frame.setLocationRelativeTo(null);frame.setVisible(true);//順序問題frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); b1.addActionListener(new ActionListener() {//建立監聽事件public void actionPerformed(ActionEvent e) { if(b1==e.getSource()) for(int i=0;i<19;i )for(int j=0;j<19;j ){judgle[i][j]=false; black[i][j]=false; white[i][j]=false;b[i][j].setIcon(null); frame.dispose();repaint(); }} });b2.addActionListener(new ActionListener() {//建立監聽事件public void actionPerformed(ActionEvent e) {if(b2==e.getSource())System.exit(0);} }); //System.exit(0);//結束}}public wuziqi2(){setTitle("這是一個五子棋游戲");//Container c=getContentPane();//setLayout(new FlowLayout(2,10,10));//流布局管理器DrawPanel1 d1=new DrawPanel1();d1.setSize(950,950);d1.setLayout(new GridLayout(19,19,0,0));//網格布局19行19列d1.setBackground(new Color(0,200,200));setResizable(false);add(d1);for(int i=0;i<19;i ) {for(int j=0;j<19;j ) {b[i][j]=new JButton("");d1.add(b[i][j]); b[i][j].setContentAreaFilled(false);//透明b[i][j].setBorderPainted(false);//取消邊框b[i][j].setFocusPainted(false); //取消焦點}}for( int i=0;i<19;i ){ for(int j=0;j<19;j )b[i][j].addActionListener(new ActionListener() {//建立監聽事件public void actionPerformed(ActionEvent e){ for( int i=0;i<19;i ){for(int j=0;j<19;j )if(!judgle[i][j])if(e.getSource()==b[i][j]){ if(n%2==0) { drawlconw icon = new drawlconw(40, 40);white[i][j]=true;//有bai棋子b[i][j].setIcon(icon);judgle1(white,i,j);}if(n%2!=0) { drawlconb icon = new drawlconb(40, 40);black[i][j]=true;//有hei棋子b[i][j].setIcon(icon);judgle1(black,i,j);}n ;//奇數偶數的計量方式,沒有特別意義judgle[i][j]=true;//有了棋子}}}});}setSize(950,950);setLocationRelativeTo(null);//要放在sitsize后面,不然整個圖形就在setVisible(true);setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);}public static void main(String[] args){wuziqi2 f= new wuziqi2();} static class drawlconw implements Icon{ // 實現Icon接口白色棋子private int width; // 聲明圖標的寬private int height; // 聲明圖標的長public int getIconHeight() { // 實現getIconHeight()方法return this.height;}public int getIconWidth() { // 實現getIconWidth()方法return this.width;}public drawlconw(int width, int height) { // 定義構造方法this.width = width;this.height = height;}// 實現paintIcon()方法public void paintIcon(Component arg0, Graphics arg1, int x, int y) {arg1.setColor(Color.white);arg1.fillOval(x, y, width, height); // 繪制一個圓形}} static class drawlconb implements Icon{ // 實現Icon接口白色棋子private int width; // 聲明圖標的寬private int height; // 聲明圖標的長public int getIconHeight() { // 實現getIconHeight()方法return this.height;}public int getIconWidth() { // 實現getIconWidth()方法return this.width;}public drawlconb(int width, int height) { // 定義構造方法this.width = width;this.height = height;}// 實現paintIcon()方法public void paintIcon(Component arg0, Graphics arg1, int x, int y) {arg1.setColor(Color.black);arg1.fillOval(x, y, width, height); // 繪制一個圓形 } } class DrawPanel1 extends JPanel {// Graphics g=getGraphics();public void paint(Graphics g) {super.paint(g);for ( int i = 25; i < 910; i = i 48) {//畫豎線g.drawLine(30, i, 912, i);g.setColor(Color.black);}for (int i = 30; i <= 925; i = i 49) {//畫橫線g.drawLine(i, 25, i, 890);}} } }


本人菜雞剛學java,有很多理解不好的地方,忘大佬指出!

總結

以上是生活随笔為你收集整理的简单的五子棋操作用两种方法实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。