




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、JAVA程序設(shè)計(jì)實(shí)訓(xùn)報告課程名稱:JAVA程序設(shè)計(jì)專 業(yè):計(jì)丁機(jī)應(yīng)用技術(shù)班 級:11計(jì)簟:機(jī)應(yīng)用班小組成員:巨敏石麗濤張婭雯李延 尚文學(xué)董丁喜周致遠(yuǎn) 指導(dǎo)老師:武文廷二 . 實(shí)訓(xùn)題目和要求2.1 實(shí)訓(xùn)題目描述12.2 實(shí)訓(xùn)要求1三實(shí)訓(xùn)報告內(nèi)容3.1 五子棋主框架13.2 棋盤、棋子及說明信息13.3 對弈算法相關(guān)問題設(shè)計(jì)14 .實(shí)訓(xùn)中的部分代碼25 .五子棋源程序代碼36 .總、結(jié)17一、實(shí)訓(xùn)目的本次實(shí)訓(xùn),學(xué)生可以將理論知識與具體實(shí)踐相結(jié)合,鞏固對JAVA 相關(guān)方法和概念的理解。通過實(shí)訓(xùn)單機(jī)版五子棋游戲的編程,掌握J(rèn)AVA 語言編程的基礎(chǔ)知識并能熟練運(yùn)用,熟悉累聲明與對象的使用,運(yùn)用JAVAs
2、wing 編寫單機(jī)版五子棋游戲,并實(shí)現(xiàn)其功能。通過本次實(shí)訓(xùn),可以開拓思維,增強(qiáng)編程思想,為深入學(xué)習(xí) JAVA 打下良好的基礎(chǔ)。二、實(shí)訓(xùn)題目描述和要求2.1 實(shí)訓(xùn)題目描述實(shí)訓(xùn)題目:JAVA 五子棋單機(jī)版游戲。描述:通過JAVA 的 swing 組件,實(shí)現(xiàn)五子棋簡單的雙人對弈,并通過內(nèi)部條件判斷實(shí)現(xiàn)輸贏的雙方的下棋過程。2.2 實(shí)訓(xùn)要求( 1 )五子棋游戲的主窗口也就是游戲界面的實(shí)現(xiàn)( 2)棋子黑白色的設(shè)置及判定( 3)完成判斷某一橫行是否練成五子及所有方向是否練成五子的功能( 4)幾個簡單按鈕的實(shí)現(xiàn), “重新開始” “悔棋” “退出”按鈕( 5)菜單欄的實(shí)現(xiàn),“重新開始” “悔棋” “退出”菜單
3、項(xiàng)三、實(shí)訓(xùn)報告內(nèi)容3.1 主框架編寫一個startCheesJFrame類,主要用來顯行主窗體界面,包括工具條面板、菜單欄項(xiàng)。設(shè)置界面關(guān)閉事件。并編寫一個內(nèi)部類MyItemListener 來監(jiān)聽按鈕和菜單欄的單機(jī)事件。3.2 棋盤、棋子(1)編寫point類,包括棋子的X/Y索引,顏色。定義構(gòu)造函數(shù)和相應(yīng)的get方法。(2)編寫ChessBoard類,設(shè)置棋盤背景顏色為橘黃色(3)在主框架類中創(chuàng)建 ChessBoard對象,并添加到主框架中(4)編寫mousePressed方法來進(jìn)行繪制棋盤和棋子3.3 對弈算法相關(guān)問題設(shè)計(jì)(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。四、實(shí)訓(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(單機(jī)版五子棋”); 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("單機(jī)版五子棋");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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二次根式的性質(zhì)教學(xué)設(shè)計(jì)
- 醫(yī)療廢棄物處理的可持續(xù)發(fā)展路徑
- 區(qū)塊鏈技術(shù)在智能合約中的應(yīng)用實(shí)例
- 健康產(chǎn)業(yè)人才激勵機(jī)制研究報告
- 健康教育與生活方式改變的關(guān)聯(lián)性研究
- 周圍血管病的臨床護(hù)理
- 醫(yī)療健康數(shù)據(jù)隱私保護(hù)HIPAA的核心要點(diǎn)
- 2025年5月山西省晉中市高考適應(yīng)訓(xùn)練考試物理試卷(含答案)
- 企業(yè)出兌合同范例
- 1000元租房合同范例
- 人教版七年級生物下冊《血流的管道-血管》評課稿
- 裝配式建筑施工技術(shù)PPT(高職)完整全套教學(xué)課件
- 涉詐風(fēng)險賬戶審查表
- 門診辦運(yùn)用PDCA提高門診預(yù)約掛號率品管圈成果匯報
- 市場開拓委托合同書
- 2023年副主任醫(yī)師(副高)-兒童保健(副高)考試歷年真題精華集選附答案
- 跟骨牽引 跟骨牽引圖片
- 《莊子過惠子之墓》中考文言文閱讀試題2篇(含答案與翻譯)
- 6、柴油發(fā)電機(jī)房安全風(fēng)險告知卡
- 2023學(xué)年完整公開課版ThelastdayofPompeii
- 橋式起重機(jī)司機(jī)(初級)職業(yè)技能鑒定考試題庫(職校培訓(xùn))
評論
0/150
提交評論