




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、JAVA程序設(shè)計實訓(xùn)報告課程名稱:JAVA程序設(shè)計專 業(yè):計丁機應(yīng)用技術(shù)班 級:11計簟:機應(yīng)用班小組成員:巨敏石麗濤張婭雯李延 尚文學(xué)董丁喜周致遠 指導(dǎo)老師:武文廷二 . 實訓(xùn)題目和要求2.1 實訓(xùn)題目描述12.2 實訓(xùn)要求1三實訓(xùn)報告內(nèi)容3.1 五子棋主框架13.2 棋盤、棋子及說明信息13.3 對弈算法相關(guān)問題設(shè)計14 .實訓(xùn)中的部分代碼25 .五子棋源程序代碼36 .總、結(jié)17一、實訓(xùn)目的本次實訓(xùn),學(xué)生可以將理論知識與具體實踐相結(jié)合,鞏固對JAVA 相關(guān)方法和概念的理解。通過實訓(xùn)單機版五子棋游戲的編程,掌握J(rèn)AVA 語言編程的基礎(chǔ)知識并能熟練運用,熟悉累聲明與對象的使用,運用JAVAs
2、wing 編寫單機版五子棋游戲,并實現(xiàn)其功能。通過本次實訓(xùn),可以開拓思維,增強編程思想,為深入學(xué)習(xí) JAVA 打下良好的基礎(chǔ)。二、實訓(xùn)題目描述和要求2.1 實訓(xùn)題目描述實訓(xùn)題目:JAVA 五子棋單機版游戲。描述:通過JAVA 的 swing 組件,實現(xiàn)五子棋簡單的雙人對弈,并通過內(nèi)部條件判斷實現(xiàn)輸贏的雙方的下棋過程。2.2 實訓(xùn)要求( 1 )五子棋游戲的主窗口也就是游戲界面的實現(xiàn)( 2)棋子黑白色的設(shè)置及判定( 3)完成判斷某一橫行是否練成五子及所有方向是否練成五子的功能( 4)幾個簡單按鈕的實現(xiàn), “重新開始” “悔棋” “退出”按鈕( 5)菜單欄的實現(xiàn),“重新開始” “悔棋” “退出”菜單
3、項三、實訓(xùn)報告內(nèi)容3.1 主框架編寫一個startCheesJFrame類,主要用來顯行主窗體界面,包括工具條面板、菜單欄項。設(shè)置界面關(guān)閉事件。并編寫一個內(nèi)部類MyItemListener 來監(jiān)聽按鈕和菜單欄的單機事件。3.2 棋盤、棋子(1)編寫point類,包括棋子的X/Y索引,顏色。定義構(gòu)造函數(shù)和相應(yīng)的get方法。(2)編寫ChessBoard類,設(shè)置棋盤背景顏色為橘黃色(3)在主框架類中創(chuàng)建 ChessBoard對象,并添加到主框架中(4)編寫mousePressed方法來進行繪制棋盤和棋子3.3 對弈算法相關(guān)問題設(shè)計(1)編寫mousePressed方法的內(nèi)容,預(yù)定義isBlack表
4、示下的是黑棋還是白棋。PointCount表示當(dāng)前棋子的個數(shù)。(2)添加相應(yīng)的判斷:不能畫到棋盤外,下過的地方不能再下(需要輔助方法find point) 。(3)添加勝利的判斷iswin ,添加標(biāo)記變量 gameOver。在mousePresse昉法的最前面調(diào)用加入 gameOver 的判斷,在mousePressed方法的最后調(diào)用iswin ,返回true則給出消息提示,gameOver設(shè)置為tuer。四、實訓(xùn)中的部分代碼1.ChessBoard.javaPrivate ChessBoard chessboard;Private JPanel toolbar;Private JButton
5、 shartButton;Private JButton backButton;Private JButton exiButton;2.point.javaPublic class pointPrivate int x;Private int y;Private color color;Public static final int DIAMETER=30;Public point lint x, int y, color colorThis. X=x;This.y=y;This . color=color;3.startChessJFrame.java Public startChess J
6、Frame() Set Title(單機版五子棋”); chessboard=new chessboard(); menuBar=new JMenuBar();sysMenu=new JMenu(系統(tǒng)”);startMenuItem=new JMenuItem( “重新開始” );exitMenuItem=new JMenuItem( “退出 ”); backMenuItem=new JMenuItem( “ 悔棋 ”); sysMenu.add(startMenuItem);五、五子棋源程序代碼/StartChessFrameimport javax.swing.*;import java.
7、awt.event.*;import java.awt.*;public class StartChessFrame extends JFrame private ChessBoard chessBoard;private JPanel toolbarprivate JButton startButton, backButton, exitButton;private JMenuBar menuBar;private JMenu sysMenu;private JMenuItem startMenuItem, exitMenuItem, backMenuItem; public StartCh
8、essFrame() setTitle("單機版五子棋");chessBoard = new ChessBoard();menuBar = new JMenuBar();sysMenu = new JMenu(" 系統(tǒng) ");startMenuItem = new JMenuItem(" 重新開始");exitMenuItem = new JMenuItem(" 退出 ");backMenuItem = new JMenuItem(" 悔棋 "); sysMenu.add(startMenuIt
9、em);sysMenu.add(backMenuItem); sysMenu.add(exitMenuItem); MyItemListener lis = new MyItemListener(); this.startMenuItem.addActionListener(lis); backMenuItem.addActionListener(lis); exitMenuItem.addActionListener(lis);menuBar.add(sysMenu); setJMenuBar(menuBar);import java.awt.Color;public class Point
10、 private int x;private int y;private Color color;public static final int DIAMETER = 30;public Point(int x, int y, Color color) this.x = x;this.y = y;this.color = color;public int getX() return x;public int getY() return y;public Color getColor() return color;toolbar = new JPanel();startButton = new
11、JButton(" 重新開始");backButton = new JButton(" 悔棋 ");exitButton = new JButton(" 退出 ");toolbar.setLayout(new FlowLayout(FlowLayout.LEFT);toolbar.add(startButton);toolbar.add(backButton);toolbar.add(exitButton);startButton.addActionListener(lis);backButton.addActionListener(
12、lis);exitButton.addActionListener(lis);add(toolbar, BorderLayout.SOUTH);add(chessBoard);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/setSize(800,800);pack();private class MyItemListener implements ActionListener public void actionPerformed(ActionEvent e) Object obj = e.getSource();if (obj = Start
13、ChessFrame.this.startMenuItem | obj = startButton)System.out.println(" 重新開始.");chessBoard.restartGame(); else if (obj = exitMenuItem | obj = exitButton) System.exit(0); else if (obj = backMenuItem | obj = backButton) System.out.println(" 悔棋.");chessBoard.goback();public static vo
14、id main(String args) StartChessFrame f = new StartChessFrame();f.setVisible(true);/ChessBoardimport javax.swing.*;import java.awt.*;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.event.MouseEvent;public class ChessBoard extends JPanel implements MouseLi
15、stener public static final int MARGIN = 30;public static final int GRID_SPAN = 35;public static final int ROWS = 10;public static final int COLS = 10;Point chessList = new Point(ROWS + 1) * (COLS + 1);boolean isBlack = true;boolean gameOver = false;int chessCount;int xIndex, yIndex;public ChessBoard
16、()setBackground(Color.ORANGE);addMouseListener(this);addMouseMotionListener(new MouseMotionListener() public void mouseDragged(MouseEvent e) public void mouseMoved(MouseEvent e) int x1 = (e.getX() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;int y1 = (e.getY() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;if (x1 &
17、lt; 0 | x1 > ROWS | y1 < 0 | y1 > COLS | gameOver| findChess(x1, y1) setCursor(new Cursor(Cursor.DEFAULT_CURSOR);elsesetCursor(new Cursor(Cursor.HAND_CURSOR););public void paintComponent(Graphics g) super.paintComponent(g);for (int i = 0; i <= ROWS; i+) g.drawLine(MARGIN, MARGIN + i * GR
18、ID_SPAN, MARGIN + COLS* GRID_SPAN, MARGIN + i * GRID_SPAN);for (int i = 0; i <= COLS; i+) g.drawLine(MARGIN + i * GRID_SPAN, MARGIN, MARGIN + i * GRID_SPAN, MARGIN + ROWS * GRID_SPAN);for (int i = 0; i < chessCount; i+) int xPos = chessListi.getX() * GRID_SPAN + MARGIN;int yPos = chessListi.ge
19、tY() * GRID_SPAN + MARGIN;g.setColor(chessListi.getColor();g.fillOval(xPos - Point.DIAMETER / 2, yPos - Point.DIAMETER / 2,Point.DIAMETER, Point.DIAMETER);if (i = chessCount - 1) g.setColor(Color.red);g.drawRect(xPos - Point.DIAMETER / 2,yPos - Point.DIAMETER / 2, Point.DIAMETER, Point.DIAMETER);pub
20、lic void mousePressed(MouseEvent e) if (gameOver)return;String colorName = isBlack ? " 黑棋 " : " 白棋 "xIndex = (e.getX() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;yIndex = (e.getY() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;if (xIndex < 0 | xIndex > ROWS | yIndex < 0 | yIndex >
21、 COLS) return;if (findChess(xIndex, yIndex)return;Point ch = new Point(xIndex, yIndex, isBlack ? Color.black: Color.white); chessListchessCount+ = ch;repaint();if (isWin() String msg = String.format(" 恭喜, %s 贏了! ", colorName);JOptionPane.showMessageDialog(this, msg);gameOver = true;isBlack
22、 = !isBlack;public void mouseClicked(MouseEvent e) public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) public void mouseReleased(MouseEvent e) private boolean findChess(int x, int y) for (Point c : chessList) if (c != null && c.getX() = x && c.getY() = y) ret
23、urn true;return false;private boolean isWin() int continueCount = 1;for (int x = xIndex - 1; x >= 0; x-) Color c = isBlack ? Color.black : Color.white;if (getChess(x, yIndex, c) != null) continueCount+; elsebreak;for (int x = xIndex + 1; x <= ROWS; x+) Color c = isBlack ? Color.black : Color.w
24、hite;if (getChess(x, yIndex, c) != null) continueCount+; else break;if (continueCount >= 5) return true; elsecontinueCount = 1;for (int y = yIndex - 1; y >= 0; y-) Color c = isBlack ? Color.black : Color.white;if (getChess(xIndex, y, c) != null) continueCount+; elsebreak;for (int y = yIndex +
25、1; y <= ROWS; y+) Color c = isBlack ? Color.black : Color.white;if (getChess(xIndex, y, c) != null) continueCount+; else break;if (continueCount >= 5) return true; elsecontinueCount = 1;for (int x = xIndex + 1, y = yIndex - 1; y >= 0 && x <= COLS; x+, y-) Color c = isBlack ? Colo
26、r.black : Color.white;if (getChess(x, y, c) != null) continueCount+; else break;for (int x = xIndex - 1, y = yIndex + 1; y <= ROWS && x >= 0; x-, y+) Color c = isBlack ? Color.black : Color.white;if (getChess(x, y, c) != null) continueCount+; else break;if (continueCount >= 5) retur
27、n true; elsecontinueCount = 1; for (int x = xIndex - 1, y = yIndex - 1; y >= 0 && x >= 0; x-, y-) Color c = isBlack ? Color.black : Color.white;if (getChess(x, y, c) != null) continueCount+; elsebreak;for (int x = xIndex + 1, y = yIndex + 1; y <= ROWS && x <= COLS; x+, y+) Color c = isBlack ? Color.black : Color.white;if (getChess(x, y, c) != null) continueCo
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 森林改培與農(nóng)村安全生產(chǎn)考核試卷
- 綠色能源與可持續(xù)能源考核試卷
- 環(huán)境安全在能源開發(fā)利用中的約束與挑戰(zhàn)分析考核試卷
- 碳酸飲料行業(yè)包裝材料與環(huán)保創(chuàng)新考核試卷
- 小升初英語陳述句專題復(fù)習(xí)課件
- 經(jīng)紀(jì)人如何處理藝人合作關(guān)系糾紛考核試卷
- 聚己內(nèi)酰胺共聚物纖維單體制備考核試卷
- 電力設(shè)備風(fēng)力發(fā)電設(shè)備與并網(wǎng)技術(shù)考核試卷
- 紙張物理性能測試與優(yōu)化考核試卷
- 玻璃精密軸承考核試卷
- 景墻的施工技術(shù)方案
- 石油化工設(shè)備維護檢修規(guī)程設(shè)備完好標(biāo)準(zhǔn)SHS010012004-副本
- 施工標(biāo)準(zhǔn)化九牌一圖
- 單角子宮疾病演示課件
- 義務(wù)教育物理課程標(biāo)準(zhǔn)(2022年版)測試題文本版(附答案)
- FSSC22000V6.0體系文件清單
- 賈玲春晚搞笑公司年會小品《真假老師》臺詞劇本完整版
- 23S519 小型排水構(gòu)筑物(帶書簽)
- 第三章掃描電子顯微鏡【完整版】PPT
- SL631-637-2012-水利水電工程單元工程施工質(zhì)量驗收評定標(biāo)準(zhǔn)
- 胸腔穿刺術(shù)課件
評論
0/150
提交評論