連連看java程序-論文_第1頁
連連看java程序-論文_第2頁
連連看java程序-論文_第3頁
連連看java程序-論文_第4頁
連連看java程序-論文_第5頁
已閱讀5頁,還剩87頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、連連看j ava程序-論文連連看j ava程序package kyodai.map;import java.awtimport javax.swing.*;import kyodai/*消除連連看方塊的類*/public class animatedelete implements runnable static jbutton dots;static long delay 二 201;int array二new int44; /最大距離只可能為2行1列private int count = 0;private volatile thread thread;public animatedele

2、te(jbutton dots) this.dots = dots;array 二 new int0;/*初始化*/public animatedelete( int di red, point a, point b) ini tarray();calctwopoint(di rect, a, b);start();/* direct 方向* 1表示a, b在同一直線上,b, c在同一豎線上;* 0表示a, b在同一豎線上,b, c在同一直線上*/public animatedelete(int di rect, point a, point b, point c) ini tarray();

3、if (direct = 1) /先橫后豎calctwopoint(1, a, b);calctwopoint(0, b, c);else calctwopoint(0, a, b);count-;calctwopoint(1, b, c);start();/* direct 方向* 1表示a, b為橫線,b, c為豎線,c, d為橫線* 0表示d, b為豎線,b, c為橫線,c, d為豎線*/public animatedelete(int direct, point a, point b, point c, point d) ini tarray();i f (di rect = 1) /

4、橫、豎、橫方式處理calctwopoint(1, a, b);count;calctwopoint(0, b, c);count-;calctwopoint(1, c, d);else /豎、橫、豎方式處理calctwopoint(0, a, b);count-;calctwopoint(1, b, c);count-;calctwopoint(0, c, d);start();/*計(jì)算消除的兩點(diǎn)*/private void calctwopoint(int di rect, point a, point b) int offset = 0;if (direct = 1) /橫向連接if (a

5、.y > b.y) /a點(diǎn)向b點(diǎn)是從右向左在水平線上消除for (int y = a.y; y >= b.y; y-) offset 二 a.x * setting.column + y;arraycount二 offset;count+;else /a點(diǎn)向b點(diǎn)是從左向右在水平線上消除for (int y 二 a.y; y <二 b.y; y+) offset 二 a.x * setting.collft'in + y;ar raycount二 offset;count-h-;else /豎向連接if (a.x > b.x) /a點(diǎn)向b點(diǎn)是從下向上垂直消除for

6、 (int x = a.x; x >= b.x; x-) offset 二 x * setting.column + a.y;arraycount二 offset;count;else /a點(diǎn)向b點(diǎn)是從上向下垂直消除for (int x 二 a.x; x <二 b.x; x+) offset = x * setting.column + a.y;ar raycount = offset;count;/*設(shè)置動(dòng)畫速度*/public void setspeed(int speed) delay 二 speed * 10;private void initarray() i f (ar

7、ray = nul1 i i array.length = 0) return;public void test() if (array = nul1 i i array.length = 0) return;for (int i 二 0; i < array, length; i-h-) if (arrayi != -1) message(” + arrayi + "");system.out.println();public void start() thread = new thread(this);thread start();public void run(

8、) if (count < 2) return;thread currentthread 二 thread.currentthread();boolean animate 二 true;while (thread = currentthread && animate) for (int i = 1; i < count 1; i+) dotsarrayi.setenabled( t rue);dotsarrayi.seticon(kyodai.guideicon); try thread.sleep(delay);catch (interruptedexceptio

9、n ex) for (int i = 1; i < count 1; i+) dotsarrayi.setlcon(nul1);dotsarrayi.setenabled(false);try thread.sleep(delay);catch (interruptedexception ex) dotsarray0 seticon(nul1);dotsarray0.setenabled(false);dotsarraycount - 1setlcon(nul1);dotsarraycount - 1.setenabled(false);animate 二 false;stop();pu

10、blic void stop() i f (thread !二 nul1) thread = nul1;void message(string str) system.out.println(str);151連連看j ava程序package kyodai.map;import java.awt.point;/*定義直線的類*/public class linepublic point a;public point b;public int di rect;public line()/*通過兩點(diǎn)和方向構(gòu)造直線*/public line(int direct, point a, point b)

11、this.di rect = di rect;this.a = a;this.b 二 b;package kyodai.map;import java.awt.point;import java.util.random;import java.util.vector;/*生成連連看方塊的類*/public class mapprivate int level;private int map;int array;private int restblock;private vector vector;animatcdclctc animate;private boolean test;public

12、 map()level = 28;map = new int1017;array 二 new int170;rcstblock = level * 4; vector = new vector();test 二 false;ini tmap();public map(int level)this.level 二 28;map 二 new int1017;array 二 new int170;reslblock = this.level * 4;vector 二 new vector();test = false;this level 二 level;restblock 二 level * 4;

13、ini tmap();public voidsettest(boolean test)this.test 二二 test;public void setlevel(int level)this.1cvcl = level;restblock = level * 4;ini tmap();private void initmap()for( int i 二 0; i < level; i-h-)arrayi*4 = i+1;arrayi*4+1二i+1;arrayi*4+2二i+1;arrayi*4+3二i+1;random(array);for( int i 二 0; i < 10

14、; i4"+)for(int j = 0; j < 17; j-h-)mapij = ar rayi * 17 + j;private void random(int array)random random 二 new random();for(int i = array.length; i > 0; i-)int j = random.nextlnt(i);int temp 二 arrayj; arrayj二 arrayi 1 ;ar rayi - 1 = temp;public void earse(point a, point b)mapa.xa.y = 0;map

15、b.xb.y = 0;restblock 二 2;public int getcount()return restblock <= 0 ? 0 : restblock;public void rcfrcsh()int count 二 getcount();if(count <二 0)int tcmp二 new intcount;count = 0;for(int row 二 0; row < 10; row+) for(int col = 0; col < 17; col-h-)i f(maprowcol > 0)tempcount二 maprowcol; cou

16、nt+;random(temp);count 二 0;for(int row 二 0; row < 10; row+)for(int col 二 0; col < 17; col+)if(maprowcol > 0)maprowcol = tempcount; countprivate boolean horizon(point a, point b, boolean recorder)i f(a.x = b.x && a.y = b.y)return false;int x_start = a.y <= b.y ? a.y : b.y;int x_en

17、d = a.y <= b.y? b.y : a.y;for( int x 二 x_start 4- 1; x < x_end; x+) if(mapa.xx != 0)return false;if(!test && recorder)animate = new animatedelete( 1, a, b);return true;private boolean vertical(point a, point b, boolean recorder)if(a.x = b.x && a.y = b.y)return false;int y_start

18、 二 a.x <= b.x ? a.x : b.x;int y_end 二 a.x v二 b.x? b.x : a.x;for( int y 二 y_start + 1; y < y_end; y卄)if(mapya.y != 0)if(!test && recorder)animate = new animatedelete(o, a, b); return true;private boolean onecorner(point a, point b)point c = new point(a.x, b.y);point d 二 new point(b.x, a

19、.y);if(mapc.xc.y = 0)boolean method1 = horizon(a, c, false) && vertical(b, c, false);i f(methodl)i f(!test)animate 二 new animatedelete(1, a, c, b);return methodi;if(mapd.xd.y = 0)boolean method2 二 vertical(a, d, false) && horizon(b, d, false);if(method2 && !test)animate = new

20、 animatcdclctc(o, a, d, b);clscreturn false;private vector scan(point a, point b)vector v 二 new vector();point c 二 new point(a.x, b.y);point d 二 new point(b.x, a.y);for(int y 二 a.y; y 二 0; y-)if(mapa.xy = 0 && mapb.xy = 0 && vertical(new point(a.x, y), new point(b.x, y), false)v.add(

21、new line(0, new point(a.x, y), new point(b.x, y);for(int y = a.y; y < 17; y+)if(mapa.xy = 0 && mapb.xy = 0 && vert ical(ncw point(a.x, y), new point(b.x, y), false)v.add(new line(0, new point(ax, y), new point(b.x, y);for(int x 二 a.x; x 二 0; x-)i f(mapx a.y = 0 && mapx b.y

22、 = 0 && horizon (new point(x, a.y), new point(x, b.y), false)v.add(ncw linc(1, new point(x, a.y), new point(x, b.y);for(int x 二 a.x; x < 10; x+)if(mapxa.y = 0 && mapx b.y = 0 && horizon (new point(x, a.y),new point(x, b.y), false)v.add(new line(1, new point(x, a.y), new po

23、int(x, b.y);return v;private boolean twocorner(point a, point b)vector = scan(a, b);i f(vector.isemp(y()return false;for(int index 二 0; index < vector.size(); index+)line 1ine 二(line)vector.elementat(index);if(linc.direct = 1)if(vertical(a, 1ine.a, false) && vertical(b, 1ine.b, false)i f(

24、!test)animate = new animatedelete(o, a, 1ine.a, 1ine.b, b);return true;else if(horizon(a, 1ine.a, false) && horizon(b, 1ineb, false) if(!test)animate = new animatcdclctc(1, a, 1inc.a, 1inc.b, b);return true;return false;public boolean test(point a, point b)if(mapa.xa.y != mapb.xb.y)return fa

25、lse;if(a.x = b.x && horizon(a, b, true)return true;if(a.y = b.y && vertical(a, b, true)return true;if(onecorner(a, b)return true;elsereturn twocorner(a, b);public line findnext(point a)point b 二 new point();a = findfi rst(a);if(a.equals(new point(-1, -1)return new line(0, a, b);for(;

26、 !a.equals(new point(-1, -1); a = findfi rst(a)for(b 二 findsecond(a, b); !bequals(new point(-1, -1); b 二 findsecond(a, b)if(test(a, b)return new line(1, a, b);return new line(0, a, b);private point findfirst(point a)int offset = 0;if(a !二 nul1)offset 二 a.x * 17 + a.y;if(offset < 0)offset 二1;for(

27、int x = offset + 1; x < 170; x-h-)int row = math.round(x / 17);int col = x - row * 17;if(maprowcol != 0)return new point(row, col);(point a, point b)i f(a = nul1)return new point(-1, -1);if(a.x + a.y < 0)return new point(-1, -1);i f(b = nul1)b 二 new point(0, 0);int offset 二 math.max(a.x * 17 +

28、 a.y, b.x * 17 + b.y);for(int x 二 offset + 1; x < 170; x+)int row = math.round(x / 17);int col = x - row * 17;i f(maprowcol = mapa.xa.y)return new point(row, col);return new point(-1, -1);public int gctmap()return map;水晶連連看package kyodai.map;import javax.swingimport javadvtimport java.awf.event*;

29、import java.nei*;import kyodai/*生成圖形用戶界面*/public class mapui extends jpanel implements actionlistener, runnable private map map;private jbutton dots;private point lastpoint = new point(0, 0); /上一個(gè)點(diǎn)的坐標(biāo)private boolean isselected 二 false; /是否已經(jīng)選擇了一個(gè)點(diǎn)private int score = 0; /記錄用戶的得分private clockanimatc c

30、lockanimatc; /同步顯示時(shí)鐘/animatedelete animate; /動(dòng)畫jbutton gotoplo;private scoreanimate scoreanimate;int stepscore二0; /計(jì)算距離的分int limittime = 0; /限定尋找的時(shí)間(秒)private boolean isplaying = false; /當(dāng)前是否正在游戲中/*構(gòu)造函數(shù)*/public mapui(map map, jbutton dots) this.map = map;this.dots = dots;gridlayout gridlayout = new

31、gridlayout();this.setlayout(gridlayout);gridlayout sctrows(sct t ing.row);gridlayout.setcolumns(set t ing.column);gridlayout.sethgap(2);gridlayout.sctvgap(2);this.s e t layou t(g r i dlayou t);this setbackground(kyodai darkcolor);for (int row 二 0; row < setting.row; row+) for (int col 二 0; col &l

32、t; setting.cblumn; col-h-) int index 二 row * setting.column + col;dotsindexaddact ionlistener(this);this.add(dotsindex);/*設(shè)置地圖*/public void setmap(map map) this.map = map;*獲取主界面上的gotopl 0按鈕5以便操作*/public void scttoplobutton(jbutton gotoplo) thi s.gotoplo = gotoplo;*根據(jù)數(shù)組來繪置畫面*/private void paint() for

33、 (int row 二 0; row < setting.row; row+) for (int col 二 0; col < setting.collft'in; col+) int index 二 row * setting.column + col;i f (map.getmap()rowcol > 0) dotsindex.seticon(kyodai.blockslconmapgetmap()rowcol-u);dotsindex setenabled(t rue);else dotsindex.setlcon(nul1);dotsindex setenab

34、led(false);public void repaint(graphics g) paint();/*判斷當(dāng)前是否已經(jīng)沒有可消除的方塊*/private boolean validmap(point a) if (map.getcount() = 0) return true;line line = new line(0, new point(), new point(); map.settest(true); /只測試line = mapfindnext(a);int offset = 0;if (line.direct = 1) /找到了可消除的return true;else ret

35、urn false;*更新當(dāng)前顯示的分?jǐn)?shù)*/private void showscore(int 1, int c)if (scoreanimate = nul1) return;scoreanimate.setscore( 1, c);*刷新當(dāng)前的排列方式*/public void rcfrcsh() if (! isplaying) /不在游戲中,返回return;if (map.getcount() = 0) kyodai .showhint(m還刷,都沒了 ! ”);if (setting.sound = 1) new sound(sound.refresh);if (validmap

36、(new point( -1, -1) score -= settingfreshscore;showscore(score - 1, score);else showscore(score, score + settingfreshscore);score +二 settingfreshscore;score -= settingfreshscore;showscore(score - 1, score);map.refresh();paint();/*消除兩個(gè)點(diǎn)*/void earse(point a, point b) /paint();int offset;offset = a.x *

37、 setting.column + a.y;dotsoffset.setlcon(nul1);dotsfoffset setenabled(false);offset 二 b.x * setting.column + b.y;dotsoffset.setlcon(null);dotsoffsct.sctenablcd(false);/如果地圖清除完成,矢閉if (map.getcount() = 0) int rema intime 二 1imi ttime - clockanimate.getusedtime();mess age (” 剩余時(shí)間="+ remaintime);if

38、 (remaintime > 0) showscorc(score, score + rcmaintimc 自動(dòng)尋找最佳答案*/public void findnext(point a) if (! isplaying) /不在游戲中,返回 return;if (map.getcount() = 0) setting.timcscorc);score +二 rema intime * setting.timescore;isplaying = false;stop();kyodai. showhint ("時(shí)間 + " + rema intime * setting.

39、 timescore + ",想看 看你的排名嗎? ”);gotopl0.setenabled(t rue);else / / test 1(map.getdeletearray();kyodai .showhint('r你找昏了頭吧'沒了 ! ”);return;line 1 inc 二 new linc(0, new point(), new point();map.settest(true); /告訴map當(dāng)前只是測試,并不需要進(jìn)行刪除動(dòng)畫1ine 二 mapfindnext(a);int offset 二 0;if (line.direct = 1) /找到了

40、可消除的if (setting.sound = 1) new sound(sound.hint);offset 二 1 ine.a.x * setting.collft'in + 1 ine.a.y;dotsoffset.setborder(kyodai.hint);offset = 1inc.b.x * setting.column + 1inc.b.y;dotsoffset.setborder(kyodai.hint);score 二 sett inghintscore;showscore(score - 1, score);else kyodai. showhint("

41、找不到'請(qǐng)刷新”);*自動(dòng)找出并消除地圖上的兩個(gè)點(diǎn)*/public boolean bomb(point a, boolean showmessage) if (! isplaying) /不在游戲中,返回return false;if (map.getcount() = 0) kyodai.showhint(”你炸昏了頭吧,沒了 ! ”);return false;line line = new line(0, new point(), new point();map.settest(false);1 inc = mapfindncxt(a);int offset = 0;if (l

42、ine.direct = 1) /找到了可消除的if (setting.sound = 1) new sound(sound.bomb);offset = 1ine.a.x * setting.column + 1ine.a.y;dotsoffsetsetborder(kyodai unsclcctcd);offset 二 1 ine.b.x * setting.collft'ln + 1 ine.b.y; dotsoffsct.sctbordcr(kyodai.unsclcctcd); map.earse(1ine.a, 1ine.b);earse(1ine.a, 1ine.b);s

43、core -= setting.bombscore;showscore(score 1, score);return true;else i f (showmessage) kyodai.showhint(n炸彈用不了,請(qǐng)刷新! ”);return false;private void message(string str) kyodai showhint(str);/*自動(dòng)游戲*/public void autoplay() if (! isplaying) /不在游戲中,返回return;/如果使用該功能,不計(jì)時(shí)間分1imittime 二 0;while (map.getcount() &

44、gt; 0) if (bomb(new point( -1, -1), false) message(”炸彈使用成功! ”);else message("找不到可用點(diǎn),刷新”);refresh();/*獲取系統(tǒng)的計(jì)分板*/ public void sctscorc(scorcanimatc score) this.scoreanimate = score;*獲取系統(tǒng)的計(jì)時(shí)板*/public void setclock(clockanimate clock) this.clockanimdtc 二 clock;/*事件處理*/public void actionperformed(ac

45、tionevent e) jbut ton button = (jbut ton) c.gctsourcc();int offset = integer .parselnt:(but: (on.getact ioncommand();int row, col;row = math.round(offset / setting.column);col 二 offset - row * setting.column;/如果上面沒有圖片i f (map.getmap()rowcol < 1) return;/選擇吋的聲音if (setting.sound = 1) new sound(soun

46、d.select);if (isselected) message(n±次已經(jīng)選擇了一個(gè)點(diǎn)”);message("上次選擇點(diǎn)的坐標(biāo)為:” + lastpoint .x + ”," + lastpoint .y +"值為:"+ map.getmap() lastpoint .x lastpoint ,y +"位移為:” + (lastpoint .x * setting.column + lastpoint .y);/是上次選擇的點(diǎn)if (lastpoint.x = row && lastpoint.y = col) m

47、essage(”這次選擇的點(diǎn)和上次的是同一點(diǎn),取消選擇狀態(tài)”);but ton.setborder(kyodai.unselected);isselected 二 false;else /判斷是否可以消除mess age (”這次選擇的點(diǎn)和上次的點(diǎn)并不相同”);point current = new point(row, col);message(”這次選擇的點(diǎn)的坐標(biāo)為:” + row + ”,” + col + ”值為:“ + map.ge tmap()rowcol +"位移為:” + (row * sett ing.column + col);map.settest(false)

48、;if (map.test(lastpoint, current) message(”兩點(diǎn)可以消除,執(zhí)行消除”);/消除前先取消當(dāng)前選擇點(diǎn)的邊框,因?yàn)橛锌赡苁翘崾綿otsrow * setting.column + col.sctbordcr(kyodai.unsclcctcd);map.earse(current, lastpoint);earse(current, lastpoint);dotslastpoint.x * setting.column +lastpoint.y setborder(kyodai.unsclcctcd);lastpoint 二 new point(0, 0);

49、isselected = false;showscorc(score, score + setting.corrcctscoic + stcpscorc);score +二 setting.correctscore + stepscore;if (setting.sound = 1) new sound(sound.earse);else message(”這次選擇的點(diǎn)與上次選擇的點(diǎn)無解,改變選擇為當(dāng)前點(diǎn)”);dotslastpoint.x * setting.column +lastpoint y setborder(kyodai unsclcctcd);but ton.setborder(

50、kyodai selected);lastpoint.x 二 row;lastpoint.y = col;isselected = t rue;score -= set ting.wrongscore;showscore(score - 1, score);else message("上次并未選擇的點(diǎn)5置當(dāng)前點(diǎn)為選擇點(diǎn)”);message(”當(dāng)前點(diǎn)坐標(biāo)為:” + row + ”,” + col + “值為:”+mapge(map()rowcol+"位移為:” + (row * sei i ing.column + col);but ton.setborder(kyodai.

51、selected);lastpoint.x = row;lastpoint.y = col;kyodai.showhint(”負(fù)分還想有排名?下次努力吧! ”);int level, fl, f2;level 二 setting.levelindex;fl 二(score - 102) * (score - 102);f2 二(1978 - score) * (1978 - score);return ”s二” + score + ”二” + level + ”&fl二” + fl + ”&f2二” + f2;public void start() gotoplo.setena

52、bled(false);isplaying 二 true;1imittimc = map.gctcount() * setting.1imitscorc;message(”時(shí)限=” + 1 imi ttime);paint();public void run() public void stop() clockanimate.stop();連連看下載package kyodai;import java.awtimport javax.swing處理時(shí)間的類*/public class clockanimate extends jpanelimplements runnable private

53、volatile thread thread;long starttime = 01; /開始時(shí)間long usedtime = 01; /使用時(shí)間color color = new color(212, 255, 200);font font48 = new font("serif", font.plain, 28);java.text.dccimalformat df = new java.text.dccimalformat("000"); java.(ext: .decimalformat: df2 = new java.(ext: decimalformat:("0");public clockanimate() this.se(minimumsize(new dimension(156, 48);this.selpreferredsize(new dimension(156, 48);/*吋間的繪制*/public void paint(graphics g) graphics2d g2 二(graphics2d) g;dimension d 二 getsize();g2.setbackground(new color(lll, 146, 212);g2

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論