连连看游戏课程设计+源代码
游戏程序设计(2)--“连连看”设计

3、消除相同图案(相同数字)的图形 块
当2个不同图形块的图案相同时,则将其设为 setVisible(false)。 if(n1 == n2 && btn2 != btn1) { btn2.setVisible(false); btn1.setVisible(false); }
【例1-4】消除两个相同图案的图形块
• (4)如果P1、P2 在同一直线上,但不相邻,如图15.4(b)所示。 先判断两个点P1,P2之间是否能直接建立连接(连线之间无折点), 如果可以的话消去该两点,如果不可以的话就在其它三个方向上的空 点按照折点法基本概念开始取折点Z1, Z2, 然后判断 P1 — Z1, Z1—Z2,Z2—P2 是否能连通,如果都可以连通就消去P1, P2两点。 要消去的两个点不在同一直线上也不相邻。
• 1、“连连看”游戏界面算法设计思路
• 在设计“连连看”游戏时,我们把所有图形(30个)按如图15.5(a)所示 的方法排列,即把图形安排到8行×7列的表格中,四周有一排空格。把图形 块做成按钮,这样,游戏操作者点击图形块时,就能触发事件。再定义一组 二维数组与按钮的位置相对应,只要数组元素的值相同就代表图形相同。这 样,要判断二块图形是否相同,只要比较二维数组元素的值是否相同。如图 15.5(b)所示。
•
x1 == x2 且 y1 == y2 + 1,
•
或 x1 == x2 且 y1 == y2 – 1,
•
或 x1 == x2 + 1 且 y1 == y2,
•
或 x1 == x2 - 1 且 y1 == y2。
• 即:
• (x1 == x2 && (y1 == y2+1 || y1 == y2-1)) ||
java小游戏连连看源代码

JAVA小游戏报告院别电子信息工程专业计算机科学与技术班级01班学号121040210116姓名姚银杰西安思源学院教务处制二零一四年连连看java源代码import javax.swing.*;import java.awt.*;import java.awt.event.*;public class lianliankan implements ActionListener{JFrame mainFrame; //主面板Container thisContainer;JPanel centerPanel,southPanel,northPanel; //子面板JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮JLabel fractionLable=new JLabel("0"); //分数标签JButton firstButton,secondButton; //分别记录两次被选中的按钮int grid[][] = new int[8][7];//储存游戏按钮位置static boolean pressInformation=false; //判断是否有按钮被选中int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标int i,j,k,n;//消除方法控制public void init(){mainFrame=new JFrame("JKJ连连看");thisContainer = mainFrame.getContentPane();thisContainer.setLayout(new BorderLayout());centerPanel=new JPanel();southPanel=new JPanel();northPanel=new JPanel();thisContainer.add(centerPanel,"Center");thisContainer.add(southPanel,"South");thisContainer.add(northPanel,"North");centerPanel.setLayout(new GridLayout(6,5));for(int cols = 0;cols < 6;cols++){for(int rows = 0;rows < 5;rows++ ){diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1])); diamondsButton[cols][rows].addActionListener(this);centerPanel.add(diamondsButton[cols][rows]);}exitButton=new JButton("退出");exitButton.addActionListener(this);resetButton=new JButton("重列");resetButton.addActionListener(this);newlyButton=new JButton("再来一局");newlyButton.addActionListener(this);southPanel.add(exitButton);southPanel.add(resetButton);southPanel.add(newlyButton);fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText()))); northPanel.add(fractionLable);mainFrame.setBounds(280,100,500,450);mainFrame.setVisible(true);}public void randomBuild() {int randoms,cols,rows;for(int twins=1;twins<=15;twins++) {randoms=(int)(Math.random()*25+1);for(int alike=1;alike<=2;alike++) {cols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);while(grid[cols][rows]!=0) {cols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);}this.grid[cols][rows]=randoms;}}}public void fraction(){fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100)); }public void reload() {int save[] = new int[30];int n=0,cols,rows;int grid[][]= new int[8][7];for(int i=0;i<=6;i++) {for(int j=0;j<=5;j++) {if(this.grid[i][j]!=0) {save[n]=this.grid[i][j];n++;}}n=n-1;this.grid=grid;while(n>=0) {cols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);while(grid[cols][rows]!=0) {cols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);}this.grid[cols][rows]=save[n];n--;}mainFrame.setVisible(false);pressInformation=false; //这里一定要将按钮点击信息归为初始init();for(int i = 0;i < 6;i++){for(int j = 0;j < 5;j++ ){if(grid[i+1][j+1]==0)diamondsButton[i][j].setVisible(false);}}}public void estimateEven(int placeX,int placeY,JButton bz) {if(pressInformation==false) {x=placeX;y=placeY;secondMsg=grid[x][y];secondButton=bz;pressInformation=true;}else {x0=x;y0=y;fristMsg=secondMsg;firstButton=secondButton;x=placeX;y=placeY;secondMsg=grid[x][y];secondButton=bz;if(fristMsg==secondMsg。
连连看源代码

panel1.add(b7); panel1.add(b8); panel1.add(b9);
panel1.add(b10); panel1.add(b11); panel1.add(b12);
f.pack();
f.setBounds(25,10,600,750);
f.setResizable(false);
f.setVisible(true);
bc.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) { ex(); }
bc=new Button(" EXIT");
br=new Button("RESET");
p1=new Button();
b1=new Button(String.valueOf(d[1][1]));
b2=new Button(String.valueOf(d[1][2]));
});
b23=new Button(String.valueOf(d[5][3]));
b24=new Button(String.valueOf(d[5][4]));
b25=new Button(String.valueOf(d[5][5]));
b26=new Button(String.valueOf(d[6][1]));
public void mouseClicked(MouseEvent e){ wei(1,1,b1); }
});
连连看源码(C++)

{ int i,j; InitMap(); //map[38]=map[0]=0; //memcpy(map,in_map,MAP_SIZE*sizeof(int)); for (i=0;i<MAP_SIZE;i++){ printf("%2d",map[i]); if(i%MAP_WIDTH==MAP_WIDTH-1) printf("\n"); } printf("\n"); for (i=0;i<TYPE;i++){ for(j=0;j<16;j++){ printf("%-3d ",pic[i][j]); } printf("\n"); } printf("%d %d\n",Match(20,23),Match(24,27)); printf("%d %d\n",Match(57,94),Match(155,159)); int sq[2],type; type=Prompt(sq); while(type!=-1){ printf("%-2d:%-3d,%-3d:%3d\n",type,sq[0],sq[1],totpic); DelPic(sq[0]);DelPic(sq[1]); type=Prompt(sq); } printf("totpic:%d\n",totpic); return 0;
int i,j; int td,tid; for (i=0;i<n-1;i++){
for (j=i;j<n;j++){ if (data[i]>data[j]){ td=data[i];data[i]=data[j];data[j]=td; tid=id[i];id[i]=id[j];id[j]=tid; }
《连连看》算法c语言演示(自动连连看)

《连连看》算法c语⾔演⽰(⾃动连连看)(图⽚是游戏的⽰意图,来⾃互联⽹,与本⽂程序⽆关)看题⽬就知道是写给初学者的,没需要的就别看了,⾃⼰都觉得怪⽆聊的。
很多游戏的耐玩性都来⾃精巧的算法,特别是⼈⼯智能的⽔平。
⽐如前⼏天看了著名的Alpha GO的算法,⽤了复杂的⼈⼯智能⽹络。
⽽最简单的,可能就是连连看了,所以很多⽼师留作业,直接就是实现连连看。
连连看游戏的规则⾮常简单:1. 两个图⽚相同。
2. 两个图⽚之间,沿着相邻的格⼦画线,中间不能有障碍物。
3. 画线中间最多允许2个转折。
所以算法主要是这样⼏部分:1. ⽤数据结构描述图板。
很简单,⼀个2维的整数数组,数组的值就是图⽚的标志,相同的数字表⽰相同的图⽚。
有⼀个⼩的重点就是,有些连连看的地图中,允许在边界的两个图⽚,从地图外连线消除。
这种情况⼀般需要建⽴的图板尺⼨,⽐实际显⽰的图板,周边⼤⼀个格⼦,从⽽描述可以连线的空⽩外边界。
本例中只是简单的使⽤完整的图板,不允许利⽤边界外连线。
2. ⽣成图板。
通常⽤随机数产⽣图⽚ID来填充图板就好。
⽐较复杂的游戏,会有多种的布局⽅式,例如两个三⾓形。
这种⼀般要⼿⼯编辑图板模板,在允许填充的区域事先⽤某个特定的整数值来标注,随后的随机数填充只填充允许填充的区域。
本例中只是简单的随机填充。
3. 检查连线中的障碍物。
确定有障碍物的关键在于确定什么样的格⼦是空。
通常定义格⼦的值为0就算空。
要求所有的图⽚ID从1开始顺序编码。
复杂的游戏还会定义负数作为特定的标志,⽐如允许填充区之类的。
4. 检查直接连接:两张图⽚的坐标,必然x轴或者y轴有⼀项相同,表⽰两张图⽚在x轴或者y轴的同⼀条线上才可能出现直接连接。
随后循环检查两者之间是否有障碍物即可确定。
5. 检查⼀折连接:与检查直接连接相反,两个图⽚必须不在⼀条直线上,才可能出现⼀折连接,也就是x/y必须都不相同。
随后以两张图⽚坐标,可以形成⼀个矩阵,矩阵的⼀对对⾓是两张图⽚,假设是A/B两点。
JAVA连连看课程设计报告

滨江学院实验报告||实验名称JAVA小游戏(连连看)设计课程名称智能手机程序设计| |专业班级:信息工程1班学生姓名:车宇翔学号:20112309002指导教师:高超学期:2013-2014(2)成绩:【选题背景】:连连看游戏经验,玩法简单,休闲,益智,趣味,广受欢迎。
【选题目的】:学会JAVA程序开发的环境搭建与配置,并在实际运用中学习和掌握JAVA程序开发的全过程。
进一步熟悉掌握JAVA程序设计语音的基础内容,如用户图形界面设计、JAVA多线程编程、JAVA数据库编程等。
通过亲自动手写程序,拓展知识面,锻炼调试能力。
【系统分析与设计】:功能分析:实现连连看的基本游戏功能和重置、提示、消除功能设计:通过对图片的调用以及设置是否可见来完成连连看的效果【课程设计中碰到的问题及解决方案】:1.不知道如何进行对数组中两个元素是否可以消除的判断2.时间条的动态表现解决方案:1.对每个相同图案进行循环判断,直到找出满足条件的情况boolean verticalMatch(Point a, Point b) // 竖线上的判断boolean horizonMatch(Point a, Point b) // 横线上的判断2.为了保证动画过程和游戏过程的平行运行,因此将动画分离成一个独立的控件,并且要保证动画有自己单独的线程来运行。
当每次用户的分数发生变化时,我们可以使用setScore(int l, int c) 方法同步分数显示的动画效果。
【程序输出结果】:游戏开始【程序代码】:ImageFactorypackage nicholas.game.kyodai;import javax.swing.ImageIcon;import .*;public class ImageFactory {private static ImageFactory imagefactory;private static ImageIcon images[];private ImageFactory() {images = new ImageIcon[54];URLClassLoader loader = (URLClassLoader)getClass().getClassLoader();for(int i=0;i<39;i++) {images[i] = new ImageIcon(getClass().getResource("images/"+i+".gif"));}images[39] = new ImageIcon(getClass().getResource("images/dots.gif"));images[40] = new ImageIcon(getClass().getResource("images/ico.gif"));images[41] = new ImageIcon(getClass().getResource("images/topbar.gif"));images[42] = new ImageIcon(getClass().getResource("images/splash.gif"));images[43] = new ImageIcon(getClass().getResource("images/sico.gif"));}public ImageIcon getImageicon(int i) {return images[i];}public static synchronized ImageFactory getInstance() {if(imagefactory != null) {return imagefactory;} else {imagefactory = new ImageFactory();return imagefactory;}}}KyodaiGridpackage nicholas.game.kyodai;import java.awt.*;import javax.swing.*;public class KyodaiGrid extends JLabel {private int xpos;private int ypos;public KyodaiGrid(int x, int y) {xpos = x;ypos = y;this.setHorizontalAlignment(SwingConstants.CENTER);}public int getXpos() {return xpos;}public int getYpos() {return ypos;}public boolean isPassable() {return !isVisible();}}LevelInfopackage nicholas.game.kyodai;import java.io.Serializable;public class LevelInfo implements Serializable {//xBound为行号,yBound为列号private int xBound;private int yBound;public LevelInfo() {xBound = 16;yBound = 9;}public LevelInfo(int x, int y){xBound = x;yBound = y;}public int getXBound() {return xBound;}public int getYBound() {return yBound;}}MainFrame.javapackage nicholas.game.kyodai;import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;import nicholas.swing.AboutDialog;import nicholas.swing.JSplashWindow;public class MainFrame extends JFrame implements ActionListener {private JMenuItem aboutItem;//菜单栏private JMenuItem exitItem;private JMenuItem startItem;private JMenuItem optionItem;private JMenuItem tipItem;private JMenuItem refreshItem;private JMenuItem logItem;private JMenuItem bombItem;private JMenuItem pauseItem;private MainPanel mainPanel;//完成主要功能private LevelInfo levelInfo;public MainFrame() {super("连连看");levelInfo = new LevelInfo();//设定游戏大小setMenuBar();//设置菜单setUI();setIconImage(ImageFactory.getInstance().getImageicon(43).getImage());setSize(650,520);Dimension screen = getToolkit().getScreenSize();setLocation((screen.width-getSize().width)/2,(screen.height-getSize().height)/2);this.setVisible(true);addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});}private void setMenuBar() {JMenu fileMenu = new JMenu("游戏(G)");JMenu helpMenu = new JMenu("帮助(H)");JMenu contMenu = new JMenu("辅助(C)");fileMenu.setMnemonic('G');helpMenu.setMnemonic('H');contMenu.setMnemonic('C');startItem = new JMenuItem("开局(N)");startItem.setMnemonic('N');startItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2,0));pauseItem = new JMenuItem("暂停(P)");pauseItem.setMnemonic('P');pauseItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PAUSE,0));refreshItem = new JMenuItem("刷新(R)");refreshItem.setMnemonic('R');refreshItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,0));tipItem = new JMenuItem("提示(T)");tipItem.setMnemonic('T');tipItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0));optionItem = new JMenuItem("选项(O)...");optionItem.setMnemonic('O');logItem = new JMenuItem("排行榜(B)...");logItem.setMnemonic('B');exitItem = new JMenuItem("退出(X)");exitItem.setMnemonic('X');aboutItem = new JMenuItem("关于(A)...");aboutItem.setMnemonic('A');aboutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,0));bombItem = new JMenuItem("炸弹(M)");bombItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,0));bombItem.setMnemonic('M');startItem.addActionListener(this);pauseItem.addActionListener(this);refreshItem.addActionListener(this);tipItem.addActionListener(this);optionItem.addActionListener(this);logItem.addActionListener(this);exitItem.addActionListener(this);aboutItem.addActionListener(this);bombItem.addActionListener(this);fileMenu.add(startItem);fileMenu.add(pauseItem);contMenu.add(refreshItem);contMenu.add(bombItem);contMenu.add(tipItem);fileMenu.addSeparator();fileMenu.add(exitItem);helpMenu.add(aboutItem);helpMenu.add(contMenu);JMenuBar bar = new JMenuBar();bar.add(fileMenu);bar.add(helpMenu);setJMenuBar(bar);}private void setUI() {mainPanel = new MainPanel(levelInfo);getContentPane().add(mainPanel,BorderLayout.CENTER); }public static void main(String args[]) {MainFrame application = new MainFrame();}private void showAboutDialog() {String s1="作者:车宇翔";String s2="邮箱: 531608022@";String s3="Have Fun!!";TextArea ta=new TextArea();ta.setText(s1+"\n"+"\n"+"\n"+s2+"\n"+"\n"+"\n"+s3);ta.setEditable(false);JFrame f=new JFrame("关于");f.setLocation(300, 300);f.setSize(200,200);f.add(ta);f.setBackground(new Color(200,120,150));f.setResizable(false);f.setVisible(true);}public void actionPerformed(ActionEvent ae) { if(ae.getSource()==startItem) {mainPanel.restart();} else if(ae.getSource()==pauseItem) {mainPanel.setPaused(!mainPanel.isPaused());} else if(ae.getSource()==exitItem) {System.exit(0);} else if(ae.getSource()==aboutItem) {showAboutDialog();} else if(ae.getSource()==bombItem) {eBomb();} else if(ae.getSource()==refreshItem) {mainPanel.refresh();} else if(ae.getSource()==tipItem) {mainPanel.showNext();}}}MainPanel.javapackage nicholas.game.kyodai;import java.awt.*;import java.awt.event.*;import java.util.Vector;import javax.swing.*;import javax.swing.border.Border;import nicholas.game.kyodai.*;public class MainPanel extends JPanel {private int BOMB = 5;private int BOMBP = 200;private int REFRESH = 4;private int REFRP = 250;private int TIP = 7;private int TIPP = 120;private int PROGRESS = 1200;private int xBound;private int yBound;private int pcount;private int score;private int refreshcount;private int bombcount;private int tipcount;private LevelInfo levelInfo; private GridMouseAdapter gma; private KyodaiGrid grid[][]; private KyodaiGrid nexts, nexte;private Border selectedBorder; private Border opaqueBorder; private Border tipBorder;private Vector path[];private Thread pthread;private JProgressBar progress; private JLabel scoreLabel; private JLabel refreshLabel; private JLabel bombLabel;private JLabel tipLabel;private JPanel gridPanel;private boolean wingame;public MainPanel(LevelInfo li) {super(new BorderLayout());levelInfo = li;path = new Vector[3];path[0] = new Vector();path[1] = new Vector();path[2] = new Vector();setBackground(Color.black);gma = new GridMouseAdapter();opaqueBorder = BorderFactory.createLineBorder(getBackground());//selectedBorder = BorderFactory.createLineBorder(Color.red);selectedBorder = BorderFactory.createLineBorder(Color.red);tipBorder = BorderFactory.createLineBorder(Color.green);setGridPanel();setStatusPanel();}/***设置状态面板*/private void setStatusPanel() {wingame = false;JPanel panel = new JPanel();panel.setBackground(Color.black);JLabel label = new JLabel("剩余时间:");label.setForeground(Color.white);panel.add(label);progress = new JProgressBar(0,PROGRESS);//时间条显示progress.setValue(PROGRESS);progress.setPreferredSize(new Dimension(400,20));progress.setForeground(Color.blue);progress.setBorderPainted(false);panel.add(progress);score = 0;scoreLabel = new JLabel(""+score);scoreLabel.setForeground(Color.yellow);scoreLabel.setFont(new Font("Dialog",Font.BOLD,25));scoreLabel.setHorizontalAlignment(SwingConstants.RIGHT);scoreLabel.setPreferredSize(new Dimension(100,20));panel.add(scoreLabel);add(panel,BorderLayout.NORTH);panel = new JPanel();panel.setBackground(Color.black);label = new JLabel("剩余提示:");label.setForeground(Color.yellow);panel.add(label);tipcount = TIP;tipLabel = new JLabel(""+tipcount);tipLabel.setForeground(Color.green);panel.add(tipLabel);label = new JLabel("剩余炸弹:");label.setForeground(Color.yellow);panel.add(label);bombcount = BOMB;bombLabel = new JLabel(""+bombcount);bombLabel.setForeground(Color.green);panel.add(bombLabel);label = new JLabel("可用刷新:");label.setForeground(Color.yellow);panel.add(label);refreshcount = REFRESH;refreshLabel = new JLabel(""+refreshcount);refreshLabel.setForeground(Color.green);panel.add(refreshLabel);add(panel,BorderLayout.SOUTH);pthread = new ProgressThread();pthread.start();}private void setGridPanel() {//完成布局gridPanel = new JPanel();gridPanel.setBackground(getBackground());xBound = levelInfo.getXBound()+2;yBound = levelInfo.getYBound()+2;gridPanel.setLayout(new GridLayout(yBound,xBound,0,0));grid = new KyodaiGrid[yBound][xBound];int count = 0;int sub = levelInfo.getXBound()*levelInfo.getYBound()/4;KyodaiGrid temp[] = new KyodaiGrid[xBound*yBound];for(int y=0;y<yBound;y++) {for(int x=0;x<xBound;x++) {grid[y][x] = new KyodaiGrid(x, y);if(x==0||x==(xBound-1)||y==0||y==(yBound-1)) {grid[y][x].setIcon(ImageFactory.getInstance().getImageicon(39));grid[y][x].setVisible(false);} else {grid[y][x].setIcon(ImageFactory.getInstance().getImageicon(count%sub));grid[y][x].setBorder(opaqueBorder);grid[y][x].addMouseListener(gma);temp[count] = grid[y][x];count++;}gridPanel.add(grid[y][x]);}}JPanel t = new JPanel();t.setBackground(Color.black);t.add(gridPanel);add(t,BorderLayout.CENTER);shuffle(temp, count);}/***开始新游戏*/public void restart() {//重新开始resetStatusPanel();resetGridPanel();}/***重置面板状态和游戏图标*/private void resetStatusPanel() {wingame = false;score = 0;scoreLabel.setText(""+score);bombcount = BOMB;bombLabel.setText(""+bombcount);refreshcount = REFRESH;refreshLabel.setText(""+refreshcount);tipcount = TIP;tipLabel.setText(""+tipcount);progress.setValue(PROGRESS);pthread.resume();}private void resetGridPanel() {int count = 0;int sub = (xBound-2)*(yBound-2)/4;KyodaiGrid temp[] = new KyodaiGrid[xBound*yBound];for(int y=1;y<yBound-1;y++) {for(int x=1;x<xBound-1;x++){grid[y][x].setIcon(ImageFactory.getInstance().getImageicon(count%sub));grid[y][x].setBorder(opaqueBorder);grid[y][x].setVisible(true);temp[count] =grid[y][x];count++;}}shuffle(temp,count);}/***暂停*/public void setPaused(boolean p) {if(p) {pthread.suspend();gridPanel.setVisible(false);} else {pthread.resume();gridPanel.setVisible(true);}}/***是否暂停*/public boolean isPaused() {return !gridPanel.isVisible();}/***没有布局存在时胜利*计算得分*/private void win() {wingame = true;pthread.suspend();score += progress.getValue()/20+bombcount*BOMBP+refreshcount*REFRP+tipcount*TIPP;scoreLabel.setText(""+score);}private void shuffle(KyodaiGrid array[], int count) {if(wingame) return;do {setVisible(false);int j,k;Icon temp;for(int i=0;i<count;i++) {j = (int)(Math.random()*count);k = (int)(Math.random()*count);temp = array[k].getIcon();array[k].setIcon(array[j].getIcon());array[j].setIcon(temp);}setVisible(true);} while(!findPair());}public void refresh() {if(wingame||progress.getValue()==0||refreshcount==0) return;KyodaiGrid temp[] = new KyodaiGrid[xBound*yBound];int count = 0;for(int y=1;y<yBound-1;y++) {for(int x=1;x<xBound-1;x++) {if(grid[y][x].isVisible()) {grid[y][x].setBorder(opaqueBorder);temp[count] = grid[y][x];count++;}}}if(count!=0) {refreshcount--;refreshLabel.setText(""+refreshcount);shuffle(temp,count);} else win();}private boolean xdirect(KyodaiGrid start, KyodaiGrid end,Vector path) { if(start.getYpos()!=end.getYpos()) return false;int direct = 1;if(start.getXpos()>end.getXpos()) {direct = -1;}path.removeAllElements();for(intx=start.getXpos()+direct;x!=end.getXpos()&&x<xBound&&x>=0;x+=direct) { if(grid[start.getYpos()][x].isVisible()) return false;path.add(grid[start.getYpos()][x]);}path.add(end);return true;}private boolean ydirect(KyodaiGrid start, KyodaiGrid end,Vector path) { if(start.getXpos()!=end.getXpos()) return false;int direct = 1;if(start.getYpos()>end.getYpos()) {direct = -1;}path.removeAllElements();for(inty=start.getYpos()+direct;y!=end.getYpos()&&y<yBound&&y>=0;y+=direct) { if(grid[y][start.getXpos()].isVisible()) return false;path.add(grid[y][start.getXpos()]);}path.add(end);return true;}private int findPath(KyodaiGrid start, KyodaiGrid end) {//0 connerif(xdirect(start,end,path[0])) {return 1;}if(ydirect(start,end,path[0])) {return 1;}//1 connerKyodaiGrid xy = grid[start.getYpos()][end.getXpos()];if(!xy.isVisible()&&xdirect(start,xy,path[0])&&ydirect(xy,end,path[1])) {return 2;}KyodaiGrid yx = grid[end.getYpos()][start.getXpos()];if(!yx.isVisible()&&ydirect(start,yx,path[0])&&xdirect(yx,end,path[1])) {return 2;}//2 conner//uppath[0].removeAllElements();for(int y=start.getYpos()-1;y>=0;y--) {xy = grid[y][start.getXpos()];yx = grid[y][end.getXpos()];if(xy.isVisible()) break;path[0].add(xy);if(!yx.isVisible()&&xdirect(xy,yx,path[1])&&ydirect(yx,end,path[2])) {return 3;}}//downpath[0].removeAllElements();for(int y=start.getYpos()+1;y<yBound;y++) {xy = grid[y][start.getXpos()];yx = grid[y][end.getXpos()];if(xy.isVisible()) break;path[0].add(xy);if(!yx.isVisible()&&xdirect(xy,yx,path[1])&&ydirect(yx,end,path[2])) {return 3;}}//leftpath[0].removeAllElements();for(int x=start.getXpos()-1;x>=0;x--) {yx = grid[start.getYpos()][x];xy = grid[end.getYpos()][x];if(yx.isVisible()) break;path[0].add(yx);if(!xy.isVisible()&&ydirect(yx,xy,path[1])&&xdirect(xy,end,path[2])) {return 3;}}//rightpath[0].removeAllElements();for(int x=start.getXpos()+1;x<xBound;x++) {yx = grid[start.getYpos()][x];xy = grid[end.getYpos()][x];if(yx.isVisible()) break;path[0].add(yx);if(!xy.isVisible()&&ydirect(yx,xy,path[1])&&xdirect(xy,end,path[2])) {return 3;}}return 0;}/***在布局中消除配对*/private void deletePair(KyodaiGrid prev, KyodaiGrid current) {//尝试寻找路径//如果找到路径//animateVector temp = new Vector();temp.add(prev);for(int i=0;i<pcount;i++) {temp.addAll(path[i]);path[i].removeAllElements();}AnimateThread thread = new AnimateThread(temp);thread.start();score += progress.getValue()/20;scoreLabel.setText(""+score);progress.setValue(progress.getValue()+60);}/***展示找到的配对*/public void showNext() {if(wingame||progress.getValue()==0||tipcount==0) return;tipcount--;tipLabel.setText(""+tipcount);if(nexts!=null&&nexte!=null) {nexts.setBorder(tipBorder);nexte.setBorder(tipBorder);}}/***删除找到的配对*/public void useBomb() {if(wingame||progress.getValue()==0||bombcount==0) return;bombcount--;bombLabel.setText(""+bombcount);if(nexts!=null&&nexte!=null) {deletePair(nexts,nexte);}}/***发现有连接路径的配对*@返回是否发现*/private boolean findPair() {nexts = null;nexte = null;for(int sy=1;sy<yBound-1;sy++) {for(int sx=1;sx<xBound-1;sx++) {if(!grid[sy][sx].isVisible()) continue;for(int ey=sy;ey<yBound-1;ey++) {for(int ex=1;ex<xBound-1;ex++) {if(!grid[ey][ex].isVisible()||(ey==sy&&ex==sx)) continue;if(grid[sy][sx].getIcon()==grid[ey][ex].getIcon()) {pcount = findPath(grid[sy][sx],grid[ey][ex]);if(pcount!=0) {nexts = grid[sy][sx];nexte = grid[ey][ex];return true;}}}}}}return false;}private class GridMouseAdapter extends MouseAdapter { private KyodaiGrid prev;public void mouseClicked(MouseEvent me) {if(prev == null) {prev = (KyodaiGrid)me.getSource();prev.setBorder(selectedBorder);} else {if(progress.getValue()==0) return;KyodaiGrid current = (KyodaiGrid)me.getSource();if(current == prev) return;if(current.getIcon()==prev.getIcon()) {pcount = findPath(prev,current);if(pcount!=0) {deletePair(prev,current);//setprev = null;return;}}prev.setBorder(opaqueBorder);prev = current;prev.setBorder(selectedBorder);if(!findPair()) refresh();}}}private class AnimateThread extends Thread {private Vector v;public AnimateThread(Vector temp) {v = temp;}public void run() {KyodaiGrid prev = null;KyodaiGrid current;int j = 0;while(j<v.size()) {prev = (KyodaiGrid)v.remove(0);prev.setVisible(true);v.add(prev);j++;try {sleep(20);} catch(InterruptedException ire) {System.err.println("sleep interrupted");}}current = prev;prev = (KyodaiGrid)v.remove(0);while(!v.isEmpty()) {((KyodaiGrid)v.remove(0)).setVisible(false);try {sleep(20);} catch(InterruptedException ire) {System.err.println("sleep interrupted");}}prev.setVisible(false);current.setVisible(false);current.setIcon(ImageFactory.getInstance().getImageicon(39));prev.setIcon(ImageFactory.getInstance().getImageicon(39));current.setBorder(opaqueBorder);prev.setBorder(opaqueBorder);if(!findPair()) refresh();}//end of method run}private class ProgressThread extends Thread {public ProgressThread() {}public void run() {while(true) {while(progress.getValue()>0) {progress.setValue(progress.getValue()-1);try {sleep(100);} catch(InterruptedException ire) {System.err.println("sleep interrupted");}}repaint();suspend();}}}}【总结自己的体会和收获】:通过这次课程设计我学到了不少东西,也发现了大量的问题,同时在设计的过程中也发现了自己的不足之处,对以前学过的知识理解的不够深刻,对安卓手机小游戏的程序编写有了一定的了解。
连连看java代码课程设计

连连看java代码课程设计一、课程目标知识目标:1. 理解Java基本语法,掌握连连看游戏的核心算法;2. 学会使用Java编写图形用户界面,实现游戏窗口的布局与设计;3. 了解Java事件处理机制,能编写鼠标点击事件监听器。
技能目标:1. 培养学生运用Java语言解决实际问题的能力;2. 提高学生分析问题、设计算法、编写代码、调试程序的综合技能;3. 培养学生的创新思维和团队协作能力,通过分组完成任务,提高沟通与协作效率。
情感态度价值观目标:1. 培养学生对编程的兴趣,激发学习积极性,增强自信心;2. 培养学生面对困难时,保持积极的心态,勇于尝试,不断克服挫折;3. 培养学生的集体荣誉感,通过团队协作,共同完成任务,体验成功的喜悦。
分析课程性质、学生特点和教学要求,本课程将目标分解为以下具体学习成果:1. 学生能独立完成连连看游戏的Java代码编写,实现游戏的基本功能;2. 学生能通过小组合作,共同优化游戏界面,提升用户体验;3. 学生在课程学习过程中,能主动探究新知识,勇于挑战更高难度的编程任务;4. 学生在学习过程中,培养良好的编程习惯,注重代码规范和团队协作。
二、教学内容根据课程目标,教学内容主要包括以下几部分:1. Java基本语法:- 数据类型、变量、常量- 运算符、表达式、流程控制(分支、循环)- 数组、字符串2. Java图形用户界面设计:- Swing图形用户界面库简介- JFrame、JPanel容器组件- 布局管理器(边界布局、流布局等)3. Java事件处理机制:- 事件监听器、事件源- 鼠标事件、键盘事件- 事件适配器、事件处理线程4. 连连看游戏核心算法:- 游戏规则、数据结构设计- 算法实现(路径查找、消去逻辑等)- 游戏界面与逻辑分离教学大纲安排如下:1. 第一阶段(2课时):Java基本语法复习与巩固2. 第二阶段(2课时):Swing图形用户界面设计3. 第三阶段(2课时):Java事件处理机制4. 第四阶段(4课时):连连看游戏核心算法分析与实现教学内容与教材关联章节:1. 《Java程序设计》第3章:Java基本语法2. 《Java程序设计》第6章:Swing图形用户界面设计3. 《Java程序设计》第7章:Java事件处理机制4. 《Java程序设计》实例分析:连连看游戏案例三、教学方法针对本课程的教学目标和内容,采用以下多样化的教学方法:1. 讲授法:- 对于Java基本语法、Swing图形用户界面设计、Java事件处理机制等基础知识点,采用讲授法进行教学,使学生在短时间内掌握必要的理论知识;- 讲授过程中注重启发式教学,引导学生主动思考问题,提高课堂互动性。
JAVA课程设计连连看(含代码)

Java程序课程设计任务书1、主要内容:本程序基本实现了小游戏连连看的功能,玩家找出游戏中2个相同图案的方块,如果它们之间的连接线不多于3根直线,则将其连接起来,就可以成功将图案相同的方块消除,否则不会消失,当游戏中已没有满足条件的图案时,点击重列,可重新排序,游戏结束会跳出所得分数,该游戏的特点是与自己竞争,超过自己之前所创纪录。
2、具体要求(包括技术要求等):a. 该游戏界面为方格类型,由纵6横7的直线平行垂直交叉组成,分别是6行5列方块拼接,共有30格小方块。
方块上随机分布一些数字,数字的要求是至少两两相同,位置随机打乱。
b.当将相同数字的方块连接,但要满足只能至少单边无阻碍呈直线趋势连接,否则无效,若连接一对成功就消失于界面,继续游戏,直到游戏结束,并能返回所得分数。
c. 重列按钮(帮助)的功能:游戏过程中,遇到困难难以寻找下一符合要求的一对数字,可按左下按钮重置重新排列方可继续游戏。
d. 退出按钮:击左下方的“退出游戏”按钮,即可结束游戏。
e.运用基于SWING的图形用户界面设计知识等。
3、学习并掌握以下技术:Java等4、熟练使用以下开发工具:Jcreate 等实现系统上述的功能。
三、计划进度12月28日-12月29 日:课程设计选题,查找参考资料12月30日-12月31日:完成需求分析、程序设计1月1日-1月3日:完成程序代码的编写1月4日-1月6日:系统测试与完善1月7日-1月8日:完成课程设计报告,准备答辩四、主要参考文献[1] (美)埃克尔著陈昊鹏,饶若楠等译. Java编程思想[J]. 机械工业出版社,2005[2](美)Gary J.Bronson著张珑刘雅文译. Java编程原理[J]. 清华大学出版社,2004[3](美)Michael Morrison著徐刚,于健,薛雷译. 游戏编程入门[J]. 人民邮电出版社,2005.9[4](美)Wendy Stahler著冯宝坤,曹英译. 游戏编程中的数理应用[J]. 红旗出版社,2005[5](美)克罗夫特(David Wallace Croft)著彭晖译. Java游戏高级编程[J]. 清华大学出版社,2005[6](美)David Brackeen著邱仲潘译. Java游戏编程[J]. 科学出版社,2004[7] 聂庆亮编著. Java应用开发指南[J]. 清华大学出版社,2010[8] 耿祥义,张跃平编著. Java面向对象程序设计[J]. 清华大学出版社,2010[9] 杨绍方编著. Java编程实用技术与案例[J]. 清华大学出版社,2000.11[10] 明日科技编著. Java编程全能词典[J]. 电子工业出版社,2010摘要随着Java语言的不断发展和壮大,现在的Java已经广泛的应用于各个领域,包括医药,汽车工业,手机行业,游戏,等等地方。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
课程设计课程名称:程序设计课程设计课题名称:网络连连看游戏班级:xxx学号:xxx姓名:xxx指导教师:计算机学院一、课程设计目的本课程设计的目的最主要是掌握linux系统下C++编程思想,以及关于QT软件编程,设计出连连看图形界面,实现连连看各项功能,提高编程和解决问题的能力。
二、课程设计内容设计一个连连看游戏项目,实现如下功能:1、设计连连看游戏界面,能让用户在该界面上进行相关操作。
2、为游戏添加功能:聊天对话,逻辑层相消,游戏级别,时间限制等。
三、课程设计要求1、课程设计的程序必须用C++语言完成。
2、课程设计必须在linux系统下进行。
3、要求写出需求分析报告。
分析部分包括功能需求和界面需求。
4、本项目要求分团队完成,连连看游戏五人一组共同合作,培养学生团队合作的能力。
四、系统的需求分析和模块设计1、需求分析本项目需完成两大模块设计:一为游戏界面设计,二为游戏功能设计。
2、模块分解系统功能层次模块图:五、系统的程序设计与实现程序的运行环境:Linux终端开发环境:QT、C++程序的详细设计:连连看消去算法实现在检验两个方块能否消掉的时候,我们要让两个方块同时满足两个条件才行,就是两者配对并且连线成功。
分3种情况:(从下面的这三种情况,我们可以知道,需要三个检测,这三个检测分别检测一条直路经。
这样就会有三条路经。
若这三条路经上都是空按钮,那么就刚好是三种直线(两个转弯点)把两个按钮连接起来了)* 1.相邻* 2. 若不相邻的先在第一个按钮的同行找一个空按钮。
1).找到后看第二个按钮横向到这个空按钮所在的列是否有按钮。
2).没有的话再看第一个按钮到与它同行的那个空按钮之间是否有按钮。
3).没有的话,再从与第一个按钮同行的那个空按钮竖向到与第二个按钮的同行看是否有按钮。
没有的话路经就通了,可以消了.* 3.若2失败后,再在第一个按钮的同列找一个空按钮。
1).找到后看第二个按钮竖向到这个空按钮所在的行是否有按钮2).没有的话,再看第一个按钮到与它同列的那个空按钮之间是否有按钮。
3).没有的话,再从与第一个按钮同列的那个空按钮横向到与第二个按钮同列看是否有按钮。
没有的话路经就通了,可以消了。
* 若以上三步都失败,说明这两个按钮不可以消去。
六、系统的运行结果与分析程序源代码:Mainwindow.cpp#include <resolv.h>#include <stdlib.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <QScrollBar>#include "mainwindow.h"#include "ui_mainwindow.h"#include"udp.h"#include<QImage>#include <QTime>#include "llk.h"#include<iostream>using namespace std;MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow){ui->setupUi(this);connect(this, SIGNAL(recvMessage(QString,QString)), this, SLOT(onRecvMessage(QString,QString)));//this->setCentralWidget(link);//link->setMaximumSize(300,300);ui->editChatRec->setReadOnly(true);// link = new llk();size=6;isLinkE = false;sendMessage("192.168.3.255",8838,"o192.168.3.117",true); }MainWindow::~MainWindow(){delete ui;}void MainWindow::changeEvent(QEvent *e){QMainWindow::changeEvent(e);switch (e->type()) {case QEvent::LanguageChange:ui->retranslateUi(this);break;default:break;}}void MainWindow::on_btnSend_clicked(){QString string = ui->lineEdit->text();if(ui->radiosingle->isChecked()==true){sendMessage(string,8838,'c' + ui->editChat->text(),true);}else{string = "192.168.3.255";sendMessage("192.168.3.255",8838,'c' + ui->editChat->text(),true);}QString str =trUtf8("自己对") + string +trUtf8("发送:") + "\n" + ui->editChat->text();ui->editChatRec->setPlainText(ui->editChatRec->toPlainText() +" \n" +str); ui->editChat->clear();QScrollBar *bar = ui->editChatRec->verticalScrollBar();bar->setSliderPosition(bar->maximum());}Ui::MainWindow* MainWindow::getUi(){return ui;}void MainWindow::onRecvMessage(QString msg, QString ipAdd){//QString time = QTime::currentTime().toString();QString string =time + "\n" +QString(trUtf8("收到来自")) + ipAdd + trUtf8(":的消息:") + "\n" + msg.right(msg.length()-1);switch(msg.at(0).toAscii()){case 'o': //如果接受的是上线消息ui->listWidget->addItem(msg.right(msg.length()-1));break;default: //如果接受的是普通消息ui->editChatRec->setPlainText(ui->editChatRec->toPlainText() +"\n" + string);break;}QScrollBar *bar = ui->editChatRec->verticalScrollBar();bar->setSliderPosition(bar->maximum());}void MainWindow::on_action_begin_triggered(){/* link=new llk();link->setsize(6);link->arrayInit(6);link->Create(6);link->show();isLinkE = true;*/// size=4;if(isLinkE){//link=new llk();//link->setsize(8);// link->arrayInit(8); //link->Create(8);link->close();isLinkE = false;}else{link=new llk();link->setsize(size); link->arrayInit(size); link->Create(size);link->show();isLinkE = true;}if(isLinkE==false){link=new llk();link->setsize(size); link->arrayInit(size); link->Create(size);link->show();isLinkE = true;}}void MainWindow::on_action_close_triggered() {this->close();link->close();}void MainWindow::on_action_pause_triggered() {if(isLinkE)link->setEnabled(false);}void MainWindow::on_action_return_triggered() {link->setEnabled(true);}void MainWindow::on_action_easy_triggered() {size=6;if(isLinkE){//link=new llk();//link->setsize(8); // link->arrayInit(8); //link->Create(8);link->close();isLinkE = false;}else{link=new llk();link->setsize(6);link->arrayInit(6); link->Create(6);link->show();isLinkE = true;}if(isLinkE==false){link=new llk();link->setsize(6);link->arrayInit(6); link->Create(6);link->show();isLinkE = true;}}void MainWindow::on_action_soso_triggered() {size=8;/*llk *lin;lin = new llk();// this->setCentralWidget(lin);//lin->setGeometry(30,30,500,500);lin->setsize(8);lin->arrayInit(8);lin->Create(8);lin->setVisible(true);lin->close();*/if(isLinkE){//link=new llk();//link->setsize(8);// link->arrayInit(8);//link->Create(8);link->close();isLinkE = false;}else{link=new llk();link->setsize(8);link->arrayInit(8);link->Create(8);link->show();isLinkE = true;}if(isLinkE==false){link=new llk();link->setsize(8);link->arrayInit(8);link->Create(8);link->show();isLinkE = true;}}void MainWindow::on_action_diffcult_triggered() {size=10;/*llk *lin;lin = new llk();// this->setCentralWidget(lin);//lin->setGeometry(30,30,500,500);lin->setsize(8);lin->arrayInit(8);lin->Create(8);lin->setVisible(true);lin->close();*/if(isLinkE){//link=new llk();//link->setsize(8); // link->arrayInit(8); //link->Create(8);link->close();isLinkE = false;}else{link=new llk();link->setsize(10);link->arrayInit(10); link->Create(10);link->show();isLinkE = true;}if(isLinkE==false){link=new llk();link->setsize(10);link->arrayInit(10);link->Cremybutton.cpp#include "mybutton.h"MyButton::MyButton(int value, QWidget *parent){this->value = value;this->setParent(parent);QObject::connect(this,SIGNAL(clicked()),this,SLOT(mySlot())); }void MyButton::mySlot(){emit myClicked(this->value);}Main.cpp#include <QtGui/QApplication>#include "mainwindow.h"#include <QtGui/QApplication>#include <errno.h>#include <sys/socket.h>#include <resolv.h>#include <stdlib.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <iostream>#include <string>using namespace std;const unsigned int MAXBUF = 1024;void* thd_handler(void* window){MainWindow* w=(MainWindow*)window;int sockfd;struct sockaddr_in my_addr;socklen_t addrlen;char buffer[MAXBUF];unsigned int myport = 8838;printf("server started!\n");//create a socket for udpif((sockfd = socket(AF_INET,SOCK_DGRAM,0)) < 0) //attention {perror("socket creation failure");exit(errno);}//initialize sockaddr_inbzero(&my_addr,sizeof(my_addr));my_addr.sin_family = AF_INET;my_addr.sin_port = htons(myport);my_addr.sin_addr.s_addr = INADDR_ANY;//bindif(bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr)) == -1){perror("bind failure");exit(errno);}printf("bind success\n");addrlen = sizeof(struct sockaddr);while(true){bzero(buffer,MAXBUF);int len = recvfrom(sockfd, buffer, MAXBUF, 0,(sockaddr*)&my_addr, &addrlen); if (len < 0) {perror("recvfrom");exit(errno);}emitw->recvMessage(QObject::trUtf8(buffer),inet_ntoa(my_addr.sin_addr)); }//close connectionclose(sockfd);}int main(int argc, char *argv[]){pthread_t ntid;QApplication a(argc, argv);MainWindow w;pthread_create(&ntid,NULL,thd_handler,&w);w.show();return a.exec();}udp.cpp#include"udp.h"#include<QString>#include <errno.h>#include <sys/socket.h>#include <resolv.h>#include <stdlib.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>void sendMessage(QString ipAddress,unsigned int port,QString msg,bool isBroad) {// const unsigned int MAXBUF = 1024;int sockfd;int addr_len;int len;struct sockaddr_in dest;//char buffer[MAXBUF];char addr[16] ;strcpy(addr,ipAddress.toAscii());//create a socket for udpif((sockfd = socket(AF_INET,SOCK_DGRAM,0)) < 0) //attention{perror("socket creation failure");exit(errno);}if(isBroad){int broad=1;setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&broad,sizeof(broad));strcpy(addr,"192.168.3.255");}//initialize sockaddr_inbzero(&dest,sizeof(dest));dest.sin_family = AF_INET;dest.sin_port = htons( port);if(inet_aton(addr,(struct in_addr*)&dest.sin_addr)==0){perror("ip address format is error");exit(errno);}//send msg to serveraddr_len = sizeof(dest);// bzero(buffer,MAXBUF);len = sendto(sockfd, msg.toLocal8Bit(), msg.toLocal8Bit().length(), 0,(sockaddr*)&dest, addr_len);if (len < 0){printf("\n\rsend error.\n\r");exit(errno);}//close connection::close(sockfd);}llk.cpp#include "llk.h"#include<iostream>#include"judge.h"llk::llk(){isFirstClick = true;llk::createBtnRerank();size=6;llk::createCurrentProgressBar(size);timer = new QTimer();currentTime=0;QObject::connect(timer,SIGNAL(timeout()),this,SLOT(timeProcess())); }void llk::xxx(int x){clear(x);// std::cout<<x<<std::endl;// mybutton[x]->setVisible(false);// Widget::arrayInit(6);// progressBar[1]->setValue(20);}void llk::addPic(QString path,int i){labelPic[i] = new QLabel(this);labelPic[i]->setGeometry(i*100+5,30,100,120);QPixmap pixmap(path);labelPic[i]->setPixmap(path);labelPic[i]->setVisible(false);progressBar[i] = new QProgressBar();progressBar[i]->setGeometry(i*100+10,120,100,20);progressBar[i]->setParent(this);progressBar[i]->setValue(10);}void llk::Create(int size){createTimeProcessBar(size*size);timer->start(1000);charss[26][20]={"begin","./icon/icon1.png","./icon/icon2.png","./icon/icon3.png","./icon/icon4.png ","./icon/icon5.png","./icon/icon6.png","./icon/icon7.png","./icon/icon8.png","./icon/icon9.png","./icon/icon10.png","./icon/icon11.gif","./icon/icon12.gif","./icon/icon13.gif","./icon/icon14.gif","./icon/icon15.gif","./icon/icon16.gif","./icon/icon17.gif","./icon/icon18.gif","./icon/icon19.gif","./icon/icon20.gif","./icon/icon21.gif","./icon/icon22.gif","./icon/ic on23.gif","./icon/icon24.gif","./icon/icon25.gif"};createCurrentProgressBar(size);currentIndex=size*size;progressBarCurrent->setValue(0);for(unsigned int i=0;i<size*size;i++){mybutton[i] = new MyButton(i,this);mybutton[i]->setGeometry(20+i%size*60,150+i/size*60,60,60);int x,y;x=i/size+1;if((i+1)%size){y=(i+1)%size;}elsey=size;int m=array[x][y];// std::cout<<x<<","<<y<<std::endl;mybutton[i]->setIcon(QIcon(ss[m]));mybutton[i]->setIconSize(QSize(50,50));QObject::connect(mybutton[i],SIGNAL(myClicked(int)),this,SLOT(xxx(int )));//mybutton[i]->setIcon(QIcon(s));}}void llk::arrayInit(int size){array[size+1][0]=0;array[size+1][size+1]=0;array[0][size+1]=0;array[0][0]=0;for(int i=1;i<=size;i++){array[0][i]=0;array[size+1][i]=0;array[i][0]=0;array[i][size+1]=0;for(int j=1;j<=size;j++){array[i][j]=((i-1)*size+j-1)/4+1;}}for(int i=1;i<=size;i++){for(int j=1;j<=size;j++){int x1,x2,y1,y2,tmp1;// srand(unsigned(time(NULL)));x1=rand()%size+1;x2=rand()%size+1;y1=rand()%size+1;y2=rand()%size+1;tmp1=array[x1][y1];array[x1][y1]=array[x2][y2];array[x2][y2]=tmp1;}}for(int i=0;i<=size+1;i++){for(int j=0;j<=size+1;j++){std::cout<<array[i][j]<<" ";}std::cout<<std::endl;}}void llk::clear(int index){if(isFirstClick){firstIndex=index;isFirstClick=false;std::cout<<firstIndex<<",";}else{std::cout<<index<<std::endl;int x1,y1,x2,y2;x1=firstIndex/size+1;x2=index/size+1;if((firstIndex+1)%size){y1=(firstIndex+1)%size;}elsey1=size;if((index+1)%size){y2=(index+1)%size;}elsey2=size;std::cout<<x1<<","<<y1<<std::endl;std::cout<<x2<<","<<y2<<std::endl;std::cout<<"**************"<<std::endl;if(judge(x1,y1,x2,y2,array,size)){mybutton[firstIndex]->setVisible(false);// free(mybutton[firstIndex]);//mybutton[firstIndex]=NULL;mybutton[index]->setVisible(false);array[x1][y1]=0;array[x2][y2]=0;currentIndex-=2;progressBarCurrent->setValue(size*size-currentIndex);}isFirstClick=true;}}void llk::setsize(int size1){if(size1==8||size1==6||size1==10||size1==4){size=size1;}}void llk::mybuttonDelt(){for(int i=0;i<size*size;i++){free(mybutton[i]);}}void llk::createBtnRerank(){btnReRank = new QPushButton();btnReRank->setParent(this);btnReRank->setText("reRank");QObject::connect(btnReRank,SIGNAL(clicked()),this,SLOT(Rerank( )));}void llk::Rerank(){charss[26][20]={"begin","./icon/icon1.png","./icon/icon2.png","./icon/icon3.png","./icon/icon4.png ","./icon/icon5.png","./icon/icon6.png","./icon/icon7.png","./icon/icon8.png","./icon/icon9.png","./icon/icon10.png","./icon/icon11.gif","./icon/icon12.gif","./icon/icon13.gif","./icon/icon14.gif","./icon/icon15.gif","./icon/icon16.gif","./icon/icon17.gif","./icon/icon18.gif","./icon/icon19.gif","./icon/icon20.gif","./icon/icon21.gif","./icon/icon22.gif","./icon/ic on23.gif","./icon/icon24.gif","./icon/icon25.gif"};int tmpx,tmpy,tmp;std::cout<<"rerank"<<std::endl;for(int i=1;i<=size;i++){for(int j=1;j<=size;j++){if(array[i][j]!=0){tmpx=rand()%size+1;tmpy=rand()%size+1;tmp =array[i][j];array[i][j]=array[tmpx][tmpy];array[tmpx][tmpy]=tmp;mybutton[(i-1)*size+j-1]->setIcon(QIcon(ss[array[i][j]]));mybutton[(tmpx-1)*size+tmpy-1]->setIcon(QIcon(ss[array[tmpx][tmpy]]));if(array[i][j]){mybutton[(i-1)*size+j-1]->setVisible(true);}else{mybutton[(i-1)*size+j-1]->setVisible(false);}mybutton[(tmpx-1)*size+tmpy-1]->setVisible(true);}}}}void llk::createCurrentProgressBar(int size){progressBarCurrent = new QProgressBar();progressBarCurrent->setGeometry(20,40,60*size,30);progressBarCurrent->setMaximum(size*size);progressBarCurrent->setValue(255);progressBarCurrent->setParent(this);}void llk::createTimeProcessBar(int sumtime){progressBarTime = new QProgressBar();progressBarTime->setGeometry(20,80,60*size,30);progressBarTime->setParent(this);progressBarTime->setMaximum(sumtime);}void llk::timeProcess(){if(currentTime>=size*size)this->close();currentTime++;progressBarTime->setValue(currentTime);}Judge.cpp#include"judge.h"#include<iostream>int judge(int x1,int y1,int x2,int y2,int array[16][16],int size) {if(x1==x2&&y1==y2){return 0;}if((array[x1][y1]==array[x2][y2])){if(judge1(x1,y1,x2,y2,array,size)||judge2(x1,y1,x2,y2,array,size)||judge3(x1,y1,x2,y2,array,size )){array[x1][y1]=0;array[x2][y2]=0;return 1;}}return 0;}int judge1(int x1,int y1,int x2,int y2,int array[16][16],int size){//if(array[x1][y1]==array[x2][y2])//{int tmp;if(x1!=x2&&y1!=y2)return 0;if(y1>y2){tmp=y1;y1=y2;y2=tmp;}if(x1==x2){if(y1>y2){tmp=y1;y1=y2;y2=tmp;}for(int i=y1+1;i<y2;i++){if(array[x1][i]){return 0;}}}if(x1>x2){tmp=x1;x1=x2;x2=tmp;}if(y1==y2){if(x1>x2){tmp=x1;x1=x2;x2=tmp;}for(int j=x1+1;j<x2;j++){if(array[j][y1]){return 0;}}}//return 1;//}return 1;}int judge2(int x1,int y1,int x2,int y2,int array[16][16],int size){int flag=0;if(judge1(x1,y2,x1,y1,array,size)&&judge1(x1,y2,x2,y2,array,size)&&array[x1][y2]==0) {//flag++;//std::cout<<x1<<","<<i<<std::endl;return 1;}if(judge1(x2,y1,x1,y1,array,size)&&judge1(x2,y1,x2,y2,array,size)&&array[x2][y1]==0) {//flag++;//std::cout<<j<<","<<y1<<std::endl;return 1;}return 0;}int judge3(int x1,int y1,int x2,int y2,int array[16][16],int size){for(int i=0;i<=size+1;i++){if(judge2(x1,i,x2,y2,array,size)&&judge1(x1,i,x1,y1,array,size)&&array[x1][i]==0) { //std::cout<<x1<<","<<i<<std::endl;return 1;}}for(int j=0;j<=size+1;j++){if(judge2(j,y1,x2,y2,array,size)&&judge1(j,y1,x1,y1,array,size)&&array[j][y1]==0) {// std::cout<<j<<","<<y1<<std::endl;return 1;}}return 0;}头文件源代码:Judge.h#ifndef JUDGE_H#define JUDGE_Hint judge(int x1,int y1,int x2,int y2,int array[16][16],int size); int judge1(int x1,int y1,int x2,int y2,int array[16][16],int size); int judge2(int x1,int y1,int x2,int y2,int array[16][16],int size); int judge3(int x1,int y1,int x2,int y2,int array[16][16],int size);#endif // JUDGE_HLlk.h#ifndef LLK_H#define LLK_H#include<mybutton.h>#include <QWidget>#include<QPicture>#include <QWidget>#include<QObject>#include<QProgressBar>#include<QLabel>#include<QTimer>class llk : public QWidget{Q_OBJECTpublic:llk();void setsize(int size1);void addPic(QString path,int i);void Create(int size);void clear(int index);void mybuttonDelt();void createBtnRerank();void newGame();void createCurrentProgressBar(int size);void createTimeProcessBar(int sumtime);int currentTime;private:MyButton * mybutton[150];QLabel * labelPic[4];QProgressBar * progressBar[4];int array[16][16];bool isFirstClick;int firstIndex;QPushButton * btnReRank;QProgressBar *progressBarCurrent;QProgressBar *progressBarTime;int currentIndex;QTimer *timer;int size;public slots:void xxx(int x);void arrayInit(int size);void Rerank();void timeProcess();};#endif // LLK_HMainwindow.h#ifndef JUDGE_H#define JUDGE_Hint judge(int x1,int y1,int x2,int y2,int array[16][16],int size); int judge1(int x1,int y1,int x2,int y2,int array[16][16],int size); int judge2(int x1,int y1,int x2,int y2,int array[16][16],int size); int judge3(int x1,int y1,int x2,int y2,int array[16][16],int size);#endif // JUDGE_HMybutton.h#ifndef MYBUTTON_H#define MYBUTTON_H#include <QPushButton>class MyButton : public QPushButton{Q_OBJECTpublic:MyButton(int value,QWidget * parent = 0);private:int value;signals:void myClicked(int value);public slots:void mySlot();};#endif // MYBUTTON_HUdp.h#ifndef UDP_H#define UDP_H#include<QString>void sendMessage(QString ipAddress,unsigned int port,QString msg,bool isBroad); #endif // UDP_H游戏主界面:1、聊天界面:2、游戏界面:3、玩家游戏过程截图:。