#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 xint yint color}Snk[12]
struct Food
{int xint yint 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=aflag2=bflag3=cflag4=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=3i<numi++)
{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=1i<numi++)
{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=1i<=timesi++)
delay(15000)
}
生命游戏/* ------------------------------------------------------ */
/* 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 UNOCCUPIED0
#define YES 1
#define NO0
char cell[MAXSIZE][MAXSIZE] /* the board */
char workcopy[MAXSIZE][MAXSIZE] /* a working copy */
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 = 0i <rowi++)/* clear the board */
for (j = 0j <columnj++)
cell[i][j] = UNOCCUPIED
max_col = 0/* read in the config. */
for (max_row = 0gets(line) != NULLmax_row++) {
for (i = 0line[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 - 1i >= row_gapi--) {
for (j = max_col + col_gap - 1j >= col_gapj--)
cell[i][j] = cell[i-row_gap][j-col_gap]
for ( j >= 0j--)
cell[i][j] = UNOCCUPIED
}
for ( i >= 0i--)
for (j = 0j <columnj++)
cell[i][j] = UNOCCUPIED
}
/* ------------------------------------------------------ */
/* FUNCTION display :*/
/*Display the board. */
/* ------------------------------------------------------ */
#define DRAW_BOARDER(n) { int i \
printf("\n+") \
for (i = 0i <ni++) \
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 = 0i <rowi++) {
printf("\n|")
for (j = 0j <columnj++)
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 = 1iter <= generations &&!doneiter++) {
memmove(workcopy, cell, MAXSIZE*MAXSIZE)/*copy*/
stable = YES /* assume it is in stable */
cell_count = 0/* # of survived cells = 0 */
for (i = 0i <rowi++) { /* scan each cell...*/
top= (i == 0) ? 0 : i - 1
bottom = (i == row - 1) ? row-1 : i + 1
for (j = 0j <columnj++) {
left = (j == 0) ? 0 : j - 1
right = (j == column - 1) ? column-1 : j + 1
/* compute number of neighbors*/
neighbors = 0
for (p = topp <= bottomp++)
for (q = leftq <= rightq++)
neighbors += workcopy[p][q]
neighbors -= workcopy[i][j]
/* determine life or dead */
if (workcopy[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 &&(workcopy[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语言是计算机专业都要学习的一门基础学科。一般来说,是比较枯燥的.那么,我们能不能通过编一些小游戏来提高它的趣味性呢?这样学习程序设计,就不会是一件艰苦 ,枯燥的事,它变得象电脑游戏一样充满好奇,富有乐趣。
例如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=0i<SIZEi++)
{
for(j=0j<SIZEj++)
{
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=0i<SIZEi++)
{
for(j=0j<SIZEj++)
printf("| ")
printf("|\n")
for(j=0j<SIZEj++)
{
if(n[i][j]==0)
printf("| ")
else
printf("|%4d ",n[i][j])
}
printf("|\n")
for(j=0j<SIZEj++)
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=0i<SIZEi++)
for(j=0j<SIZEj++)
if(n[i][j]==0)
count++
return count
}
int isfull(int n[][SIZE])
{
int i,j,count=0
for(i=0i<SIZEi++)
{
for(j=1j<SIZE-1j++)
{
if(n[i][j]==n[i][j+1]||n[i][j]==n[i][j-1])
count++
}
}
for(j=0j<SIZEj++)
{
for(i=1i<SIZE-1i++)
{
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=0a<SIZEa++)
m[a]=0
for(j=0j<SIZEj++)
{
for(a=0a<SIZEa++)
{
for(i=0i<SIZE-1i++)
{
if(n[i][j]==0)
{
n[i][j]=n[i+1][j]
n[i+1][j]=0
}
}
}
}
for(j=0j<SIZEj++)
{
for(a=0,i=0i<SIZEi++)
{
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=0i<SIZEi++)
{
n[i][j]=m[i]
m[i]=0
}
}
}
void toa(int n[][SIZE])
{
int i,j,a
int m[SIZE]
for(a=0a<SIZEa++)
m[a]=0
for(i=0i<SIZEi++)
{
for(a=0a<SIZEa++)
{
for(j=0j<SIZE-1j++)
{
if(n[i][j]==0)
{
n[i][j]=n[i][j+1]
n[i][j+1]=0
}
}
}
}
for(i=0i<SIZEi++)
{
for(a=0,j=0j<SIZEj++)
{
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=0j<SIZEj++)
{
n[i][j]=m[j]
m[j]=0
}
}
}
void tos(int n[][SIZE])
{
int i,j,a
int m[SIZE]
for(a=0a<SIZEa++)
m[a]=0
for(j=SIZE-1j>=0j--)
{
for(a=SIZE-1a>=0a--)
{
for(i=SIZE-1i>0i--)
{
if(n[i][j]==0)
{
n[i][j]=n[i-1][j]
n[i-1][j]=0
}
}
}
}
for(j=SIZE-1j>=0j--)
{
for(a=SIZE-1,i=SIZE-1i>=0i--)
{
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-1i>=0i--)
{
n[i][j]=m[i]
m[i]=0
}
}
}
void tod(int n[][SIZE])
{
int i,j,a
int m[SIZE]
for(a=0a<SIZEa++)
m[a]=0
for(i=SIZE-1i>=0i--)
{
for(a=SIZE-1a>=0a--)
{
for(j=SIZE-1j>0j--)
{
if(n[i][j]==0)
{
n[i][j]=n[i][j-1]
n[i][j-1]=0
}
}
}
}
for(i=SIZE-1i>=0i--)
{
for(a=SIZE-1,j=SIZE-1j>=0j--)
{
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-1j>=0j--)
{
n[i][j]=m[j]
m[j]=0
}
}
}