版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、JAVA程序設(shè)計(jì)實(shí)訓(xùn)報(bào)告課程名稱:JAVA程序設(shè)計(jì)專 業(yè):計(jì)丁機(jī)應(yīng)用技術(shù)班 級(jí):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)報(bào)告內(nèi)容3.1 五子棋主框架13.2 棋盤、棋子及說(shuō)明信息13.3 對(duì)弈算法相關(guān)問(wèn)題設(shè)計(jì)14 .實(shí)訓(xùn)中的部分代碼25 .五子棋源程序代碼36 .總、結(jié)17一、實(shí)訓(xùn)目的本次實(shí)訓(xùn),學(xué)生可以將理論知識(shí)與具體實(shí)踐相結(jié)合,鞏固對(duì)JAVA 相關(guān)方法和概念的理解。通過(guò)實(shí)訓(xùn)單機(jī)版五子棋游戲的編程,掌握J(rèn)AVA 語(yǔ)言編程的基礎(chǔ)知識(shí)并能熟練運(yùn)用,熟悉累聲明與對(duì)象的使用,運(yùn)用JAVAs
2、wing 編寫(xiě)單機(jī)版五子棋游戲,并實(shí)現(xiàn)其功能。通過(guò)本次實(shí)訓(xùn),可以開(kāi)拓思維,增強(qiáng)編程思想,為深入學(xué)習(xí) JAVA 打下良好的基礎(chǔ)。二、實(shí)訓(xùn)題目描述和要求2.1 實(shí)訓(xùn)題目描述實(shí)訓(xùn)題目:JAVA 五子棋單機(jī)版游戲。描述:通過(guò)JAVA 的 swing 組件,實(shí)現(xiàn)五子棋簡(jiǎn)單的雙人對(duì)弈,并通過(guò)內(nèi)部條件判斷實(shí)現(xiàn)輸贏的雙方的下棋過(guò)程。2.2 實(shí)訓(xùn)要求( 1 )五子棋游戲的主窗口也就是游戲界面的實(shí)現(xiàn)( 2)棋子黑白色的設(shè)置及判定( 3)完成判斷某一橫行是否練成五子及所有方向是否練成五子的功能( 4)幾個(gè)簡(jiǎn)單按鈕的實(shí)現(xiàn), “重新開(kāi)始” “悔棋” “退出”按鈕( 5)菜單欄的實(shí)現(xiàn),“重新開(kāi)始” “悔棋” “退出”菜單
3、項(xiàng)三、實(shí)訓(xùn)報(bào)告內(nèi)容3.1 主框架編寫(xiě)一個(gè)startCheesJFrame類,主要用來(lái)顯行主窗體界面,包括工具條面板、菜單欄項(xiàng)。設(shè)置界面關(guān)閉事件。并編寫(xiě)一個(gè)內(nèi)部類MyItemListener 來(lái)監(jiān)聽(tīng)按鈕和菜單欄的單機(jī)事件。3.2 棋盤、棋子(1)編寫(xiě)point類,包括棋子的X/Y索引,顏色。定義構(gòu)造函數(shù)和相應(yīng)的get方法。(2)編寫(xiě)ChessBoard類,設(shè)置棋盤背景顏色為橘黃色(3)在主框架類中創(chuàng)建 ChessBoard對(duì)象,并添加到主框架中(4)編寫(xiě)mousePressed方法來(lái)進(jìn)行繪制棋盤和棋子3.3 對(duì)弈算法相關(guān)問(wèn)題設(shè)計(jì)(1)編寫(xiě)mousePressed方法的內(nèi)容,預(yù)定義isBlack表
4、示下的是黑棋還是白棋。PointCount表示當(dāng)前棋子的個(gè)數(shù)。(2)添加相應(yīng)的判斷:不能畫(huà)到棋盤外,下過(guò)的地方不能再下(需要輔助方法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( “重新開(kāi)始” );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(" 重新開(kāi)始");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(" 重新開(kāi)始");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(" 重新開(kāi)始.");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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025版施工現(xiàn)場(chǎng)安全評(píng)價(jià)與驗(yàn)收協(xié)議責(zé)任書(shū)3篇
- 2025版?zhèn)€人退股協(xié)議書(shū):創(chuàng)業(yè)投資退出與收益確認(rèn)合同4篇
- 2025年全球及中國(guó)絕緣干式電力變壓器行業(yè)頭部企業(yè)市場(chǎng)占有率及排名調(diào)研報(bào)告
- 2025-2030全球光強(qiáng)度調(diào)制器行業(yè)調(diào)研及趨勢(shì)分析報(bào)告
- 2025-2030全球多相真空萃取機(jī)行業(yè)調(diào)研及趨勢(shì)分析報(bào)告
- 2025-2030全球太陽(yáng)能商用EV充電車棚行業(yè)調(diào)研及趨勢(shì)分析報(bào)告
- 2025年全球及中國(guó)紫外超快光纖激光器行業(yè)頭部企業(yè)市場(chǎng)占有率及排名調(diào)研報(bào)告
- 2024年科普知識(shí)競(jìng)賽試題庫(kù)及答案(共60題)
- 2025年度個(gè)人個(gè)人間環(huán)保技術(shù)研發(fā)借款協(xié)議4篇
- 2025年度個(gè)人住宅租賃定金支付與保障協(xié)議書(shū)2篇
- 2024-2025學(xué)年北京石景山區(qū)九年級(jí)初三(上)期末語(yǔ)文試卷(含答案)
- 第一章 整式的乘除 單元測(cè)試(含答案) 2024-2025學(xué)年北師大版數(shù)學(xué)七年級(jí)下冊(cè)
- 春節(jié)聯(lián)歡晚會(huì)節(jié)目單課件模板
- 中國(guó)高血壓防治指南(2024年修訂版)
- 糖尿病眼病患者血糖管理
- 抖音音樂(lè)推廣代運(yùn)營(yíng)合同樣本
- 《春酒》琦君完整版
- 教育促進(jìn)會(huì)會(huì)長(zhǎng)總結(jié)發(fā)言稿
- 北師大版(2024新版)七年級(jí)上冊(cè)數(shù)學(xué)第四章《基本平面圖形》測(cè)試卷(含答案解析)
- 心理調(diào)適教案調(diào)整心態(tài)積極應(yīng)對(duì)挑戰(zhàn)
- 小學(xué)數(shù)學(xué)6年級(jí)應(yīng)用題100道附答案(完整版)
評(píng)論
0/150
提交評(píng)論