java撲克游戲_第1頁
java撲克游戲_第2頁
java撲克游戲_第3頁
java撲克游戲_第4頁
java撲克游戲_第5頁
已閱讀5頁,還剩20頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、課程設計說明書 NO.1撲克游戲1.課程設計的目的Java語言是當今流行的網(wǎng)絡編程語言,它具有面向對象、跨平臺、分布應用等特點。面向對象的開發(fā)方法是當今世界最流行的開發(fā)方法,它不僅具有更貼近自然的語義,而且有利于軟件的維護和繼承。為了進一步鞏固課堂上所學到的知識,深刻把握Java語言的重要概念及其面向對象的特性,使我們能夠熟練的應用面向對象的思想和設計方法解決實際問題的能力。通過此次課程設計,鞏固所學Java語言基本知識,增進Java語言編輯基本功,掌握JDK、Editplus、Eclipse、JCreator等開發(fā)工具的運用,拓寬常用類庫的應用。使學生通過該教學環(huán)節(jié)與手段,把所學課程及相關知

2、識加以融會貫通,全面掌握Java語言的編程思想及面向對象程序設計的方法,為今后從事實際工作打下堅實的基礎。本設計使用JAVA語言開發(fā)撲克游戲程序,將電腦多次分發(fā)給你的牌按照相同的花色由大至小排列起來。2.設計方案論證2.1設計思路 用java語言,編程實現(xiàn)紙牌游戲,擁有如下規(guī)則,將電腦多次分發(fā)給你的牌按照相同的花色由大至小排列起來。游戲分為三個難度,簡單,普通,困難。簡單為單一花色。困難所分發(fā)給的牌有四種花色。將大小相鄰的紙牌依次排列到一起當每種花色的全部紙牌都按順序排列到一起,則游戲結束。2.2設計方法將程序設計成為4個類,AboutDialog類用于實現(xiàn)全部的對話。PKCard類用于實現(xiàn)游

3、戲的規(guī)則。SpiderMenuBar類用于實現(xiàn)各個模塊的功能。Spider為主界面。在SpiderMenuBar建立主界面菜單,通過構造函數(shù)public SpiderMenuBar構造函數(shù),生成JMenuBar的圖形界面,對菜單中各按鈕進行事件監(jiān)聽。在該方法中調用spider的構造方法,在其中生成SpiderMenuBar對象,并放置在框架之上,同時設置框架標題 沈 陽 大 學課程設計說明書 NO.2框架大小背景顏色,布局為空。public Spider()setTitle(陶時撲克); setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_C

4、LOSE);setSize(1024, 742);setJMenuBar(new SpiderMenuBar(this); pane = this.getContentPane(); pane.setBackground(new Color(14, 25, 26); pane.setLayout(null); clickLabel = new JLabel(); clickLabel.setBounds(883, 606, 121, 96); pane.add(clickLabel);在Spider類中有如下方法:newGame新游戲的方法,setGrade設置等級方法,初始化等級方法。set

5、Grade,randomCards隨即函數(shù)。PKCard getPreviousCard獲得card上面的那張牌的方法等。由PKCard getPreviousCard,PKCard getNextCard,getLastCardLocation方法對類PKCard調用,代碼如下:public PKCard getPreviousCard(PKCard card) Point point = new Point(card.getLocation(); point.y -= 5; card = (PKCard) table.get(point); if (card != null)return

6、card; point.y -= 15; card = (PKCard) table.get(point); 沈 陽 大 學課程設計說明書 NO.3 return card;public PKCard getNextCard(PKCard card) Point point = new Point(card.getLocation(); point.y += 5;card = (PKCard) table.get(point); if (card != null)return card; point.y += 15; card = (PKCard) table.get(point); retu

7、rn card;public Point getLastCardLocation(int column) Point point = new Point(20 + column * 101, 25); PKCard card = (PKCard) this.table.get(point); if (card = null) return null; while (card != null) point = card.getLocation(); card = this.getNextCard(card); return point;public Point getGroundLabelLoc

8、ation(int column) return new Point(groundLabelcolumn.getLocation();public void setGroundLabelZOrder() for (int i = 0; i 10; i+) 沈 陽 大 學課程設計說明書 NO.4pane.setComponentZOrder(groundLabeli, 105 + i); 23功能模塊圖圖1 功能模塊圖 沈 陽 大 學課程設計說明書 NO.52.4程序流程圖圖2 程序流程圖3.設計結果與分析(1)首先是對游戲中主菜單的設計,設計的菜單包括兩個大部分,選項和幫助,如圖3所示: 沈

9、陽 大 學課程設計說明書 NO.6圖3 顯示菜單通過如下代碼實現(xiàn):JMenu jNewGame = new JMenu(選項); JMenu jHelp = new JMenu(幫助);(2)在選項菜單下面包含7級子菜單,如圖4所示:圖4 顯示菜單 沈 陽 大 學課程設計說明書 NO.7通過如下代碼實現(xiàn):JMenuItem jItemOpen = new JMenuItem(開局);JMenuItem jItemPlayAgain = new JMenuItem(重發(fā)牌);JRadioButtonMenuItem jRMItemEasy = new JRadioButtonMenuItem(簡

10、單);JRadioButtonMenuItem jRMItemNormal = new JRadioButtonMenuItem(較難);JRadioButtonMenuItem jRMItemHard = new JRadioButtonMenuItem(困難);JMenuItem jItemExit = new JMenuItem(退出);JMenuItem jItemValid = new JMenuItem(顯示可執(zhí)行行操作);(3)幫助下面包含2級子菜單,分別為游戲規(guī)則和聲明,如圖5所示:圖5 顯示幫助通過如下代碼實現(xiàn):JTabbedPane jTabbedPane = new JT

11、abbedPane();private JPanel jPanel1 = new JPanel();private JPanel jPanel2 = new JPanel();(4)主窗體通過類Spider實現(xiàn)。將窗體名稱設置為“陶時撲克”,框架的大小設置為1024*742,背景顏色設置為黑色,布局管理設置為空,通過如下代碼實現(xiàn):public Spider() 沈 陽 大 學課程設計說明書 NO.8setTitle(陶時撲克);setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);setSize(1024, 742);setJMe

12、nuBar(new SpiderMenuBar(this); pane = this.getContentPane();pane.setBackground(new Color(14, 25, 26);pane.setLayout(null);(5)進入游戲之后,首先選擇開始新游戲,通過類Spider調用它的方法newGame方法,采用隨機函數(shù)隨機初始化牌的順序(這樣做的目的是,使游戲性增加可玩性,使每次出現(xiàn)牌的順序不同),如圖6所示。圖6 進入新游戲界面用如下代碼實現(xiàn):public void newGame() this.randomCards(); this.setCardsLocatio

13、n(); 沈 陽 大 學課程設計說明書 NO.9this.setGroundLabelZOrder();this.deal(); public int getC() return c; public void setGrade(int grade) this.grade = grade; public void initCards() if (cards0 != null) for (int i = 0; i 104; i+) pane.remove(cardsi); int n = 0;if (this.grade = Spider.EASY) n = 1; else if (this.gr

14、ade = Spider.NATURAL) n = 2; else n = 4; for (int i = 1; i = 8; i+) 沈 陽 大 學課程設計說明書 NO.10for (int j = 1; j = 13; j+) cards(i - 1) * 13 + j - 1 = new PKCard(i % n + 1) + - + j, this); this.randomCards(); public void randomCards() PKCard temp = null; for (int i = 0; i 52; i+) int a = (int) (Math.random

15、() * 104); int b = (int) (Math.random() * 104); temp = cardsa; cardsa = cardsb; cardsb = temp; (6)在游戲界面的右下角做一張圖片,顯示撲克牌的背面,同時點擊圖片,系統(tǒng)自動發(fā)牌(當游戲無法繼續(xù)的時候,可以點擊該圖片,在每副紙牌上發(fā)一張牌,使得進入僵局的紙牌游戲得以繼續(xù)進行),運行界面如圖7,圖8所示。 沈 陽 大 學課程設計說明書 NO.11圖7 發(fā)牌功能界面圖8 顯示發(fā)牌 沈 陽 大 學課程設計說明書 NO.12用如下代碼實現(xiàn):首先在界面上添加紙牌背面的圖片:public void turnRear

16、() this.setIcon(new ImageIcon(images/6.gif); this.isFront = false; this.canMove = false;public PKCard getPreviousCard(PKCard card) Point point = new Point(card.getLocation(); point.y -= 5; card = (PKCard) table.get(point); if (card != null)return card; point.y -= 15; card = (PKCard) table.get(point)

17、;return card; public PKCard getNextCard(PKCard card) Point point = new Point(card.getLocation(); point.y += 5; card = (PKCard) table.get(point); if (card != null)return card; point.y += 15; card = (PKCard) table.get(point); return card; 沈 陽 大 學課程設計說明書 NO.13public Point getLastCardLocation(int column

18、) Point point = new Point(20 + column * 101, 25); PKCard card = (PKCard) this.table.get(point); if (card = null) return null; while (card != null) point = card.getLocation(); card = this.getNextCard(card); return point; public Point getGroundLabelLocation(int column) return new Point(groundLabelcolu

19、mn.getLocation(); public void setGroundLabelZOrder() for (int i = 0; i 10; i+)pane.setComponentZOrder(groundLabeli, 105 + i); (7)在游戲中每次開始游戲都由程序隨機發(fā)牌,發(fā)牌的過程是,先設置紙牌的初始位置由隨機函數(shù)產(chǎn)生,并隨機生成牌號,設置紙牌的位置,初始化待展開的紙牌,將紙牌放置到固定位置(這里調用PKcard類中的setNextCardLocation方法調用圖片文件夾里的紙牌圖片),如圖9,圖10所示。 沈 陽 大 學課程設計說明書 NO.14圖9 圖片庫圖10

20、圖片被載入 沈 陽 大 學課程設計說明書 NO.15使用如下代碼實現(xiàn):for (int i = 0; i 6; i+) for (int j = 0; j 5; i-) for (int j = 0; j = 104) continue;pane.add(cardsn); cardsn.turnRear(); cardsn.moveto(new Point(x, y); table.put(new Point(x, y), cardsn); x += 101; x = 20; y -= 5; 沈 陽 大 學課程設計說明書 NO.16public void showEnableOperator(

21、) int x = 0; out: while (true) Point point = null; PKCard card = null; do if (point != null)n+; point = this.getLastCardLocation(n); while (point = null) point = this.getLastCardLocation(+n); if (n = 10) n = 0; x+; if (x = 10) break out; card = (PKCard) this.table.get(point); while (!card.isCardCanM

22、ove(); while (this.getPreviousCard(card)!= null& this.getPreviousCard(card).isCardCanMove()card = this.getPreviousCard(card); if (a = 10)a = 0;for (; a 10; a+) if (a != n) 沈 陽 大 學課程設計說明書 NO.17Point p = null; PKCard c = null; do if (p != null)a+;p = this.getLastCardLocation(a); int z = 0; while (p =

23、null) p = this.getLastCardLocation(+a); if (a = 10) a = 0; if (a = n) a+; z+; if (z = 10) break out; c = (PKCard) this.table.get(p); while (!c.isCardCanMove(); if (c.getCardValue() = card.getCardValue() + 1) card.flashCard(card); try Thread.sleep(800); catch (InterruptedException e)e.printStackTrace

24、(); c.flashCard(c); 沈 陽 大 學課程設計說明書 NO.18a+; if (a = 10)n+; break out; n+; if (n = 10)n = 0; x+; if (x = 10)break out; 通過調用如下代碼實現(xiàn)圖片的調用:public void setNextCardLocation(Point point)PKCard card = main.getNextCard(this);if (card != null)if (point = null)card.setNextCardLocation(null);main.table.remove(ca

25、rd.getLocation();card.setLocation(card.initPoint);main.table.put(card.initPoint, card); 沈 陽 大 學課程設計說明書 NO.19elsepoint = new Point(point);point.y += 20;card.setNextCardLocation(point);point.y -= 20;main.table.remove(card.getLocation();card.setLocation(point);main.table.put(card.getLocation(), card);c

26、ard.initPoint = card.getLocation();(8)進入游戲之后能對游戲的難度進行選擇,難度分為三種,分別為簡單,較難,困難,簡單為單一花色的紙牌進行游戲,而較難和困難的紙牌花色相應的增多。在設計時首先在SpiderMenuBar類中,將三種難度的菜單實現(xiàn),然后通過調用PKcard的方法實現(xiàn)各難度的功能,如圖11,12所示。 沈 陽 大 學課程設計說明書 NO.20圖11 難度選擇圖12 選擇困難 沈 陽 大 學課程設計說明書 NO.21通過如下代碼實現(xiàn):實現(xiàn)菜單分成三種難度:JRadioButtonMenuItem jRMItemEasy = new JRadioBu

27、ttonMenuItem(簡單); JRadioButtonMenuItem jRMItemNormal = new JRadioButtonMenuItem(較難);JRadioButtonMenuItem jRMItemHard = new JRadioButtonMenuItem(困難);jRMItemEasy.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) main.setGrade(Spider.EASY);

28、main.initCards(); main.newGame(); 分別對菜單進行事件監(jiān)聽,若選中相應的難度登記則觸發(fā)Spider類中的方法進行實現(xiàn):jRMItemNormal.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) main.setGrade(Spider.NATURAL); main.initCards(); main.newGame(); jRMItemHard.addActionListener(new

29、java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) main.setGrade(Spider.HARD);main.initCards(); main.newGame(); 沈 陽 大 學課程設計說明書 NO.22jNewGame.addMenuListener(new javax.swing.event.MenuListener() public void menuSelected(javax.swing.event.MenuEvent e) if(main.get

30、C() 60) jItemPlayAgain.setEnabled(true); else jItemPlayAgain.setEnabled(false); public void menuDeselected(javax.swing.event.MenuEvent e) public void menuCanceled(javax.swing.event.MenuEvent e) (9)退出游戲,點擊退出即可退出游戲,在SpiderMenuBar類中actionPerformed方法實現(xiàn),如圖13所示。圖13 退出游戲 沈 陽 大 學課程設計說明書 NO.23使用如下代碼實現(xiàn):jItemExit.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) main.dispose(); System.exit(0); 4.設計體會通過本次課程設計,我學會了很多東西,在課堂上學習的知識是理論的,平時沒有多少機會去實踐,平時覺得自己JAVA學得還不錯但真到想用它實現(xiàn)點什么的時候卻又覺得好多地方不知

溫馨提示

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

評論

0/150

提交評論