當前位置:首頁 » 編程語言 » c語言點燈游戲
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言點燈游戲

發布時間: 2022-11-29 23:24:10

c語言有一個8層燈塔,每層所點燈數為上一層兩倍,共有765盞燈,求塔底燈數 (運用函數實現如遞歸函數)

#include <stdio.h>

size_t foo(size_t n)
{
return (n==1) ? 1 : f(n-1) + (1<<(n-1));
}

void main(void)
{
printf("%d\n", 765-foo(7));
}

② 如何使用C語言編寫簡單小游戲

C語言是計算機專業都要學習的一門基礎學科。一般來說,是比較枯燥的.那麼,我們能不能通過編一些小游戲來提高它的趣味性呢?這樣學習程序設計,就不會是一件艱苦 ,枯燥的事,它變得象電腦游戲一樣充滿好奇,富有樂趣。

例如2048這款游戲:

方法/步驟:

#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( "*********************** "

    " 2048(%dX%d) "

    " control:W/A/S/D "

    "press any key to begin "

    "*********************** ",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! ",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("| ");

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

    {

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

    printf("| ");

    else

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

    }

    printf("| ");

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

    printf("|_____");

    printf("| ");

    }

    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;

    }

    }

    }

③ C語言程序設計24點游戲,能算出24的運演算法則的代碼,很急

#include<iostream>
#include<math.h>
using namespace std;
const double MIN=1E-6;
void Print(int *Rank,double *FourNum)
{
for(int i=0;i<4;i++)
cout<<FourNum[Rank[i]]<<" ";
cout<<endl;
}
void Calculate_24(int *Rank,int *FourNum,char *Oper,int i,int j,int k,bool &def)
{
double res=0;
switch(i)
{
case 0:
res=FourNum[Rank[0]]+FourNum[Rank[1]];
break;
case 1:
res=FourNum[Rank[0]]-FourNum[Rank[1]];
break;
case 2:
res=FourNum[Rank[0]]*FourNum[Rank[1]];
break;
case 3:
res=FourNum[Rank[0]]/FourNum[Rank[1]];
break;
}
switch(j)
{
case 0:
res=res+FourNum[Rank[2]];
break;
case 1:
res=res-FourNum[Rank[2]];
break;
case 2:
res=res*FourNum[Rank[2]];
break;
case 3:
res=res/FourNum[Rank[2]];
break;
}
switch(k)
{
case 0:
res=res+FourNum[Rank[3]];
break;
case 1:
res=res-FourNum[Rank[3]];
break;
case 2:
res=res*FourNum[Rank[3]];
break;
case 3:
res=res/FourNum[Rank[3]];
break;
}
if(fabs(res-24)>MIN)
return;
else
{
def=true;
for(int num=1;num<=7;num++)
{
switch(num)
{
case 1:
cout<<FourNum[Rank[0]];
break;
case 3:
cout<<FourNum[Rank[1]];
break;
case 5:
cout<<FourNum[Rank[2]];
break;
case 7:
cout<<FourNum[Rank[3]];
break;
case 2:
cout<<Oper[i];
break;
case 4:
cout<<Oper[j];
break;
case 6:
cout<<Oper[k];
break;
}
}
cout<<endl;
}
}
void SearchTree(int Depth,int *Rank,int *FourNum,char *Oper,bool &def)
{
int i,j,k;
if(Depth==4)
{
for(i=0;i<4;i++)
for(j=0;j<4;j++)
for(k=0;k<4;k++)
Calculate_24(Rank,FourNum,Oper,i,j,k,def);
}
else
{
for(i=0;i<4;i++)
{
int Remember=0;
for(j=0;j<Depth;j++)
{
if(Rank[j]==i)
Remember=1;
}
if(Remember)
continue;
Rank[Depth]=i;
SearchTree(Depth+1,Rank,FourNum,Oper,def);
}
}
}
int main()
{
int a[4],b[4],time;
char c[4]={'+','-','*','/'};
bool def=false;
cin>>time;
while(time--)
{
for(int i=0;i<4;i++)//輸入測試數據
cin>>a[i];
cout<<"所有可能的結果:"<<endl;
SearchTree(0,b,a,c,def);
if(def==false)
cout<<"No"<<endl;
}
return 0;
}

④ c語言怎麼做應用程序或游戲啊

C語言做游戲

點燈游戲(把燈都打開變黃):
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int j[8][6][6]={
0,0,0,0,0,0,0,1,1,0,1,1,0,1,0,1,0,1,0,0,1,1,1,0,0,1,0,1,0,1,0,1,1,0,1,1,
0,0,0,0,0,0,0,0,1,0,1,0,0,1,1,0,1,1,0,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,
0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,1,0,1,1,0,0,0,
0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,1,1,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,1,1,0,
0,0,0,0,0,0,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,
0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,0,0,0,0,0,0,0
},x,y,s,i,h=0,r=0,n;
pr(int u)
{ int g,f;
for (g=1;g<=5;g++)
for (f=1;f<=5;f++)
if (j[u][g][f]==1)
{gotoxy(f,g); textcolor(1); putch('o');}
else {gotoxy(f,g); textcolor(14); putch('o');}
}
hkg(int t,int y)
{ if (j[h][t][y]==1) {j[h][t][y]=0;}
else {j[h][t][y]=1;}
}
kg()
{ int e,r;
hkg(y,x); hkg(y-1,x); hkg(y,x+1); hkg(y+1,x); hkg(y,x-1); pr(h);
}
inkey()
{ char o;
if (kbhit()) {
o=getch();
if (o==27) {exit(0);}
if (o==13) {
kg(); r++;
gotoxy(7,5); printf("Bu Shu: %d",r);
if (r>=299) {over(); getch(); exit(0);}
}
if (o==0) {
o=getch();
if (o==72&&y>1) {y--;}
if (o==77&&x<5) {x++;}
if (o==80&&y<5) {y++;}
if (o==75&&x>1) {x--;}
}
}
}
test()
{ int o,p,k=0;
for (o=1;o<=5;o++)
for (p=1;p<=5;p++)
if (j[h][o][p]==1) {k=1;}
if (k==0)
{gotoxy(7,3); printf("Ni Ying Le! An Jian Ji Xu."); getch(); h++; pr(h);
gotoxy(7,3); printf(" ");
textcolor(10); gotoxy(3,7); printf("Di %d Guan",h+1);
}
}
over()
{ int v,b;
for (v=1;v<=24;v++)
for (b=1;b<=40;b++)
{gotoxy(b,v); textcolor(v); putch(219); delay(1000);}
for (v=1;v<=40;v++)
for (b=1;b<=24;b++)
{gotoxy(v,b); textcolor(v); putch(219); delay(1000);}
gotoxy(20,12); printf("Ni Shu Le!");
}
zai()
{ int g,h,y; gotoxy(15,10); printf("Zai Ru......");
gotoxy(15,15); printf("Zai Ru : Dong Hua ");
for (g=10;g<=15;g++)
{gotoxy(g,12); textcolor(2); putch(219);
for (h=1;h<=5;h++) delay(g*10000000);
gotoxy(19,17); printf("%d%",(g-10)*5);}
gotoxy(15,15); printf("Zai Ru : Di Tu ");
for (g=16;g<=25;g++)
{gotoxy(g,12); textcolor(2); putch(219);
for (h=1;h<=5;h++) delay(g*10000000);
gotoxy(19,17); printf("%d%",(g-10)*5);}
gotoxy(15,15); printf("Zai Ru : Tu Xing He Zi Ti");
for (g=26;g<=30;g++)
{gotoxy(g,12); textcolor(2); putch(219);
for (h=1;h<=5;h++) delay(g*10000000);
gotoxy(19,17); printf("%d%",(g-10)*5);}
for (y=1;y<=5;y++) delay(10000);
}
main()
{ textmode(C40); randomize(); clrscr(); zai(); clrscr(); textcolor(10);
gotoxy(7,5); printf("Bu Shu: %d",0);
gotoxy(3,7); printf("Di %d Guan",h+1);
for (i=1;i<=n;i++) {
do {x=rand()%5+1; y=rand()%5+1;} while (j[h][y][x]==1);
j[h][y][x]=1;
} pr(h); x=1; y=1;
while (1)
{ inkey(); gotoxy(x,y); test();
}
getch();
}

作者: 54qiuwenda

2008-7-26 12:17 回復此發言

--------------------------------------------------------------------------------

3 回復:C語言做游戲
走迷宮(用工具走到*):
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
main()
{ int map[20][20]={1,1,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,7,
1,0,1,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,0,7,
1,0,1,1,1,1,3,3,1,1,0,1,0,0,1,0,0,1,0,7,
1,0,0,0,1,2,4,2,0,1,0,1,1,0,1,0,0,1,1,7,
1,0,0,0,1,2,1,2,1,1,0,0,1,0,1,2,1,1,2,7,
1,0,1,1,1,0,1,2,2,2,2,1,1,0,2,2,1,2,2,7,
1,0,1,0,0,0,1,2,1,2,2,1,1,0,1,2,2,2,2,7,
1,0,1,1,1,0,1,2,1,2,2,1,0,0,1,1,2,1,2,7,
1,0,0,0,1,0,1,2,1,2,2,1,0,1,1,0,2,1,2,7,
1,0,1,0,0,0,1,2,1,1,0,1,0,0,0,0,2,1,2,7,
1,0,1,1,1,1,1,2,2,3,0,1,1,1,1,2,2,1,2,7,
1,0,0,0,2,2,2,2,8,1,0,0,1,2,2,2,1,1,2,7,
1,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,2,7,
1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,2,7,
1,2,1,1,1,1,0,0,1,1,1,1,2,2,1,2,2,2,2,7,
1,2,1,6,0,1,0,0,0,0,0,2,2,2,1,2,1,1,1,7,
1,2,1,1,5,1,1,1,1,1,1,2,2,2,1,2,1,2,0,7,
1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0,9,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
,x,y,q,w,
z[5][2]={{-1,0},{0,1},{1,0},{0,-1},{0,0}},l=4,s=0,d=0,j,i,u=0;
char o;
textmode(C40); randomize(); q=2; w=2; clrscr();
while (1) {
gotoxy(22,3); printf("D: ChuanQiang");
gotoxy(22,4); printf("(CrossWall)");
gotoxy(22,6); putch(219); printf(": QiangBi(Wall)");
gotoxy(22,8); printf("=: Shui(Water)");
gotoxy(22,10); printf("F: ShuiShang");
gotoxy(22,11); printf("FeiXing(FlyOn");
gotoxy(22,12); printf("Water)");
gotoxy(22,14); printf("/: Men(Door)");
gotoxy(22,16); printf("*: BaoZang");
gotoxy(22,17); printf("(Treasure)");
gotoxy(22,19); printf("Esc:TuiChu(Exit)");
for (y=0;y<20;y++) {
for (x=0;x<20;x++) {
if (map[y][x]==1||map[y][x]==7) {gotoxy(x+1,y+1); putch(219);}
if (map[y][x]==9) {gotoxy(x+1,y+1); putch('*');}
if (map[y][x]==3) {gotoxy(x+1,y+1); putch(219);}
if (map[y][x]==2) {gotoxy(x+1,y+1); putch('=');}
if (map[y][x]==5) {gotoxy(x+1,y+1); putch('/');}
if (map[y][x]==8) {gotoxy(x+1,y+1); putch('8');}
if (map[y][x]==4) {gotoxy(x+1,y+1); putch('F');}
if (map[y][x]==6) {gotoxy(x+1,y+1); putch('D');}
}
}
if (kbhit()) {
o=getch();
if (o==27) {exit(0);}
if (o==72) {l=0;}
if (o==77) {l=1;}
if (o==80) {l=2;}
if (o==75) {l=3;}
j=map[q-1+z[l][0]][w-1+z[l][1]];
if (j!=0&&j!=4&&j!=8&&j!=3&&j!=s*2&&j!=9&&j!=6&&j!=u*1&&j!=7)
{l=4;}
gotoxy(w,q); putch(' ');
gotoxy(22,22); for (i=1;i<=18;i++) putch(' ');
q=q+z[l][0]; w=w+z[l][1]; l=4;
if (j==4) {map[q-1][w-1]=0; s=1; gotoxy(35,10); printf(" /");
gotoxy(35,11); printf(" \\/");}
if (j==8) {map[q-1][w-1]=0; map[17][4]=0; gotoxy(5,18); putch(' ');
gotoxy(35,14); printf(" /"); gotoxy(35,15); printf(" \\/");}
if (j==3) {gotoxy(22,22); printf("AnMen(SecretDoor)");}
if (j==6) {map[q-1][w-1]=0; u=1; gotoxy(4,16); putch(' ');
gotoxy(35,3); printf(" /"); gotoxy(35,4); printf(" \\/");}
} gotoxy(w,q); putch(178);
if (map[q-1][w-1]==9)
{gotoxy(10,23); printf("You won! Very good!"); gotoxy(35,16);
printf(" /"); gotoxy(35,17); printf(" \\/"); break;}
delay(8000);
}
getch();
}

貪吃蛇(上下左右移動吃食物):
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
int x=5,y=2,r=1,mx[4]={0,1,0,-1},my[4]={-1,0,1,0},s[40][3],u=5,g=1,
map[40][24]={0},score=0;
boxx(int x1,int y1,int x2,char c)
{ int j;
for (j=x1;j<=x2;j++) {gotoxy(j,y1); putch&;;}
}
boxy(int x1,int y1,int y2,char c)
{ int j;
for (j=y1;j<=y2;j++) {gotoxy(x1,j); putch&;;}
}
inkey()
{ char o;
randomize();
if (kbhit()) {
o=getch();
if (o==27) {exit(0);}
if (o==0) {
o=getch();
if (o==72) r=0;
if (o==77) r=1;
if (o==80) r=2;
if (o==75) r=3;
}
}
x=x+mx[r]; y=y+my[r];
if (x<2||x>39) {x=x-mx[r];}
if (y<2||y>23) {y=y-my[r];}
gotoxy(x,y); putch(178); p(u); if (map[x][y]==1) {score++;
gotoxy(20,1); printf("Score : %d",score); map[x][y]=0;
u++; s[u][1]=s[u-1][1]; s[u][2]=s[u-1][2];
}
gotoxy(s[g][1],s[g][2]); putch(' ');
s[g][1]=x; s[g][2]=y; g=g++;
if (g>u) g=1;
}
p(int h)
{int z; for (z=1;z<=h;z++) {gotoxy(s[z][1],s[z][2]); putch(178);}
}
egg()
{int ex,ey,h;
for (h=1;h<=30;h++){ex=rand()%38+2; ey=rand()%22+2;
map[ex][ey]=1; gotoxy(ex,ey); putch('O');}
}
main()
{ randomize();
textmode(C40); textcolor(rand()%16);
s[1][1]=2; s[1][2]=2; s[2][1]=3; s[2][2]=2; s[3][1]=4;
s[3][2]=2; s[4][1]=5; s[4][2]=2; s[5][1]=6; s[5][2]=2;
boxx(1,1,40,219); boxx(1,24,40,219);
boxy(1,1,24,219); boxy(40,1,24,219); egg();
gotoxy(20,1); printf("Score : %d",score);
while (1) {
gotoxy(x,y); putch(' ');
inkey();
delay(30000); delay(30000);
}
getch();
}

貓捉老鼠(躲開貓吃食物):
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
main()
{ int cx,cy,x,y,xx,yy,fx,fy,n,kb,r=0,p,o,g=0,sc=0; int set[25][41]={0};
int mx[2][5]={{0,1,0,-1,0},{0,2,0,-2,0}},
my[2][5]={{0,0,-1,0,1},{0,0,-2,0,2}};
clrscr(); textmode(C40); randomize(); n=0;
while (n<50) { do{ fx=rand()%38+2; fy=rand()%22+2;} while(set[fy][fx]!=0);
set[fy][fx]=1; gotoxy(fx,fy); putch('.'); n++;} n=3;
while (n) { do{ fx=rand()%38+2;fy=rand()%22+2;} while (set[fy][fx]!=0);
set[fy][fx]=2; gotoxy(fx,fy); putch('o'); n--;}
gotoxy(2,2); cx=2; cy=2; putch('&');
for (x=1;x<=40;x++) {gotoxy(x,1); putch(219); gotoxy(x,24); putch(219);
if (x<=24) {gotoxy(1,x); putch(219); gotoxy(40,x); putch(219);}}
gotoxy(15,1); printf("Score: %d",sc);
gotoxy(39,23); putch('@'); x=39; y=23;
gotoxy(40,8); putch(' '); gotoxy(1,16); putch(' ');
while (1)
{ if (kbhit()) { kb=getch(); if (kb==27) exit(0); kb=getch();
if (x==40&&y==8) {
if (kb==75)
{gotoxy(x,y); putch(' '); x=x-1; gotoxy(x,y); putch('@');}
if (kb==77)
{gotoxy(x,y); putch(' '); x=2; gotoxy(x,y); putch('@');}
}
if (x==1&&y==16) {
if (kb==75)
{gotoxy(x,y); putch(' '); x=39; gotoxy(x,y); putch('@');}
if (kb==77)
{gotoxy(x,y); putch(' '); x=x+1; gotoxy(x,y); putch('@');}
}
if (kb==77&&y==8&&x==39) {gotoxy(x,y); putch(' ');
x=x+1; gotoxy(x,y); putch('@');} else
if (kb==75&&y==16&&x==2) {gotoxy(x,y); putch(' ');
x=x-1; gotoxy(x,y); putch('@');} else
{if (kb==77&&x<39) r=1; else if (kb==72&&y>2) r=2; else
if (kb==75&&x>2) r=3; else if (kb==80&&y<23) r=4; else r=0;
gotoxy(x,y); putch(' '); xx=x+mx[0][r]; yy=y+my[0][r];
if (g==0) {x=x+mx[0][r]; y=y+my[0][r];} else
{x=x+mx[1][r]; y=y+my[1][r]; g=g-1;
gotoxy(3,1); printf("Gao neng:%d",g); putch(219);}
if ((x==39&&kb==77)||(x==38&&kb==77&&g>0)) x=2;
if ((y==2&&kb==72)||(y==3&&kb==72&&g>0)) y=23;
if ((x==2&&kb==75)||(x==3&&kb==75&&g>0)) x=39;
if ((y==23&&kb==80)||(y==22&&kb==80&&g>0)) y=2;
gotoxy(x,y); putch('@'); if (set[y][x]==1||set[yy][xx]==1)
{gotoxy(15,1); printf("Score: %d",sc+1); sc=sc+1; set[y][x]=0;
gotoxy(xx,yy); putch(' '); gotoxy(x,y); putch('@');} else
if (set[y][x]==2||set[yy][xx]==2)
{gotoxy(3,1); printf("Gao neng:%d",g+10); putch(219); g=g+10;
set[y][x]=0; set[yy][xx]=0;}
if (g==0) { gotoxy(3,1); for (n=1;n<=11;n++) putch(219);}}
if (sc==20) break; if (x==cx&&y==cy) break;}
if ((x==40&&y==8)||(x==1&&y==16)) {} else {
gotoxy(cx,cy); if (set[cy][cx]==1) putch('.'); else putch(' ');
if (cx<x) cx=cx+1; if (cx>x) cx=cx-1;
if (cy<y) cy=cy+1; if (cy>y) cy=cy-1;
gotoxy(cx,cy); putch('&'); delay(10000); if (x==cx&&y==cy) break;}
}
if (cx==x&&cy==y) {gotoxy(15,3); printf("The cat win!");} else
if (sc>=20) {gotoxy(15,3); printf("The mouse(you) win!");}
getch();
}

俄羅斯方塊(移動方塊消行):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
int x[8][4]={0,0,0,0,
0,1,2,0,
0,1,-1,0,
0,0,0,0,
0,0,1,2,
0,1,0,1,
0,-1,0,1,
0,1,0,-1};
int y[8][4]={0,0,0,0,
0,0,0,-1,
0,0,0,-1,
0,1,2,3,
0,1,0,0,
0,0,1,1,
0,0,1,1,
0,0,1,1};
int fxj[4]={0,1,0,-1},fyj[4]={-1,0,1,0},k[8]={0,0,0,3,1,3,2,2};
int fx,fy,j=2,map[25][41]={0},mapc[25][41]={0},h,w=0,z,nx[4],ny[4],score;
p(int t,char o,int b)
{ int i;
for (i=0;i<=3;i++)
{textcolor(t); gotoxy(fx+nx[i],fy+ny[i]); putch(o);
map[fy+ny[i]][fx+nx[i]]=b;
}
}
inkey()
{ int l,e,k,nnx[4],nny[4]; char o;
if (kbhit()) {
o=getch();
if (o==27) exit(0);
if (o==0) {
o=getch();
if (o==77)
{e=0; for (l=0;l<=3;l++) {
if (fx+nx[l]+1==41) {e=1; break;}
if (map[fy+ny[l]][fx+nx[l]+1]==1) {e=1; break;}
}
if (e==0) {j=1;}
}
if (o==75)
{e=0; for (l=0;l<=3;l++) {
if (fx+nx[l]-1==10) {e=1; break;}
if (map[fy+ny[l]][fx+nx[l]-1]==1) {e=1; break;}
}
if (e==0) {j=3;}
}
if (o==80)
{e=0; for (l=0;l<=3;l++) {
if (map[fy+ny[l]+1][fx+nx[l]]==1) {e=1; break;}
}
if (e==0) {w=10; z=19;}
}
if (o==72)
{ k=0;
for (l=0;l<=3;l++) {
e=nx[l]; nx[l]=ny[l]*(-1); ny[l]=e;
if (map[fy+ny[l]][fx+nx[l]]==1) {k=1;}
if (fx+nx[l]<11||fx+nx[l]>40) {k=1;}
if (fy+ny[l]>24||fy+ny[l]<2) {k=1;}
}
if (k==1)
for (l=0;l<=3;l++) {e=nx[l]; nx[l]=ny[l]; ny[l]=e/(-1);}
}
}
}
}
win()
{gotoxy(20,12); textcolor(3); printf("You won!");
}
over()
{gotoxy(19,12); textcolor(14); printf("Game over!");
}
main()
{ int g,c,u,b,v; textmode(C40); clrscr(); randomize(); h=rand()%7+1;
for (g=1;g<=24;g++) {gotoxy(10,g); putch('|');}
gotoxy(3,3); printf("Next :"); gotoxy(3,15); printf("Score:");
gotoxy(3,16); printf("%d",0); gotoxy(3,11); printf("Esc=");
gotoxy(3,12); printf("TuiChu");
while (1) {
c=0; for (g=0;g<=3;g++) {nx[g]=x[h][g]; ny[g]=y[h][g];}
b=rand()%7+1; fx=25; fy=2;
for (g=0;g<=3;g++)
{textcolor(b); gotoxy(3+x[b][g],5+y[b][g]); putch(219);
}
while (c==0) {
inkey();
p(h,219,0); for (g=1;g<=30-z;g++) delay(1000);
w=w-1; if (w==0) z=0;
p(h,32,0); if (j!=2) {fx=fx+fxj[j]; fy=fy+fyj[j];}
j=2; fx=fx+fxj[2]; fy=fy+fyj[2];
for (g=0;g<=3;g++) {
if (fy+ny[g]>24) {c=1; break;}
if (map[fy+ny[g]][fx+nx[g]]==1) {c=1; break;}
}
} fx=fx-fxj[j]; fy=fy-fyj[j];
p(h,219,1); u=0; w=0;
for (g=0;g<=3;g++) {mapc[fy+ny[g]][fx+nx[g]]=h;}
for (g=0;g<=3;g++)
{textcolor(b); gotoxy(3+x[b][g],5+y[b][g]); putch(32);
}
for (g=1;g<=40;g++) if (map[2][g]==1||map[1][g]) {u=1; break;}
if (u==1)
{ over(); b=rand()%7+1; fx=30; fy=2;
for (g=0;g<=3;g++)
{textcolor(b); gotoxy(3+x[b][g],5+y[b][g]); putch(219);}
getch(); exit(0);
}
h=b; z=1;
do {
u=0; for (g=11;g<=40;g++) {if (map[z][g]==0) {u=1; break;}}
if (u==0) {
for (g=11;g<=40;g++) {gotoxy(g,z); putch(32);}
for (g=z-1;g>=1;g--) for (v=11;v<=40;v++) {map[g+1][v]=map[g][v];}
for (g=z-1;g>=1;g--) for (v=11;v<=40;v++) {mapc[g+1][v]=mapc[g][v];}
for (g=1;g<=24;g++) for (v=11;v<=40;v++)
{gotoxy(v,g); textcolor(mapc[g][v]); putch(219);}
score=score+10; gotoxy(3,16); printf("%d",score);
if (score>=30) {win(); getch(); exit;}
}
z++;
} while (z<25); z=0;
}
}
圈圈叉叉游戲(游標控制):
#include <conio.h>
#include <stdio.h>
int w[3][3]={2,1,2,
1,3,1,
2,1,2},i,j,k,l,kk,ll,x,y,m,prx=3,pry=3,s=0,f,g,e,r,h,m,q;
inkey()
{ char o,c[1];
if (kbhit())
{ o=getch();
if (o==27) {exit(0);}
if (o==13) {
gettext(x,y,x,y,&c);
if (c[0]==32) {gotoxy(x,y); putch('o'); s=1;
w[(y-pry)/2][(x-prx)/2]=-1;
}
}
if (o==0) {
o=getch();
if (o==72&&y>pry) {y--;}
if (o==77&&x<prx+4) {x++;}
if (o==80&&y<pry+4) {y++;}
if (o==75&&x>prx) {x--;}
}
}
}
pr()
{ gotoxy(prx,pry+0); printf(" | | ");
gotoxy(prx,pry+1); printf("-+-+-");
gotoxy(prx,pry+2); printf(" | | ");
gotoxy(prx,pry+3); printf("-+-+-");
gotoxy(prx,pry+4); printf(" | | ");
}
p()
{ for (h=0;h<=2;h++) {
q=0; for (m=0;m<=2;m++) if (w[h][m]==-1) q++;
if (q==3) {gotoxy(10,5); printf("Ni Ying Le"); getch(); exit(0);}
q=0; for (m=0;m<=2;m++) if (w[m][h]==-1) q++;
if (q==3) {gotoxy(10,5); printf("Ni Ying Le"); getch(); exit(0);}
q=0; for (m=0;m<=2;m++) if (w[h][m]==-2) q++;
if (q==3) {gotoxy(10,5); printf("Wo Ying Le"); getch(); exit(0);}
q=0; for (m=0;m<=2;m++) if (w[m][h]==-2) q++;
if (q==3) {gotoxy(10,5); printf("Wo Ying Le"); getch(); exit(0);}
}
q=w[0][0]+w[1][1]+w[2][2];
if (q==-6) {gotoxy(10,5); printf("Wo Ying Le"); getch(); exit(0);}
q=w[0][2]+w[1][1]+w[2][0];
if (q==-6) {gotoxy(10,5); printf("Wo Ying Le"); getch(); exit(0);}
q=0; for (h=0;h<=2;h++) for (m=0;m<=2;m++) if (w[h][m]>0) {q=1;}
if (q==0) {gotoxy(10,5); printf("Ping Ju"); getch(); exit(0);}
}
main()
{ textmode(C40); pr(); gotoxy(prx+2,pry+2); putch('x');
w[1][1]=-2; x=prx; y=pry; gotoxy(10,7); printf("Esc Tui Chu");
while (1) {
inkey(); gotoxy(x,y);
if (s==1) {s=0; e=-1; r=-1;
for (i=0;i<=2;i++)
{ l=0; f=-1; g=-1; ll=0; for (j=0;j<=2;j++) {
if (w[i][j]==-1) {l++;}
if (w[i][j]==-2) {ll++;}
if (w[i][j]>0) {f=i; g=j;}
}
if ((l==2)&&(e<0||r<0)&&f>=0&&g>=0) {e=f; r=g;}
if (ll==2&&f>=0&&g>=0) {e=f; r=g;}
k=0; f=-1; g=-1; kk=0; for (j=0;j<=2;j++) {
if (w[j][i]==-1) {k++;}
if (w[j][i]==-2) {kk++;}
if (w[j][i]>0) {f=j; g=i;}
}
if ((k==2)&&(e<0||r<0)&&f>=0&&g>=0) {e=f; r=g;}
if (kk==2&&f>=0&&g>=0) {e=f; r=g;}
}
k=0; f=-1; g=-1;
if (w[0][0]==-1) {k++;} if (w[0][0]>0) {f=0; g=0;}
if (w[1][1]==-1) {k++;} if (w[1][1]>0) {f=1; g=1;}
if (w[2][2]==-1) {k++;} if (w[2][2]>0) {f=2; g=2;}
if (k==2) {e=f; r=g;}
l=0; f=-1; g=-1;
if (w[0][2]==-1) {l++;} if (w[0][2]>0) {f=0; g=0;}
if (w[1][1]==-1) {l++;} if (w[1][1]>0) {f=1; g=1;}
if (w[2][0]==-1) {l++;} if (w[2][0]>0) {f=2; g=2;}
if (l==2&&f>=0&&g>=0) {e=f; r=g;}
if (e<0||r<0) {
k=0; l=0; m=0;
for (i=0;i<=2;i++)
for (j=0;j<=2;j++)
if (w[i][j]>m) {m=w[i][j]; k=i; l=j;}
gotoxy(l+prx+l,k+pry+k); w[k][l]=-2; putch('x');
} else {gotoxy(r+prx+r,e+pry+e); putch('x'); w[e][r]=-2;}
p();
}
}
}
彈球游戲(WASD和8546控制):
#include <stdio.h>
#include <conio.h>
main()
{ int x,y,xf=1,yf=1,bf=6,bg=22,pf=30,pg=22,k=800,o,j,i,score=0;
char p;
clrscr(); textmode(C40);
for (x=1;x<=40;x++) { gotoxy(x,1); putch(219); gotoxy(x,24); putch(219);}
for (y=1;y<=24;y++) { gotoxy(1,y); putch(219); gotoxy(40,y); putch(219);}
gotoxy(3,3); putch('O'); x=3; y=3; gotoxy(bf,bg); printf("----");
gotoxy(pf,pg); printf("----");
gotoxy(3,1); printf("<=-20--Lost");
gotoxy(16,1); printf("Score: %d **",score);
gotoxy(30,1); printf(">=50--Win");
while (k&&score<50&&score>-20)
{ gotoxy(bf,bg); printf("----");
gotoxy(pf,pg); printf("----");
gotoxy(x,y); putch('O'); delay(1000000000);
gotoxy(x,y); putch(' ');
if (x==39) {xf=-1;} if (x==2) {xf=1;}
if (y==23) {yf=-1;} if (y==2) {yf=1;}
x+=xf; y+=yf;
if (y==bg-1&&x>=(bf-1)&&x<=(bf+6)&&yf==1)
if ((x==bf-1&&xf==1)||(x>=bf&&x<=(bf+5))||(x==bf+6&&xf==-1)) {
if (x==bf-1&&xf==1) xf=-1; if (x==bf+6&&xf==-1) xf=1; yf=-1;
if (yf==1) {score+=5; gotoxy(15,1);
printf("Score: %d +5",score); putch(219);}
}
if (y==pg-1&&x>=(pf-1)&&x<=(pf+6))
if ((x==pf-1&&xf==1)||(x>=pf&&x<=(pf+5))||(x==pf+6&&xf==-1)) {
yf=-1; score+=5; gotoxy(15,1);
printf("Score: %d +5",score); putch(219);}
if (y==23) {score-=5; gotoxy(15,1);
printf("Score: %d -5",score); putch(219);}
if (kbhit())
{ p=getch(); o=p;
if (o==27) exit();
if (p=='a'&&bf>3) {gotoxy(bf,bg); printf(" "); bf=bf-2;
gotoxy(bf,bg); printf("----");}
if (p=='d'&&bf+5<pf-1) {gotoxy(bf,bg); printf(" "); bf=bf+2;
gotoxy(bf,bg); printf("----");}
if (p=='w'&&bg-1>1) {gotoxy(bf,bg); printf(" "); bg=bg-1;
gotoxy(bf,bg); printf("----");}
if (p=='s'&&bg+1<24) {gotoxy(bf,bg); printf(" "); bg=bg+1;
gotoxy(bf,bg); printf("----");}
if (p=='4'&&pf>bf+6) {gotoxy(pf,pg); printf(" "); pf=pf-2;
gotoxy(pf,pg); printf("----");}
if (p=='6'&&pf+5<38) {gotoxy(pf,pg); printf(" "); pf=pf+2;
gotoxy(pf,pg); printf("----");}
if (p=='8'&&pg-1>1) {gotoxy(pf,pg); printf(" "); pg=pg-1;
gotoxy(pf,pg); printf("----");}
if (p=='5'&&pg+1<24) {gotoxy(pf,pg); printf(" "); pg=pg+1;
gotoxy(pf,pg); printf("----");}
}

k--;
} gotoxy(x,y); putch('O');
if (score>=50) {gotoxy(10,3); printf("Score>=50! You win!");}
if (score<=-20) {gotoxy(10,3); printf("Score<=-20! You lost!");}
getch();
}

⑤ C語言如何編程讓一個按鍵輪流點燈。按一下1燈亮,在按下1燈滅2燈亮,在按下3燈亮,1 2燈是滅,一直到8個燈

C51獨立按鍵的識別示常式序

每按一次獨立鍵盤的S2鍵,與P1口相連的一個發光二極體往下移動一位。

#include <reg52.h>

sbit BY1=P3^4; //定義按鍵的輸入端S2鍵

unsigned char count; //按鍵計數,每按一下,count加1

unsigned char temp;
unsigned char a,b;

void delay10ms(void) //延時程序

{

unsigned char i,j;

for(i=20;i>0;i--)

for(j=248;j>0;j--);

}

key() //按鍵判斷程序

{

if(BY1==0) //判斷是否按下鍵盤,當單片機上電時所有IO口為//高電平,S2鍵一端接地另一端接P3.4,所以當鍵被按下時P3.4口//直接接地,此時檢測P3.4肯定為低電平。

{

delay10ms(); //延時,軟體去干擾

if(BY1==0) //確認按鍵按下

{

count++; //按鍵計數加1

if(count==8) //計8次重新計數

{

count=0; //將count清零

}

}

while(BY1==0);//等待按鍵釋放,如果鍵未釋放則一直在此等待。

}

}

move() //廣告燈向下移動移動函數

{

a=temp<<count; //這三句為一個循環移位,相當於前面提到的

b=temp>>(8-count);// _crol_()函數

P1=a|b;

}

main()

{

count=0; //初始化參數設置

temp=0xfe;

P1=0xff;

P1=temp;

while(1) //永遠循環,掃描判斷按鍵是否按下

{

key(); //調用按鍵識別函數

move(); //調用廣告燈移動函數

}

}

⑥ 怎樣用C語言編寫一個小游戲

「貪吃蛇」C代碼:

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <time.h>

#include <Windows.h>

#define W 78 //游戲框的寬,x軸

#define H 26 //游戲框的高,y軸

int dir=3; //方向變數,初值3表示向「左」

int Flag=0; //吃了食物的標志(1是0否)

int score=0; //玩家得分

struct food{ int x; //食物的x坐標

int y; //食物的y坐標

}fod; //結構體fod有2個成員

struct snake{ int len; //身長

int speed; //速度

int x[100];

int y[100];

}snk; //結構體snk有4個成員

void gtxy( int x,int y) //控制游標移動的函數

{ COORD coord;

coord.X=x;

coord.Y=y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

void gtxy( int x,int y); //以下聲明要用到的幾個自編函數

void csh( ); //初始化界面

void keymove( ); //按鍵操作移動蛇

void putFod( ); //投放食物

int Over( ); //游戲結束(1是0否)

void setColor(unsigned short p, unsigned short q); //設定顯示顏色

int main( ) //主函數

{ csh( );

while(1)

{ Sleep(snk.speed);

keymove( );

putFod( );

if(Over( ))

{system(「cls」);

gtxy(W/2+1,H/2); printf(「游戲結束!T__T」);

gtxy(W/2+1,H/2+2); printf(「玩家總分:%d分」,score);

getch( );

break;

}

}

return 0;

}

void csh( ) //初始化界面

{ int i;

gtxy(0,0);

CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下兩行是隱藏游標的設置

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

for(i=0;i<=W;i=i+2) //橫坐標要為偶數,因為這個要列印的字元佔2個位置

{ setColor(2, 0); //設定列印顏色為綠字黑底

gtxy(i,0); printf("■"); //列印上邊框

gtxy(i,H); printf("■"); //列印下邊框

}

for(i=1;i<H;i++)

{ gtxy(0,i); printf("■"); //列印左邊框

gtxy(W,i); printf("■"); //列印右邊框

}

while(1)

{ srand((unsigned)time(NULL)); //初始化隨機數發生器srand( )

fod.x=rand()%(W-4)+2; //隨機函數rand( )產生一個從0到比」(W-4)」小1的數再加2

fod.y=rand()%(H-2)+1; //隨機函數rand( )產生一個從0到比」(H-2)」小1的數再加1

if (fod.x%2==0) break; //fod.x是食物的橫坐標,要是2的倍數(為偶數)

}

setColor(12, 0); //設定列印顏色為淡紅字黑底

gtxy(fod.x,fod.y); printf("●"); //到食物坐標處列印初試食物

snk.len=3; //蛇身長

snk.speed=350; //刷新蛇的時間,即是移動速度

snk.x[0]=W/2+1; //蛇頭橫坐標要為偶數(因為W/2=39)

snk.y[0]=H/2; //蛇頭縱坐標

setColor(9, 0); //設定列印顏色為淡藍字黑底

gtxy(snk.x[0], snk.y[0]); printf("■"); //列印蛇頭

for(i=1;i<snk.len;i++)

{ snk.x[i]=snk.x[i-1]+2; snk.y[i]=snk.y[i-1];

gtxy(snk.x[i],snk.y[i]); printf("■"); //列印蛇身

}

setColor(7, 0); //恢復默認的白字黑底

return;

}

void keymove( ) //按鍵操作移動蛇

{ int key;

if( kbhit( ) ) //如有按鍵輸入才執行下面操作

{ key=getch( );

if (key==224) //值為224表示按下了方向鍵,下面要再次獲取鍵值

{ key=getch( );

if(key==72&&dir!=2)dir=1; //72表示按下了向上方向鍵

if(key==80&&dir!=1)dir=2; //80為向下

if(key==75&&dir!=4)dir=3; //75為向左

if(key==77&&dir!=3)dir=4; //77為向右

}

if (key==32)

{ while(1) if((key=getch( ))==32) break; } //32為空格鍵,這兒用來暫停

}

if (Flag==0) //如沒吃食物,才執行下面操作擦掉蛇尾

{ gtxy(snk.x[snk.len-1],snk.y[snk.len-1]); printf(" "); }

int i;

for (i = snk.len - 1; i > 0; i--) //從蛇尾起每節存儲前一節坐標值(蛇頭除外)

{ snk.x[i]=snk.x[i-1]; snk.y[i]=snk.y[i-1]; }

switch (dir) //判斷蛇頭該往哪個方向移動,並獲取最新坐標值

{ case 1: snk.y[0]--; break; //dir=1要向上移動

case 2: snk.y[0]++; break; //dir=2要向下移動

case 3: snk.x[0]-=2; break; //dir=3要向左移動

case 4: snk.x[0]+=2; break; //dir=4要向右移動

}

setColor(9, 0);

gtxy(snk.x[0], snk.y[0]); printf("■"); //列印蛇頭

if (snk.x[0] == fod.x && snk.y[0] == fod.y) //如吃到食物則執行以下操作

{ printf("07"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; } //007是響鈴

else Flag = 0; //沒吃到食物Flag的值為0

if(snk.speed<150) snk.speed= snk.speed+5; //作弊碼,不讓速度無限加快

}

void putFod( ) //投放食物

{ if (Flag == 1) //如吃到食物才執行以下操作,生成另一個食物

{ while (1)

{ int i,n= 1;

srand((unsigned)time(NULL)); //初始化隨機數發生器srand( )

fod.x = rand( ) % (W - 4) + 2; //產生在游戲框范圍內的一個x坐標值

fod.y = rand( ) % (H - 2) + 1; //產生在游戲框范圍內的一個y坐標值

for (i = 0; i < snk.len; i++) //隨機生成的食物不能在蛇的身體上

{ if (fod.x == snk.x[i] &&fod.y == snk.y[i]) { n= 0; break;} }

if (n && fod.x % 2 == 0) break; //n不為0且橫坐標為偶數,則食物坐標取值成功

}

setColor(12, 0);

gtxy(fod.x, fod.y); printf("●"); //游標到取得的坐標處列印食物

}

return;

}

int Over( ) //判斷游戲是否結束的函數

{ int i;

setColor(7, 0);

gtxy(2,H+1); printf(「暫停鍵:space.」); //以下列印一些其它信息

gtxy(2,H+2); printf(「游戲得分:%d」,score);

if (snk.x[0] == 0 || snk.x[0] == W) return 1; //蛇頭觸碰左右邊界

if (snk.y[0] == 0 || snk.y[0] == H) return 1; //蛇頭觸碰上下邊界

for (i = 1; i < snk.len; i++)

{ if (snk.x[0] == snk.x[i] && snk.y[0] == snk.y[i]) return 1; } //蛇頭觸碰自身

return 0; //沒碰到邊界及自身時就返回0

}

void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)

{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTextAttribute( handle, ForeColor + BackGroundColor * 0x10 );

} //用來設定顏色的函數

⑦ C語言可以寫哪些小游戲

C語言可以編手機游戲. 你叫他去死 不過我這有 貪吃蛇的代碼,你倒可以看看 (用TC 編譯一定過)

#include
#include
#include
#include
#include
#define Enter 7181
#define ESC 283
#define UP 18432
#define DOWN 20480
#define LEFT 19200
#define RIGHT 19712
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
void interrupt (*oldhandler)(__CPPARGS);
void interrupt newhandler(__CPPARGS);
void SetTimer(void interrupt (*IntProc)(__CPPARGS));
void KillTimer(void);
void Initgra(void);
void TheFirstBlock(void);
void DrawMap(void);
void Initsnake(void);
void Initfood(void);
void Snake_Headmv(void);
void Flag(int,int,int,int);
void GameOver(void);
void Snake_Bodymv(void);
void Snake_Bodyadd(void);
void PrntScore(void);
void Timer(void);
void Win(void);
void TheSecondBlock(void);
void Food(void);
void Dsnkorfd(int,int,int);
void Delay(int);
struct Snake
{int x;int y;int color;}Snk[12];
struct Food
{int x;int y;int color;}Fd;
int flag1=1,flag2=0,flag3=0,flag4=0,flag5=0,flag6=0,
checkx,checky,num,key=0,Times,Score,Hscore,Snkspeed,TimerCounter,TureorFalse;
char Sco[2],Time[6];
void main()
{ Initgra();
SetTimer(newhandler);
TheFirstBlock();
while(1)
{DrawMap();
Snake_Headmv();
GameOver();
Snake_Bodymv();
Snake_Bodyadd();
PrntScore();
Timer();
Win();
if(key==ESC)
break;
if(key==Enter)
{cleardevice();
TheFirstBlock();
}
TheSecondBlock();
Food();
Delay(Snkspeed);
}
closegraph();
KillTimer();
}
void interrupt newhandler(__CPPARGS)
{
TimerCounter++;
oldhandler();
}
void SetTimer(void interrupt (*IntProc)(__CPPARGS))
{
oldhandler=getvect(0x1c);
disable();
setvect(0x1c,IntProc);
enable();
}

void KillTimer()
{
disable();
setvect(0x1c,oldhandler);
enable();
}
void Initgra()
{int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\tc");
}
void TheFirstBlock()
{setcolor(11);
settextstyle(0,0,4);
outtextxy(100,220,"The First Block");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
Initsnake();
Initfood();
Score=0;
Hscore=1;
Snkspeed=10;
num=2;
Times=0;
key=0;
TureorFalse=1;
TimerCounter=0;
Time[0]='0';Time[1]='0';Time[2]=':';Time[3]='1';Time[4]='0';Time[5]='\0';
}
else if(key==ESC) cleardevice();
else goto loop;
}
void DrawMap()
{line(10,10,470,10);
line(470,10,470,470);
line(470,470,10,470);
line(10,470,10,10);
line(480,20,620,20);
line(620,20,620,460);
line(620,460,480,460);
line(480,460,480,20);
}
void Initsnake()
{randomize();
num=2;
Snk[0].x=random(440);
Snk[0].x=Snk[0].x-Snk[0].x%20+50;
Snk[0].y=random(440);
Snk[0].y=Snk[0].y-Snk[0].y%20+50;
Snk[0].color=4;
Snk[1].x=Snk[0].x;
Snk[1].y=Snk[0].y+20;
Snk[1].color=4;
}
void Initfood()
{randomize();
Fd.x=random(440);
Fd.x=Fd.x-Fd.x%20+30;
Fd.y=random(440);
Fd.y=Fd.y-Fd.y%20+30;
Fd.color=random(14)+1;
}
void Snake_Headmv()
{if(bioskey(1))
{key=bioskey(0);
switch(key)
{case UP:Flag(1,0,0,0);break;
case DOWN:Flag(0,1,0,0);break;
case LEFT:Flag(0,0,1,0);break;
case RIGHT:Flag(0,0,0,1);break;

default:break;
}
}
if(flag1)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].y-=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
if(flag2)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].y+=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
if(flag3)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].x-=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
if(flag4)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].x+=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
}
void Flag(int a,int b,int c,int d)
{flag1=a;flag2=b;flag3=c;flag4=d;}
void GameOver()
{int i;
if(Snk[0].x460||Snk[0].y460)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"Game Over");
loop1:key=bioskey(0);
if(key==Enter)
{cleardevice();
TheFirstBlock();
}
else
if(key==ESC)
cleardevice();
else
goto loop1;
}
for(i=3;i<num;i++)
{if(Snk[0].x==Snk[i].x&&Snk[0].y==Snk[i].y)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"Game Over");
loop2:key=bioskey(0);
if(key==Enter)
{cleardevice();
TheFirstBlock();
}
else
if(key==ESC)
cleardevice();
else goto loop2;
}
}
}
void Snake_Bodymv()
{int i,s,t;
for(i=1;i<num;i++)
{Dsnkorfd(checkx,checky,Snk[i].color);
Dsnkorfd(Snk[i].x,Snk[i].y,0);
s=Snk[i].x;
t=Snk[i].y;
Snk[i].x=checkx;
Snk[i].y=checky;
checkx=s;
checky=t;
}
}
void Food()
{if(flag5)
{randomize();
Fd.x=random(440);
Fd.x=Fd.x-Fd.x%20+30;
Fd.y=random(440);
Fd.y=Fd.y-Fd.y%20+30;
Fd.color=random(14)+1;
flag5=0;
}
Dsnkorfd(Fd.x,Fd.y,Fd.color);
}
void Snake_Bodyadd()
{if(Snk[0].x==Fd.x&&Snk[0].y==Fd.y)
{if(Snk[num-1].x>Snk[num-2].x)
{num++;
Snk[num-1].x=Snk[num-2].x+20;
Snk[num-1].y=Snk[num-2].y;
Snk[num-1].color=Fd.color;
}
else
if(Snk[num-1].x<Snk[num-2].x)
{num++;
Snk[num-1].x=Snk[num-2].x-20;
Snk[num-1].y=Snk[num-2].y;
Snk[num-1].color=Fd.color;
}
else
if(Snk[num-1].y>Snk[num-2].y)
{num++;
Snk[num-1].x=Snk[num-2].x;
Snk[num-1].y=Snk[num-2].y+20;
Snk[num-1].color=Fd.color;
}
else
if(Snk[num-1].y<Snk[num-2].y)
{num++;
Snk[num-1].x=Snk[num-2].x;
Snk[num-1].y=Snk[num-2].y-20;
Snk[num-1].color=Fd.color;
}
flag5=1;
Score++;
}
}
void PrntScore()
{if(Hscore!=Score)
{setcolor(11);
settextstyle(0,0,3);
outtextxy(490,100,"SCORE");
setcolor(2);
setfillstyle(1,0);
rectangle(520,140,580,180);
floodfill(530,145,2);
Sco[0]=(char)(Score+48);
Sco[1]='\0';
Hscore=Score;
setcolor(4);
settextstyle(0,0,3);
outtextxy(540,150,Sco);
}
}
void Timer()
{if(TimerCounter>18)
{Time[4]=(char)(Time[4]-1);
if(Time[4]<'0')
{Time[4]='9';
Time[3]=(char)(Time[3]-1);
}
if(Time[3]<'0')
{Time[3]='5';
Time[1]=(char)(Time[1]-1);
}
if(TureorFalse)
{setcolor(11);
settextstyle(0,0,3);
outtextxy(490,240,"TIMER");
setcolor(2);
setfillstyle(1,0);
rectangle(490,280,610,320);
floodfill(530,300,2);
setcolor(11);
settextstyle(0,0,3);
outtextxy(495,290,Time);
TureorFalse=0;
}
if(Time[1]=='0'&&Time[3]=='0'&&Time[4]=='0')
{setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"Game Over");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
TheFirstBlock();
}
else if(key==ESC) cleardevice();
else goto loop;
}
TimerCounter=0;
TureorFalse=1;
}
}
void Win()
{if(Score==3)
Times++;
if(Times==2)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"You Win");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
TheFirstBlock();
key=0;
}
else if(key==ESC) cleardevice();
else goto loop;
}
}
void TheSecondBlock()
{if(Score==3)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(100,220,"The Second Block");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
Initsnake();
Initfood();
Score=0;
Hscore=1;
Snkspeed=8;
num=2;
key=0;
}
else if(key==ESC) cleardevice();
else goto loop;
}
}
void Dsnkorfd(int x,int y,int color)
{setcolor(color);
setfillstyle(1,color);
circle(x,y,10);
floodfill(x,y,color);
}
void Delay(int times)
{int i;
for(i=1;i<=times;i++)
delay(15000);
}

⑧ c語言 點燈問題

#include"stdio.h"
struct Light
{
int flag;
} light[2000001];
void initLight(Light light[] ,long n)
{long i;
for( i=1;i<=n;i++)
{light[i].flag=-1;}
}
int change(Light light[],long n,long num)
{long i;
if(num==1){for(i=1;i<=n;i++)light[i].flag=-light[i].flag;}
else for(i=num;i<=n;i=i+num)
{if(i<=0&&i>2000000) return 0;
light[i].flag=-light[i].flag;
}
}

void print(Light light[],long n)
{long count=0;
for(long i=1;i<=n;i++)
if(light[i].flag==1)
{printf("%ld號燈亮著\n",i);count++;}
printf("總共有%ld盞的燈亮著\n",count);
}
void main()
{long n;
do{
printf("請輸入猩猩的數目:(1~2000000)\n");
scanf("%ld",&n);
if(n==0)break;
if(n>2000000)continue;
initLight(light,n);
for(long i=1;i<=n;i++)
change(light,n,i);
print(light,n);}while(1);
}

⑨ 如何用C語言做一個24點游戲的程序

/*6.3.4 源程序*/
#define N 20
#define COL 100
#define ROW 40
#include "stdio.h"
#include "time.h" /*系統時間函數*/
#include "graphics.h" /*圖形函數*/
#include "alloc.h"/*動態地址分配函數*/
#include "stdlib.h" /*庫函數*/
#include "string.h" /*字元串函數*/
#include "ctype.h" /*字元操作函數*/
char p[4][13]={
{'A','2','3','4','5','6','7','8','9','0','J','Q','K'},/*撲克牌,10用0來表示*/
{'A','2','3','4','5','6','7','8','9','0','J','Q','K'},
{'A','2','3','4','5','6','7','8','9','0','J','Q','K'},
{'A','2','3','4','5','6','7','8','9','0','J','Q','K'}};
typedef struct node
{
int data;
struct node *link;
}STACK1; /*棧1*/
typedef struct node2
{
char data;
struct node2 *link;
}STACK2; /*棧2*/
void init(void);/*圖形驅動*/
void close(void);/*圖形關閉*/
void play(void);/*發牌的具體過程*/
void rand1(int j);/*隨機發牌函數*/
void change(char *e,char *a); /*中綴變後綴函數*/
int computer(char *s); /*後綴表達式計算函數*/
STACK1 *initstack1(STACK1 *top); /*棧1初始化*/
STACK1 *push(STACK1 *top,int x); /*棧1入棧運算*/
STACK1 *pop(STACK1 *top); /*棧1刪除棧頂元素*/
int topx(STACK1 *top); /*棧1讀棧頂元素*/
STACK1 *ptop(STACK1 *top,int *x); /*棧1讀出棧頂元素值並刪除棧頂元素*/
int empty(STACK1 *top); /*判棧1是否為空函數*/
STACK2 *initstack2(STACK2 *top); /*棧2初始化*/
STACK2 *push2(STACK2 *top,char x); /*棧2入棧運算*/
STACK2 *pop2(STACK2 *top); /*棧2刪除棧頂元素*/
char topx2(STACK2 *top); /*棧2讀棧頂元素*/
STACK2 *ptop2(STACK2 *top,char *x); /*棧2讀出棧頂元素值並刪除棧頂元素*/
int empty2(STACK2 *top); /*判棧2是否為空函數*
int text1(char *s) ; /*顯示文本*/
main()
{
char s[N],s1[N],ch;
int i,result;
int gdriver, gmode;
clrscr(); /*清屏*/
init(); /*初始化函數*/
while(1)
{
setbkcolor(BLACK); /*設置背景顏色*/
cleardevice();/*清屏*/
play(); /*發牌*/
gotoxy(1,15); /*移動游標*/
printf("--------------------Note-------------------\n");
printf(" Please enter express accroding to above four number\n"); /*提示信息*/
printf(" Format as follows:2.*(5.+7.)\n");/*提示輸入字元串格式*/
printf(" ----------------------------------------------\n");
scanf("%s%c",s1,&ch); /*輸入字元串壓回車鍵*/
change(s1,s); /*調用change函數將中綴表達式s1轉換為後綴表達式s*/
result=computer(s); /*計算後綴表達式的值,返回結果result */
if(result==24) /*如果結果等於24*/
text1("very good"); /*調用函數text1顯示字元串"very good"*/
else
text1("wrong!!!");/*否則函數text1顯示字元串"wrong!!!"*/
printf("Continue (y/n)?\n"); /*提示信息,是否繼續*/
scanf("%c",&ch); /*輸入一字元*/
if(ch=='n'||ch=='N') /*如果該字元等於n或N*/
break; /*跳出循環,程序結束*/
} /*否則,開始下一輪循環*/
close();
return; /*返回*/
}
void rand1(int j)/*隨機發牌函數*/
{
int kind,num;
char str[3],n;
randomize();
while(1)/*循環直到有牌發*/
{
kind=random(4); /*花色隨機數*/
num=random(13); /*大小隨機數*/
if(p[kind][num]!=-1) /*該數未取過*/
{
n=p[kind][num]; /*取相應位置的撲克牌數*/
p[kind][num]=-1; /*牌發好以後相應位置的元素置-1*/
break;
}
}
switch(kind)/*花式的判斷*/
{
case 0:setcolor(RED);sprintf(str,"%c",3);break; /*紅桃*/
case 1:setcolor(BLACK);sprintf(str,"%c",3);break; /*黑桃*/
case 2:setcolor(RED);sprintf(str,"%c",4);break; /*方片*/
case 3:setcolor(BLACK);sprintf(str,"%c",5);break; /*草花*/
}
settextstyle(0,0,2);
outtextxy(COL+j*100-30,ROW+100-46,str);/*顯示左上角花色*/
outtextxy(COL+j*100+16,ROW+100+32,str); /*顯示右下角花色*/
if(n!='0')/*輸出其他牌*/
{
settextstyle(0,0,3);
sprintf(str,"%c",n);
outtextxy(COL+j*100-5,ROW+100-5,str);/*顯示牌的大小*/
}
else/*輸出10的時候*/
{
sprintf(str,"%d",10);
outtextxy(COL+j*100-6,ROW+100-5,str);
}
}
void play(void)/*發牌的具體過程*/
{
int j;
for(j=0;j<4;j++)
{
bar(COL+j*100-35,ROW+100-50,COL+j*100+35,ROW+1*100+50);/*畫空牌*/
setcolor(BLUE);
rectangle(COL+j*100-32,ROW+100-48,COL+j*100+32,ROW+100+48); /*畫矩形框*/
rand1(j); /*隨機取牌*/
delay(10000); /*延時顯示*/
}
}
void init(void)/*圖形驅動*/
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
void close(void)/*圖形關閉*/
{
closegraph();
}
void change(char *e,char *a) /*中綴字元串e轉後綴字元串a函數*/
{
STACK2 *top=NULL; /* 定義棧頂指針*/
int i,j;char w;
i=0;
j=0;
while(e[i]!='\0') /*當字元串沒有結束時*/
{
if(isdigit(e[i])) /*如果字元是數字*/
{
do{
a[j]=e[i]; /*將數字原樣拷貝到數組a中*/
i++; /*e數組的下標加1*/
j++; /*a數組的下標加1*/
}while(e[i]!='.'); /*直到字元為數字結束符「.」為止*/
a[j]='.';j++; /*將數字結束符「.」拷貝到a數組依然保持結束標記*/
}
if(e[i]=='(') /*如果字元是「(」時*/
top=push2(top,e[i]); /*將其壓入堆棧*/
if(e[i]==')') /*如果字元是「)」時*/
{
top=ptop2(top,&w); /*取出棧頂元素,並從棧頂刪除該元素*/
while(w!='(') /*如果字元不是「(」時反復循環*/
{
a[j]=w; /*將棧頂元素存入a數組*/
j++; /*下標加1*/
top=ptop2(top,&w) ; /*取出棧頂元素,並從棧頂刪除該元素*/
}
}
if(e[i]=='+'||e[i]=='-') /*如果字元是加或減號時*/
{
if(!empty2(top)) /*如棧不為空*/
{
w=topx2(top);
while(w!='(') /*當棧頂元素不是「(」時反復循環*/
{
a[j]=w;
j++; /*將棧頂元素存入表達式a中,a的下標加1*/
top=pop2(top); /*刪除棧頂元素*/
if(empty2(top)) /*如果棧為空*/
break; /*跳出循環*/
else
w=topx2(top); /*否則讀棧頂元素*/
}
}
top=push2(top,e[i]); /*將當前e的字元元素壓入堆棧*/
}
if(e[i]=='*'||e[i]=='/') /*如果字元是乘或除號時*/
{
if(!empty2(top)) /*如棧不為空*/
{
w=topx2(top); /*讀棧頂元素存入w*/
while(w=='*'||w=='/')/*當棧頂元素是乘或除時反復循環*/
{
a[j]=w;
j++; /*將棧頂元素存入字元串a中,a的下標加1*/
top=pop2(top); /*刪除棧頂元素*/
if(empty2(top)) /*如果棧為空*/
break; /*跳出循環*/
else
w=topx2(top); /*否則讀棧頂元素*/
}
}
top=push2(top,e[i]); /*將當前e字元元素壓入堆棧*/
}
i++; /*e的下標加1*/
}
while(!empty2(top)) /*當不為空時反復循環*/
top=ptop2(top,&a[j++]); /*將棧頂元素存入數組a中*/
a[j]='\0'; /*將字元串結束標記寫入最後一個數組元素中構成字元串*/
}
int computer(char *s) /* 計算函數*/
{
STACK1 *top=NULL;
int i,k,num1,num2,result;
i=0;
while(s[i]!='\0') /*當字元串沒有結束時作以下處理*/
{
if(isdigit(s[i])) /*判字元是否為數字*/
{
k=0; /*k初值為0*/
do{
k=10*k+s[i]-'0'; /*將字元連接為十進制數字*/
i++; /*i加1*/
}while(s[i]!='.'); /*當字元不為『.』時重復循環*/
top=push(top,k); /*將生成的數字壓入堆棧*/
}
if(s[i]=='+') /*如果為'+'號*/
{
top=ptop(top,&num2); /*將棧頂元素取出存入num2中*/
top=ptop(top,&num1); /*將棧頂元素取出存入num1中*/
result=num2+num1; /*將num1和num2相加存入result中*/
top=push(top,result); /*將result壓入堆棧*/
}
if(s[i]=='-') /*如果為'-'號*/
{
top=ptop(top,&num2); /*將棧頂元素取出存入num2中*/
top=ptop(top,&num1); /*將棧頂元素取出存入num1中*/
result=num1-num2; /*將num1減去num2結果存入result中*/
top=push(top,result); /*將result壓入堆棧*/
}
if(s[i]=='*') /*如果為'*'號*/
{
top=ptop(top,&num2); /*將棧頂元素取出存入num2中*/
top=ptop(top,&num1); /*將棧頂元素取出存入num1中*/
result=num1*num2; /*將num1與num2相乘結果存入result中*/
top=push(top,result); /*將result壓入堆棧*/
}
if(s[i]=='/') /*如果為'/'號*/
{
top=ptop(top,&num2); /*將棧頂元素取出存入num2中*/
top=ptop(top,&num1); /*將棧頂元素取出存入num1中*/
result=num1/num2; /*將num1除num2結果存入result中*
top=push(top,result); /*將result壓入堆棧*/
}
i++; /*i加1*/
}
top=ptop(top,&result); /*最後棧頂元素的值為計算的結果*/
return result; /*返回結果*/
}
STACK1 *initstack1(STACK1 *top) /*初始化*/
{
top=NULL; /*棧頂指針置為空*/
return top; /*返回棧頂指針*/
}
STACK1 *push(STACK1 *top,int x) /*入棧函數*/
{
STACK1 *p; /*臨時指針類型為STACK1*/
p=(STACK1 *)malloc(sizeof(STACK1)); /*申請STACK1大小的空間*/
if(p==NULL) /*如果p為空*/
{
printf("memory is overflow\n!!"); /*顯示內存溢出*/
exit(0); /*退出*/
}
p->data=x; /*保存值x到新空間*/
p->link=top; /*新結點的後繼為當前棧頂指針*/
top=p; /*新的棧頂指針為新插入的結點*/
return top; /*返回棧頂指針*/
}
STACK1 *pop(STACK1 *top) /*出棧*/
{
STACK1 *q; /*定義臨時變數*/
q=top; /*保存當前棧頂指針*/
top=top->link; /*棧頂指針後移*/
free(q); /*釋放q*/
return top; /*返回棧頂指針*/
}
int topx(STACK1 *top) /*讀棧頂元素*/
{
if(top==NULL) /*棧是否為空*/
{
printf("Stack is null\n"); /*顯示棧為空信息*/
return 0; /*返回整數0*/
}
return top->data; /*返回棧頂元素*/
}
STACK1 *ptop(STACK1 *top,int *x) /*取棧頂元素,並刪除棧頂元素*/
{
*x=topx(top); /*讀棧頂元素*/
top=pop(top); /*刪除棧頂元素*/
return top; /*返回棧頂指針*/
}
int empty(STACK1 *top) /*判棧是否為空*/
{
if(top==NULL) /*如果為空*/
return 1; /*返回1*/
else
return 0; /*否則返回0*/
}
STACK2 *initstack2(STACK2 *top) /*初始化*/
{
top=NULL; /*棧頂指針置為空*/
return top; /*返回棧頂指針*/
}
STACK2 *push2(STACK2 *top,char x) /*入棧函數*/
{
STACK2 *p; /*臨時指針類型為STACK2*/
p=(STACK2 *)malloc(sizeof(STACK2)); /*申請STACK2大小的空間*/
if(p==NULL) /*如果p為空*/
{
printf("memory is overflow\n!!"); /*顯示內存溢出*/
exit(0); /*退出*/
}
p->data=x; /*保存值x到新空間*/
p->link=top; /*新結點的後繼為當前棧頂指針*/
top=p; /*新的棧頂指針為新插入的結點*/
return top; /*返回棧頂指針*/
}
STACK2 *pop2(STACK2 *top) /*出棧*/
{
STACK2 *q; /*定義臨時變數*/
q=top; /*保存當前棧頂指針*/
top=top->link; /*棧頂指針後移*/
free(q); /*釋放q*/
return top; /*返回棧頂指針*/
}
char topx2(STACK2 *top) /*讀棧頂元素*/
{
if(top==NULL) /*棧是否為空*/
{
printf("Stack is null\n"); /*顯示棧為空信息*/
return ''; /*返回空字元*/
}
return top->data; /*返回棧頂元素*/
}
STACK2 *ptop2(STACK2 *top,char *x) /*取棧頂元素,並刪除棧頂元素*/
{
*x=topx2(top); /*讀棧頂元素*/
top=pop2(top); /*刪除棧頂元素*/
return top; /*返回棧頂指針*/
}
int empty2(STACK2 *top) /*判棧是否為空*/
{
if(top==NULL) /*如果為空*/
return 1; /*返回1*/
else
return 0; /*否則返回0*/
}

int text1(char *s)
{
setbkcolor(BLUE); /*設置背景顏色為藍色*/
cleardevice(); /*清除屏幕*/
setcolor(12); /*設置文本顏色為淡紅色*/
settextstyle(1, 0, 8);/*三重筆劃字體, 放大8倍*/
outtextxy(120, 120, s); /*輸出字元串s*/
setusercharsize(2, 1, 4, 1);/*水平放大2倍, 垂直放大4倍*/
setcolor(15); /*設置文本顏色為*白色/
settextstyle(3, 0, 5); /*無襯字筆劃, 放大5倍*/
outtextxy(220, 220, s); /*輸出字元串s*/
getch(); /*鍵盤輸入任一字元*/
return ; /*返回*/
}

⑩ 編程C語言 24點游戲

{
printf("%-5d%-8s%-6s%-13s%-15s%-15s\n",per[i-1].score,per[i-1].name,per[i-1].age,per[i-1].num,per[i-1].adds,per[i-1].email);
if(i>1&&i%10==0)
{
printf("\t-----------------------------------\n");
printf("\t");
system("pause");
printf("\t-----------------------------------\n");
}
}