Java實(shí)現(xiàn)簡單連連看游戲
本文實(shí)例為大家分享了Java實(shí)現(xiàn)連連看游戲的具體代碼,供大家參考,具體內(nèi)容如下
大二時(shí)做的Java課程設(shè)計(jì),拿了個(gè)優(yōu)秀,用了icon來模擬做了個(gè)簡單的連連看,代碼量不多,僅供參考。
課設(shè)要求是實(shí)現(xiàn)連連看最基本的功能,所以這里寫了個(gè)簡單的初始界面和經(jīng)典模式的一個(gè)界面。
初始界面
代碼如下:
public class PictureMatching { ? ? JButton classical = new JButton("經(jīng)典模式"); ? ? JButton about = new JButton("關(guān)于游戲"); ? ? JButton exit= new JButton("退出游戲"); ? ? JFrame menus = new JFrame("連連看"); ? ? public PictureMatching(){ ? ? ? ? ?menus.setLayout(new FlowLayout(FlowLayout.CENTER,40,40));//布局 ? ? ? ? ?JLabel label = new JLabel("連連看"); ? ? ? ? ? //設(shè)置字體 ? ? ? ? ?Font font = new Font("黑體",Font.PLAIN,26); ? ? ? ? ? ? label.setFont(font); ? ? ? ? ? ? ? ? ? ? classical.setFont(font);? ? ? ? ? ? ? about.setFont(font); ? ? ? ? ? ? exit.setFont(font); ? ? ? ? ? ?//組件放入容器中 ? ? ? ? ? ? menus.add(label); ? ? ? ? ? ? menus.add(classical); ? ? ? ? ? ? menus.add(about); ? ? ? ? ? ? menus.add(exit); ? ? ? ? ? ? ? Buttonlistener listener = new Buttonlistener(); ? ? ? ? ? ? classical.addActionListener(listener); ? ? ? ? ? ? about.addActionListener(listener); ? ? ? ? ? ? exit.addActionListener(listener); ? ? ? ? ? ? menus.setSize(300, 450); ? ? ? ? ? ? menus.setLocationRelativeTo(null); ? ? ? ? ? ? menus.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ? ? ? ? ? ? menus.setVisible(true); ? ? } ? ? public class Buttonlistener implements ActionListener{//初始界面的監(jiān)聽器 ? ? ? ? @Override ? ? ? ? public void actionPerformed(ActionEvent e) { ? ? ? ? ? ? if((JButton)e.getSource() == classical){ ? ? ? ? ? ? ? ? new Classical(); ? ? ? ? ? ? } ? ? ? ? ? ? else if ((JButton)e.getSource() == about) { ? ? ? ? ? ? ? ? new About(); ? ? ? ? ? ? } ? ? ? ? ? ? else if((JButton)e.getSource() == exit) ? ? ? ? ? ? ? ? System.exit(0); ? ? ? ? } ? ? } ? ?//主函數(shù) ? ? public static void main(String args[]){ ? ? ? ? try{ ? ? ? ? ? ? UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); ? ? ? ? }catch (Exception e) {} ? ? ? ? new PictureMatching(); ? ? } }
“關(guān)于游戲"界面可以寫一些信息,這里不多贅述。
經(jīng)典模式的界面如下:
代碼如下:
class Classical extends JFrame implements ActionListener{ ?? ?Piture piture[][]=new Piture[12][12]; ?? ?Check check;JLabel label,time,score,rule,prop,win,lose; ?? ?Time timer; ?? ?CardLayout card = new CardLayout(); ?? ?JPanel panel = new JPanel(card); ?? ?JPanel addpanel1 = new JPanel();//按鈕面板 ?? ?JPanel addpanel2 = new JPanel();//暫停面板 ?? ?JPanel addpanel3 = new JPanel();//win面板 ?? ?JPanel addpanel4 = new JPanel();//lose面板 ?? ?private int s = 0;//分?jǐn)?shù)? ?? ?private int best = 0; //最高分 ?? ?JButton tip,reform,exit,stop,restart; ?? ?int t = 3,r = 1;//提示次數(shù),重置次數(shù) ?? ?Path path = Paths.get("D://課程設(shè)計(jì)//最高分.txt"); ?? ?public Classical(){ ?? ??? ?setTitle("經(jīng)典模式"); ?? ??? ?setLayout(null); ?? ??? ?label = new JLabel("經(jīng)典模式"); ?? ??? ?Font font = new Font("黑體", Font.PLAIN, 40); ?? ??? ?label.setFont(font); ?? ??? ?tip = new JButton("提示X3"); ?? ??? ?reform = new JButton("重置X1"); ?? ??? ?exit = new JButton("返回"); ?? ??? ?stop = new JButton("暫停"); ?? ??? ?restart = new JButton("重新開始"); ?? ??? ?time = new JLabel(); ?? ??? ?Font song = new Font("宋體", Font.PLAIN, 24); ?? ??? ?time.setFont(song);?? ? ?? ??? ?score = new JLabel("分?jǐn)?shù):"+s); ?? ??? ?score.setFont(song); ?? ??? ?add(label);add(tip);add(reform);add(exit);add(stop);add(time);add(restart);add(score);add(panel); ?? ??? ?addpanel1.setLayout(null); ?? ??? ?addpanel2.setLayout(null); ?? ??? ?addpanel3.setLayout(null); ?? ??? ?addpanel4.setLayout(null); ?? ??? ?label.setBounds(410,50,200, 50); ?? ??? ?tip.setBounds(300,125,80,50); ?? ??? ?reform.setBounds(400,125,80,50); ?? ??? ?exit.setBounds(500,125,80,50); ?? ??? ?restart.setBounds(600,125, 100, 50); ?? ??? ?stop.setBounds(150,125,80,50); ?? ??? ?time.setBounds(80,70,250,50); ?? ??? ?score.setBounds(800,125,250,50); ?? ??? ?panel.setBounds(100, 210, 900,770); ?? ??? ?try(OutputStream output = Files.newOutputStream(path, StandardOpenOption.CREATE)){//讀取最高分 ?? ??? ??? ?java.io.InputStream input = Files.newInputStream(path,StandardOpenOption.READ); ?? ??? ??? ?DataInputStream in = new DataInputStream(new BufferedInputStream(input));? ?? ??? ??? ?best = in.readInt(); ?? ??? ?} catch (IOException e) { ?? ??? ?} ?? ??? ?tip.addActionListener(this); ?? ??? ?reform.addActionListener(this); ?? ??? ?exit.addActionListener(this); ?? ??? ?stop.addActionListener(this); ?? ??? ?restart.addActionListener(this); ?? ??? ?//初始化所有的Piture類對(duì)象 ?? ? ? ?for(int i = 0;i<12;i++){ ?? ? ? ??? ?for(int j = 0;j<12;j++){ ?? ? ? ??? ??? ?piture[i][j] = new Piture(); ?? ? ? ??? ??? ?piture[i][j].setX(i); ?? ? ? ??? ??? ?piture[i][j].setY(j); ?? ??? ??? ??? ?if(i>=1&&i<=10&&j>=1&&j<=10) ?? ??? ??? ??? ??? ?piture[i][j].setK(true); ?? ??? ??? ?} ?? ??? ? } ?? ? ? ?ImageIcon icons[] = new ImageIcon[28]; ?? ? ? ?for(int q = 0;q<28;q++){ ?? ? ? ??? ?icons[q] = new ImageIcon("D://課程設(shè)計(jì)//圖標(biāo)//圖標(biāo)//"+(q+1)+".png");//圖標(biāo)路徑 ?? ? ? ?} ?? ? ? ?//用循環(huán)將按鈕賦予圖標(biāo) ?? ? ? ?for(int i = 1; i < 11;i++){ ?? ??? ??? ?for(int j = 1;j<11;j+=2){ ?? ??? ??? ??? ?int l = (int) (Math.random()*28); ?? ??? ??? ??? ?piture[i][j].setBtn(new JButton(icons[l])); ?? ??? ??? ??? ?piture[i][j+1].setBtn(new JButton(icons[l])); ?? ??? ??? ?} ?? ??? ?} ?? ? ? ?check = new Check(); ?? ? ? ?Reform(); ?? ??? ?buttonclick listener = new buttonclick(); ?? ??? ?//用循環(huán)將按鈕裝上監(jiān)聽器 ?? ??? ?for(int i = 1; i < 11;i++){ ?? ??? ??? ?for(int j = 1;j<11;j++){ ?? ??? ??? ??? ?piture[i][j].getBtn().addActionListener(listener); ?? ??? ??? ??? ?addpanel1.add(piture[i][j].getBtn()); ?? ??? ??? ??? ?piture[i][j].getBtn().setBounds(80*(j-1),70*(i-1),80,70); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?rule = new JLabel("<html>規(guī)則介紹:<br><font>連連看是一個(gè)把相同兩張牌連線后<br><font>消除的益智游戲,游戲時(shí)請(qǐng)注意,<br></font>兩張牌間連線的拐點(diǎn)不能超過兩個(gè)。</font></html> prop = new JLabel("<html>道具介紹:<br><font>提示:自動(dòng)消除一對(duì)相同的卡牌<br><font>重置:用于重新洗牌的道具"); ?? ??? ?win = new JLabel(); ?? ??? ?lose = new JLabel(); ?? ??? ?rule.setFont(song); ?? ??? ?prop.setFont(song); ?? ??? ?win.setFont(song);win.setBounds(350,200, 200, 200); ?? ??? ?lose.setFont(song);lose.setBounds(350,200, 200, 200); ?? ??? ?addpanel2.setLayout(new FlowLayout(FlowLayout.CENTER,20,20)); ?? ??? ?addpanel2.add(rule);addpanel2.add(prop); ?? ??? ?addpanel3.add(win); ?? ??? ?addpanel4.add(lose); ?? ??? ?panel.add(addpanel1,"p1");panel.add(addpanel2,"p2");panel.add(addpanel3,"p3");panel.add(addpanel4, "p4"); ?? ??? ?setSize(1000,1000); ?? ??? ?setResizable(false); ?? ??? ?setLocationRelativeTo(null); ?? ??? ?setVisible(true);timer =new Time(); ?? ?} ?? ?//時(shí)間類 ?? ?class Time { ?? ??? ?private int minute = 0; ?? ??? ?private int second = 0; ?? ??? ?private int totalSeconds; ?? ??? ?Timer t=new Timer(); ?? ??? ?TimerTask task; ?? ??? ?private boolean Run = true; ?? ??? ?private boolean minuteNotAlready = false; ?? ??? ?private boolean secondNotAlready = false; ?? ??? ?public Time(){ ?? ??? ??? ?totalSeconds = 60 * 3;?? ??? ??? ? ?? ??? ??? ?initData(totalSeconds); ?? ??? ??? ?t.schedule(task = new TimerTask() { ?? ??? ??? ??? ?public void run() { ?? ??? ??? ??? ??? ?if(Run){ ?? ??? ??? ??? ??? ??? ?if (secondNotAlready) { ?? ??? ??? ??? ??? ??? ??? ?startCount(); ?? ??? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ??? ?cancel(); ?? ??? ??? ??? ??? ??? ??? ?best = best>s?best:s; ?? ??? ??? ??? ??? ??? ??? ?lose.setText("<html>You are lose!<br><font>分?jǐn)?shù):"+s+"<br><font>最高分:"+best); ?? ??? ??? ??? ??? ??? ??? ?card.show(panel, "p4"); ?? ??? ??? ??? ??? ??? ??? ?try(OutputStream output = Files.newOutputStream(path, StandardOpenOption.CREATE); ?? ??? ??? ? ? ? ? ? ? ? ? ??? ??? ?DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(output))){ ?? ??? ??? ? ? ? ? ? ? ? ? ??? ?dataOutputStream.writeInt(best); ?? ??? ??? ? ? ? ? ? ? ? ? ?}catch (Exception e3) { ?? ??? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ?}, 0, 1000); ?? ??? ?} ?? ??? ?//初始化賦值 ?? ??? ?private void initData(int totalSeconds) { ?? ??? ??? ?minute = 0; ?? ??? ??? ?second = 0; ?? ??? ??? ?minuteNotAlready = false; ?? ??? ??? ?secondNotAlready = false; ?? ??? ??? ?if (totalSeconds > 0) { ?? ??? ??? ??? ?secondNotAlready = true; ?? ??? ??? ??? ?second = totalSeconds; ?? ??? ??? ??? ?if (second >= 60) { ?? ??? ??? ??? ??? ?minuteNotAlready = true; ?? ??? ??? ??? ??? ?minute = second / 60; ?? ??? ??? ??? ??? ?second = second % 60; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?time.setText("剩余時(shí)間:"+minute+"分鐘"+second+"秒"); ?? ??? ?}? ?? ??? ?//計(jì)算各個(gè)值的變動(dòng) ?? ??? ?public void startCount() { ?? ??? ??? ?if (secondNotAlready) { ?? ??? ??? ??? ?if (second > 0) { ?? ??? ??? ??? ??? ?second--; ?? ??? ??? ??? ??? ?if (second == 0 && !minuteNotAlready) { ?? ??? ??? ??? ??? ??? ?secondNotAlready = false; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ?if (minuteNotAlready) { ?? ??? ??? ??? ??? ??? ?if (minute > 0) { ?? ??? ??? ??? ??? ??? ??? ?minute--; ?? ??? ??? ??? ??? ??? ??? ?second = 59; ?? ??? ??? ??? ??? ??? ??? ?if (minute == 0) { ?? ??? ??? ??? ??? ??? ??? ??? ?minuteNotAlready = false; ?? ??? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?time.setText("剩余時(shí)間:"+minute+"分鐘"+second+"秒"); ?? ??? ?} ?? ?? ?? ?} ?? ?@Override ?? ?public void actionPerformed(ActionEvent e) { ?? ??? ?if((JButton)e.getSource() == tip){ ?? ??? ??? ?if(t>0){ ?? ??? ??? ??? ?Tip(); ?? ??? ??? ??? ?t--; ?? ??? ??? ??? ?tip.setText("提示X"+ t ); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?else if((JButton)e.getSource() == reform){ ?? ??? ??? ?if(r>0){ ?? ??? ??? ??? ?Reform(); ?? ??? ??? ??? ?r--; ?? ??? ??? ??? ?reform.setText("重置X"+r); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?else if((JButton)e.getSource() == stop){ ?? ??? ??? ??? ?if(stop.getText().equals("暫停")){ ?? ??? ??? ??? ??? ?timer.Run = false; ?? ??? ??? ??? ??? ?stop.setText("開始"); ?? ??? ??? ??? ??? ?card.show(panel, "p2");//顯示暫停面板,即游戲規(guī)則 ?? ??? ??? ??? ??? ?return; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?else if (stop.getText().equals("開始")) { ?? ??? ??? ??? ??? ?timer.Run = true; ?? ??? ??? ??? ??? ?stop.setText("暫停"); ?? ??? ??? ??? ??? ?card.show(panel, "p1"); ?? ??? ??? ??? ??? ?return; ?? ??? ??? ??? ?} ?? ??? ?} ?? ??? ?else if((JButton)e.getSource() == exit){ ?? ??? ??? ?setVisible(false); ?? ??? ??? ?} ?? ??? ?else if((JButton)e.getSource() == restart){ ?? ??? ??? ?setVisible(false); ?? ??? ??? ?Classical classical = new Classical(); ?? ??? ?} ?? ?} ?? ?//圖案的匹配消除監(jiān)聽器 ?? ?public class buttonclick implements ActionListener{ ?? ??? ?int x1,y1,x2,y2;//分別表示第一個(gè)卡牌和第二個(gè)卡牌的坐標(biāo) ?? ??? ?public buttonclick () { ?? ??? ??? ?x1 = -1;//初始化 ?? ??? ??? ?x2 = -1; ?? ??? ??? ?y1 = -1; ?? ??? ??? ?y2 = -1; ?? ??? ?} ?? ??? ?@Override ?? ??? ?public void actionPerformed(ActionEvent e) { ?? ??? ??? ?if(x1 == -1){//如果第一個(gè)卡牌的坐標(biāo)為初始值,將此時(shí)點(diǎn)擊的按鈕的內(nèi)容賦予第一個(gè)卡牌 ?? ??? ??? ??? ?for(int i = 1; i < 11;i++){ ?? ??? ??? ??? ??? ?for(int j = 1;j<11;j++){ ? ?? ?? ??? ??? ??? ??? ??? ?if((JButton)e.getSource()==piture[i][j].getBtn()){ ?? ??? ??? ??? ??? ??? ??? ?x1 = i;y1 = j;//將卡牌坐標(biāo)賦予第一個(gè)卡牌 ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?else{//如果第一個(gè)卡牌的坐標(biāo)不為初始值,將此時(shí)點(diǎn)擊的按鈕的內(nèi)容賦予第er個(gè)卡牌 ?? ??? ??? ??? ?for(int i = 1; i < 11;i++){ ?? ??? ??? ??? ??? ?for(int j = 1;j<11;j++){ ?? ??? ??? ??? ??? ??? ?if((JButton)e.getSource()==piture[i][j].getBtn()){ ?? ??? ??? ??? ??? ??? ??? ?if(x1!=i||y1!=j){ ?? ??? ??? ??? ??? ??? ??? ??? ?x2 = i;y2 = j;//將 ?? ??? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ??? ?if(x1 != -1&&x2 != -1){//當(dāng)兩個(gè)卡牌的值都不為初始值 ?? ??? ??? ??? ?if(piture[x1][y1].getBtn().getIcon() == piture[x2][y2].getBtn().getIcon()){//比較兩個(gè)按鈕的圖標(biāo) ?? ??? ??? ??? ??? ?boolean w = check.isMatch(new Point(x1, y1),new Point(x2,y2));//比較是否匹配 ?? ??? ??? ??? ??? ?if(w){ ?? ??? ??? ??? ??? ??? ?addpanel1.remove(piture[x1][y1].getBtn()); ?? ??? ??? ??? ??? ??? ?piture[x1][y1].setK(false); ?? ??? ??? ??? ??? ??? ?addpanel1.remove(piture[x2][y2].getBtn()); ?? ??? ??? ??? ??? ??? ?piture[x2][y2].setK(false); ?? ??? ??? ??? ??? ??? ?x1 = -1;y1 = -1; ?? ??? ??? ??? ??? ??? ?x2 = -1;y2 = -1; ?? ??? ??? ??? ??? ??? ?s = s + 200; ?? ??? ??? ??? ??? ??? ?score.setText("分?jǐn)?shù):"+s); ?? ??? ??? ??? ??? ??? ?setVisible(false); ?? ??? ??? ??? ??? ??? ?setVisible(true); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ??? ?x1 = x2; ?? ??? ??? ??? ?y1 = y2; ?? ??? ??? ??? ?x2 = -1; ?? ??? ??? ??? ?y2 = -1; ?? ??? ??? ?} ?? ??? ??? ?try { ?? ??? ??? ??? ?if(isReform()) ?? ??? ??? ??? ? ? ?Reform(); ?? ??? ??? ?} catch (Exception e2) { ?? ??? ??? ??? ?timer.Run = false; ?? ??? ??? ??? ?s = s + (timer.minute*60+timer.second)*100; ? ? ? ? ? ? ? ? best = best>s?best:s; ? ? ? ? ? ? ? ? win.setText("<html>You are win!<br><font>分?jǐn)?shù):"+s+"<br><font>最高分:"+best); ? ? ? ? ? ? ? ? card.show(panel, "p3");//顯示win面板 ? ? ? ? ? ? ? ? //將最高分寫入文件 ? ? ? ? ? ? ? ? try(OutputStream output = Files.newOutputStream(path, StandardOpenOption.CREATE); ? ? ? ? ? ? ? ? ?? ??? ?DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(output))){ ? ? ? ? ? ? ? ? ?? ?dataOutputStream.writeInt(best); ? ? ? ? ? ? ? ? }catch (Exception e3) { ?? ??? ??? ??? ?} ? ? ? ? ? ? ? ?? ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ?} ?? ?} ?? ?//提示功能 ?? ?public void Tip(){ ?? ??? ?int p=0,p1 = 0; ?? ??? ?for(int i = 0;i<12;i++){ ?? ??? ??? ?for(int j = 0;j<12;j++){ ?? ??? ??? ??? ?if(piture[i][j].isK()){ ?? ??? ??? ??? ??? ?p++; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ??? ?Piture pit[] = new Piture[p]; ?? ??? ?for(int i = 1;i<12;i++){ ?? ??? ??? ?for(int j = 1;j<12;j++){ ?? ??? ??? ??? ?if(piture[i][j].isK()){ ?? ??? ??? ??? ??? ?pit[p1] = piture[i][j]; ?? ??? ??? ??? ??? ?p1++; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ??? ?//檢測匹配的卡牌,消除找到的第一對(duì)卡牌 ?? ??? ?for(int m = 0;m<pit.length-1;m++){ ?? ??? ??? ?for(int n = m+1;n<pit.length;n++){ ?? ??? ??? ??? ?if(pit[m].getBtn().getIcon() == pit[n].getBtn().getIcon()) ?? ??? ??? ??? ??? ?if(check.isMatch(new Point(pit[m].getX(),pit[m].getY()), new Point(pit[n].getX(),pit[n].getY()))){ ?? ??? ??? ??? ??? ??? ?addpanel1.remove(pit[m].getBtn()); ?? ??? ??? ??? ??? ??? ?pit[m].setK(false); ?? ??? ??? ??? ??? ??? ?addpanel1.remove(pit[n].getBtn()); ?? ??? ??? ??? ??? ??? ?pit[n].setK(false); ?? ??? ??? ??? ??? ??? ?s = s + 200; ?? ??? ??? ??? ??? ??? ?score.setText("分?jǐn)?shù):"+s); ?? ??? ??? ??? ??? ??? ?setVisible(false);?? ??? ??? ? ?? ??? ??? ? ? ? ? ? ? ?setVisible(true); ?? ??? ??? ??? ??? ??? ?return; ?? ??? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ??? ? ?? ?} ?? ?//重置功能 ?? ?public void Reform() { ?? ??? ?int p=0,p1 = 0; ?? ??? ?//將有圖案的放入一個(gè)數(shù)組中 ?? ??? ?for(int i = 0;i<12;i++){ ?? ??? ??? ?for(int j = 0;j<12;j++){ ?? ??? ??? ??? ?if(piture[i][j].isK()){ ?? ??? ??? ??? ??? ?p++; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ??? ?Piture pit[] = new Piture[p]; ?? ??? ?for(int i = 1;i<12;i++){ ?? ??? ??? ?for(int j = 1;j<12;j++){ ?? ??? ??? ??? ?if(piture[i][j].isK()){ ?? ??? ??? ??? ??? ?pit[p1] = piture[i][j]; ?? ??? ??? ??? ??? ?p1++; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ??? ?//將圖案進(jìn)行打亂 ?? ??? ?for(int k = 0;k<=pit.length/2;k++){ ?? ??? ??? ?int l = (int)(Math.random()*pit.length); ?? ??? ??? ?Piture t = new Piture(); ?? ??? ??? ?t.getBtn().setIcon(pit[k].getBtn().getIcon());? ?? ??? ??? ?pit[k].getBtn().setIcon(pit[l].getBtn().getIcon()); ?? ??? ??? ?pit[l].getBtn().setIcon(t.getBtn().getIcon()); ?? ??? ?} ?? ??? ?setVisible(false); ?? ??? ?setVisible(true); ?? ??? ?try { ?? ??? ??? ?if(isReform()) ?? ??? ??? ?Reform(); ?? ??? ?} catch (Exception e) { ?? ??? ?} ?? ??? ? ?? ?} ?? ?//檢測是否需要重置 ?? ?public boolean isReform(){ ?? ??? ?boolean is = true; ?? ??? ?int p=0,p1 = 0; ?? ??? ?//將有圖案的放入一個(gè)數(shù)組中 ?? ??? ?for(int i = 0;i<12;i++){ ?? ??? ??? ?for(int j = 0;j<12;j++){ ?? ??? ??? ??? ?if(piture[i][j].isK()){ ?? ??? ??? ??? ??? ?p++; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ??? ?Piture pit[] = new Piture[p]; ?? ??? ?for(int k = 1;k<12;k++){ ?? ??? ??? ?for(int l = 1;l<12;l++){ ?? ??? ??? ??? ?if(piture[k][l].isK()){ ?? ??? ??? ??? ??? ?pit[p1] = piture[k][l]; ?? ??? ??? ??? ??? ?p1++; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ??? ?for(int m = 0;m<pit.length-1;m++) ?? ??? ??? ?for(int n =m+1;n<pit.length;n++) ?? ??? ??? ??? ?if(check.isMatch(new Point(pit[m].getX(),pit[m].getY()), new Point(pit[n].getX(),pit[n].getY()))) ?? ??? ??? ??? ??? ?is = false; ?? ??? ?return is; ?? ?} ?? ?class Check{ ?? ??? ?public boolean isMatch(Point a,Point b){?? ?//檢測是否匹配 ?? ??? ??? ?boolean b1,b2,b3; ?? ??? ??? ?b3 = this.noCorner(a,b); ?? ??? ??? ?b1 = this.oneCorner(a, b); ?? ??? ??? ?b2 = this.twoCorner(a, b); ?? ??? ??? ?if(b1||b2||b3) ?? ??? ??? ??? ?return true; ?? ??? ??? ?else ?? ??? ??? ??? ?return false; ?? ??? ?} ?? ??? ?boolean horizonMatch(Point a,Point b){//橫線上的掃描 ?? ??? ??? ?int i,j; ?? ??? ??? ?i=a.y>b.y?a.y:b.y; ?? ??? ??? ?j=a.y>b.y?b.y:a.y; ?? ??? ??? ?boolean hor = true; ?? ??? ??? ?for(int y = j+1;y<i;y++){ ?? ??? ??? ??? ?if(piture[a.x][y].isK()){ ?? ??? ??? ??? ??? ?hor = false;break; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?return hor; ?? ??? ?} ?? ??? ?boolean verticalMatch(Point a,Point b){//豎線上的掃描 ?? ??? ??? ?int i,j; ?? ??? ??? ?i=a.x>b.x?a.x:b.x; ?? ??? ??? ?j=a.x>b.x?b.x:a.x; ?? ??? ??? ?boolean ver = true; ?? ??? ??? ?for(int x = j+1;x<i;x++){ ?? ??? ??? ??? ?if(piture[x][a.y].isK()){ ?? ??? ??? ??? ??? ?ver = false;break;} ?? ??? ??? ?} ?? ??? ??? ?return ver; ?? ??? ?} ?? ??? ?boolean noCorner(Point a,Point b){ ?? ??? ??? ?if(a.x == b.x){ ?? ??? ??? ??? ?if(this.horizonMatch(a, b)){ ?? ??? ??? ??? ??? ?return true; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?else if(a.y == b.y){ ?? ??? ??? ??? ?if(this.verticalMatch(a, b)){ ?? ??? ??? ??? ??? ?return true; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?return false; ?? ??? ?} ?? ??? ?boolean oneCorner(Point a,Point b){ //一個(gè)拐點(diǎn) ?? ??? ??? ?Point c,d; ?? ??? ??? ?boolean isMatch; ?? ??? ??? ?c = new Point(a.x,b.y); ?? ??? ??? ?d = new Point(b.x,a.y); ?? ??? ??? ?if(piture[c.x][c.y].isK() == false){ ?? ??? ??? ??? ?isMatch = horizonMatch(a,c)&&verticalMatch(b,c);? ?? ??? ??? ??? ?if(isMatch){ ?? ??? ??? ??? ??? ?return isMatch; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?if(piture[d.x][d.y].isK() == false){ ?? ??? ??? ??? ?return isMatch = horizonMatch(b,d)&&verticalMatch(a,d); ?? ??? ??? ?} ?? ??? ??? ?else ?? ??? ??? ??? ?return false; ?? ??? ?} ?? ??? ?boolean twoCorner(Point a,Point b){//兩個(gè)拐點(diǎn) ?? ??? ??? ?boolean v = false; ?? ??? ??? ?//掃描a點(diǎn)左邊的所有線 ?? ??? ??? ?for(int y = a.y-1;y>=0;y--){ ?? ??? ??? ??? ?if((!piture[a.x][y].isK())&&(!piture[b.x][y].isK())&&horizonMatch(a, new Point(a.x, y))&&horizonMatch(b, new Point(b.x, y))&&verticalMatch(new Point(a.x,y),new Point(b.x,y))){ ?? ??? ??? ??? ??? ?v = true;break; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?//掃描a點(diǎn)右邊的所有線 ?? ??? ??? ?for(int y = a.y+1;y<12;y++){ ?? ??? ??? ??? ?if((!piture[a.x][y].isK())&&(!piture[b.x][y].isK())&&horizonMatch(a, new Point(a.x, y))&&horizonMatch(b, new Point(b.x, y))&&verticalMatch(new Point(a.x,y),new Point(b.x,y))){ ?? ??? ??? ??? ??? ??? ??? ??? ?v = true;break; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?//掃描a點(diǎn)上面的所有線 ?? ??? ??? ?for(int x = a.x-1;x>=0;x--){ ?? ??? ??? ??? ?if((!piture[x][a.y].isK())&&(!piture[x][b.y].isK())&&verticalMatch(a, new Point(x, a.y))&&verticalMatch(b,new Point(x, b.y))&&horizonMatch(new Point(x,a.y),new Point(x,b.y))){ ?? ??? ??? ??? ??? ??? ??? ?v = true;break; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?//掃描a點(diǎn)下面的所有線 ?? ??? ??? ?for(int x = a.x+1;x<12;x++){ ?? ??? ??? ??? ?if(!piture[x][a.y].isK()&&!piture[x][b.y].isK()&&verticalMatch(a, new Point(x, a.y))&&verticalMatch(b,new Point(x, b.y))&&horizonMatch(new Point(x,a.y),new Point(x,b.y))){ ?? ??? ??? ??? ??? ?v = true;break; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?return v; ?? ??? ?} ?? ??? ? ?? ?} }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java 使用Axis調(diào)用WebService的示例代碼
這篇文章主要介紹了Java 使用Axis調(diào)用WebService的示例代碼,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-09-09HttpClient 請(qǐng)求 URL字符集轉(zhuǎn)碼問題
這篇文章主要介紹了HttpClient 請(qǐng)求 URL字符集轉(zhuǎn)碼問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Java報(bào)錯(cuò)Non-terminating?decimal?expansion解決分析
這篇文章主要為大家介紹了Java報(bào)錯(cuò)Non-terminating?decimal?expansion解決方案及原理分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09淺析Java 數(shù)據(jù)結(jié)構(gòu)常用接口與類
本篇文章主要介紹了Java中的數(shù)據(jù)結(jié)構(gòu),Java工具包提供了強(qiáng)大的數(shù)據(jù)結(jié)構(gòu)。需要的朋友可以參考下2017-04-04Java實(shí)現(xiàn)一個(gè)簡單計(jì)算器
這篇文章主要介紹了Java實(shí)現(xiàn)一個(gè)簡單計(jì)算器,文章我圍繞實(shí)現(xiàn)簡單計(jì)算器的相關(guān)代碼展現(xiàn)全文,具有一定的參考價(jià)值,需要的小伙伴可以參考一下,2022-01-01