c语言连连看游戏源码_c语言编程小游戏连连看

hacker|
112

文章目录:

用dev c++怎么用c语言编写一个连连看游戏?写这个游戏难吗?一个星期够吗?

连的元素是什么? 图片?还是纯字母?

难度不高,不知你水平如何,如果是我写的话,应该几个小时就行了。

求连连看源代码

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

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;

}

}

}

}

}

}

}

}

求任何可在VC/C++环境下运行的连连看游戏完整源代码文件 谢谢了。。

#include stdio.h

#include conio.h

#include windows.h

#include time.h

#define Height 25 //迷宫的高度,必须为奇数

#define Width 25 //迷宫的宽度,必须为奇数

#define Wall 1

#define Road 0

#define Start 2

#define End 3

#define Esc 5

#define Up 1

#define Down 2

#define Left 3

#define Right 4

int map[Height+2][Width+2];

void gotoxy(int x,int y) //移动坐标

{

COORD coord;

coord.X=x;

coord.Y=y;

SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );

}

void hidden()//隐藏光标

{

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

CONSOLE_CURSOR_INFO cci;

GetConsoleCursorInfo(hOut,cci);

cci.bVisible=0;//赋1为显示,赋0为隐藏

SetConsoleCursorInfo(hOut,cci);

}

void create(int x,int y) //随机生成迷

{

int c[4][2]={0,1,1,0,0,-1,-1,0}; //四个方向

int i,j,t;

//将方向打乱

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

{

j=rand()%4;

t=c[i][0];c[i][0]=c[j][0];c[j][0]=t;

t=c[i][1];c[i][1]=c[j][1];c[j][1]=t;

}

map[x][y]=Road;

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

if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall)

{

map[x+c[i][0]][y+c[i][1]]=Road;

create(x+2*c[i][0],y+2*c[i][1]);

}

}

int get_key() //接收按键

{

char c;

while(c=getch())

{

if(c==27) return Esc; //Esc

if(c!=-32)continue;

c=getch();

if(c==72) return Up; //上

if(c==80) return Down; //下

if(c==75) return Left; //左

if(c==77) return Right; //右

}

return 0;

}

void paint(int x,int y) //画迷宫

{

gotoxy(2*y-2,x-1);

switch(map[x][y])

{

case Start:

printf("入");break; //画入口

case End:

printf("出");break; //画出口

case Wall:

printf("▇");break; //画墙

case Road:

printf(" ");break; //画路

}

}

void game()

{

int x=2,y=1; //玩家当前位置,刚开始在入口处

int c; //用来接收按键

while(1)

{

gotoxy(2*y-2,x-1);

printf("●"); //画出玩家当前位置

if(map[x][y]==End) //判断是否到达出口

{

gotoxy(30,24);

printf("到达终点,按任意键结束");

getch();

break;

}

c=get_key();

if(c==Esc)

{

gotoxy(0,24);

break;

}

switch(c)

{

case Up: //向上走

if(map[x-1][y]!=Wall)

{

paint(x,y);

x--;

}

break;

case Down: //向下走

if(map[x+1][y]!=Wall)

{

paint(x,y);

x++;

}

break;

case Left: //向左走

if(map[x][y-1]!=Wall)

{

paint(x,y);

y--;

}

break;

case Right: //向右走

if(map[x][y+1]!=Wall)

{

paint(x,y);

y++;

}

break;

}

}

}

int main()

{

int i,j;

srand((unsigned)time(NULL)); //初始化随即种子

hidden(); //隐藏光标

for(i=0;i=Height+1;i++)

for(j=0;j=Width+1;j++)

if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫

map[i][j]=Road;

else map[i][j]=Wall;

create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数

for(i=0;i=Height+1;i++) //边界处理

{

map[i][0]=Wall;

map[i][Width+1]=Wall;

}

for(j=0;j=Width+1;j++) //边界处理

{

map[0][j]=Wall;

map[Height+1][j]=Wall;

}

map[2][1]=Start; //给定入口

map[Height-1][Width]=End; //给定出口

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

for(j=1;j=Width;j++) //画出迷宫

paint(i,j);

game(); //开始游戏

getch();

return 0;

}

求c语言的连连看源程序

参考:

;vw=allssid=from=bd_page_type=1uid=D707A757C67E11B4F7ACD4D0D2C9C212pu=rc@1,pic@on,sl@1,pw@1000,sz@224_220,pd@1,fz@2,lp@0,tpl@color,st=1wk=rdmaxpage=6pos=all

以下是部分代码:

/*

* 连连看游戏C语言源代码

*/

#include stdio.h

#include graphics.h

#include stdlib.h

#include math.h

#include dos.h

#define true 1

#define false 0

/* ---------------------全局变量------------------------------------ */

int BkGndColor=BLACK;

int BorderColor=LIGHTGRAY;

int LineColor=LIGHTBLUE;/* 消除一对方块时时候的连线颜色 */

/* Pb - ProgressBar */

int PbColor=LIGHTGREEN;

int PbY=4;

int PbHeight=4;

int PbValue; /* 进度条百分比,初始值为100.*/

long StartTime; /* 开始时间的秒数,只统计分钟,秒 */

long TotalTime; /* 游戏总共的最大秒数!,*/

/* BoardDatas: a small-size board */

/* Board[x][y][0] - 0:empty, 1:filled */

/* Board[x][y][1] - cell's key; */

unsigned char Board[10][10][2];

int CellSize=30;

int BoardX=20;

int BoardY=60;

int BoardWidth=10;

int BoardHeight=10;

int CellColor=WHITE;

int SelColor=BLUE; /* selCell's border rect color */

int CurColor=RED; /* curCell's border rect color */

int EraColor=CYAN; /* 用于擦除cell的颜色!*/

int PairsCount; /* how much pairs we have put on board */

/* 用于存储逻辑坐标(索引) */

typedef struct _tagCELL

{

char x;

char y;

} CELL;

CELL selCell,curCell;/*缓存前一个被选中的位置以及当前所处位置!*/

/*Scan Codes Define*/

enum KEYCODES

{

K_ESC =0x011b,

K_UP =0x4800, /* upward arrow */

K_LEFT =0x4b00,

K_DOWN =0x5000,

K_RIGHT =0x4d00,

K_SPACE =0x3920,

K_P =0x1970,

K_RETURN =0x1c0d, /* Enter */

};

/* ---------------------函数列表------------------------------------ */

void InitGame(char *bgiPath);

void PlayGame();

void QuitGame();

void InitProgressBar();

void UpdateProgressBar(int percent);

void DrawCell(int key,int x,int y,int color);

void EraseCell(int x,int y);

void DrawBorderRect(CELL *c,int color);

void DrawGameOver(char* info);

int GetKeyCode();

int FindPath(CELL *c1,CELL *c2);

/*绘制消除方块时候的连接路径!,用指定颜色!*/

void DrawPath(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int color);

/* ----------------------函数实现----------------------------------- */

/* ----------------------[ 核心算法 ]---------------------------------

* 先进行水平方向判断,找出两点所在的水平直线活动范围,

* 算出这两条线段在垂直方向的共同区域!!!,

* 遍历该区域判断能否在两线段间架起公垂线,能则两点连接上;

* 接着进行垂直方向判断,类同。无论两点在不在一条直线上,

* 都能使用该算法,因为两点同线只是两点作为矩形对角点的特例而已。

*/

/* 找到两个CELL之间的路径,成功返回true */

int FindPath(CELL *c1,CELL *c2)

{

int i,j,

path,min1,max1,min2,max2,left,right,top,bottom;

/*---------------(0)判断是否点中相同块! ------------*/

if(Board[c1-x][c1-y][1] != Board[c2-x][c2-y][1])

return false;

/*---------------(1)查找水平方向公共区域!-----------*/

min1=max1=c1-x;

min2=max2=c2-x;

while(min1-1=0 Board[min1-1][c1-y][0]==0) min1--;

while(min2-1=0 Board[min2-1][c2-y][0]==0) min2--;

left=max(min1,min2); /* 左边界 */

while(max1+1BoardWidth Board[max1+1][c1-y][0]==0) max1++;

while(max2+1BoardWidth Board[max2+1][c2-y][0]==0) max2++;

right=min(max1,max2); /* 右边界 */

/* 检查两条水平线之间是否有公垂线连通!*/

/* 可以在边缘连通 */

if(left==0)

{

/* 左边缘连通 */

DrawPath(c1-x,c1-y, -1,c1-y, -1,c2-y, c2-x,c2-y, LineColor);

delay(6000);

DrawPath(c1-x,c1-y, -1,c1-y, -1,c2-y, c2-x,c2-y, BkGndColor);/*插除线条!*/

return true;

}

if(right==(BoardWidth-1))

{

DrawPath(c1-x,c1-y, BoardWidth,c1-y, BoardWidth,c2-y, c2-x,c2-y, LineColor);

delay(6000);

DrawPath(c1-x,c1-y, BoardWidth,c1-y, BoardWidth,c2-y, c2-x,c2-y, BkGndColor);/*插除线条!*/

return true;

}

for(i=left;i=right;i++)

{

path=0;/*统计垂直的公垂线长度!*/

for(j=min(c1-y,c2-y)+1;jmax(c1-y,c2-y);j++)

{

path+=Board[i][j][0];

if(path0) break;

}

if(path==0)

{

DrawPath(c1-x,c1-y, i,c1-y, i,c2-y, c2-x,c2-y, LineColor);

delay(6000);

DrawPath(c1-x,c1-y, i,c1-y, i,c2-y, c2-x,c2-y, BkGndColor);/*插除线条!*/

return true;

}

}

/*---------------(2)查找垂直方向公共区域!-----------*/

min1=max1=c1-y;

min2=max2=c2-y;

while(min1-1=0 Board[c1-x][min1-1][0]==0) min1--;

while(min2-1=0 Board[c2-x][min2-1][0]==0) min2--;

top=max(min1,min2);

while(max1+1BoardHeight Board[c1-x][max1+1][0]==0) max1++;

while(max2+1BoardHeight Board[c2-x][max2+1][0]==0) max2++;

bottom=min(max1,max2);

/* 检查两条垂直线之间是否有公垂线连通!*/

/* 可以在边缘连通 */

if(top==0)

{

/* 同在顶端消除 */

DrawPath(c1-x,c1-y, c1-x,-1, c2-x,-1, c2-x,c2-y, LineColor);

delay(6000);

DrawPath(c1-x,c1-y, c1-x,-1, c2-x,-1, c2-x,c2-y, BkGndColor);/*插除线条!*/

return true;

}

if(bottom==(BoardHeight-1))

{

DrawPath(c1-x,c1-y, c1-x,BoardHeight, c2-x,BoardHeight, c2-x,c2-y, LineColor);

delay(6000);

DrawPath(c1-x,c1-y, c1-x,BoardHeight, c2-x,BoardHeight, c2-x,c2-y, BkGndColor);/*插除线条!*/

return true;

}

谁能帮忙写一个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语言编写的 连连看游戏 代码啊,需要简易一点的,适合刚入门新手看懂的

你这10财富也太少了,这种范式代码很多好吧 自己百度下 一搜一大堆

3条大神的评论

  • avatar
    访客 2022-07-15 下午 11:37:55

    guli(); s="no"; k=0; d[x0][y0]=0; d[x][y]=0; } if (y0y) { for (n=y0;n=y-1 ;n++ ) { if

  • avatar
    访客 2022-07-16 上午 05:59:23

    ) { wei(4,2,b17); } }); b18.addMouseListener(new MouseAdapter(){ public void mouseClicke

  • avatar
    访客 2022-07-16 上午 01:01:28

    c1-y][1] != Board[c2-x][c2-y][1])return false;/*---------------(1)查找水平方向公共区域!-------

发表评论