当前位置:首页 > 代码 > 正文

连连看c代码(连连看的代码)[20240429更新]

admin 发布:2024-04-29 04:15 124


今天给各位分享连连看c代码的知识,其中也会对连连看的代码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如何用c语言实现连连看

晕晕晕晕晕

用TC.........石器时代的工具喔

刚刚学完C语言初级恐怕还是不能编写这种复杂程序嗒,建议楼主再考察一下自己的能力

如果觉得C没意思,可以考虑编一个命令行窗口中运行的"猜数字"游戏(跟以前文曲星里面的一样),大概还算是初级水平

.....如果楼主允许用BC,我兴许还能胜任,TC...我们这都没人用了...

VC编写连连看怎么引入图片

最简单的做法就是把每个图片都做成一

个按钮,这可以利用MFC的CButton类。编写CLLKanButton

类,使其继承于Cbutton类.使每个Button都有自己的ID号,

用来存储图片类型,还应该有一个CPoint类型的成员变量,用

来存储每个Button的位置信息。最后在创建时。将和ID号对应的图片贴到相应的位置上即可。

求连连看源代码

连连看的代码(基本算法)加了部分注释

import java.awt.*;

import java.awt.event.*;

public class lianliankan implements ActionListener

{

static String s="no"; //用来纪录点击按钮的信息

int x0=0,y0=0,x=0,y=0,n1=0,n2=0; //用来纪录按钮的位置信息

Frame f,f1;

Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10; //用比较笨的方法添加了

Button b11,b12,b13,b14,b15,b16,b17,b18; //30个按钮来实现游戏界面

Button b19,b20,b21,b22,b23,b24,b25; //可以用数组实现,这是本人

Button b26,b27,b28,b29,b30,bc; //学java时,入门的联系,所以

Button b,ba,br,bt1,bt2; //有些东西很业余!!嘻嘻

Panel p1,p2,p3;

TextField t; //用来显示一些随机信息,方法是下面的guli().

Label l;

int d[][]={ //用来和界面的按钮建立映射关系

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0}

};

public static void main(String[] args)

{

lianliankan t=new lianliankan();

t.suiji();

t.go();

}

public void actionPerformed(ActionEvent e) //再来一次按钮的响应事件。

{

int d[][]={

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0}

};

this.d=d;

suiji();

f.setVisible(false);

f1.setVisible(false);

s="no";

go();

}

public void go()//初始化界面

{

l=new Label("亲爱的玩家,");

f=new Frame("连连看");

t=new TextField();

p2=new Panel();

p1=new Panel();

p3=new Panel();

bc=new Button("退出");

br=new Button("重列");

b=new Button();

b1=new Button(String.valueOf(d[1][1]));

b2=new Button(String.valueOf(d[1][2]));

b3=new Button(String.valueOf(d[1][3]));

b4=new Button(String.valueOf(d[1][4]));

b5=new Button(String.valueOf(d[1][5]));

b6=new Button(String.valueOf(d[2][1]));

b7=new Button(String.valueOf(d[2][2]));

b8=new Button(String.valueOf(d[2][3]));

b9=new Button(String.valueOf(d[2][4]));

b10=new Button(String.valueOf(d[2][5]));

b11=new Button(String.valueOf(d[3][1]));

b12=new Button(String.valueOf(d[3][2]));

b13=new Button(String.valueOf(d[3][3]));

b14=new Button(String.valueOf(d[3][4]));

b15=new Button(String.valueOf(d[3][5]));

b16=new Button(String.valueOf(d[4][1]));

b17=new Button(String.valueOf(d[4][2]));

b18=new Button(String.valueOf(d[4][3]));

b19=new Button(String.valueOf(d[4][4]));

b20=new Button(String.valueOf(d[4][5]));

b21=new Button(String.valueOf(d[5][1]));

b22=new Button(String.valueOf(d[5][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]));

b27=new Button(String.valueOf(d[6][2]));

b28=new Button(String.valueOf(d[6][3]));

b29=new Button(String.valueOf(d[6][4]));

b30=new Button(String.valueOf(d[6][5]));

p3.setLayout(null);

p1.setSize(250,300);

p2.setSize(100,40);

p3.setSize(300,30);

t.setSize(60,30);

l.setSize(70,30);

p1.setLayout(new GridLayout(6,5));

p1.setBackground(Color.pink);

p1.setLocation(100,100);

p2.setLocation(0,400);

p3.setLocation(50,50);

t.setLocation(230,2);

l.setLocation(150,2);

bc.setLocation(0,40);

br.setLocation(0,100);

f.add(p1);

f.add(p2);

f.add(p3);

p3.add(l);

p3.add(t);

p2.add(bc);

p2.add(br);

p1.add(b1);

p1.add(b2);

p1.add(b3);

p1.add(b4);

p1.add(b5);

p1.add(b6);

p1.add(b7);

p1.add(b8);

p1.add(b9);

p1.add(b10);

p1.add(b11);

p1.add(b12);

p1.add(b13);

p1.add(b14);

p1.add(b15);

p1.add(b16);

p1.add(b17);

p1.add(b18);

p1.add(b19);

p1.add(b20);

p1.add(b21);

p1.add(b22);

p1.add(b23);

p1.add(b24);

p1.add(b25);

p1.add(b26);

p1.add(b27);

p1.add(b28);

p1.add(b29);

p1.add(b30);

f.pack();

f.setBounds(280,100,500,450);

f.setResizable(false);

f.setVisible(true);

bc.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

ex();

}

});

br.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

chonglie();

}

});

b1.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(1,1,b1);

}

});

b2.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(1,2,b2);

}

});

b3.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(1,3,b3);

}

});

b4.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(1,4,b4);

}

});

b5.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(1,5,b5);

}

});

b6.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(2,1,b6);

}

});

b7.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(2,2,b7);

}

});

b8.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(2,3,b8);

}

});

b9.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(2,4,b9);

}

});

b10.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(2,5,b10);

}

});

b11.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(3,1,b11);

}

});

b12.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(3,2,b12);

}

});

b13.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(3,3,b13);

}

});

b14.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(3,4,b14);

}

});

b15.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(3,5,b15);

}

});

b16.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(4,1,b16);

}

});

b17.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(4,2,b17);

}

});

b18.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(4,3,b18);

}

});

b19.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(4,4,b19);

}

});

b20.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(4,5,b20);

}

});

b21.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(5,1,b21);

}

});

b22.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(5,2,b22);

}

});

b23.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(5,3,b23);

}

});

b24.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(5,4,b24);

}

});

b25.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(5,5,b25);

}

});

b26.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(6,1,b26);

}

});

b27.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(6,2,b27);

}

});

b28.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(6,3,b28);

}

});

b29.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(6,4,b29);

}

});

b30.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

wei(6,5,b30);

}

});

}

public void ex() //退出界面,可用diolog来实现有模式的类型,更加符合

{

f1=new Frame("游戏作业");

f1.setLayout(new GridLayout(1,1));

bt1=new Button("确定退出");

bt2=new Button("再来一局");

f1.add(bt1);

f1.add(bt2);

f1.pack();

f1.setBounds(400,250,90,60);

f1.setResizable(false);

f1.show();

f1.setVisible(true);

bt1.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e)

{

System.exit(0);

}

});

bt2.addActionListener(this);

}

public void suiji() //产生随机数,来填充游戏界面对应的数组的各个位置

{

int m,n,k=0,k1,k2,k3;

for(m=1;m=15;m++)

{

k1=(int)(Math.random()*25+1);

for(n=1;n=2;n++)

{

k2=(int)(Math.random()*6+1);

k3=(int)(Math.random()*5+1);

while(d[k2][k3]!=0 k!=30)

{

k2=(int)(Math.random()*6+1);

k3=(int)(Math.random()*5+1);

}

this.d[k2][k3]=k1;

k++;

}

}

}

public void guli() //随机信息

{

int l=0;

t.setText("");

l=(int)(Math.random()*10);

System.out.println(l);

switch(l)

{

case 1:

t.setText("好!加油!");

break;

case 3:

t.setText("你真棒!");

break;

case 5:

t.setText("加快速度!");

break;

case 6:

t.setText("不错啊!");

break;

case 8:

t.setText("加油吧!");

break;

case 9:

t.setText("够聪明!");

break;

default:

break;

}

}

public void chonglie() //重列方法

{

int save[],i,j,n=0,k2,k3,k;

int d[][]={

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0},

{0,0,0,0,0,0,0}

};

save=new int[30];

for(n=0;n30;n++)

save[n]=0; //定义一个数组来保存当前的每个按钮位置上的信息

n=0;

for(i=0;i=6;i++)

for(j=0;j=5;j++)

{

if(this.d[i][j]!=0)

{

save[n]=this.d[i][j];

n++;

}

}

n=n-1;

this.d=d;

while(n=0) //产生随机位置,放置按钮

{

k2=(int)(Math.random()*6+1);

k3=(int)(Math.random()*5+1);

while(d[k2][k3]!=0)

{

k2=(int)(Math.random()*6+1);

k3=(int)(Math.random()*5+1);

}

this.d[k2][k3]=save[n];

n--;

}

f.setVisible(false);

s="no"; //这里一定要将按钮点击信息归为初始

go();

ling();

}

public void ling() //将数组中为零的成员对应的按钮消去

{ //用按钮类型的数组实现会简化得多,

if(d[1][1]==0)

b1.setVisible(false);

if(d[1][2]==0)

b2.setVisible(false);

if(d[1][3]==0)

b3.setVisible(false);

if(d[1][4]==0)

b4.setVisible(false);

if(d[1][5]==0)

b5.setVisible(false);

if(d[2][1]==0)

b6.setVisible(false);

if(d[2][2]==0)

b7.setVisible(false);

if(d[2][3]==0)

b8.setVisible(false);

if(d[2][4]==0)

b9.setVisible(false);

if(d[2][5]==0)

b10.setVisible(false);

if(d[3][1]==0)

b11.setVisible(false);

if(d[3][2]==0)

b12.setVisible(false);

if(d[3][3]==0)

b13.setVisible(false);

if(d[3][4]==0)

b14.setVisible(false);

if(d[3][5]==0)

b15.setVisible(false);

if(d[4][1]==0)

b16.setVisible(false);

if(d[4][2]==0)

b17.setVisible(false);

if(d[4][3]==0)

b18.setVisible(false);

if(d[4][4]==0)

b19.setVisible(false);

if(d[4][5]==0)

b20.setVisible(false);

if(d[5][1]==0)

b21.setVisible(false);

if(d[5][2]==0)

b22.setVisible(false);

if(d[5][3]==0)

b23.setVisible(false);

if(d[5][4]==0)

b24.setVisible(false);

if(d[5][5]==0)

b25.setVisible(false);

if(d[6][1]==0)

b26.setVisible(false);

if(d[6][2]==0)

b27.setVisible(false);

if(d[6][3]==0)

b28.setVisible(false);

if(d[6][4]==0)

b29.setVisible(false);

if(d[6][5]==0)

b30.setVisible(false);

}

public void wei(int w1,int w2,Button bz) //判断并纪录每次点击按钮的信息

{ //当两次的按钮相同才能消去

if((s.trim()).equals("no"))

{

s=b1.getLabel();

x0=w1;

y0=w2;

n1=d[x0][y0];

b=bz;

x=w1;

y=w2;

n2=d[x][y];

ba=bz;

}

else

{

x0=x;

y0=y;

n1=d[x0][y0];

b=ba;

x=w1;

y=w2;

n2=d[x][y];

ba=bz;

if(n1==n2 ba!=b)

{

xiao();

}

}

}

public void xiao() //这里是整个游戏最重要的部分,就是判断两个按钮在信息

{ //相同的情况下能不能消去。仔细分析,不一条条注释

int i=0, j=0,n=0,k=0;

if((x0==x (y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)(y0==y))) //相邻的情况

{

ba.setVisible(false);

b.setVisible(false);

guli();

s="no";

d[x0][y0]=0;

d[x][y]=0;

}

else

{

for (j=0;j7;j++ ) //两个按钮按行分析,看能否消去

{

if (d[x0][j]==0)

{

if (yj)

{

for (i=y-1;i=j;i-- )

{

if (d[x][i]!=0)

{

k=0;

break;

}

else

{

k=1;

}

}

if (k==1)

{

if (y0j)

{

for (i=y0-1;i=j ;i-- )

{

if (d[x0][i]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

if (y0j)

{

for (i=y0+1;i=j ;i++)

{

if (d[x0][i]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

}

}

if (yj)

{

for (i=y+1;i=j ;i++ )

{

if (d[x][i]!=0)

{

k=0;

break;

}

else

{

k=1;

}

}

if (k==1)

{

if (y0j)

{

for (i=y0-1;i=j ;i-- )

{

if (d[x0][i]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

if (y0j)

{

for (i=y0+1;i=j ;i++)

{

if (d[x0][i]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

}

}

if (y==j )

{

if (y0j)

{

for (i=y0-1;i=j ;i-- )

{

if (d[x0][i]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

if (y0j)

{

for (i=y0+1;i=j ;i++)

{

if (d[x0][i]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

}

}

if (k==2)

{ if (x0==x)

{

b.setVisible(false);

ba.setVisible(false);

guli();

s="no";

k=0;

d[x0][y0]=0;

d[x][y]=0;

}

if (x0x)

{

for (n=x0;n=x-1;n++ )

{

if (d[n][j]!=0)

{

k=0;

break;

}

if(d[n][j]==0 n==x-1)

{

b.setVisible(false);

ba.setVisible(false);

guli();

s="no";

k=0;

d[x0][y0]=0;

d[x][y]=0;

}

}

}

if (x0x)

{

for (n=x0;n=x+1 ;n-- )

{

if (d[n][j]!=0)

{

k=0;

break;

}

if(d[n][j]==0 n==x+1)

{

b.setVisible(false);

ba.setVisible(false);

guli();

s="no";

k=0;

d[x0][y0]=0;

d[x][y]=0;

}

}

}

}

}

for (i=0;i8;i++ ) //按列分析,看能不能消去

{

if (d[i][y0]==0)

{

if (xi)

{

for (j=x-1;j=i ;j-- )

{

if (d[j][y]!=0)

{

k=0;

break;

}

else

{

k=1;

}

}

if (k==1)

{

if (x0i)

{

for (j=x0-1;j=i ;j-- )

{

if (d[j][y0]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

if (x0i)

{

for (j=x0+1;j=i;j++ )

{

if (d[j][y0]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

}

}

if (xi)

{

for (j=x+1;j=i;j++ )

{

if (d[j][y]!=0)

{

k=0;

break;

}

else

{

k=1;

}

}

if (k==1)

{

if (x0i)

{

for (j=x0-1;j=i ;j-- )

{

if (d[j][y0]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

if (x0i)

{

for (j=x0+1;j=i ;j++ )

{

if (d[j][y0]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

}

}

if (x==i)

{

if (x0i)

{

for (j=x0-1;j=i ;j-- )

{

if (d[j][y0]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

if (x0i)

{

for (j=x0+1;j=i ;j++ )

{

if (d[j][y0]!=0)

{

k=0;

break;

}

else

{

k=2;

}

}

}

}

}

if (k==2)

{

if (y0==y)

{

b.setVisible(false);

ba.setVisible(false);

guli();

s="no";

k=0;

d[x0][y0]=0;

d[x][y]=0;

}

if (y0y)

{

for (n=y0;n=y-1 ;n++ )

{

if (d[i][n]!=0)

{

k=0;

break;

}

if(d[i][n]==0 n==y-1)

{

b.setVisible(false);

ba.setVisible(false);

guli();

s="no";

k=0;

d[x0][y0]=0;

d[x][y]=0;

}

}

}

if (y0y)

{

for (n=y0;n=y+1 ;n--)

{

if (d[i][n]!=0)

{

k=0;

break;

}

if(d[i][n]==0 n==y+1)

{

b.setVisible(false);

ba.setVisible(false);

guli();

s="no";

k=0;

d[x0][y0]=0;

d[x][y]=0;

}

}

}

}

}

}

}

}

C++连连看算法

////////////////////////////////////////////////////////////////////////// // LLKAg.h//////////////////////////////////////////////////////////////////////////// 版权所有// 作者:董波// 日期:2008.12.26// 简介:连连看算法//////////////////////////////////////////////////////////////////////////#ifndef _ND_LLKAG_H_ #define _ND_LLKAG_H_#if _MSC_VER 1000 #pragma once#endif#include vector // for/* 约定: 0代表空白,其它编号从1开始向后排。。。*/static const int BLANK_GRID = 0;typedef std::vector POINT2D PT2D_VEC;class CLLKAg {public:CLLKAg( int iRow = 9, int iCol = 16 );~CLLKAg();public:// 开始游戏,服务器调用void Start( int iCardNum = 20 );// 获得数据信息,用于客户端渲染,也可用于服务器获得初始化信息后下发void GetState( std::vectorint vec ) const;// 客户端调用,从网络数据获取状态 void SetState( const int* pStates, unsigned uSize );// 获得内部指针,但是不能修改,只读的const int* GetMap() const;int GetRow() const;int GetCol() const;// 是否是可消除的,是否是可连接的。 bool IsLink( POINT2D ptFirst, POINT2D ptSecond ) const;// 判断是否已经胜利bool IsWin() const;// 清除两个棋子,客户端调用之前通常需要调用IsLinkbool ClearPair( POINT2D ptFirst, POINT2D ptSecond );// 用于调试 #if defined( _DEBUG ) || defined( DEBUG )int* GetMap_D(); // 返回内部指针void RePermutation(); // 重新排列#endif // #if defined( _DEBUG ) || defined( DEBUG )­// 内部实现的函数,外部不需要调用。 protected:­// 是否是同一直线连通 bool DirectLink( POINT2D ptFirst, POINT2D ptSecond ) const;// 1直角接口连通 bool OneCornerLink( POINT2D ptFirst, POINT2D ptSecond ) const;// 2直角接口连通bool TwoCornerLink( POINT2D ptFirst, POINT2D ptSecond ) const;private:int* m_pMap; // 用于代表地图int m_iRow; // 行数int m_iCol; // 列数};­////////////////////////////////////////////////////////////////////////// // 得到地图inline const int* CLLKAg::GetMap() const{return m_pMap;}// 得到行数 inline int CLLKAg::GetRow() const{return m_iRow;}// 得到列数inline int CLLKAg::GetCol() const{return m_iCol;}#if defined( _DEBUG ) || defined( DEBUG ) // 用于调试 inline int* CLLKAg::GetMap_D(){return m_pMap;}#endif // #if defined( _DEBUG ) || defined( DEBUG )­#endif // #ifndef _ND_LLKAG_H_­­////////////////////////////////////////////////////////////////////////// // LLKAg.h//////////////////////////////////////////////////////////////////////////// 版权所有// 作者:董波// 日期:2008.12.26// 简介:连连看算法实现//////////////////////////////////////////////////////////////////////////// 使用MFC的时候请解注释下面这行: #include "stdafx.h"#include "LLKAg.h"#include cassert #include stdexcept#include ctime#include algorithm/* 一些模板函数*/template class T T _max( T lhs, T rhs ){return lhs rhs ? lhs : rhs;}template class T T _min( T lhs, T rhs ){return lhs rhs ? lhs : rhs;}­// 构造,初始化一些内存 CLLKAg::CLLKAg( int iRow /* = 9 */, int iCol /* = 16 */ ):m_iRow(iRow),m_iCol(iCol),m_pMap(NULL){assert( m_iRow * m_iCol = 2 );m_pMap = new int[ m_iRow*m_iCol ];if( NULL == m_pMap ) { throw std::bad_alloc( "内存分配失败" );}memset( m_pMap, BLANK_GRID, m_iCol * m_iRow );}CLLKAg::~CLLKAg() {if( NULL != m_pMap ){ delete [] m_pMap;}}// 服务器调用,这将随机生成一个棋盘(地图、桌面) void CLLKAg::Start( int iCardNum /* = 20 */){assert( ( m_iCol * m_iRow % 2 == 0 ) "必须为偶数" );// 根据系统时间初始化种子 srand( static_castunsigned( time(NULL) ) );// 初始化的基本思想: // 成对的随机填充,然后随机打乱。int iSize = m_iCol * m_iRow;int i = 0;while ( i iSize ){ int iTarget = rand() % iCardNum + 1; m_pMap[i] = iTarget; ++i; m_pMap[i] = iTarget; ++i;}std::random_shuffle( m_pMap, m_pMap + iSize ); }// 客户端调用,使用网络传递来的信息来初始化游戏状态 void CLLKAg::SetState( const int* pStates, unsigned uSize ){assert( uSize == m_iRow * m_iCol * sizeof (int) );memcpy( m_pMap, pStates, uSize ); }// 得到地图状态 void CLLKAg::GetState( std::vectorint vec ) const{vec.assign( m_pMap, m_pMap + m_iRow*m_iCol );}// 判断是否已经获胜 bool CLLKAg::IsWin() const{const int iSize = m_iCol * m_iRow;for( int i=0; i iSize; ++i ){ if( BLANK_GRID != m_pMap[i] ) { return false; }}return true; }// 是否是直接连通! bool CLLKAg::DirectLink( POINT2D ptFirst, POINT2D ptSecond )const{// 根本不可能在一条直线上的时候直接返回falseif( (ptFirst.x != ptSecond.x) (ptFirst.y != ptSecond.y) ){ return false;}// 不应该是相同的点,这是不能接受的! if( (ptFirst.x == ptSecond.x) ( ptFirst.y == ptSecond.y ) ){ return false;}// 分情况// 同一x if( ptFirst.x == ptSecond.x ){ int iMin = _min( ptFirst.y, ptSecond.y ); int iMax = _max( ptFirst.y, ptSecond.y ); for( int i=iMin +1; i iMax; ++i ) { if( m_pMap[i*m_iCol + ptFirst.x] != BLANK_GRID ) { return false; } } return true; }else{ int iMin = _min( ptFirst.x, ptSecond.x ); int iMax = _max( ptFirst.x, ptSecond.x ); for( int i=iMin+1; iiMax; ++i ) { if( m_pMap[ptFirst.y*m_iCol + i] != BLANK_GRID ) { return false; } } return true; }}// 一折型的 // 就是找出矩形的另外两个顶点然后判断他们是否是直线相连的bool CLLKAg::OneCornerLink( POINT2D ptFirst, POINT2D ptSecond )const{// 函数到这里的时候应该保证两个点的x、y都互不相等,否则调用就有错// 分步骤,看第一个顶点 POINT2D ptFirstCorner = { ptFirst.x, ptSecond.y };­// 两个直通的话就是相连的if( ( m_pMap[ptFirstCorner.y*m_iCol + ptFirstCorner.x ] == BLANK_GRID ) DirectLink( ptFirst, ptFirstCorner ) DirectLink( ptSecond, ptFirstCorner ) ){ return true;}// 判断第二个顶点是否是直连的 POINT2D ptSecondCorner = { ptSecond.x, ptFirst.y };if( ( m_pMap[ptSecondCorner.y*m_iCol + ptSecondCorner.x ] == BLANK_GRID ) DirectLink( ptFirst, ptSecondCorner) DirectLink( ptSecondCorner, ptSecond ) ){ return true;}return false; }// 二折型的 bool CLLKAg::TwoCornerLink( POINT2D ptFirst, POINT2D ptSecond )const{// 先扫描x方向,这是指在矩阵中横向移动// 先向左int i =0;int j =0;for( i=ptFirst.x-1; i=0; --i ){ if( m_pMap[ptFirst.y*m_iCol +i] != BLANK_GRID ) { break; } POINT2D ptOne = { i, ptFirst.y }; if( OneCornerLink( ptOne, ptSecond ) ) { return true; }}// 再向右 for( i=ptFirst.x+1; im_iCol; ++i ){ if( m_pMap[ptFirst.y*m_iCol +i] != BLANK_GRID ) { break; } POINT2D ptOne = { i, ptFirst.y }; if( OneCornerLink( ptOne, ptSecond ) ) { return true; }}// 扫描y方向// 向上 for( i=ptFirst.y-1; i=0; --i ){ if( m_pMap[i*m_iCol+ptFirst.x] != BLANK_GRID ) { break; } POINT2D ptOne = { ptFirst.x, i }; if( OneCornerLink( ptOne, ptSecond ) ) { return true; }}// 向下 for( i=ptFirst.y+1; im_iRow; ++i ){ if( m_pMap[i*m_iCol+ptFirst.x] != BLANK_GRID ) { break; } POINT2D ptOne = { ptFirst.x, i }; if( OneCornerLink( ptOne, ptSecond ) ) { return true; }}­return false; }bool CLLKAg::IsLink( POINT2D ptFirst, POINT2D ptSecond )const {// 首先必须是放置的相同的if( m_pMap[ptFirst.y*m_iCol + ptFirst.x] != m_pMap[ptSecond.y*m_iCol + ptSecond.x] ){ return false;}// 如果是同样的点,则直接返回错误 if( ptFirst.x == ptSecond.x ptFirst.y == ptSecond.y ){ return false;}// 如果任何一个为空,也返回 if( m_pMap[ ptFirst.y*m_iCol + ptFirst.x] == BLANK_GRID || m_pMap[ ptSecond.y*m_iCol + ptSecond.x] == BLANK_GRID ){ return false;}// 如果是直线相连,就返回了 if( DirectLink( ptFirst, ptSecond ) ){ return true;}// 否则做一折检查 if( OneCornerLink( ptFirst, ptSecond ) ){ return true;}// 否则做二折检查 if( TwoCornerLink( ptFirst, ptSecond ) ){ return true;}// 什么都不符合,则返回false return false;}// 清除一对Pair,不要乱调用哦! bool CLLKAg::ClearPair(POINT2D ptFirst, POINT2D ptSecond){m_pMap[ptFirst.y*m_iCol + ptFirst.x] = m_pMap[ptSecond.y*m_iCol + ptSecond.x] = BLANK_GRID;return true; }#if defined( _DEBUG ) || defined( DEBUG ) // 用于调试 void CLLKAg::RePermutation(){std::random_shuffle( m_pMap, m_pMap + m_iCol*m_iRow );}#endif // #if defined( _DEBUG ) || defined( DEBUG )

谁能帮忙写一个c语言连连看游戏,要4x4的,功能最简单的就可以!但是要实现连连看应该有的功能

刚写的,新鲜出炉:

#include stdio.h

#include string.h

#include stdlib.h

#include time.h

#define MAPSIZE 4

#define MAXLINESIZE 60

//typedef enum{false,true} bool;

typedef struct{

    int x,y;

}Point;

const char pictureTable[]={" ABCEDFGHI"};

bool judgeLine(char **MP,Point *start,Point *end){

int i;

if(start-x==end-x){

if(start-y  end-y){

for(i = start-y - 1 ; i  end-y ; i--)

if(MP[start-x][i]!=0) 

return false;

return true;

}

else{

for(i = start-y + 1 ; i  end-y ; i++)

if(MP[start-x][i]!=0)

return false;

return true;

}

}

else if(start-y==end-y){

if(start-x  end-x ){

for(i = start-x - 1 ; i  end-x ; i--)

if(MP[i][start-y]!=0) 

return false;

return true;

}

else{

for(i = start-x + 1 ; i  end-x ; i++)

if(MP[i][start-y]!=0) 

return false;

return true;

}

}

return false;

}

bool judgeTwoLines(char **MP,Point *start,Point *end,Point *mid){

    Point p1,p2;

    mid-x=-1;

    mid-y=-1;

    if(judgeLine(MP,start,end)==true) return true;

    p1.x=start-x;

    p1.y=end-y;

    p2.x=end-x;

    p2.y=start-y;

    mid-x=p1.x;

    mid-y=p1.y;

    if(MP[p1.x][p1.y]==0  judgeLine(MP,start,p1)  judgeLine(MP,end,p1)) return true;

    mid-x=p2.x;

    mid-y=p2.y;

    if(MP[p2.x][p2.y]==0  judgeLine(MP,start,p2)  judgeLine(MP,end,p2)) return true;

    return false;

}

bool judgeTreeLines(char **MP,Point *start,Point *end,Point *mid1,Point *mid2,int n){

    int i;

    mid1-x=-1;mid1-y=-1;

    mid2-x=-1;mid2-y=-1;

    if(judgeTwoLines(MP,start,end,mid1)) return true;

    for( i=start-x - 1;i=0;i--){

     if(MP[i][start-y]!=0) break;

     mid1-x=i;

     mid1-y=start-y;

     if(judgeTwoLines(MP,mid1,end,mid2)) return true;

    }

    for( i=start-x + 1;i=n+1;i++){

     if(MP[i][start-y]!=0) break;

     mid1-x=i;

     mid1-y=start-y;

     if(judgeTwoLines(MP,mid1,end,mid2)) return true;

    }

    for( i=start-y - 1;i=0;i--){

     if(MP[start-x][i]!=0) break;

     mid1-x=start-x;

     mid1-y=i;

     if(judgeTwoLines(MP,mid1,end,mid2)) return true;

    }

    for( i=start-y + 1;i=n+1;i++){

     if(MP[start-x][i]!=0) break;

     mid1-x=start-x;

     mid1-y=i;

     if(judgeTwoLines(MP,mid1,end,mid2)) return true;

    }

    return false;

}

void ptMap(char **MP,int n){

    int space=(MAXLINESIZE-n*2)/2;

    int i,j;

    for(i=0;i(MAXLINESIZE-10)/2;i++)

     printf(" ");

    printf("《连连看》\n");

    for(i=2;ispace;i++) printf(" ");

    printf("x\n");

    for(i=1;i=n;i++){

     for(j=2;jspace;j++)

     printf(" ");

     printf("%d ",i);

     for(j=1;j=n;j++)

     printf("%c ",pictureTable[MP[i][j]]);

     printf("\n");

    }

    for(i=0;ispace;i++)

     printf("*");

    for(i=0;in;i++)

     printf("%d*",i+1);

for(i=1;ispace;i++)

     printf("*");

    printf("\n");

}

char **createMap(int n){

    char **ret;

    int i;

    ret=(char**)malloc(sizeof(char*)*(n+2));

    for(i=0;in+2;i++)

     ret[i]=(char*)malloc(sizeof(char)*(n+2));

    return ret;

}

void ranMap(char **MP,int n){

    int *all=(int*)malloc(sizeof(int)*n*n);

    int i,tmpi,tmp;

    for(i=0;in*n;i++)

     all[i]=i/4+1;

    for(i=0;in*n;i++){

     tmpi=rand()%(n*n-i);

     tmp=all[tmpi];

     all[tmpi]=all[n*n-i-1];

     all[n*n-i-1]=tmp;

    }

    for(i=0;in+2;i++){

     MP[0][i]=0;

     MP[n+1][i]=0;

     MP[i][0]=0;

     MP[i][n+1]=0;

    }

    tmpi=0;

    for(i=1;i=n;i++)

     for(tmp=1;tmp=n;tmp++)

     MP[i][tmp]=all[tmpi++];

}

void deletePoints(char **MP,Point *p1,Point *p2){

    MP[p1-x][p1-y]=0;

    MP[p2-x][p2-y]=0;

}

int playTurns(int n){

int rest=n*n;

char **mp=createMap(n),c;

ranMap(mp,n);

Point mid1,mid2,pt1,pt2;

while(1){

ptMap(mp,n);

printf("请输入消去的坐标1(x1 y1):\n");

scanf("%d%d",pt1.x,pt1.y);

printf("请输入消去的坐标2(x2 y2):\n");

scanf("%d%d",pt2.x,pt2.y);

if((pt1.x==pt2.x  pt1.y==pt2.y) || (pt1.x1 || pt1.xn || pt2.x  1 || pt2.x  n || pt1.y1 || pt1.yn || pt2.y  1 || pt2.y  n)){

printf("无法消除这两图案,请再次检查。");

}

    else if(mp[pt1.x][pt1.y]!=0  mp[pt1.x][pt1.y]==mp[pt2.x][pt2.y]  judgeTreeLines(mp,pt1,pt2,mid1,mid2,n)){

     if(mid1.x==-1){

     printf("Direct\n");

     }

     else if(mid2.x==-1){

     printf("TwoLines :(%d,%d)\n",mid1.x,mid1.y);

     }

     else{

     printf("TreeLines:(%d,%d)(%d,%d)\n",mid1.x,mid1.y,mid2.x,mid2.y);

     }

     deletePoints(mp,pt1,pt2);

     printf("消去成功!\n");

     rest-=2;

     if(rest==0){

     printf("恭喜!你已消去所有图案!\n");

     break;

     }

    }

    else{

     printf("无法消除这两图案,请再次检查。");

    }

    printf("继续游戏(N/n不继续)?");

    scanf(" %c",c);

    if(c=='N' || c=='n') break;

}

printf("是否重新开局(Y/y继续)?");

scanf(" %c",c);

if(c=='y' || c=='Y') return 1;

return 0;

}

int main(){

    srand(time(0));

    while(playTurns(4));

    return 0;

}

连连看c语言代码 不要图片

#include stdio.h

#include stdlib.h

#include time.h

#include conio.h

void init();

void near1(int a,int b,int a1,int b1);

void print();

int exit();

char chessman[10][10];

void main()

{

int op;

int row,line;

int row2,line2;

init();

do

{

print();

printf("options:\n");

printf("\t1.select\n");

printf("\t2.restart\n");

printf("\t3.sort\n");

printf("\t4.exit\n");

scanf("%d",op);

if(op==1)

{

printf("输入第一个:");

scanf("%d %d",line,row);

printf("输入第二个:");

scanf("%d %d",line2,row2);

if(chessman[line][row]!=chessman[line2][row2])

{

printf("两数不相同!\n");

}

else if(line==line2row==row2)

{

printf("错误输入!!!\n");

}

else if(line=line2)

{

near1(line,row,line2,row2);

}

else

{

near1(line2,row2,line,row);

}

}

if(op==2)

{

}

}while(exit());

system("pause");

printf("\n\n\n\t\t恭喜您,您胜利了!\n");

}

void init()

{

int i,j;

int a[8]={0};

srand(time(NULL));

for(i=1;i8;i++)

for(j=1;j9;j++)

{

chessman[i][j]=rand()%8+65;

if(chessman[i][j]==65) a[0]++;

if(chessman[i][j]==66) a[1]++;

if(chessman[i][j]==67) a[2]++;

if(chessman[i][j]==68) a[3]++;

if(chessman[i][j]==69) a[4]++;

if(chessman[i][j]==70) a[5]++;

if(chessman[i][j]==71) a[6]++;

if(chessman[i][j]==72) a[7]++;

}

for(i=0;i8;i++)

{

if(a[i]%2!=0)

chessman[8][i+1]=i+65;

else

chessman[8][i+1]=73;

}

}

void print()

{

int i,j;

system("cls");

printf("\t 1 2 3 4 5 6 7 8 \n");

for(i=0;i10;i++)

{

printf("\t\n\t");

for(j=0;j10;j++)

{

printf("%4c",chessman[i][j]);

if(j==9i!=0i!=9)

printf("\t%d",i);

if(chessman[i][j]==0)

printf(" ");

}

}

printf("\n\n\n\n");

}

int exit()

{

int i,j;

for(i=0;i10;i++)

{

for(j=0;j10;j++)

if(chessman[i][j]!=0)

return 1;

}

return 0;

}

void near1(int a,int b,int a1,int b1)

{

int arow[2];

int aline[2];

int brow[2];

int bline[2];

int start=0,end=0;

int i,j,n;

for(i=0;i2;i++)

{

arow[i]=a;

brow[i]=a1;

aline[i]=b;

bline[i]=b1;

}

for(i=a+1;i10;i++)

{

if(chessman[i][b]==0)

arow[1]=i;

else

break;

}

for(i=a-1;i=0;i--)

{

if(chessman[i][b]==0)

arow[0]=i;

else

break;

}

for(i=a1+1;i10;i++)

{

if(chessman[i][b1]==0)

brow[1]=i;

else

break;

}

for(i=a1-1;i=0;i--)

{

if(chessman[i][b1]==0)

brow[0]=i;

else

break;

}

for(i=b+1;i10;i++)

{

if(chessman[a][i]==0)

aline[1]=i;

else

break;

}

for(i=b-1;i=0;i--)

{

if(chessman[a][i]==0)

aline[0]=i;

else

break;

}

for(i=b1+1;i10;i++)

{

if(chessman[a1][i]==0)

bline[1]=i;

else

break;

}

for(i=b1-1;i=0;i--)

{

if(chessman[a1][i]==0)

bline[0]=i;

else

break;

}

if(brow[0]=arow[0])

start=brow[0];

else

start=arow[0];

if(brow[1]=arow[1])

end=brow[1];

else

end=arow[1];

for(;start=end;start++)

{

if(bb1)

{

j=b;

i=b1;

}

else

{

j=b1;

i=b;

}

for(n=0;ji;j++)

{

if(chessman

今天给各位分享连连看c代码的知识,其中也会对连连看的代码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

[j]==0)

n++;

else

break;

}

if((n==b-b1)(n0))

chessman[a][b]=chessman[a1][b1]=0;

if((n==b1-b)(n0))

chessman[a][b]=chessman[a1][b1]=0;

}

if(aline[0]=bline[0])

start=aline[0];

else

start=bline[0];

if(aline[1]bline[1])

end=aline[1];

else

end=bline[1];

for(i=start;i=end;i++)

{

for(j=a,n=0;ja1;j++)

{

if(chessman[j][i]==0)

n++;

else

break;

}

if(n==a1-an0)

{

chessman[a][b]=chessman[a1][b1]=0;

break;

}

}

if(a==a1)

{

if((aline[1]=b1)||(aline[0]=b1))

chessman[a][b]=chessman[a1][b1]=0;

}

if(b==b1)

{

if(arow[1]=a1)

chessman[a][b]=chessman[a1][b1]=0;

}

}

关于连连看c代码和连连看的代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/798.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载