本篇文章给大家谈谈连连看c语言实验报告,以及C语言连连看对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、谁能帮忙写一个c语言连连看游戏,要4×4的,功能最简单的就可以!但是要实现连连看应该有的功能2、我要一个用c语言做连连看(有图片),怎样生成地图,怎样实现算法。3、如何用c语言编写一个简单的连连看。因为要移植到ucgui上,最好是要图形化的方法。4、C++连连看算法5、连连看游戏程序主要功能是可以实现什么
谁能帮忙写一个c语言连连看游戏,要4×4的,功能最简单的就可以!但是要实现连连看应该有的功能
刚写的,新鲜出炉:
#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语言做连连看(有图片),怎样生成地图,怎样实现算法。
太麻烦了,要调用Windows API 。估计一天都不一定能搞定。
用MFC .net 估计都没人帮你做。
生成地图你可以将 每种图片的数量保存好 比如 苹果用 1表示 西瓜用2 表示 ,可以弄一个链表,方便删除, 生成地图时随机一个数,到链表里取出,然后在从连表里删除。
判断的话 你可以将 地图在加大一格(不显示),然后用类似寻路算法来做。
我没实践过,你可以试试。
如何用c语言编写一个简单的连连看。因为要移植到ucgui上,最好是要图形化的方法。
先评我为最佳答案吧,然后给我留个信箱,我发给你,是我”多”年前学C鼓倒的。交流一下
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 )
连连看游戏程序主要功能是可以实现什么
////
// 简单连连看游戏
//
//
//
#include stdio.h
#include stdlib.h
#include string.h
#define ROW 4 //行数目
#define COL 4
#define ICO_NUM 5
//游戏图标
const char ico[ICO_NUM] = “@#$ “;
char games[ROW][COL] = {0};
//初始化数据
void init_game(void)
{
int i, j;
for(i = 0; i ROW; i++)
{
for(j = 0; j COL; j++)
{
games[i][j] = rand() % (ICO_NUM – 1); //不能为空白
if()
}
}
}
//绘制游戏
void draw_game(void)
{
int i, j;
printf(“(连连看)\n”);
printf(“=x= =0==1==2==3==4==5==6==7==8==9=\n”);
for(i = 0; i ROW; i++)
{
printf(“=%d= “, i);
for(j = 0; j COL; j++)
{
printf(” %c “, ico[games[i][j]]);
}
printf(“\n”);
}
printf(“== y=0==1==2==3==4==5==6==7==8==9=\n”);
}
//运行游戏
void run_game(void)
{
//坐标从0开始
int x1 = -1, y1 = -1,
x2 = -1, y2 = -1;
int num = 0;
char c = 0;
while(1)
{
//初始化数据
init_game();
while(1)
{
//绘制游戏
draw_game();
while(1)
{
printf(“请输入要消除的坐标1(x1 y1):\n”);
scanf(“%d%d”, x1, y1);
if(x1 0 || x1 = ROW)
{
printf(“x1输入有误,请从新输入:(0 = x1 = %d)\n”,
ROW);
}
else if(y1 0 || y1 = COL)
{
printf(“y1输入有误,请从新输入:(0 = y1 = %d)\n”,
COL);
}
else
{
printf(” (%d, %d):%c \n”,x1, y1, ico[games[x1][y1]]);
break;
}
}
while(1)
{
printf(“请输入要消除的坐标2(x2 y2):\n”);
scanf(“%d%d”, x2, y2);
if(x2 0 || x2 = ROW)
{
printf(“x2输入有误,请从新输入:(0 = x2 = %d)\n”,
ROW);
}
else if(y2 0 || y2 = COL)
{
printf(“y2输入有误,请从新输入:(0 = y2 = %d)\n”,
COL);
}
else
{
printf(” (%d, %d):%c \n”,x2, y2, ico[games[x2][y2]]);
break;
}
}
if(games[x1][y1] == games[x2][y2])
{
printf(“%c —消除成功—%c\n”, ico[games[x1][y1]],
ico[games[x2][y2]]);
games[x1][y1] = ICO_NUM – 1;
games[x2][y2] = ICO_NUM – 1;
num++;
if(num = (ROW * COL)/2)
{
printf(“恭喜你获得胜利~\n”);
break;
}
}
else
{
printf(“%c —消除失败!—%c\n”, ico[games[x1][y1]],
ico[games[x2][y2]]);
}
}
printf(“是否再来一局?(Y/N)\n”);
scanf(“%c”, c);
if(c == ‘N’ || c == ‘n’)
break;
}
}
int main(void)
{
//运行游戏
run_game();
return 0;
}
连连看c语言实验报告的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于C语言连连看、连连看c语言实验报告的信息别忘了在本站进行查找喔。