用C语言编写一个单词接龙的小游戏

核心提示//囧,代码写好回来一看居然已经有推荐答案了.....#include <stdio.h>#include <iostream>#include <string.h>#include <stdlib.h>#include <windows.

//囧,代码写好回来一看居然已经有推荐答案了.....

#include <stdio.h>

#include <iostream>

#include <string.h>

#include <stdlib.h>

#include <windows.h>

#define MAX 80

char buff[MAX+1];

char first[4];

char last;

bool spellCheck()

{

int p = 0;

while(buff[p] != 0)

{

if(buff[p] > 'z'||buff[p] < 'a')

return false;

p++;

}

return true;

}

bool syntaxCheck()

{

int len = strlen(first);

if(len == 3)

{

if(strncmp(first,buff,3) == 0)

return true;

else if(strncmp(&first[1],buff,2) == 0)

return true;

else if(strncmp(&first[2],buff,1) == 0)

return true;

}

else if(len == 2)

{

if(strncmp(&first[0],buff,2) == 0)

return true;

else if(strncmp(&first[1],buff,1) == 0)

return true;

}

else if(len == 1)

{

if(strncmp(&first[0],buff,1) == 0)

return true;

}

return false;

}

void getWord()

{

do

{

printf("请输入一个全小写的单词,和%s匹配n",first);

scanf("%s",buff);

if(!spellCheck())

{

printf("拼写错误,请输入一个全小写的单词......n");

continue;

}

if(strlen(buff) == 0)

{

printf("请输入一个单词......n");

continue;

}

return;

}while(true);

}

void start()

{

FILE *fp = fopen("D:1.txt","w");//<--------这里改输出文件的路径

int len;

first[0] = rand()%26+'a';

first[1] = 0;

last = rand()%26+'a';

printf("a1:%c a2:%cn",first[0],last);

while(true)

{

getWord();

if(!syntaxCheck())

{

printf("输入的单词不符合要求,请重新输入......n");

continue;

}

else

{

fprintf(fp,"%sn",buff);

len = strlen(buff);

if(buff[len-1] == last)

{

printf("%s末尾和a2:%c匹配,所以游戏结束n",buff,last);

break;

}

else

{

if(len >= 3)

strcpy(first,&buff[len-3]);

else

strcpy(first,buff);

printf("匹配成功,请继续输入......n");

}

}

}

fclose(fp);

}

int main()

{

srand(GetTickCount());

start();

system("pause");

}

//声明

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

#include<conio.h>

#include<windows.h>

#define SIZE 4

static int score=0;

void putn(int n[][SIZE]);

void getn(int n[][SIZE]);

int isempty(int n[][SIZE]);

int isfull(int n[][SIZE]);

void math(int n[][SIZE],char c);

void tow(int n[][SIZE]);

void toa(int n[][SIZE]);

void tos(int n[][SIZE]);

void tod(int n[][SIZE]);

//主函数

int main()

{

int i,j;

int n[SIZE][SIZE];

char c=' ';

for(i=0;i<SIZE;i++)

{

for(j=0;j<SIZE;j++)

{

n[i][j]=0;

}

}

printf( "***********************n"

" 2048(%dX%d) n"

" control:W/A/S/D n"

"press any key to begin n"

"***********************n",SIZE,SIZE);

getch();

system("cls");

//n[0][1]=2048;

//n[0][3]=2048;

while(1)

{

if(isempty(n))

getn(n);

putn(n);

if(!isempty(n)&&isfull(n))

break;

sleep(200);

c=getch();

while(c!='w'&&c!='a'&&c!='s'&&c!='d')

c=getch();

math(n,c);

system("cls");

}

printf(" Game Over!n",score);

return 0;

}

//函数

void putn(int n[][SIZE])

{

int i,j;

for(i=0;i<SIZE;i++)

{

for(j=0;j<SIZE;j++)

printf("| ");

printf("|n");

for(j=0;j<SIZE;j++)

{

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

printf("| ");

else

printf("|%4d ",n[i][j]);

}

printf("|n");

for(j=0;j<SIZE;j++)

printf("|_____");

printf("|n");

}

printf("score: %d",score);

}

void getn(int n[][SIZE])

{

int a,b;

a=rand()%SIZE;

b=rand()%SIZE;

while(n[a][b]!=0)

{

a=rand()%SIZE;

b=rand()%SIZE;

}

n[a][b]=2;

}

int isempty(int n[][SIZE])

{

int i,j,count=0;

for(i=0;i<SIZE;i++)

for(j=0;j<SIZE;j++)

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

count++;

return count;

}

int isfull(int n[][SIZE])

{

int i,j,count=0;

for(i=0;i<SIZE;i++)

{

for(j=1;j<SIZE-1;j++)

{

if(n[i][j]==n[i][j+1]||n[i][j]==n[i][j-1])

count++;

}

}

for(j=0;j<SIZE;j++)

{

for(i=1;i<SIZE-1;i++)

{

if(n[i][j]==n[i+1][j]||n[i][j]==n[i-1][j])

count++;

}

}

return count>0?0:1;

}

void math(int n[][SIZE],char c)

{

switch(c)

{

case 'w':tow(n);break;

case 'a':toa(n);break;

case 's':tos(n);break;

case 'd':tod(n);break;

default :;

}

}

void tow(int n[][SIZE])

{

int i,j,a;

int m[SIZE];

for(a=0;a<SIZE;a++)

m[a]=0;

for(j=0;j<SIZE;j++)

{

for(a=0;a<SIZE;a++)

{

for(i=0;i<SIZE-1;i++)

{

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

{

n[i][j]=n[i+1][j];

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

}

}

}

}

for(j=0;j<SIZE;j++)

{

for(a=0,i=0;i<SIZE;i++)

{

if(n[i][j]!=n[i+1][j]&&n[i][j]!=0||n[i][j]==2048)

{

m[a++]=n[i][j];

n[i][j]=0;

}

else if(n[i][j]==n[i+1][j])

{

m[a++]=n[i][j]+n[i+1][j];

score+=m[a-1];

n[i][j]=0,n[i+1][j]=0;

}

}

for(i=0;i<SIZE;i++)

{

n[i][j]=m[i];

m[i]=0;

}

}

}

void toa(int n[][SIZE])

{

int i,j,a;

int m[SIZE];

for(a=0;a<SIZE;a++)

m[a]=0;

for(i=0;i<SIZE;i++)

{

for(a=0;a<SIZE;a++)

{

for(j=0;j<SIZE-1;j++)

{

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

{

n[i][j]=n[i][j+1];

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

}

}

}

}

for(i=0;i<SIZE;i++)

{

for(a=0,j=0;j<SIZE;j++)

{

if(n[i][j]!=n[i][j+1]&&n[i][j]!=0||n[i][j]==2048)

{

m[a++]=n[i][j];

n[i][j]=0;

}

else if(n[i][j]==n[i][j+1])

{

m[a++]=n[i][j]+n[i][j+1];

score+=m[a-1];

n[i][j]=0,n[i][j+1]=0;

}

}

for(j=0;j<SIZE;j++)

{

n[i][j]=m[j];

m[j]=0;

}

}

}

void tos(int n[][SIZE])

{

int i,j,a;

int m[SIZE];

for(a=0;a<SIZE;a++)

m[a]=0;

for(j=SIZE-1;j>=0;j--)

{

for(a=SIZE-1;a>=0;a--)

{

for(i=SIZE-1;i>0;i--)

{

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

{

n[i][j]=n[i-1][j];

n[i-1][j]=0;

}

}

}

}

for(j=SIZE-1;j>=0;j--)

{

for(a=SIZE-1,i=SIZE-1;i>=0;i--)

{

if(n[i][j]!=n[i-1][j]&&n[i][j]!=0||n[i][j]==2048)

{

m[a--]=n[i][j];

n[i][j]=0;

}

else if(n[i][j]==n[i-1][j])

{

m[a--]=n[i][j]+n[i-1][j];

score+=m[a+1];

n[i][j]=0,n[i-1][j]=0;

}

}

for(i=SIZE-1;i>=0;i--)

{

n[i][j]=m[i];

m[i]=0;

}

}

}

void tod(int n[][SIZE])

{

int i,j,a;

int m[SIZE];

for(a=0;a<SIZE;a++)

m[a]=0;

for(i=SIZE-1;i>=0;i--)

{

for(a=SIZE-1;a>=0;a--)

{

for(j=SIZE-1;j>0;j--)

{

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

{

n[i][j]=n[i][j-1];

n[i][j-1]=0;

}

}

}

}

for(i=SIZE-1;i>=0;i--)

{

for(a=SIZE-1,j=SIZE-1;j>=0;j--)

{

if(n[i][j]!=n[i][j-1]&&n[i][j]!=0||n[i][j]==2048)

{

m[a--]=n[i][j];

n[i][j]=0;

}

else if(n[i][j]==n[i][j-1])

{

m[a--]=n[i][j]+n[i][j-1];

score+=m[a+1];

n[i][j]=0,n[i][j-1]=0;

}

}

for(j=SIZE-1;j>=0;j--)

{

n[i][j]=m[j];

m[j]=0;

}

}

}

需要注意的是:

srand((unsigned) time(0);rand();是固定形式,不要更改任何一个字符!

如果不能编译,请把sleep(200);注释掉,如果提示不能找到system("cls");请把system("cls")更换为clrscr()。

c语言中没有类,只有结构,也可以像类一样编写,用结构,但其成员都是公开访问的,C++才有真正的类。

 
友情链接
鄂ICP备19019357号-22