java實(shí)現(xiàn)簡單的五子棋游戲
本文實(shí)例為大家分享了java實(shí)現(xiàn)簡單五子棋游戲的具體代碼,供大家參考,具體內(nèi)容如下
一、主要界面
1、登錄界面;2、游戲選擇界面;3、五子棋下棋界面
它們間的邏輯關(guān)系為先通過登錄界面登錄跳轉(zhuǎn)到游戲選擇界面,再通過游戲選擇界面進(jìn)入到五子棋下棋界面。
二、功能概況
1、登錄界面:
a、設(shè)置一定的布局和添加一些組件;b、通過點(diǎn)擊登錄按鈕判斷賬號(hào)和密碼是否正確,正確則成功登錄,反之則彈出一個(gè)提示賬號(hào)或密碼輸入錯(cuò)誤的窗口;c、在窗口中添加一個(gè)按鈕,在點(diǎn)擊此按鈕后關(guān)閉當(dāng)前窗口,并在第三次輸入錯(cuò)誤后彈出提示并關(guān)閉登錄界面
2、游戲選擇界面:a、添加背景圖片;b、在背景面板上添加游戲選擇的按鈕;c、通過游戲選擇按鈕跳轉(zhuǎn)到相應(yīng)的游戲。
3、五子棋下棋界面:
(1)畫出一個(gè)16*16的棋盤
(2)黑白棋實(shí)現(xiàn)輪流落子,落子位置在交叉點(diǎn)上,并且同一位置只能落一子
(3)黑白落子計(jì)數(shù)器
(4)重繪功能,在拖動(dòng)改變窗體大小時(shí)保留之前的棋盤棋子及計(jì)數(shù)顯示
(5)按鈕:開始游戲,重新開始,悔棋,棋局回放,存檔,讀檔,退出游戲等功能
(6)判斷輸贏,當(dāng)且僅當(dāng)五顆同色的棋子連在一起時(shí)得勝,當(dāng)出現(xiàn)六顆及以上同色棋子相連時(shí)不算得勝
三、代碼部分
1.GoBangUI類:
import javax.swing.*;?? ?//導(dǎo)入swing包的所有類和接口 import java.awt.*;?? ??? ?//導(dǎo)入awt包的所有類和接口 ?? ?//登錄密碼:123?? ? ? 賬號(hào):123 public class GoBangUI extends JFrame implements GoBangConfig { ?? ?private static final Image bgimg = new ImageIcon("C:\\Users\\zyx\\Pictures\\Camera Roll\\17.jpeg").getImage(); ?? ?Font f=new Font("楷體", Font.PLAIN, 30);//楷體,常規(guī),30號(hào)字體 ?? ?//主函數(shù)入口 ?? ?public static void main(String[]args) {?? ? ?? ??? ?Login cx=new Login(); ?? ??? ?cx.UI();?? ??? ??? ??? ? ?? ?} ? ?int[][] Array=new int[16][16];//實(shí)例化一個(gè)二維數(shù)組的對(duì)象來存儲(chǔ)棋子信息,0表示無棋,1表示黑棋,2表示白棋;在我們未給定初值的情況下,會(huì)默認(rèn)初值為0 ?? ?public void initGobangUI(){?? ? ?? ??? ?this.setTitle("五子棋"); ?? ??? ?this.setSize(1200,850);// 設(shè)置窗體的大小,單位為像素 ?? ??? ?this.setLocationRelativeTo(null);//設(shè)置窗體相對(duì)于組件的居中位置,null表示窗體相對(duì)于屏幕的中央位置 ?? ??? ?this.setDefaultCloseOperation(3);?? ??? ?// 設(shè)置窗體的關(guān)閉操作 ?? ??? ?Dimension dm1=new Dimension(200,0);//設(shè)置JPanel的大小 ?? ??? ?Dimension dm2=new Dimension(140,50);//設(shè)置JButton的大小 ?? ??? ?Dimension dm3=new Dimension(170,110);//設(shè)置JLabel的大小 ?? ??? ?JPanel jp=new JPanel(); ?? ??? ?jp.setBackground(Color.lightGray);//設(shè)置面板JPanel的背景色 ?? ??? ?jp.setPreferredSize(dm1); ?? ??? ?this.add(jp,BorderLayout.EAST);//通過邊框布局將面板添加到窗體的右邊 ?? ??? ?JLabel jl=new JLabel(" ? ?"); ?? ??? ?jl.setPreferredSize(dm3); ?? ??? ?jp.add(jl); ?? ??? ?String[] name= {"開始游戲","重新開始","悔棋","棋局回放","存檔","讀檔","退出游戲"}; ?? ??? ?JButton[] button=new JButton[7]; ?? ??? ?//依次把五個(gè)按鈕組件加上去 ?? ??? ?for(int i=0;i<name.length;i++) { ?? ??? ??? ?button[i]=new JButton(name[i]); ?? ??? ??? ?button[i].setPreferredSize(dm2); ?? ??? ??? ?jp.add(button[i]); ?? ??? ?} ?? ??? ?//設(shè)置窗體的背景顏色 ?? ??? ?Container con=this.getContentPane();?? ? ? ? ? ? con.setBackground(Color.darkGray) ; ?? ??? ?this.setVisible(true); ?? ??? ?// 從當(dāng)前窗體獲取畫筆并通過構(gòu)造方法傳入監(jiān)聽類對(duì)象中 ?? ??? ?Graphics gr=this.getGraphics(); ?? ??? ? GoBangListener goBangListener = new GoBangListener(gr); ?? ??? ? //給按鈕組件添加動(dòng)作監(jiān)聽方法 ?? ??? ? for(int i=0;i<name.length;i++) { ?? ? ? ? ?button[i].addActionListener(goBangListener); ?? ??? ??? ?} ?? ??? ??? ?// 給窗體添加鼠標(biāo)監(jiān)聽方法 ?? ??? ?this.addMouseListener(goBangListener); ?? ??? ?goBangListener.go=this; ?? ??? ?}?? ? ?? ??? ? ?? ?@Override ?? ?public void paint(Graphics g) { ?? ?// 先調(diào)用父類繪制窗體的方法 ?? ??? ?super.paint(g); ?? ?//繪制背景圖片 ?? ?g.drawImage(bgimg,X,Y,columns*SIZE,rows*SIZE,null); ?? ?// 繪制棋盤 ?? ?for (int i = 0; i <= rows; i++) { ?? ?g.drawLine(X,Y+i*SIZE,X+columns*SIZE,Y+i*SIZE);//畫橫線 ?? ?} ?? ?for(int j=0;j<=columns;j++) { ?? ?g.drawLine(X+j*SIZE,Y,X+j*SIZE,Y+rows*SIZE);//畫豎線 ?? ?} ?? ?//黑白棋的落子數(shù) ?? ?g.setColor(Color.darkGray); ?? ?g.fillRect( X*2+columns*SIZE, Y*11/4,SIZE*5/2, 3*SIZE); ?? ?g.setColor(Color.ORANGE); ?? ?g.setFont(f); ?? ?g.drawString("黑棋:"+GoBangListener.m, 2*X+columns*SIZE, Y*10/3); ?? ?g.drawString("白棋:"+GoBangListener.n, 2*X+columns*SIZE,Y*13/3); ? ?? ?g.setFont(f);//設(shè)置字體 ?? ?//遍歷二維數(shù)組,畫出已賦值的對(duì)應(yīng)位置的黑白棋 ?? ??? ?for(int j=0;j<=columns;j++) { ?? ??? ??? ?for(int i=0;i<=rows;i++) { ?? ??? ??? ?if(Array[i][j]==1) { ?? ??? ??? ??? ?g.setColor(Color.BLACK); ?? ??? ??? ??? ?g.fillOval(j*SIZE+X-SIZE/2, i*SIZE+Y-SIZE/2, SIZE, SIZE); ? ?? ??? ??? ?}else if(Array[i][j]==2){ ?? ??? ??? ??? ?g.setColor(Color.WHITE); ?? ??? ??? ??? ?g.fillOval(j*SIZE+X-SIZE/2, i*SIZE+Y-SIZE/2, SIZE, SIZE); ? ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ? ?? ?} ? }
2.GoBangListener類:
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.ActionListener; import javax.swing.*; ? public class GoBangListener implements ActionListener,MouseListener,GoBangConfig {?? ? ?? ?int chessX;?? ??? ?//列索引 ?? ?int chessY;?? ??? ?//行索引 ?? ?int flag=0;?? ??? ?//黑白棋輪流標(biāo)記 ?? ?int count=0;?? ?//記錄落子順序 ?? ?int ct,flag1,m1,n1,q,w;?? ??? ?//用作存檔操作 ?? ?GoBangUI go; ?? ?String name; ?? ?String name1="";?? ??? ?//用于開始游戲操作 ?? ?JTextField jt; ?? ?JPasswordField jp; ?? ?JFrame jf;?? ? ?? ? JFrame j = new JFrame(); ?? ? JFrame j1 = new JFrame(); ?? ?int i=3;?? ??? ?//允許登錄失敗的次數(shù) ?? ?static int m=0,n=0;?? ??? ?//記錄黑白各自的落子數(shù) ?? ?private Graphics g; ?? ?public GoBangListener() {} ?? ?GoBangListener(Graphics g) { ?? ??? ?this.g=g; ?? ?} ?? ?GBstore[] gbs=new GBstore[16*16];?? ??? ?//儲(chǔ)存棋子的落子順序和位置信息 ?? ? public void actionPerformed(ActionEvent e) { ?? ? ?? ??? ? name=e.getActionCommand(); ?? ??? ? ?? ??? ?if(e.getActionCommand().equals("退出游戲")) { ?? ??? ??? ?j.dispose();?? ?j1.dispose();?? ?go.dispose(); ?? ??? ?} ?? ??? ??? ? ? ?? ??? ??? ?if(e.getActionCommand().equals("開始游戲")) { ?? ??? ??? ??? ?name1=e.getActionCommand(); ?? ??? ??? ?}else { ?? ??? ??? ??? ?if(e.getActionCommand().equals("重新開始")|e.getActionCommand().equals("再來一局")) { ?? ??? ??? ??? ??? ?w=count; ?? ??? ??? ??? ??? ?count=0; ?? ??? ??? ??? ??? ?m=0; ?? ??? ??? ??? ??? ?n=0; ?? ??? ??? ??? ??? ?flag=0; ?? ??? ??? ??? ??? ?for(i=0;i<w;i++) { ?? ??? ??? ??? ??? ??? ?go.Array[gbs[count+i].chessy][gbs[count+i].chessx]=0; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?j.dispose(); ?? ??? ??? ??? ??? ?j1.dispose(); ?? ??? ??? ??? ??? ?go.repaint(); ?? ??? ??? ??? ?} ?? ??? ?if(e.getActionCommand().equals("悔棋")) { ?? ??? ??? ?if(count!=0) { ?? ??? ??? ?if(flag==0) { ?? ??? ??? ?go.Array[gbs[count-1].chessy][gbs[count-1].chessx]=0; ?? ??? ??? ?n--; ?? ??? ??? ? ?? ??? ??? ?go.repaint(); ?? ??? ??? ?count--; ?? ??? ? ?? ??? ??? ?flag++;?? ??? ? ?? ??? ??? ?}else if(flag==1) { ?? ??? ??? ??? ?go.Array[gbs[count-1].chessy][gbs[count-1].chessx]=0; ?? ??? ??? ??? ?go.repaint(); ?? ??? ??? ??? ?m--; ?? ??? ??? ??? ?count--; ?? ??? ??? ??? ?flag--; ?? ??? ??? ?} ?? ??? ?}} ?? ??? ?if(e.getActionCommand().equals("棋局回放")) { ?? ??? ??? ?if(gbs[count]!=null) { ?? ??? ??? ?if(flag==0) { ?? ??? ??? ??? ?go.Array[gbs[count].chessy][gbs[count].chessx]=1; ?? ??? ??? ??? ?m++; ?? ??? ??? ??? ? ?? ??? ??? ??? ?go.repaint(); ?? ??? ??? ??? ?count++; ?? ??? ??? ? ?? ??? ??? ??? ?flag++;?? ??? ? ?? ??? ??? ??? ?}else if(flag==1) { ?? ??? ??? ??? ??? ?go.Array[gbs[count].chessy][gbs[count].chessx]=2; ?? ??? ??? ??? ??? ?go.repaint(); ?? ??? ??? ??? ??? ?n++; ?? ??? ??? ??? ??? ?count++; ?? ??? ??? ??? ??? ?flag--; ?? ??? ??? ??? ?} ?? ??? ?}} ?? ??? ?if(e.getActionCommand().equals("存檔")) { ?? ??? ??? ?flag1=flag; ?? ??? ??? ?ct=count; ?? ??? ??? ?m1=m; ?? ??? ??? ?n1=n; ?? ??? ?} ?? ??? ?if(e.getActionCommand().equals("讀檔")) { ?? ??? ??? ?q=count-ct; ?? ??? ??? ? ?? ??? ? ?? ??? ??? ?count=ct; ?? ??? ??? ?m=m1; ?? ??? ??? ?n=n1; ?? ??? ?if(flag!=0) { ?? ??? ??? ?flag=0; ?? ??? ?} ?? ??? ??? ?for(int i=0;i<count;i++) { ?? ??? ??? ??? ? ?? ??? ??? ?if(flag==0) { ?? ??? ??? ??? ?go.Array[gbs[i].chessy][gbs[i].chessx]=1; ?? ??? ??? ??? ??? ?flag++;?? ? ?? ??? ??? ??? ?}else if(flag==1) { ?? ??? ??? ??? ?go.Array[gbs[i].chessy][gbs[i].chessx]=2; ?? ??? ??? ??? ??? ??? ??? ?flag--;?? ? ?? ??? ??? ??? ?}} ?? ??? ??? ?for(i=0;i<q;i++) { ?? ??? ??? ??? ?go.Array[gbs[count+i].chessy][gbs[count+i].chessx]=0; ?? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ?flag=flag1; ?? ??? ??? ?go.repaint(); ?? ??? ?} ?? ??? ??? ?} ?? ??? ? if(name.equals("登錄")) { ?? ??? ??? ? if(jt.getText().equals("123")&&jp.getText().equals("123")) { ?? ??? ??? ??? ? GameselectUI ui=new GameselectUI(); ?? ??? ??? ??? ? ui.selectUI(); ?? ??? ??? ?jf.dispose(); ?? ??? ??? ?} else if(i>1) { ?? ??? ??? ??? ? JFrame lg1 = new JFrame(); ?? ??? ??? ??? ??? ?lg1.setSize(400,200); ?? ??? ??? ??? ??? ?lg1.setDefaultCloseOperation(3); ?? ??? ??? ??? ??? ?lg1.setLocationRelativeTo(null); ?? ??? ??? ??? ??? ?JPanel jp1 = new JPanel(); ?? ??? ??? ??? ??? ?JPanel jp2 = new JPanel(); ?? ??? ??? ??? ??? ?JPanel jp3 = new JPanel();?? ??? ??? ??? ? ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ?Dimension dm = new Dimension(0,50); ?? ??? ??? ??? ??? ?JLabel jl = new JLabel("賬號(hào)或密碼輸入錯(cuò)誤,您還有"+(i-1)+"次機(jī)會(huì)"); ?? ??? ??? ??? ??? ?jl.setFont(new Font("華文仿宋",Font.BOLD,18)); ?//華文仿宋,加粗,18號(hào)字體 ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ?jp2.add(jl); ?? ??? ??? ??? ??? ?jp1.setPreferredSize(dm); ?? ??? ??? ??? ??? ?lg1.add(jp1,BorderLayout.NORTH); ?? ??? ??? ??? ??? ?lg1.add(jp2,BorderLayout.CENTER); ?? ??? ??? ??? ??? ?lg1.add(jp3,BorderLayout.SOUTH); ?? ??? ??? ??? ??? ?JButton jb = new JButton("確定");?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ?jp3.add(jb); ?? ??? ??? ??? ??? ?lg1.add(jp3,BorderLayout.SOUTH);?? ? ?? ??? ??? ??? ??? ?jb.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)?? ?{lg1.dispose();}});?? ? ?? ??? ??? ??? ??? ?lg1.setVisible(true);?? ??? ??? ??? ??? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ?}else if(i==1) { ?? ??? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ??? ? JFrame lg1 = new JFrame(); ?? ??? ??? ??? ??? ??? ??? ?lg1.setSize(400,200); ?? ??? ??? ??? ??? ??? ??? ?lg1.setDefaultCloseOperation(3); ?? ??? ??? ??? ??? ??? ??? ?lg1.setLocationRelativeTo(null); ?? ??? ??? ??? ??? ??? ??? ?JPanel jp1 = new JPanel(); ?? ??? ??? ??? ??? ??? ??? ?JPanel jp2 = new JPanel(); ?? ??? ??? ??? ??? ??? ??? ?JPanel jp3 = new JPanel();?? ??? ??? ??? ? ?? ??? ??? ??? ??? ??? ??? ?Dimension dm = new Dimension(0,50); ?? ??? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ??? ??? ?JLabel jl = new JLabel("賬號(hào)已鎖定"); ?? ??? ??? ??? ??? ??? ??? ?jl.setFont(new Font("華文仿宋",Font.BOLD,18)); ?//華文仿宋,加粗,18號(hào)字體 ?? ??? ??? ??? ??? ??? ??? ?jp1.setPreferredSize(dm); ?? ??? ??? ??? ??? ??? ??? ?jp2.add(jl); ?? ??? ??? ??? ??? ??? ??? ?lg1.add(jp1,BorderLayout.NORTH); ?? ??? ??? ??? ??? ??? ??? ?lg1.add(jp2,BorderLayout.CENTER); ?? ??? ??? ??? ??? ??? ??? ?lg1.add(jp3,BorderLayout.SOUTH); ?? ??? ??? ??? ??? ??? ??? ?JButton jb = new JButton("確定");?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ??? ??? ?jp3.add(jb); ?? ??? ??? ??? ??? ??? ??? ?lg1.add(jp3,BorderLayout.SOUTH);?? ? ?? ??? ??? ??? ??? ??? ??? ?lg1.setVisible(true); ?? ??? ??? ??? ??? ??? ??? ?jb.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)?? ?{lg1.dispose();jf.dispose(); }}); ?? ??? ??? ??? ??? ??? ??? ?jf.dispose(); ?? ??? ??? ??? ??? ??? ??? ?} ?? ??? ??? ? ?? ??? ??? ??? ?i--; ?? ??? ??? ?? ?? ??? ??? ? }else if(name.equals("注冊(cè)")) { ?? ??? ??? ??? ? JFrame lg1 = new JFrame(); ?? ??? ??? ??? ??? ?lg1.setSize(400,200); ?? ??? ??? ??? ??? ?lg1.setDefaultCloseOperation(3); ?? ??? ??? ??? ??? ?lg1.setLocationRelativeTo(null); ?? ??? ??? ??? ??? ?JPanel jp1 = new JPanel(); ?? ??? ??? ??? ??? ?JPanel jp2 = new JPanel(); ?? ??? ??? ??? ??? ?JPanel jp3 = new JPanel();?? ??? ??? ??? ? ?? ??? ??? ??? ??? ?Dimension dm = new Dimension(0,50);?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ?JLabel jl = new JLabel("注冊(cè)功能暫未開放..."); ?? ??? ??? ??? ??? ?jl.setFont(new Font("華文仿宋",Font.BOLD,18)); ?//華文仿宋,加粗,18號(hào)字體 ?? ??? ??? ??? ??? ?jp1.setPreferredSize(dm); ?? ??? ??? ??? ??? ?jp2.add(jl); ?? ??? ??? ??? ??? ?lg1.add(jp1,BorderLayout.NORTH); ?? ??? ??? ??? ??? ?lg1.add(jp2,BorderLayout.CENTER); ?? ??? ??? ??? ??? ?lg1.add(jp3,BorderLayout.SOUTH); ?? ??? ??? ??? ??? ?JButton jb = new JButton("確定");?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ?jp3.add(jb); ?? ??? ??? ??? ??? ?lg1.add(jp3,BorderLayout.SOUTH); ?? ??? ??? ??? ??? ?lg1.setVisible(true); ?? ??? ??? ??? ??? ?jb.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)?? ?{lg1.dispose(); }}); ?? ??? ??? ? } ?? ? } ?? ? ?? ?@Override ?? ?public void mouseClicked(MouseEvent e) {?? ??? ? ?? ??? ?if(name1.equals("開始游戲")) { ?? ??? ?int x=e.getX();? ?? ??? ?int y=e.getY(); ?? ??? ? ?? ??? ?Font f=new Font("楷體", Font.PLAIN, 30); ?//楷體,常規(guī),30號(hào) ?? ??? ?// 限制下棋的范圍 ? ? ? ?? ??? ?if((x>=X&&x< X+columns*SIZE)&&(y>Y&&y<Y+rows*SIZE)){ ?? ??? ??? ? ?? ??? ??? ?// 賦值坐標(biāo) ?? ??? ??? ?chessX = (x-X+SIZE/2)/SIZE; ? ? //列索引 ?? ??? ??? ?chessY = (y-Y+SIZE/2)/SIZE;?? ??? ?//行索引 ?? ??? ??? ? ?? ??? ??? ? ?? ??? ??? ?if(go.Array[chessY][chessX]!=0) { //Array[][] arr=new Array[一維數(shù)組的個(gè)數(shù)(行數(shù))][一維數(shù)組中元素的個(gè)數(shù)(列數(shù))] ?? ??? ??? ??? ?System.out.println("此處已有棋子"); ?? ??? ??? ?}else { ?? ??? ??? ??? ?//輪到黑方下棋 ?? ??? ??? ?if(flag==0) { ?? ??? ??? ??? ?m++; ?? ??? ??? ??? ?g.setColor(Color.BLACK); ?? ??? ??? ??? ?g.fillOval(chessX*SIZE+X-SIZE/2, chessY*SIZE+Y-SIZE/2, SIZE, SIZE); ?? ??? ??? ??? ?flag++; ?? ??? ??? ??? ?//實(shí)現(xiàn)黑子計(jì)數(shù) ?? ??? ??? ??? ?g.setColor(Color.darkGray); ?? ??? ??? ??? ?g.fillRect( X*2+columns*SIZE, Y*11/4, SIZE*5/2, SIZE); ?? ??? ??? ??? ?g.setColor(Color.ORANGE); ?? ??? ??? ??? ?g.setFont(f); ?? ??? ??? ??? ?g.drawString("黑棋:"+m, 2*X+columns*SIZE, Y*10/3); ?? ??? ??? ??? ? ?? ??? ??? ??? ?go.Array[chessY][chessX]=1; ?? ??? ??? ??? ?GBstore gb=new GBstore(chessX,chessY); ?? ??? ??? ??? ?gbs[count]=gb;//傳遞相應(yīng)順序?qū)?yīng)的行列索引值 ?? ??? ??? ??? ?count++; ?? ??? ??? ??? ?//判斷輸贏 ?? ??? ??? ??? ?if(Judge.blackjudge(go.Array,chessY, chessX)){ ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ??? ?j1.setSize(400,200); ?? ??? ??? ??? ??? ??? ?j1.setDefaultCloseOperation(3); ?? ??? ??? ??? ??? ??? ?j1.setLocationRelativeTo(null); ?? ??? ??? ??? ??? ??? ?JPanel jp1 = new JPanel(); ?? ??? ??? ??? ??? ??? ?JPanel jp2 = new JPanel(); ?? ??? ??? ??? ??? ??? ?JPanel jp3 = new JPanel();?? ??? ??? ??? ? ?? ??? ??? ??? ??? ??? ?Dimension dm = new Dimension(0,50); ?? ??? ??? ??? ??? ??? ?JLabel jl = new JLabel("恭喜黑棋獲勝"); ?? ??? ??? ??? ??? ??? ?jl.setFont(new Font("華文仿宋",Font.BOLD,18)); ?//華文仿宋,加粗,18號(hào)字體 ?? ??? ??? ??? ??? ??? ?jp2.add(jl); ?? ??? ??? ??? ??? ??? ?jp1.setPreferredSize(dm); ?? ??? ??? ??? ??? ??? ?j1.add(jp1,BorderLayout.NORTH); ?? ??? ??? ??? ??? ??? ?j1.add(jp2,BorderLayout.CENTER); ?? ??? ??? ??? ??? ??? ?j1.add(jp3,BorderLayout.SOUTH); ?? ??? ??? ??? ??? ??? ?JButton jb = new JButton("回顧棋局");?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ??? ?jp3.add(jb); ?? ??? ??? ??? ??? ??? ?JButton jb1 = new JButton("再來一局");?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ??? ?jp3.add(jb1); ?? ??? ??? ??? ??? ??? ?j1.add(jp3,BorderLayout.SOUTH);?? ? ?? ??? ??? ??? ??? ??? ?jb.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)?? ?{j1.dispose();}});?? ? ?? ??? ??? ??? ??? ??? ?jb1.addActionListener(this);?? ? ?? ??? ??? ??? ??? ??? ?j1.setResizable(false); ?? ??? ??? ??? ??? ??? ?j1.setVisible(true);?? ? ?? ??? ??? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ?} ?? ??? ? ?? ??? ??? ?}else {?? ??? ? ?? ??? ??? ??? ?//輪到白方下棋 ?? ??? ??? ??? ?n++; ?? ??? ??? ??? ?g.setColor(Color.WHITE);? ?? ??? ??? ??? ?g.fillOval(chessX*SIZE+X-SIZE/2, chessY*SIZE+Y-SIZE/2, SIZE, SIZE); ?? ??? ??? ??? ?flag--; ?? ??? ??? ??? ?g.setColor(Color.darkGray); ?? ??? ??? ??? ?g.fillRect( 2*X+columns*SIZE, Y*15/4, SIZE*5/2, SIZE); ?? ??? ??? ??? ?g.setColor(Color.ORANGE);?? ??? ??? ? ?? ??? ??? ??? ?g.setFont(f); ?? ??? ??? ??? ?g.drawString("白棋:"+n, 2*X+columns*SIZE,Y*13/3);?? ??? ??? ??? ? ?? ??? ??? ??? ?go.Array[chessY][chessX]=2; ?? ??? ??? ??? ?GBstore gb=new GBstore(chessX,chessY); ?? ??? ??? ??? ?gbs[count]=gb; ?? ??? ??? ??? ?count++; ?? ??? ??? ??? ? ?? ??? ??? ??? ?if(Judge.whitejudge(go.Array,chessY, chessX)){ ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ??? ?j.setSize(400,200); ?? ??? ??? ??? ??? ??? ?j.setDefaultCloseOperation(3); ?? ??? ??? ??? ??? ??? ?j.setLocationRelativeTo(null); ?? ??? ??? ??? ??? ??? ?JPanel jp1 = new JPanel(); ?? ??? ??? ??? ??? ??? ?JPanel jp2 = new JPanel(); ?? ??? ??? ??? ??? ??? ?JPanel jp3 = new JPanel();?? ??? ??? ??? ? ?? ??? ??? ??? ??? ??? ?Dimension dm = new Dimension(0,50); ?? ??? ??? ??? ??? ??? ?JLabel jl = new JLabel("恭喜白棋獲勝"); ?? ??? ??? ??? ??? ??? ?jl.setFont(new Font("華文仿宋",Font.BOLD,18)); ?//華文仿宋,加粗,18號(hào)字體?? ? ?? ??? ??? ??? ??? ??? ?jp2.add(jl); ?? ??? ??? ??? ??? ??? ?jp1.setPreferredSize(dm); ?? ??? ??? ??? ??? ??? ?j.add(jp1,BorderLayout.NORTH); ?? ??? ??? ??? ??? ??? ?j.add(jp2,BorderLayout.CENTER); ?? ??? ??? ??? ??? ??? ?j.add(jp3,BorderLayout.SOUTH); ?? ??? ??? ??? ??? ??? ?JButton jb = new JButton("回顧棋局");?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ??? ?jp3.add(jb); ?? ??? ??? ??? ??? ??? ?JButton jb1 = new JButton("再來一局");?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ??? ?jp3.add(jb1); ?? ??? ??? ??? ??? ??? ?j.add(jp3,BorderLayout.SOUTH);?? ? ?? ??? ??? ??? ??? ??? ?jb.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)?? ?{j.dispose();}});?? ? ?? ??? ??? ??? ??? ??? ?jb1.addActionListener(this);?? ? ?? ??? ??? ??? ??? ??? ?j.setResizable(false); ?? ??? ??? ??? ??? ??? ?j.setVisible(true);?? ??? ? ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ?}?? ??? ??? ??? ? ?? ??? ??? ??? ? ?? ??? ??? ?} ?? ??? ?} ?? ??? ??? ?} ?? ??? ?}} ?? ?@Override ?? ?public void mouseEntered(MouseEvent e) { ?? ?} ?? ?@Override ?? ?public void mouseExited(MouseEvent e) { ?? ?} ?? ?@Override ?? ?public void mousePressed(MouseEvent e) {?? ? ?? ?} ?? ? ?? ?@Override ?? ?public void mouseReleased(MouseEvent e) { ?? ?} }
3.GoBangConfig類:
?public interface GoBangConfig { ?? ?/** ?? ?* 間距|尺寸 ?? ?*/?? ? ?? ?int SIZE=50; ?? ?/** ?? ?* 棋盤左上角 X坐 標(biāo) ?? ?*/ ?? ?int X=40; ?? ?/** ?? ?* 棋盤左上角 Y 坐 標(biāo) ?? ?*/ ?? ?int Y=65; ?? ?/** ?? ?* 棋盤的行列值 ?? ?*/ ?? ?int rows=15; ?? ?int columns=15; ? }
4.GBstore類:
/* ?* 儲(chǔ)存棋子的位置信息 ?*/ public class GBstore { int chessx,chessy; GBstore(int chessx,int chessy){ ?? ?this.chessx=chessx; ?? ?this.chessy=chessy;?? ? } ? }
5.GameselectUI類:
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public ?class ?GameselectUI extends JFrame { ?? ?public void selectUI(){ ?? ??? ?JFrame j=this;?? ??? ?//綁定當(dāng)前窗體 ?? ??? ?this.setTitle("Game selection UI");//設(shè)置標(biāo)題 ?? ??? ?j.setSize(370,400); ?? ??? ?ImageIcon img=new ImageIcon("C:\\Users\\zyx\\Pictures\\Camera Roll\\2.jpg"); ?? ??? ?BackgroundPanel bgp=new BackgroundPanel(img.getImage()); ?? ??? ?bgp.setSize(370,400); ?? ? ?? ?this.setDefaultCloseOperation(3); ?? ? ?? ?this.setLocationRelativeTo(null); ?? ??? ?Dimension d = new Dimension(370,190);?? ?//通過設(shè)置標(biāo)簽調(diào)整按鈕位置,JPanel面板默認(rèn)使用流式布局 ?? ??? ?Dimension dim = new Dimension(100,60);//設(shè)置按鈕的大小?? ? ?? ??? ?this.add(bgp);//給當(dāng)前窗體添加背景面板?? ? ?? ??? ?JLabel jl=new JLabel();?? ??? ? ?? ??? ?jl.setPreferredSize(d); ?? ??? ?bgp.add(jl); ?? ??? ?JButton gbbt1 = new JButton("五子棋");?? ? ?? ??? ?Font f=new Font("華文仿宋",Font.BOLD,18);//字體:華文仿宋;字形:加粗;字號(hào):18 ?? ??? ?gbbt1.setFont(f); ?? ??? ?gbbt1.setPreferredSize(dim); ?? ??? ?bgp.add(gbbt1);?? ?//添加按鈕到面板上 ?? ??? ?JLabel jla=new JLabel(""); ?? ??? ?jla.setPreferredSize(new Dimension(100,60)); ?? ??? ?bgp.add(jla); ?? ??? ?JButton drbt2 = new JButton("待定");?? ? ?? ??? ?drbt2.setFont(f); ?? ??? ?drbt2.setPreferredSize(dim); ?? ??? ?bgp.add(drbt2); ?? ??? ? ?? ??? ?//設(shè)置入口 ?? ??? ?ActionListener al=new ActionListener() {public void actionPerformed(ActionEvent e) { ?? ??? ??? ?if(e.getActionCommand().equals("五子棋")) { ?? ??? ??? ??? ?GoBangUI gb=new GoBangUI(); ?? ??? ??? ??? ?gb.initGobangUI(); ?? ??? ??? ??? ?j.dispose(); ?? ??? ??? ??? ?}else if(e.getActionCommand().equals("待定")) { ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ?} ?? ??? ?}};//添加監(jiān)聽方法 ?? ??? ?gbbt1.addActionListener(al); ?? ??? ?drbt2.addActionListener(al); ?? ??? ? ?? ??? ?this.setResizable(false); ?? ? ? ?? ??? ?//設(shè)置不可調(diào)整窗體大小 ?? ? ? ?GameselectUI.this.setVisible(true);?? ??? ?//設(shè)置窗體可見?? ? ?? ?} ?? ? ? } ? class BackgroundPanel extends JPanel{ Image img; BackgroundPanel(Image img){ this.img=img; } //重寫paintComponent方法畫出背景圖 public void paintComponent(Graphics g){ super.paintComponents(g); System.out.println(); g.drawImage(img,0,0,370,400,this);? } ? }?
6.Login類:
import javax.swing.*; import java.awt.*; ? public class Login ?{ ?? ? ?? ?public void UI() { ?? ??? ?javax.swing.JFrame jf= new javax.swing.JFrame(); ?? ??? ?jf.setSize(370, 505); ?? ??? ?jf.setDefaultCloseOperation(3); ?? ??? ?jf.setTitle("登錄界面"); ?? ??? ?jf.setLocationRelativeTo(null);?? ??? ? ?? ??? ?java.awt.FlowLayout fl= new java.awt.FlowLayout(); ?? ??? ?jf.setLayout(fl); ?? ??? ?Dimension di=new Dimension(360,360); ?? ??? ?javax.swing.ImageIcon image= new javax.swing.ImageIcon("D:\\360Downloads\\1.jpg"); ?? ??? ?javax.swing.JLabel jl=new javax.swing.JLabel(image); ?? ??? ?jl.setPreferredSize(di); ?? ??? ?jf.add(jl); ?? ??? ?javax.swing.JLabel zh=new javax.swing.JLabel("賬號(hào)"); ?? ??? ?jf.add(zh);?? ??? ? ?? ??? ?javax.swing.JTextField jtf=new javax.swing.JTextField(); ?? ??? ?Dimension dm=new Dimension(300,30); ?? ??? ?jtf.setPreferredSize(dm); ?? ??? ?jf.add(jtf); ?? ??? ?javax.swing.JLabel mm=new javax.swing.JLabel("密碼"); ?? ??? ?jf.add(mm);?? ? ?? ??? ?java.awt.Dimension d=new java.awt.Dimension(300,30);?? ??? ? ?? ??? ? ?? ??? ?JPasswordField wb=new JPasswordField(); ?? ??? ?wb.setPreferredSize(d); ?? ??? ?jf.add(wb); ?? ? ? ?JButton button=new JButton("登錄"); ?? ??? ?jf.add(button); ?? ??? ?javax.swing.JButton zc=new javax.swing.JButton("注冊(cè)"); ?? ??? ?jf.add(zc); ?? ??? ?jf.setResizable(false); ?? ??? ?jf.setVisible(true);?? ??? ? ?? ??? ?GoBangListener gl=new GoBangListener(); ?? ??? ?button.addActionListener(gl); ?? ??? ?zc.addActionListener(gl); ?? ??? ?gl.jt=jtf; ?? ??? ?gl.jp=wb; ?? ??? ?gl.jf=jf; ?? ?} ?? ? ? }
7.Judge類:
public class Judge { ?? ??? ?//黑棋勝負(fù)判斷 public static boolean blackjudge(int[][] Array,int chessY,int chessX) { ?? ?int count1=0,count2=0,count3=0,count4=0; ?? ? ?? ?//橫向遍歷 ?? ?for(int i=-15;i<=15;i++) { ?? ??? ?if(chessX+i>=0&&chessX+i<=15) {?? ??? ?//確定遍歷的范圍 ?? ??? ?if(count1<5) { ?? ??? ??? ?if(Array[chessY][chessX+i]==1) { ?? ??? ??? ??? ?count1++; ?? ??? ??? ?}else { ?? ??? ??? ??? ?count1=0; ?? ??? ??? ?} ?? ??? ?}else if(count1==5) { ?? ??? ??? ?if(Array[chessY][chessX+i]==1) { ?? ??? ??? ??? ?count1++; ?? ??? ?}}}} ?? ?//縱向遍歷 ?? ?for(int i=-15;i<=15;i++) { ?? ??? ?if(chessY+i>=0&&chessY+i<=15) { ?? ??? ?if(count2<5) { ?? ??? ??? ?if(Array[chessY+i][chessX]==1) { ?? ??? ??? ??? ?count2++; ?? ??? ??? ?}else { ?? ??? ??? ??? ?count2=0; ?? ??? ??? ?} ?? ??? ?}else if(count2==5) { ?? ??? ??? ?if(Array[chessY+i][chessX]==1) { ?? ??? ??? ??? ?count2++; ?? ??? ?}}}} ?? ?//斜下遍歷 ?? ?for(int i=-15;i<=15;i++) { ?? ??? ?if(chessY+i>=0&&chessY+i<=15&&chessX+i>=0&&chessX+i<=15) { ?? ??? ?if(count3<5) { ?? ??? ??? ?if(Array[chessY+i][chessX+i]==1) { ?? ??? ??? ??? ?count3++; ?? ??? ??? ?}else { ?? ??? ??? ??? ?count3=0; ?? ??? ??? ?} ?? ??? ?}else if(count3==5) { ?? ??? ??? ?if(Array[chessY+i][chessX+i]==1) { ?? ??? ??? ??? ?count3++; ?? ??? ?}}}} ?? ?//斜上遍歷 ?? ?for(int i=-15;i<=15;i++) { ?? ??? ?if(chessY-i>=0&&chessY-i<=15&&chessX+i>=0&&chessX+i<=15) { ?? ??? ?if(count4<5) { ?? ??? ??? ?if(Array[chessY-i][chessX+i]==1) { ?? ??? ??? ??? ?count4++; ?? ??? ??? ?}else { ?? ??? ??? ??? ?count4=0; ?? ??? ??? ?} ?? ??? ?}else if(count4==5) { ?? ??? ??? ?if(Array[chessY-i][chessX+i]==1) { ?? ??? ??? ??? ?count4++; ?? ??? ?}}}} ?? ? ?? ?if(count1==5|count2==5|count3==5|count4==5) { ?? ??? ?return true; ?? ?} ?? ?return false; ?? ? } ?? ??? ?//白棋勝負(fù)判斷 public static boolean whitejudge(int[][] Array,int chessY,int chessX) { ?? ?int count1=0,count2=0,count3=0,count4=0; ?? ?//橫向遍歷 ?? ?for(int i=-15;i<=15;i++) { ?? ??? ?if(chessX+i>=0&&chessX+i<=15) { ?? ??? ?if(count1<5) { ?? ??? ??? ?if(Array[chessY][chessX+i]==2) { ?? ??? ??? ??? ?count1++; ?? ??? ??? ?}else { ?? ??? ??? ??? ?count1=0; ?? ??? ??? ?} ?? ??? ?}else if(count1==5) { ?? ??? ??? ?if(Array[chessY][chessX+i]==2) { ?? ??? ??? ??? ?count1++; ?? ??? ?}}}} ?? ?//縱向遍歷 ?? ?for(int i=-15;i<=15;i++) { ?? ??? ?if(chessY+i>=0&&chessY+i<=15) { ?? ??? ?if(count2<5) { ?? ??? ??? ?if(Array[chessY+i][chessX]==2) { ?? ??? ??? ??? ?count2++; ?? ??? ??? ?}else { ?? ??? ??? ??? ?count2=0; ?? ??? ??? ?} ?? ??? ?}else if(count2==5) { ?? ??? ??? ?if(Array[chessY+i][chessX]==2) { ?? ??? ??? ??? ?count2++; ?? ??? ?}}}} ?? ?//斜下遍歷 ?? ?for(int i=-15;i<=15;i++) { ?? ??? ?if(chessY+i>=0&&chessY+i<=15&&chessX+i>=0&&chessX+i<=15) { ?? ??? ?if(count3<5) { ?? ??? ??? ?if(Array[chessY+i][chessX+i]==2) { ?? ??? ??? ??? ?count3++; ?? ??? ??? ?}else { ?? ??? ??? ??? ?count3=0; ?? ??? ??? ?} ?? ??? ?}else if(count3==5) { ?? ??? ??? ?if(Array[chessY+i][chessX+i]==2) { ?? ??? ??? ??? ?count3++; ?? ??? ?}}}} ?? ?//斜上遍歷 ?? ?for(int i=-15;i<=15;i++) { ?? ??? ?if(chessY-i>=0&&chessY-i<=15&&chessX+i>=0&&chessX+i<=15) { ?? ??? ?if(count4<5) { ?? ??? ??? ?if(Array[chessY-i][chessX+i]==2) { ?? ??? ??? ??? ?count4++; ?? ??? ??? ?}else { ?? ??? ??? ??? ?count4=0; ?? ??? ??? ?} ?? ??? ?}else if(count4==5) { ?? ??? ??? ?if(Array[chessY-i][chessX+i]==2) { ?? ??? ??? ??? ?count4++; ?? ??? ?}}}} ?? ? ?? ?if(count1==5|count2==5|count3==5|count4==5) { ?? ??? ?return true; ?? ?} ?? ?return false; ?? ? } }
四、部分效果展示
注意,代碼中圖片的路徑還需自己更改設(shè)置
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot Mybatis-Plus數(shù)據(jù)庫單元測(cè)試實(shí)戰(zhàn)(三種方式)
這篇文章主要介紹了Springboot Mybatis-Plus數(shù)據(jù)庫單元測(cè)試實(shí)戰(zhàn)(三種方式),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12淺談Java中的集合存儲(chǔ)數(shù)據(jù)后,輸出數(shù)據(jù)的有序和無序問題
這篇文章主要介紹了淺談Java中的集合存儲(chǔ)數(shù)據(jù)后,輸出數(shù)據(jù)的有序和無序問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09Mybatis實(shí)戰(zhàn)教程之入門到精通(經(jīng)典)
MyBatis是支持普通SQL查詢,存儲(chǔ)過程和高級(jí)映射的優(yōu)秀持久層框架,通過本文給大家介紹Mybatis實(shí)戰(zhàn)教程之入門到精通,對(duì)mybatis實(shí)戰(zhàn)教程相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01使用Spring?Retry實(shí)現(xiàn)業(yè)務(wù)異常重試
在系統(tǒng)中經(jīng)常遇到業(yè)務(wù)重試的邏輯,比如三方接口調(diào)用,timeout重試三遍,異常處理重試的兜底邏輯等,本文給大家介紹一下如何使用Spring?Retry優(yōu)雅的實(shí)現(xiàn)業(yè)務(wù)異常重試,需要的朋友可以參考下2024-01-01Java中的Callable實(shí)現(xiàn)多線程詳解
這篇文章主要介紹了Java中的Callable實(shí)現(xiàn)多線程詳解,接口Callable中有一個(gè)call方法,其返回值類型為V,這是一個(gè)泛型,值得關(guān)注的是這個(gè)call方法有返回值,這意味著線程執(zhí)行完畢后可以將處理結(jié)果返回,需要的朋友可以參考下2023-08-08利用POI讀取word、Excel文件的最佳實(shí)踐教程
Apache POI 是用Java編寫的免費(fèi)開源的跨平臺(tái)的 Java API,Apache POI提供API給Java程式對(duì)Microsoft Office格式檔案讀和寫的功能。 下面這篇文章主要給大家介紹了關(guān)于利用POI讀取word、Excel文件的最佳實(shí)踐的相關(guān)資料,需要的朋友可以參考下。2017-11-11