版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、 “Java程序設(shè)計”綜合實驗報告一、前言1、項目背景通過五子棋這個課程設(shè)計??梢宰屛覀兏邮煜ava程序設(shè)計這門課程,加強對Eclipse這個軟件的使用,加深對Java語言的掌握,提高編程水平。同時培養(yǎng)能力,為將來的工作打下堅實的基礎(chǔ)。2、 目標(biāo)和意義 目標(biāo):鍛煉我們的能力,提高組中每一個人對Java語言的認(rèn)識,培養(yǎng)編程興趣。讓每一個人都能參與進(jìn)來,提高團隊合作意識。 意義:通過編寫綜合類的程序,運用已經(jīng)學(xué)過的知識,和自主學(xué)習(xí)一些新的知識,提高了學(xué)習(xí)能力,掌握了一些自主學(xué)習(xí)的方法。3、 開發(fā)分工及進(jìn)度安排二、功能分析1、主要模塊 本項目主要分為3個大的模塊,分別為整體布局;界面繪制;與對戰(zhàn)
2、的算法。由于游戲規(guī)則的設(shè)置,這里的游戲模式分為人機對戰(zhàn)和人人對戰(zhàn)。黑白雙方依次落子,由黑子先下,當(dāng)任意一方在棋盤上形成橫向,豎向,斜向連續(xù)五個相同顏色的棋子的一方獲勝。主要功能實現(xiàn)在2種模式下五子棋的游戲。實現(xiàn)通過鼠標(biāo)的點擊位置放置棋子,達(dá)到下棋的目的。實現(xiàn)游戲玩家對游戲模式的自主選擇。實現(xiàn)對在每種游戲模式下的黑子先手下棋的規(guī)定,先達(dá)到5子即為勝利。三、關(guān)鍵功能的設(shè)計與實現(xiàn)1、數(shù)據(jù)結(jié)構(gòu)與算法數(shù)據(jù)結(jié)構(gòu):項目中主要數(shù)據(jù)結(jié)構(gòu)為二維數(shù)組。用于存儲棋盤上棋子的信息,和保存棋型表。主要算法:(一)iswin()函數(shù):用來判斷輸贏,通過鼠標(biāo)事件所得到的點或者電腦下的點的坐標(biāo),來掃描該點八個方向的相鄰的相同棋
3、子數(shù),上下,左右,斜左上下,斜右上下為四組,任意一組等于5即為勝利,由于本程序沒有考慮禁手原則,只考慮了民間規(guī)則,所以大于5也為勝利。public int iswin1(int x, int y, int heqi) int k, s1, s2, s3, s4, s5, s6, s7, s8;s1 = 0;s2 = 0;s3 = 0;s4 = 0;s5 = 0;s6 = 0;s7 = 0;s8 = 0;if (heqi = 256)return -1;for (k = 1; k < 5; k+) if (y + k < 16 && qipanqizixy + k =
4、 qipanqizixy)s1+;elsebreak;for (k = 1; k < 5; k+) if (y - k > -1 && qipanqizixy - k = qipanqizixy)s2+;elsebreak;for (k = 1; k < 5; k+) if (x + k < 16 && y + k < 16&& qipanqizix + ky + k = qipanqizixy)s3+;elsebreak;for (k = 1; k < 5; k+) if (x - k > -1 &am
5、p;& y - k > -1&& qipanqizix - ky - k = qipanqizixy)s4+;elsebreak;for (k = 1; k < 5; k+) if (x + k < 16 && qipanqizix + ky = qipanqizixy)s5+;elsebreak;for (k = 1; k < 5; k+) if (x - k > -1 && qipanqizix - ky = qipanqizixy)s6+;elsebreak;for (k = 1; k < 5;
6、k+) if (x - k > -1 && y + k < 16&& qipanqizix - ky + k = qipanqizixy)s7+;elsebreak;for (k = 1; k < 5; k+) if (x + k < 16 && y - k > -1&& qipanqizix + ky - k = qipanqizixy)s8+;elsebreak;if (s1 + s2 >= 4 | s3 + s4 >= 4 | s5 + s6 >= 4 | s7 + s8 &g
7、t;= 4) return 1; elsereturn 0;(二)人機對戰(zhàn)通過對整個棋盤上每一個點的掃描,獲得了電腦和玩家的棋型表,表中數(shù)據(jù)為該點的權(quán)值。根據(jù)權(quán)值來考慮下一步應(yīng)該下在哪里。棋型表中的權(quán)值通過引用下表的值來計算。public void renji() int i, j, max, jx = 0, jy = 0, k = 0;max = -10;q = getGraphics();if (gameover = false) if (isblack = true) if (qipanqiziy1x1 = -1) Point linshi = new Point(x1, y1, Col
8、or.black);pointii = new Point(linshi, q);isblack = false;qipanqiziy1x1 = 0;/ 0代表黑棋,1代表白棋,-1代表空if (iswin1(y1, x1, ii) = 1) String msg = String.format("恭喜,你贏了!");JOptionPane.showMessageDialog(this, msg);gameover = true;if (iswin1(y1, x1, ii) = -1) String msg = String.format("和棋!");
9、JOptionPane.showMessageDialog(this, msg);gameover = true;ii+;if (isblack = false) qixing();for (i = 0; i < 16; i+)for (j = 0; j < 16; j+) if (qixingPCij != 0 && max < qixingPCij) max = qixingPCij;jx = i;jy = j;for (i = 0; i < 16; i+)for (j = 0; j < 16; j+) if (qixingPlayerij !
10、= 0 && max < qixingPlayerij) max = qixingPlayerij;jx = i;jy = j;Point linshi1 = new Point(jy, jx, Color.white);pointii = new Point(linshi1, q);qipanqizijxjy = 1;isblack=true;if (iswin1(jx, jy, ii) = 1) String msg = String.format("哦,你輸啦!");JOptionPane.showMessageDialog(this, msg)
11、;gameover = true;if (iswin1(jx, jy, ii) = -1) String msg = String.format("和棋!");JOptionPane.showMessageDialog(this, msg);gameover = true;ii+;public void qixing() int i, j;for (i = 0; i < 16; i+)for (j = 0; j < 16; j+) qixingPCij = 0;qixingPlayerij = 0;for (i = 0; i < 16; i+) for (
12、j = 0; j < 16; j+) if (qipanqiziij = -1) / 此處沒有棋子qixingPCij = heng(i, j, 1, 0) + shu(i, j, 1, 0)+ zuoxie(i, j, 1, 0) + youxie(i, j, 1, 0);qixingPlayerij = heng(i, j, 0, 1) + shu(i, j, 0, 1)+ zuoxie(i, j, 0, 1) + youxie(i, j, 0, 1);public int heng(int hang, int lie, int num1, int num2) / num1當(dāng)前棋子顏
13、色,num2對方棋子顏色int i;int left = new int4;int right = new int4;for (i = 1; i < 5; i+)if (lie - i > -1 && qipanqizihanglie - i = num1)lefti - 1 = num1;else if (lie - i > -1 && qipanqizihanglie - i = -1)/ 空lefti - 1 = -1;else if (lie - i > -1 && qipanqizihanglie - i = n
14、um2)/ 不空,為對方棋子lefti - 1 = num2;else if (lie - i <= -1)/ 邊界,越界置-2lefti - 1 = -2;for (i = 1; i < 5; i+)if (lie + i < 16 && qipanqizihanglie + i = num1)righti - 1 = num1;else if (lie + i < 16 && qipanqizihanglie + i = -1)/ 空righti - 1 = -1;else if (lie + i < 16 &&
15、 qipanqizihanglie + i = num2)/ 不空,為對方棋子righti - 1 = num2;else if (lie + i >= 16)/ 邊界righti - 1 = -2;return jieguo(left, right, num1, num2);public int shu(int hang, int lie, int num1, int num2) int i;int left = new int4;int right = new int4;for (i = 1; i < 5; i+)if (hang - i > -1 &&
16、qipanqizihang - ilie = num1)lefti - 1 = num1;else if (hang - i > -1 && qipanqizihang - ilie = -1)/ 空lefti - 1 = -1;else if (hang - i > -1 && qipanqizihang - ilie = num2)/ 不空,為對方棋子lefti - 1 = num2;else if (hang - i <= -1)/ 邊界lefti - 1 = -2;for (i = 1; i < 5; i+)if (hang +
17、i < 16 && qipanqizihang + ilie = num1)righti - 1 = num1;else if (hang + i < 16 && qipanqizihang + ilie = -1)/ 空righti - 1 = -1;else if (hang + i < 16 && qipanqizihang + ilie = num2)/ 不空,為對方棋子righti - 1 = num2;else if (hang + i >= 16)/ 邊界righti - 1 = -2;return jieg
18、uo(left, right, num1, num2);public int zuoxie(int hang, int lie, int num1, int num2) / /int i;int left = new int4;int right = new int4;for (i = 1; i < 5; i+)if (hang + i < 16 && lie - i > -1&& qipanqizihang + ilie - i = num1)lefti - 1 = num1;else if (hang + i < 16 &&a
19、mp; lie - i > -1&& qipanqizihang + ilie - i = -1)/ 空lefti - 1 = -1;else if (hang + i < 16 && lie - i > -1&& qipanqizihang + ilie - i = num2)/ 不空,為對方棋子lefti - 1 = num2;else if (lie - i <= -1 | hang + i >= 16)/ 邊界lefti - 1 = -2;for (i = 1; i < 5; i+)if (hang -
20、 i > -1 && lie + i < 16&& qipanqizihang - ilie + i = num1)righti - 1 = num1;else if (hang - i > -1 && lie + i < 16&& qipanqizihang - ilie + i = -1)/ 空righti - 1 = -1;else if (hang - i > -1 && lie + i < 16&& qipanqizihang - ilie + i =
21、num2)/ 不空,為對方棋子righti - 1 = num2;else if (lie + i >= 16 | hang - i <= -1)/ 邊界righti - 1 = -2;return jieguo(left, right, num1, num2);public int youxie(int hang, int lie, int num1, int num2) / int i;int left = new int4;int right = new int4;for (i = 1; i < 5; i+)if (hang - i > -1 &&
22、 lie - i > -1&& qipanqizihang - ilie - i = num1)lefti - 1 = num1;else if (hang - i > -1 && lie - i > -1&& qipanqizihang - ilie - i = -1)/ 空lefti - 1 = -1;else if (hang - i > -1 && lie - i > -1&& qipanqizihang - ilie - i = num2)/ 不空,為對方棋子lefti -
23、1 = num2;else if (lie - i <= -1 | hang - i <= -1)/ 邊界lefti - 1 = -2;for (i = 1; i < 5; i+)if (hang + i < 16 && lie + i < 16&& qipanqizihang + ilie + i = num1)righti - 1 = num1;else if (hang + i < 16 && lie + i < 16&& qipanqizihang + ilie + i = -1)
24、/ 空righti - 1 = -1;else if (hang + i < 16 && lie + i < 16&& qipanqizihang + ilie + i = num2)/ 不空,為對方棋子righti - 1 = num2;else if (lie + i >= 16 | hang + i >= 16)/ 邊界righti - 1 = -2;return jieguo(left, right, num1, num2);public int jieguo(int left, int right, int num1, int
25、num2) int i, j;int qixing = new int9;qixing0 = left3;qixing1 = left2;qixing2 = left1;qixing3 = left0;qixing4 = num1;qixing5 = right0;qixing6 = right1;qixing7 = right2;qixing8 = right3;for (i = 0; i <= 4; i+) if (qixingi = num1 && qixingi + 1 = num1&& qixingi + 2 = num1 &&
26、qixingi + 3 = num1&& qixingi + 4 = num1)return 3000000;for (i = 0; i <= 3; i+) if (qixingi = -1 && qixingi + 1 = num1&& qixingi + 2 = num1 && qixingi + 3 = num1&& qixingi + 4 = num1 && qixingi + 5 = -1) return 300000;/ ?AAAA?for (i = 0; i <= 4; i
27、+) if (qixingi = num1 && qixingi + 1 = num1&& qixingi + 2 = num1 && qixingi + 3 = num1&& qixingi + 4 = -1)return 2500;/ AAAA?for (i = 0; i <= 4; i+) if (qixingi = -1 && qixingi + 1 = num1&& qixingi + 2 = num1 && qixingi + 3 = num1&&
28、qixingi + 4 = num1)return 2500;/ ?AAAAfor (i = 0; i <= 4; i+) if (qixingi = num1 && qixingi + 1 = num1&& qixingi + 2 = num1 && qixingi + 3 = -1&& qixingi + 4 = num1) return 3000;/ AAA?Afor (i = 0; i <= 4; i+) if (qixingi = num1 && qixingi + 1 = -1&&a
29、mp; qixingi + 2 = num1 && qixingi + 3 = num1&& qixingi + 4 = num1) return 3000;/ A?AAAfor (i = 0; i <= 4; i+) if (qixingi = num1 && qixingi + 1 = num1&& qixingi + 2 = -1 && qixingi + 3 = num1&& qixingi + 4 = num1) return 2600;/ AA?AAfor (i = 0; i &l
30、t;= 2; i+) if (qixingi = -1 && qixingi + 1 = -1 && qixingi + 2 = num1&& qixingi + 3 = num1 && qixingi + 4 = num1&& qixingi + 5 = -1 && qixingi + 6 = -1) return 3000;/ ?AAA?for (i = 0; i <= 4; i+) if (qixingi + 0 = num1 && qixingi + 1 = num1&
31、amp;& qixingi + 2 = num1 && qixingi + 3 = -1&& qixingi + 4 = -1)return 500;/ AAA?for (i = 0; i <= 4; i+) if (qixingi + 0 = -1 && qixingi + 1 = -1&& qixingi + 2 = num1 && qixingi + 3 = num1&& qixingi + 4 = num1)return 500;/ ?AAAfor (i = 0; i <
32、= 3; i+) if (qixingi = -1 && qixingi + 1 = num1 && qixingi + 2 = -1&& qixingi + 3 = num1 && qixingi + 4 = num1&& qixing5 = -1) return 800;/ ?A?AA?for (i = 0; i <= 3; i+) if (qixingi = -1 && qixingi + 1 = num1&& qixingi + 2 = num1 &&
33、qixingi + 3 = -1&& qixingi + 4 = num1 && qixingi + 5 = -1) return 800;/ ?AA?A?for (i = 0; i <= 4; i+) if (qixingi = num1 && qixingi + 1 = -1 && qixingi + 2 = -1&& qixingi + 3 = num1 && qixingi + 4 = num1) return 600;/ A?AAfor (i = 0; i <= 4; i+)
34、if (qixingi = num1 && qixingi + 1 = num1&& qixingi + 2 = -1 && qixingi + 3 = -1&& qixingi + 4 = num1) return 600;/ AA?Afor (i = 0; i <= 4; i+) if (qixingi = num1 && qixingi + 1 = -1&& qixingi + 2 = num1 && qixingi + 3 = -1&& qixingi
35、+ 4 = num1) return 550;/ A?A?Afor (i = 0; i <= 1; i+) if (qixingi = -1 && qixingi + 1 = -1 && qixingi + 2 = -1&& qixingi + 3 = num1 && qixingi + 4 = num1&& qixingi + 5 = -1 && qixingi + 6 = -1&& qixingi + 7 = -1) return 650;/ ?AA?for (i = 0;
36、 i <= 4; i+) if (qixingi = num1 && qixingi + 1 = num1&& qixingi + 2 = -1 && qixingi + 3 = -1&& qixingi + 4 = -1) return 150;/ AA?for (i = 0; i <= 4; i+) if (qixingi = -1 && qixingi + 1 = -1 && qixingi + 2 = -1&& qixingi + 3 = num1 &&a
37、mp; qixingi + 4 = num1) return 150;/ ?AAfor (i = 0; i <= 2; i+) if (qixingi = -1 && qixingi + 1 = -1 && qixingi + 2 = num1&& qixingi + 3 = -1 && qixingi + 4 = num1&& qixingi + 5 = -1 && qixingi + 6 = -1) return 250;/ ?A?A?for (i = 0; i <= 3; i+)
38、if (qixingi = -1 && qixingi + 1 = num1 && qixingi + 2 = -1&& qixingi + 3 = -1 && qixingi + 4 = num1&& qixingi + 5 = -1) return 200;/ ?A?A?return 10;2、 整體代碼主類(Chessboard):實現(xiàn)了程序界面的生成和事件監(jiān)聽,繼承了JFrame,作為面板。加入了兩個容器,一個是棋盤,一個是容器包含兩個按鈕。實現(xiàn)了兩個按鈕的功能-調(diào)用棋盤類的函數(shù)去重繪并且選擇算法。pack
39、age 繪制圖形1;import ;import ;import javax.swing.*;import .*;import java.awt.event.*;import java.awt.*;public class Chessboard extends JFrame implements ActionListener private JButton jbt1 = new JButton("人人對戰(zhàn)");private JButton jbt2 = new JButton("人機對戰(zhàn)");/private JButton jbt3 = new JB
40、utton("悔棋");/private JFrame Iswin = new JFrame();qipan zhegeqipan = new qipan();public Chessboard() Container container = getContentPane();container.setLayout(new BorderLayout();JPanel p1 = new JPanel();p1.setLayout(new GridLayout(1, 2);p1.add(jbt1);p1.add(jbt2);/p1.add(jbt3);container.add
41、(zhegeqipan, BorderLayout.CENTER);container.add(p1, BorderLayout.SOUTH);jbt1.addActionListener(this);jbt2.addActionListener(this);/jbt3.addActionListener(this);public void actionPerformed(ActionEvent e) / Graphics/ g=zhegeqipan.getGraphics();if (e.getActionCommand() = "人人對戰(zhàn)") / zhegeqipan.
42、paintComponent(g);zhegeqipan.huifu();zhegeqipan.repaint();zhegeqipan.renji = false;System.out.println("renrenn");if (e.getActionCommand() = "人機對戰(zhàn)") zhegeqipan.huifu();zhegeqipan.repaint();zhegeqipan.renji = true;System.out.println("renjin");/if (e.getActionCommand() = &
43、quot;悔棋")/System.out.println("悔棋n");/ System.out.println("The"+e.getActionCommand()+"button is"+"clicked atn"/*+new/ java.util.Date(e.getWhen()*/);public static void main(String args) Chessboard frame = new Chessboard();frame.setVisible(true);frame.setRes
44、izable(false);frame.setTitle("五子棋");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(600, 650); 棋子類(point):定義了棋子的屬性,包括棋子的坐標(biāo),半徑,顏色,和繪制黑白3D棋子的函數(shù)。package 繪制圖形1;import java.awt.Graphics;import java.awt.Color;import javax.swing.*;import .*;import .*;import java.awt.*;import ja
45、va.awt.geom.Ellipse2D;import java.awt.Graphics2D;import java.awt.RadialGradientPaint;import java.awt.RenderingHints;public class Point int pointx, pointy, pointr;Color pointcolor;boolean islive = false;public Point(int x, int y, Color color) pointx = x;pointy = y;pointcolor = color;public Point(Poin
46、t point, Graphics g) / z=30黑,z=130+白pointx = point.pointx;pointy = point.pointy;pointcolor = point.pointcolor;if (pointx > -1 && pointx < 16 && pointy > -1 && pointy < 16&& pointcolor = Color.black/* |point.islive=false */)Graphics2D g2d = (Graphics2D) g;R
47、adialGradientPaint paint = new RadialGradientPaint(pointx + 1) * 35 + 10, (pointy + 1) * 35 + 10, 30,new float 0f, 1f , new Color Color.WHITE,Color.BLACK );g2d.setPaint(paint);g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);g2d.setRenderingHint(RenderingHints.
48、KEY_ALPHA_INTERPOLATION,RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT);Ellipse2D e = new Ellipse2D.Float(pointx * 35 + 15,pointy * 35 + 15, 35, 35);g2d.fill(e);if (pointx > -1 && pointx < 16 && pointy > -1 && pointy < 16&& pointcolor = Color.white/* |po
49、int.islive=false */)Graphics2D g2d = (Graphics2D) g;RadialGradientPaint paint = new RadialGradientPaint(pointx + 1) * 35 + 10, (pointy + 1) * 35 + 10, 130,new float 0f, 1f , new Color Color.WHITE,Color.BLACK );g2d.setPaint(paint);g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VA
50、LUE_ANTIALIAS_ON);g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT);Ellipse2D e = new Ellipse2D.Float(pointx * 35 + 15,pointy * 35 + 15, 35, 35);g2d.fill(e);public int getX() return pointx;public int getY() return pointy;public Color getCol
51、or() return pointcolor;棋盤類(qipan):包含了程序的核心算法,鼠標(biāo)監(jiān)聽和棋盤的屬性和繪制。包括了判斷輸贏的函數(shù)和人機對戰(zhàn)的函數(shù)。package 繪制圖形1;import java.awt.Graphics;import java.awt.Color;import java.awt.Cursor;import javax.swing.*;import javax.swing.event.*;import java.awt.event.*;import java.awt.geom.Ellipse2D;import java.awt.Graphics2D;import java.awt.RadialGradientPaint;import java.awt.RenderingHints;public class qipan extends JPanel implements MouseMotionListener, MouseListener Point point = new Point16 * 16;public int qipanqizi = new int1616;public int qixingPC = new int1616;public int qixingPlayer = new int
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年廣東省建筑安全員-C證考試(專職安全員)題庫及答案
- 2025貴州省建筑安全員-C證考試(專職安全員)題庫附答案
- 2025山東省安全員知識題庫及答案
- 2025河南省安全員-C證(專職安全員)考試題庫
- 食品加工的原料和材料-課件
- 中醫(yī)內(nèi)科學(xué)-感冒
- 《教師節(jié)綜合實踐》課件
- 有機藥化實驗課件
- 《急腹癥寶力道》課件
- 《運籌學(xué)》整數(shù)規(guī)劃
- 《聯(lián)合國教科文:學(xué)生人工智能能力框架》-中文版
- 高中生物必修一知識點總結(jié)(必修1)
- 《風(fēng)力發(fā)電技術(shù)》課件-第三章 機組運行與維護
- 物料報廢回收合同范本
- 科研機構(gòu)成果轉(zhuǎn)化困境與對策
- 選礦廠建設(shè)課件
- DB32T4065-2021建筑幕墻工程技術(shù)標(biāo)準(zhǔn)
- 中國超重肥胖醫(yī)學(xué)營養(yǎng)治療指南
- 現(xiàn)代營銷學(xué)原理課件
- 《5G無線網(wǎng)絡(luò)規(guī)劃與優(yōu)化》 課件 第1、2章 5G網(wǎng)絡(luò)概述、5G關(guān)鍵技術(shù)
- 屈原【六幕話劇】郭沫若
評論
0/150
提交評論