當前位置:首頁 » 編程軟體 » c游戲編程

c游戲編程

發布時間: 2022-01-09 22:57:37

c語言 游戲編程

必讀的書 《計算機圖形學》
《數據結構》
《C語言》 中等難度的,別太淺了

建議參考讀的書 《C語言編程100例》

另外可以上論壇,網路學習.

不過書是一定要讀的。

② 用C語言編寫小游戲

用c語言編寫一個五子棋吧,不怎麼難,給你程序,自己參考一下

/*3.3.4 源程序*/
#include "graphics.h" /*圖形系統頭文件*/
#define LEFT 0x4b00 /*游標左鍵值*/
#define RIGHT 0x4d00 /*游標右鍵值*/
#define DOWN 0x5000 /*游標下鍵值*/
#define UP 0x4800 /*游標上鍵值*/
#define ESC 0x011b /* ESC鍵值*/
#define ENTER 0x1c0d /* 回車鍵值*/
int a[8][8]={0},key,score1,score2;/*具體分數以及按鍵與存放棋子的變數*/
char playone[3],playtwo[3];/*兩個人的得分轉換成字元串輸出*/
void playtoplay(void);/*人人對戰函數*/
void DrawQp(void);/*畫棋盤函數*/
void SetPlayColor(int x);/*設置棋子第一次的顏色*/
void MoveColor(int x,int y);/*恢復原來棋盤狀態*/
int QpChange(int x,int y,int z);/*判斷棋盤的變化*/
void DoScore(void);/*處理分數*/
void PrintScore(int n);/*輸出成績*/
void playWin(void);/*輸出勝利者信息*/
/******主函數*********/
void main(void)
{
int gd=DETECT,gr;
initgraph(&gd,&gr,"c:\\tc"); /*初始化圖形系統*/
DrawQp();/*畫棋盤*/
playtoplay();/*人人對戰*/
getch();
closegraph();/*關閉圖形系統*/
}
void DrawQp()/*畫棋盤*/
{
int i,j;
score1=score2=0;/*棋手一開始得分都為0*/
setbkcolor(BLUE);
for(i=100;i<=420;i+=40)
{
line(100,i,420,i);/*畫水平線*/
line(i,100,i,420); /*畫垂直線*/
}
setcolor(0);/*取消圓周圍的一圈東西*/
setfillstyle(SOLID_FILL,15);/*白色實體填充模式*/
fillellipse(500,200,15,15); /*在顯示得分的位置畫棋*/
setfillstyle(SOLID_FILL,8); /*黑色實體填充模式*/
fillellipse(500,300,15,15);
a[3][3]=a[4][4]=1;/*初始兩個黑棋*/
a[3][4]=a[4][3]=2;/*初始兩個白棋*/
setfillstyle(SOLID_FILL,WHITE);
fillellipse(120+3*40,120+3*40,15,15);
fillellipse(120+4*40,120+4*40,15,15);
setfillstyle(SOLID_FILL,8);
fillellipse(120+3*40,120+4*40,15,15);
fillellipse(120+4*40,120+3*40,15,15);
score1=score2=2; /*有棋後改變分數*/
DoScore();/*輸出開始分數*/
}
void playtoplay()/*人人對戰*/
{
int x,y,t=1,i,j,cc=0;
while(1)/*換棋手走棋*/
{
x=120,y=80;/*每次棋子一開始出來的坐標,x為行坐標,y為列坐標*/
while(1) /*具體一個棋手走棋的過程*/
{
PrintScore(1);/*輸出棋手1的成績*/
PrintScore(2);/*輸出棋手2的成績*/
SetPlayColor(t);/*t變數是用來判斷棋手所執棋子的顏色*/
fillellipse(x,y,15,15);
key=bioskey(0);/*接收按鍵*/
if(key==ESC)/*跳出遊戲*/
break;
else
if(key==ENTER)/*如果按鍵確定就可以跳出循環*/
{
if(y!=80&&a[(x-120)/40][(y-120)/40]!=1
&&a[(x-120)/40][(y-120)/40]!=2)/*如果落子位置沒有棋子*/
{
if(t%2==1)/*如果是棋手1移動*/
a[(x-120)/40][(y-120)/40]=1;
else/*否則棋手2移動*/
a[(x-120)/40][(y-120)/40]=2;
if(!QpChange(x,y,t))/*落子後判斷棋盤的變化*/
{
a[(x-120)/40][(y-120)/40]=0;/*恢復空格狀態*/
cc++;/*開始統計嘗試次數*/
if(cc>=64-score1-score2) /*如果嘗試超過空格數則停步*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
break;
}
else
continue;/*如果按鍵無效*/
}
DoScore();/*分數的改變*/
break;/*棋盤變化了,則輪對方走棋*/
}
else/*已經有棋子就繼續按鍵*/
continue;
}
else /*四個方向按鍵的判斷*/
if(key==LEFT&&x>120)/*左方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x-=40;
fillellipse(x,y,15,15);
}
else
if(key==RIGHT&&x<400&&y>80)/*右方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
x+=40;
fillellipse(x,y,15,15);
}
else
if(key==UP&&y>120)/*上方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
y-=40;
fillellipse(x,y,15,15);
}
else
if(key==DOWN&&y<400)/*下方向鍵*/
{
MoveColor(x,y);
fillellipse(x,y,15,15);
SetPlayColor(t);
y+=40;
fillellipse(x,y,15,15);
}
}
if(key==ESC)/*結束游戲*/
break;
if((score1+score2)==64||score1==0||score2==0)/*格子已經占滿或一方棋子為0判斷勝負*/
{
playWin();/*輸出最後結果*/
break;
}
t=t%2+1; /*一方走後,改變棋子顏色即輪對方走*/
cc=0; /*計數值恢復為0*/
} /*endwhile*/
}
void SetPlayColor(int t)/*設置棋子顏色*/
{
if(t%2==1)
setfillstyle(SOLID_FILL,15);/*白色*/
else
setfillstyle(SOLID_FILL,8);/*灰色*/
}
void MoveColor(int x,int y)/*走了一步後恢復原來格子的狀態*/
{
if(y<100)/*如果是從起點出發就恢復藍色*/
setfillstyle(SOLID_FILL,BLUE);
else/*其他情況如果是1就恢復白色棋子,2恢復黑色棋子,或恢復藍色棋盤*/
switch(a[(x-120)/40][(y-120)/40])
{
case 1:
setfillstyle(SOLID_FILL,15);break; /*白色*/
case 2:
setfillstyle(SOLID_FILL,8);break; /*黑色*/
default:
setfillstyle(SOLID_FILL,BLUE); /*藍色*/
}
}
int QpChange(int x,int y,int t)/*判斷棋盤的變化*/
{
int i,j,k,kk,ii,jj,yes;
yes=0;
i=(x-120)/40; /*計算數組元素的行下標*/
j=(y-120)/40; /*計算數組元素的列下標*/
SetPlayColor(t);/*設置棋子變化的顏色*/
/*開始往8個方向判斷變化*/
if(j<6)/*往右邊*/
{
for(k=j+1;k<8;k++)
if(a[i][k]==a[i][j]||a[i][k]==0)/*遇到自己的棋子或空格結束*/
break;
if(a[i][k]!=0&&k<8)
{
for(kk=j+1;kk<k&&k<8;kk++)/*判斷右邊*/
{
a[i][kk]=a[i][j]; /*改變棋子顏色*/
fillellipse(120+i*40,120+kk*40,15,15);
}
if(kk!=j+1) /*條件成立則有棋子改變過顏色*/
yes=1;
}
}
if(j>1)/*判斷左邊*/
{
for(k=j-1;k>=0;k--)
if(a[i][k]==a[i][j]||!a[i][k])
break;
if(a[i][k]!=0&&k>=0)
{
for(kk=j-1;kk>k&&k>=0;kk--)
{
a[i][kk]=a[i][j];
fillellipse(120+i*40,120+kk*40,15,15);
}
if(kk!=j-1)
yes=1;
}
}
if(i<6)/*判斷下邊*/
{
for(k=i+1;k<8;k++)
if(a[k][j]==a[i][j]||!a[k][j])
break;
if(a[k][j]!=0&&k<8)
{
for(kk=i+1;kk<k&&k<8;kk++)
{
a[kk][j]=a[i][j];
fillellipse(120+kk*40,120+j*40,15,15);
}
if(kk!=i+1)
yes=1;
}
}
if(i>1)/*判斷上邊*/
{
for(k=i-1;k>=0;k--)
if(a[k][j]==a[i][j]||!a[k][j])
break;
if(a[k][j]!=0&&k>=0)
{
for(kk=i-1;kk>k&&k>=0;kk--)
{
a[kk][j]=a[i][j];
fillellipse(120+kk*40,120+j*40,15,15);
}
if(kk!=i-1)
yes=1;
}
}
if(i>1&&j<6)/*右上*/
{
for(k=i-1,kk=j+1;k>=0&&kk<8;k--,kk++)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]&&k>=0&&kk<8)
{
for(ii=i-1,jj=j+1;ii>k&&k>=0;ii--,jj++)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i-1)
yes=1;
}
}
if(i<6&&j>1)/*左下*/
{
for(k=i+1,kk=j-1;k<8&&kk>=0;k++,kk--)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&k<8&&kk>=0)
{
for(ii=i+1,jj=j-1;ii<k&&k<8;ii++,jj--)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i+1)
yes=1;
}
}
if(i>1&&j>1)/*左上*/
{
for(k=i-1,kk=j-1;k>=0&&kk>=0;k--,kk--)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&k>=0&&kk>=0)
{
for(ii=i-1,jj=j-1;ii>k&&k>=0;ii--,jj--)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i-1)
yes=1;
}
}
if(i<6&&j<6)/* 右下*/
{
for(k=i+1,kk=j+1;kk<8&&kk<8;k++,kk++)
if(a[k][kk]==a[i][j]||!a[k][kk])
break;
if(a[k][kk]!=0&&kk<8&&k<8)
{
for(ii=i+1,jj=j+1;ii<k&&k<8;ii++,jj++)
{
a[ii][jj]=a[i][j];
fillellipse(120+ii*40,120+jj*40,15,15);
}
if(ii!=i+1)
yes=1;
}
}
return yes;/*返回是否改變過棋子顏色的標記*/
}
void DoScore()/*處理分數*/
{
int i,j;
score1=score2=0;/*重新開始計分數*/
for(i=0;i<8;i++)
for(j=0;j<8;j++)
if(a[i][j]==1)/*分別統計兩個人的分數*/
score1++;
else
if(a[i][j]==2)
score2++;
}
void PrintScore(int playnum)/*輸出成績*/
{
if(playnum==1)/*清除以前的成績*/
{
setfillstyle(SOLID_FILL,BLUE);
bar(550,100,640,400);
}
setcolor(RED);
settextstyle(0,0,4);/*設置文本輸出樣式*/
if(playnum==1)/*判斷輸出哪個棋手的分,在不同的位置輸出*/
{
sprintf(playone,"%d",score1);
outtextxy(550,200,playone);
}
else
{
sprintf(playtwo,"%d",score2);
outtextxy(550,300,playtwo);
}
setcolor(0);
}
void playWin()/*輸出最後的勝利者結果*/
{
settextstyle(0,0,4);
setcolor(12);
if(score2>score1)/*開始判斷最後的結果*/
outtextxy(100,50,"black win!");
else
if(score2<score1)
outtextxy(100,50,"white win!");
else
outtextxy(60,50,"you all win!");
}

③ c語言游戲編程

C++吧,先研究一下D3D驅動模塊吧。如果就你一個人,以你現在提出問題的水平,估計夠嗆,兩年吧,或許更長,看你有沒有毅力了

④ 求一個用C語言編寫的小游戲代碼

/*也不知道你是什麼級別的,我是一個新手,剛接觸編程語言,以下是我自己變得一個小程序,在所有c語言的編譯器(vc++6.0、turbo…………)上都能運行,你還可以進一步改進。這是一個類似貪吃蛇的小游戲。祝你好運*/
/*貪吃蛇*/
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
int head=3 ,tail=0;
int main()
{
int i,j,k=0;
int zuobiao[2][80];
long start;
int direction=77;
int gamespeed;
int timeover;
int change(char qipan[20][80],int zuobiao[2][80],char direction);
zuobiao[0][tail]=1;zuobiao[1][tail]=1;zuobiao[0][1]=1;zuobiao[1][1]=2;zuobiao[0][2]=1;zuobiao[1][2]=3;zuobiao[0][head]=1;zuobiao[1][head]=4;
/*處理棋盤*/
char qipan[20][80];//定義棋盤
for(i=0;i<20;i++)
for(j=0;j<80;j++)
qipan[i][j]=' ';//初始化棋盤
for(i=0;i<80;i++)
qipan[0][i]='_';
for(i=0;i<20;i++)
qipan[i][0]='|';
for(i=0;i<20;i++)
qipan[i][79]='|';
for(i=0;i<80;i++)
qipan[19][i]='_';
qipan[1][1]=qipan[1][2]=qipan[1][3]='*';//初始化蛇的位置
qipan[1][4]='#';
printf("This is a game of a SNAKE.\nGOOD LUCK TO YOU !\n");
printf("Input your game speed,please.(e.g.300)\n");
scanf("%d",&gamespeed);

while(direction!='q')
{
system("cls");
for(i=0;i<20;i++)//列印出棋盤
for(j=0;j<80;j++)
printf("%c",qipan[i][j]);
timeover=1;
start=clock();
while(!kbhit()&&(timeover=clock()-start<=gamespeed));
if(timeover)
{
getch();
direction=getch();
}
else
direction=direction;
if(!(direction==72||direction==80||direction==75||direction==77))
{
return 0;
system("cls");
printf("GAME OVER!\n");
}
if(!change(qipan,zuobiao,direction))
{
direction='q';
system("cls");
printf("GAME OVER!\n");
}
}
return 0;
}
int change(char qipan[20][80],int zuobiao[2][80],char direction)
{
int x,y;
if(direction==72)
x=zuobiao[0][head]-1;y=zuobiao[1][head];
if(direction==80)
x=zuobiao[0][head]+1;y=zuobiao[1][head];
if(direction==75)
x=zuobiao[0][head];y=zuobiao[0][head]-1;
if(direction==77)
x=zuobiao[0][head];y=zuobiao[1][head]+1;
if(x==0||x==18||y==78||y==0)
return 0;
if(qipan[x][y]!=' ')
return 0;
qipan[zuobiao[0][tail]][zuobiao[1][tail]]=' ';
tail=(tail+1)%80;
qipan[zuobiao[0][head]][zuobiao[1][head]]='*';
head=(head+1)%80;
zuobiao[0][head]=x;
zuobiao[1][head]=y;
qipan[zuobiao[0][head]][zuobiao[1][head]]='#';
return 1;
}

⑤ 求c語言游戲編程源代碼~~

把你郵箱給我吧~~~~

我有個火影忍者的游戲C語言編的~~~我發給你

⑥ c語言游戲編程,下落的小鳥 求代碼

下落的小鳥

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<Windows.h>
int Grade = 1, Score = 0, Max_blank = 9, Distance = 18;
struct Birds{int x; int y;}; //定義一種Birds數據類型(含3個成員)
Birds *Bird = (Birds*)malloc(sizeof(Birds)); //定義Birds類型 指針變數Bird並賦初值
struct Bg{int x, y; int l_blank; Bg *pri; Bg *next;}; //定義一種Bg數據類型(含5個成員)
Bg *Bg1 = (Bg*)malloc(sizeof(Bg)); //定義Bg類型 指針變數Bg1並賦初值

void Position(int x, int y) //游標定位函數(用於指定位置輸出)
{COORD pos = { x - 1, y - 1 };
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}

void Csh( ) //初始化界面

{

printf("══════════════════════════════════════ ");

printf(" ■■ ■■ C語言版 Flappy Bird ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ 瞎搞人:yyposs原創 ");

printf(" ■■ ■■ 瞎搞日期:2014.2 ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ 改編:鳴蟬百2021.7 ");

printf(" ■■ ■■ 操作:按向上方向鍵讓小鳥起飛 ");

printf(" ■■ ");

printf(" ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ DEVc++運行通過 ");

printf("══════════════════════════════════════ ");

printf(" 按鍵繼續…");

getch( );

system("cls");

}

void PrFK( ) //輸出方框(游戲范圍區)

{int i;

Position(1, 1); printf("╔"); Position(79, 1); printf("╗");

Position(1, 24); printf("╚"); Position(79, 24); printf("╝");

for (i = 3; i <= 78; i += 2){Position(i, 1); printf("═"); Position(i, 24); printf("═");}

for(i=2;i<=23;i++)

{ Position(1,i); printf("║");if(i<11)printf("0%d",i-1);else printf("%d",i-1);

Position(79,i); printf("║");

}

Position(4, 25); printf("小鳥即將出現,請准備按鍵起飛… ");

getch( );

Position(4, 25); printf(" ");

}

void CreatBg( ) //創建障礙物坐標(便於列印輸出)
{Bg *Bg2 = (Bg*)malloc(sizeof(Bg));
Bg1->x = 90; Bg1->y = 8; //確定障礙物的一對基本坐標(此時值是在游戲框之外)
Bg2->x = Bg1->x + Distance; Bg2->y = 9; //下一障礙物的基本坐標x、y
Bg1->l_blank = Max_blank - Grade; //障礙物上下兩部分之間的空白距離l_blank
Bg2->l_blank = Max_blank - Grade;
Bg1->next = Bg2; Bg1->pri = Bg2;
Bg2->next = Bg1; Bg2->pri = Bg1;
}


void InsertBg(Bg *p) //隨機改變障礙物的y坐標,讓空白通道有上下變化
{int temp;
Bg *Bgs = (Bg*)malloc(sizeof(Bg));
Bgs->x = p->pri->x + Distance;
Bgs->l_blank = Max_blank - Grade;
srand((int)time(0)); //啟動隨機數發生器
temp = rand( ); //產生一個隨機數並賦值給temp
if (temp % 2 == 0)
{if ((temp % 4 + p->pri->y + Max_blank - Grade)<21)
Bgs->y = p->pri->y + temp % 4;
else Bgs->y = p->pri->y;
}
else
{if ((p->pri->y - temp % 4)>2)Bgs->y = p->pri->y - temp % 4;
else Bgs->y = p->pri->y;
}
Bgs->pri = p->pri; Bgs->next = p;
p->pri->next = Bgs; p->pri = Bgs;
}

void CreatBird( ) //建立小鳥的坐標(初始列印輸出小鳥的位置)
{Bird->x = 41; Bird->y = 10;}

int CheckYN(Bg *q) //判斷游戲結束與否(值為0是要結束,為1沒有要結束)
{Bg *p = q; int i = 0;
while (++i <= 5)
{if (Bird->y>23)return 0;
if (Bird->x == p->x&&Bird->y <= p->y)return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y)return 0;
if (Bird->x == p->x&&Bird->y>p->y + p->l_blank)return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y + p->l_blank)
return 0;
p = p->next;
}
return 1;
}


void Check_Bg(Bg *q) //核查開頭的障礙物坐標是否在游戲區內
{Bg *p = q; int i = 0, temp;
while (++i <= 5)
{if (p->x>-4)p = p->next;
else
{srand((int)time(0)); temp = rand();
if (temp % 2 == 0)
{if ((temp % 4 + p->y + Max_blank - Grade)<21)p->y = p->y + temp % 4;
else p->y = p->y; p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
else
{if ((p->y - temp % 4)>2)p->y = p->y - temp % 4;
else p->y = p->y; p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
}
}
}

void Prt_Bg(Bg *q) //列印輸出障礙物(依據其x、y坐標進行相應輸出)
{Bg *p = q; int i = 0, k, j;
while (++i <= 5)
{if (p->x>0 && p->x <= 78)
{for (k = 2; k<p->y; k++){Position(p->x + 1, k); printf("■"); printf("■"); printf(" ");}
Position(p->x, p->y);
printf("■"); printf("■"); printf("■"); printf(" ");
Position(p->x, p->y + p->l_blank);
printf("■"); printf("■"); printf("■"); printf(" ");
k = k + p->l_blank + 1;
for (k; k <= 23; k++){Position(p->x + 1, k); printf("■"); printf("■"); printf(" ");}
}
p = p->next;
if (p->x == 0)
{for (j = 2; j<p->y; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
Position(p->x + 1, p->y);
printf(" "); printf(" "); printf(" ");
Position(p->x + 1, p->y + Max_blank - Grade);
printf(" "); printf(" "); printf(" ");
j = j + Max_blank - Grade + 1;
for (j; j <= 22; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
}
}
}

void PrtBird( ) //列印輸出小鳥
{Position(Bird->x, Bird->y - 1); printf(" ");
Position(Bird->x, Bird->y); printf("Ю");
Position(38, 2); printf("Score:%d", Score);
}
void Loop_Bg(Bg *q) //障礙物x坐標左移,並記錄成績
{Bg *p = q; int i = 0;
while (++i <= 5)
{p->x = p->x - 1; p = p->next;
if (Bird->x == p->x)
{Score += 1;
if (Score % 4 == 0 && Grade<4)Grade++;
}
}
}

int main( )
{int i = 0; int t;

while (1)

{
Csh( );PrFK( );CreatBg( );
InsertBg(Bg1);InsertBg(Bg1);InsertBg(Bg1);
CreatBird( );
while (1)
{if (!CheckYN(Bg1))break;
Check_Bg(Bg1);Prt_Bg(Bg1);
PrtBird( );Loop_Bg(Bg1);
Bird->y = Bird->y + 1;
if (GetAsyncKeyState(VK_UP))
//按下了向上方向鍵
{Position(Bird->x, Bird->y - 1);printf(" ");
Bird->y = Bird->y - 4;
}
Sleep(200);
//程序延時200毫秒(數值大小決定游戲速度快慢)
i = 0;
}
Position(6, 25);
printf("游戲結束! 請輸入:0.退出 1.重玩");

scanf("%d",&t);

if (t==0)break;

system("cls"); Score = 0;

}
return 0;
}

⑦ 《C游戲編程從入門到精通》pdf下載在線閱讀全文,求百度網盤雲資源

《C游戲編程從入門到精通》(浦濱)電子書網盤下載免費在線閱讀

鏈接:

提取碼: 1qek

書名:C游戲編程從入門到精通

作者:浦濱

出版社:北希電腦出版社

出版年份:2002-5-1

頁數:408

⑧ c語言游戲編程實例

生命游戲
/* ------------------------------------------------------ */
/* PROGRAM game of life : */
/* This is a finite implementation of John H. Conway's */
/* Game of Life. Refere to my book for detail please. */
/* */
/* Copyright Ching-Kuang Shene July/25/1989 */
/* ------------------------------------------------------ */

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 50 /* board size */
#define OCCUPIED 1 /* occupied flag */
#define UNOCCUPIED 0
#define YES 1
#define NO 0

char cell[MAXSIZE][MAXSIZE]; /* the board */
char work[MAXSIZE][MAXSIZE]; /* a working */
int row; /* No. of rows you want */
int column; /* no. of columns you want */
int generations; /* maximum no. of generation*/

/* ------------------------------------------------------ */
/* FUNCTION read_in : */
/* This function reads in the number of generations, */
/* the number of rows, the number of columns and finally */
/* the initial configuration (the generation 0). Then put*/
/* your configuration to the center of the board. */
/* ------------------------------------------------------ */

void read_in(void)
{
int max_row, max_col; /* max # of row and col. */
int col_gap, row_gap; /* incremnet of row and col */
int i, j;
char line[100];

gets(line); /* read in gens, row and col*/
sscanf(line, "%d%d%d", &generations, &row, &column);
for (i = 0; i < row; i++)/* clear the board */
for (j = 0; j < column; j++)
cell[i][j] = UNOCCUPIED;
max_col = 0; /* read in the config. */
for (max_row = 0; gets(line) != NULL; max_row++) {
for (i = 0; line[i] != '\0'; i++)
if (line[i] != ' ')
cell[max_row][i] = OCCUPIED;
max_col = (max_col < i) ? i : max_col;
}
row_gap = (row - max_row)/2; /* the moving gap */
col_gap = (column - max_col)/2;
for (i = max_row + row_gap - 1; i >= row_gap; i--) {
for (j = max_col + col_gap - 1; j >= col_gap; j--)
cell[i][j] = cell[i-row_gap][j-col_gap];
for ( ; j >= 0; j--)
cell[i][j] = UNOCCUPIED;
}
for ( ; i >= 0; i--)
for (j = 0; j < column; j++)
cell[i][j] = UNOCCUPIED;
}

/* ------------------------------------------------------ */
/* FUNCTION display : */
/* Display the board. */
/* ------------------------------------------------------ */

#define DRAW_BOARDER(n) { int i; \
printf("\n+"); \
for (i = 0; i < n; i++) \
printf("-"); \
printf("+"); \
}
void display(int gen_no)
{
int i, j;

if (gen_no == 0)
printf("\n\nInitial Generation :\n");
else
printf("\n\nGeneration %d :\n", gen_no);

DRAW_BOARDER(column);
for (i = 0; i < row; i++) {
printf("\n|");
for (j = 0; j < column; j++)
printf("%c", (cell[i][j] == OCCUPIED) ? '*' : ' ');
printf("|");
}
DRAW_BOARDER(column);
}

/* ------------------------------------------------------ */
/* FUNCTION game_of_life : */
/* This is the main function of Game of Life. */
/* ------------------------------------------------------ */

void game_of_life(void)
{
int stable; /* stable flag */
int iter; /* iteration count */
int top, bottom, left, right; /* neighborhood bound */
int neighbors; /* # of neighbors */
int cell_count; /* # of cells count */
int done;
int i, j, p, q;

display(0); /* display initial config. */
done = NO;
for (iter = 1; iter <= generations && !done; iter++) {
memmove(work, cell, MAXSIZE*MAXSIZE); /**/
stable = YES; /* assume it is in stable */
cell_count = 0; /* # of survived cells = 0 */
for (i = 0; i < row; i++) { /* scan each cell...*/
top = (i == 0) ? 0 : i - 1;
bottom = (i == row - 1) ? row-1 : i + 1;
for (j = 0; j < column; j++) {
left = (j == 0) ? 0 : j - 1;
right = (j == column - 1) ? column-1 : j + 1;

/* compute number of neighbors */

neighbors = 0;
for (p = top; p <= bottom; p++)
for (q = left; q <= right; q++)
neighbors += work[p][q];
neighbors -= work[i][j];

/* determine life or dead */

if (work[i][j] == OCCUPIED)
if (neighbors == 2 || neighbors == 3) {
cell[i][j] = OCCUPIED;
cell_count++;
}
else
cell[i][j] = UNOCCUPIED;
else if (neighbors == 3) {
cell[i][j] = OCCUPIED;
cell_count++;
}
else
cell[i][j] = UNOCCUPIED;
stable = stable && (work[i][j] == cell[i][j]);
}
}
if (cell_count == 0) {
printf("\n\nAll cells die out.");
done = YES;
}
else if (stable) {
printf("\n\nSystem enters a stable state.");
done = YES;
}
else
display(iter);
}
}

/* ------------------------------------------------------ */

void main(void)
{
read_in();
game_of_life();
}


⑨ c語言猜數游戲編程

在VC 平台測試通過。
#include"stdio.h"
#define MAX_NUM 4
#define TRUE 1
#define FALSE 0
void main()
{
int i,j;
int guess_num[MAX_NUM];//隨機生成四個數
int input[MAX_NUM];//用戶輸入四個數
int pos_same = 0;//位置相同個數
int num_same = 0;//數字相同個數
int correct = FALSE;//判斷輸入是否完全正確

//隨機生成四個0到10之間的數
for(i = 0;i<MAX_NUM;i++)
{
guess_num[i] = (int)rand()%10;
}
for(i = 0;i<MAX_NUM;i++)
{
printf(" %d",guess_num[i]);
}
printf("\n");printf("請輸入四個0到10之間的數\n");

while(!correct)
{
//輸入四個數
for(i = 0;i<MAX_NUM;i++)
{
scanf("%d",&input[i]);
}
//獲得位置相同的個數
for(i = 0;i<MAX_NUM;i++)
{
if(input[i] == guess_num[i])
pos_same++;
}
//獲得與隨機數相同的個數
for(i = 0;i<MAX_NUM;i++)
{
for(j = 0;j<MAX_NUM;j++)
{
if(input[i] == guess_num[j])
{
num_same++;
break;
}

}
if(i>0)//判斷與之前的數字是否相同
{
for(j=i;j>0;j--)
{
if(input[i] ==input[j-1])
{
num_same--;
break;
}
}
}
}
printf("數字正確個數: %d \n位置正確個數: %d\n",num_same,pos_same);
num_same = (num_same ==MAX_NUM)?num_same:0;
pos_same = (pos_same ==MAX_NUM)?pos_same:0;
if(pos_same ==MAX_NUM && num_same ==MAX_NUM)
correct = TRUE;
}
printf("你猜對了!\n數字就是");
for(i = 0;i<MAX_NUM;i++)
{
printf(" %d",guess_num[i]);
}
printf("\n");

}

⑩ c語言編程小游戲

原創:
TC2.0以及gcc 編譯通過

/*=======================================================
*Author :wacs5
*Date :20100601(YYYYMMDD)
*Function :剪刀石頭布
*=======================================================*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
char name[4][15]=;
int x[2];
int i;

srand(time(NULL));
for (i=0;i<10;i++) /*10 times game*/
{
x[0]=1+rand()%3; /*generate a number from 1 to 3*/
x[1]=1+rand()%3; /*generate another number from 1 to 3*/
printf("A=%-12sB=%-12s\t",name[x[0]],name[x[1]]);
if (x[0]==x[1])
printf("draw\n"); /*和*/
else if (x[0]%3+1==x[1]) /*lost*/
printf("lost\n");
else /*win*/
printf("win\n");
}

getch();
return 0;
}

熱點內容
jsp系統源碼下載 發布:2024-09-20 22:22:22 瀏覽:863
怎麼給雲伺服器套盾 發布:2024-09-20 22:06:21 瀏覽:7
我的世界建築大師設計伺服器 發布:2024-09-20 22:05:37 瀏覽:150
sqlserver的check約束 發布:2024-09-20 22:05:25 瀏覽:404
編程需要p 發布:2024-09-20 22:03:55 瀏覽:237
安卓小q畫筆哪個版本可以用 發布:2024-09-20 21:57:03 瀏覽:367
mcgs腳本常式 發布:2024-09-20 21:47:01 瀏覽:696
java配置mysql 發布:2024-09-20 21:32:44 瀏覽:758
phpapache偽靜態 發布:2024-09-20 20:54:45 瀏覽:588
新浪雲緩存 發布:2024-09-20 20:53:45 瀏覽:286