工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 2301|回复: 14

求助-C语言-贪食蛇

[复制链接]
发表于 2005-9-21 21:29 | 显示全部楼层 |阅读模式
[WAP]求助,求用C语言编贪食蛇游戏的算法思路  [/wap]
发表于 2005-9-22 23:55 | 显示全部楼层
网上大把啦..
回复

使用道具 举报

 楼主| 发表于 2005-9-24 01:51 | 显示全部楼层
搜搜看
回复

使用道具 举报

发表于 2005-9-24 11:54 | 显示全部楼层
#include "graphics.h"
#include "stdio.h"

#define MAX 200
#define MAXX 30
#define MAXY 30

#define UP    18432
#define DOWN  20480
#define LEFT  19200
#define RIGHT 19712
#define ESC   283
#define ENTER 7181
#define PAGEUP   18688
#define PAGEDOWN 20736
#define KEY_U    5749
#define KEY_K    9579
#define CTRL_P   6512
#define TRUE  1
#define FALSE 0
#define GAMEINIT  1
#define GAMESTART  2
#define GAMEHAPPY 3
#define GAMEOVER  4

struct SPlace
{
int x;
int y;
int st;
} place[MAX];
int speed;
int count;
int score;
int control;
int head;
int tear;
int x,y;
int babyx,babyy;
int class;
int eat;
int game;
int gamedelay[]={5000,4000,3000,2000,1000,500,250,100};
int gamedelay2[]={1000,1};
static int hitme=TRUE,hit = TRUE;
void init(void);
void nextstatus(void);
void draw(void);

void init(void)
{
int i;
for(i=0;i<MAX;i++)
{
   place.x  = 0;
   place.y  = 0;
   place.st = FALSE;
}
   place[0].st = TRUE;
   place[1].st = TRUE;
   place[1].x  = 1;
   speed = 9;
   count = 0;
   score = 0;
   control = 4;
   head = 1;
   tear = 0;
   x = 1;
   y = 0;
   babyx = rand()%MAXX;
   babyy = rand()%MAXY;
   eat = FALSE;
   game = GAMESTART;
}
void nextstatus(void)
{
int i;
int exit;
int xx,yy;
xx = x;
yy = y;
   switch(control)
   {
   case 1: y--; yy = y-1; break;
   case 2: y++; yy = y+1; break;
   case 3: x--; xx = x-1; break;
   case 4: x++; xx = x+1; break;
   }
   hit = TRUE;
   if ( ((control == 1) || (control ==2 )) && ( (y < 1) ||(y >= MAXY-1)) ||
        (((control == 3) || (control == 4)) && ((x < 1) ||(x >= MAXX-1) ) ) )
      {
      hit = FALSE;
      }

      if ( (y < 0) ||(y >= MAXY) ||
        (x < 0) ||(x >= MAXX) )
      {
      game = GAMEOVER;
      control = 0;
      return;
      }
   for (i = 0; i < MAX; i++)
   {
     if ((place.st) &&
        (x == place.x) &&
        (y == place.y) )
        {
        game = GAMEOVER;
        control = 0;
        return;
        }
     if ((place.st) &&
        (xx == place.x) &&
        (yy == place.y) )
        {
        hit = FALSE;
        goto OUT;
        }

   }
OUT:
   if ( (x == babyx) && (y == babyy) )
   {
      eat = TRUE;
      count ++;
      score += (1+class) * 10;
   }

   head ++;
   if (head >= MAX) head = 0;
   place[head].x = x;
   place[head].y = y;
   place[head].st= TRUE;
   if (eat == FALSE)
   {
      place[tear].st = FALSE;
      tear ++;
      if (tear >= MAX) tear = 0;
   }
   else
   {
      eat = FALSE;
      exit = TRUE;
      while(exit)
      {
      babyx = rand()%MAXX;
      babyy = rand()%MAXY;
      exit = FALSE;
      for( i = 0; i< MAX; i++ )
         if( (place.st)&&( place.x == babyx) && (place.y == babyy))
            exit ++;
      }

   }
   if (head == tear) game = GAMEHAPPY;

}
void draw(void)
{
   char temp[50];
   int i,j;
   for (i = 0; i < MAX; i++ )
   {
      setfillstyle(1,9);
      if (place.st)
         bar(place.x*15+1,place.y*10+1,place.x*15+14,place.y*10+9);
   }
   setfillstyle(1,4);
   bar(babyx*15+1,babyy*10+1,babyx*15+14,babyy*10+9);
   setcolor(8);
   setfillstyle(1,8);
   bar(place[head].x*15+1,place[head].y*10+1,place[head].x*15+14,place[head].y*10+9);
/*   for( i = 0; i <= MAXX; i++ )
      line( i*15,0, i*15, 10*MAXY);
   for( j = 0; j <= MAXY; j++ )
      line( 0, j*10, 15*MAXX, j*10);
*/
   rectangle(0,0,15*MAXX,10*MAXY);
   sprintf(temp,"Count: %d",count);
   settextstyle(1,0,2);
   setcolor(8);
   outtextxy(512,142,temp);
   setcolor(11);
   outtextxy(510,140,temp);
   sprintf(temp,"1P: %d",score);
   settextstyle(1,0,2);
   setcolor(8);
   outtextxy(512,102,temp);
   setcolor(12);
   outtextxy(510,100,temp);
   sprintf(temp,"Class: %d",class);
   setcolor(8);
   outtextxy(512,182,temp);
   setcolor(11);
   outtextxy(510,180,temp);
}
main()
{
int pause = 0;
char temp[50];
int d,m;
int key;
int p;
static int keydown = FALSE;
int exit = FALSE;
int stchange = 0;
d = VGA;
m = VGAMED;
initgraph( &d, &m, "" );
setbkcolor(3);
class = 3;
init();
p = 1;
while(!exit)
{
if (kbhit())
{
   key = bioskey(0);
   switch(key)
   {
   case UP: if( (control != 2)&& !keydown)
                control = 1;
                keydown = TRUE;
                break;
   case DOWN: if( (control != 1)&& !keydown)
                control = 2;
                keydown = TRUE;
                break;
   case LEFT: if( (control != 4)&& !keydown)
                control = 3;
                keydown = TRUE;
                break;
   case RIGHT: if( (control != 3)&& !keydown)
                control = 4;
                keydown = TRUE;
                break;
   case ESC: exit = TRUE;break;
   case ENTER: init();break;
   case PAGEUP: class --; if (class<0) class = 0; break;
   case PAGEDOWN: class ++;if (class>7) class = 7; break;
   case KEY_U: if( ( (control ==1) ||(control ==2))&& !keydown)
                   control = 3;
               else if(( (control == 3) || (control == 4))&& !keydown)
                   control = 1;
               keydown = TRUE;
               break;
   case KEY_K: if( ( (control ==1) ||(control ==2))&& !keydown)
                   control = 4;
               else if(( (control == 3) || (control == 4))&& !keydown)
                   control = 2;
               keydown = TRUE;
               break;
   case CTRL_P:pause = 1 - pause; break;
   }
}
stchange ++ ;
putpixel(0,0,0);
if (stchange > gamedelay[class] + gamedelay2[hit])
   {
   stchange = 0;
   keydown = FALSE;
   p = 1 - p;
   setactivepage(p);
   cleardevice();
   if (!pause)
      nextstatus();
   else
   {
      settextstyle(1,0,4);
      setcolor(12);
      outtextxy(250,100,"PAUSE");
   }
   draw();

   if(game==GAMEOVER)
   {
   settextstyle(0,0,6);
   setcolor(8);
   outtextxy(101,101,"GAME OVER");
   setcolor(15);
   outtextxy(99,99,"GAME OVER");
   setcolor(12);
   outtextxy(100,100,"GAME OVER");
   sprintf(temp,"Last Count: %d",count);
   settextstyle(0,0,2);
   outtextxy(200,200,temp);
   }

   if(game==GAMEHAPPY)
   {
   settextstyle(0,0,6);
   setcolor(12);
   outtextxy(100,300,"YOU WIN");
   sprintf(temp,"Last Count: %d",count);
   settextstyle(0,0,2);
   outtextxy(200,200,temp);
   }
   setvisualpage(p);
   }
}
closegraph();
}
回复

使用道具 举报

发表于 2005-9-24 11:55 | 显示全部楼层
网上找的..看起来好像可以跑
我是用java的.对c仅仅是了解而已
回复

使用道具 举报

发表于 2005-9-24 12:00 | 显示全部楼层
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <bios.h>
#include <dos.h>
#include <time.h>
#include <string.h>

#define  error  0
#define  ok  1
#define  overflow -2
#define  N  10
#define  Enter 28
#define  Esc 1
#define  up  72
#define  right  77
#define  down  80
#define  left  75
#define  d  1
typedef int status;
typedef int direction;
typedef struct snacknode{    //蛇身体节点结构
        int x;
        int y;
        struct snacknode *next;
}snacknode,*linknode;

typedef struct {        //蛇头结构
        linknode head;
        linknode tail;
}snack;
typedef struct renode{   //record struct
        char name[15];
        int  score;
        int  number;
}renode;

typedef struct{   //定义蛋的结构
        int x;
        int y;
}egg;
int key;

direction din=right; //direction
egg newegg;  //the position of new egg
int score=0,speed=4;//全局变量
status menuflag=ok;//the flag stand for return to menu or not
char name[15]; //player`s mame
renode record[10];//store the records

/************HELP FUNCTION**********/
void help(){
        textmode(C80);
        textbackground(BLACK);
        clrscr();
        window(15,8,60,21);
        textbackground(GREEN);
        clrscr();
        textcolor(YELLOW);
        gotoxy(18,2);
        cprintf("STATEMENT");
        textcolor(RED);
        gotoxy(2,3);
        cprintf("Direction key to control as follows:");
        gotoxy(4,5);
        cprintf("-\x1a Right   \x1b- Left   \x18 Up   \x19 Down");
        gotoxy(15,6);
        cprintf("Esc---->Quit");
        gotoxy(4,8);
        cprintf("Press button 'Menu' to return to menu.");
        gotoxy(4,9);
        cprintf("Press button 'Quit' to exit.");
        textcolor(YELLOW);
        gotoxy(25,11);
        cprintf("All right reserved!");
        gotoxy(27,12);
        cprintf("Programer: Victor");
        gotoxy(32,13);
        cprintf("2005\\5\\25");
        gotoxy(1,14);
        cprintf("Press any key to return...");
        getch();
}
//pregame file content


/*******************GAME BACKGROUND FUNCTION*********/
void gameground()
{  textmode(C80);
   textbackground(LIGHTGRAY);
   clrscr();

   textbackground(GREEN);
   textcolor(RED);
   window(70,4,78,4);
   clrscr();
   gotoxy(70,4);
   cputs("Speed:");
   window(70,6,78,6);
   clrscr();
   cputs("Score:");
   window(70,19,75,19);
   clrscr();
   cputs("Menu");
   window(70,23,75,23);
   clrscr();
   cputs("Quit");


   textbackground(GREEN);
   window(2,2,65,24);
   clrscr();
   textbackground(BLUE);
   window(2,2,64,24);
   clrscr();
}
//gameground file content

/*****************GAME OPERATION FUNCTIONS********/
void precompute(int *x,int *y)  //求出在当前方向上下一步的坐标
{  switch(din)
   {  case up: *y-=d;break;
      case right: *x+=d;break;
      case down:  *y+=d;break;
      case left:  *x-=d;break;
      default:  exit(error);
   }
}

void gameover(){ // 游戏出错处理函数
        void gamecontrol();
        void sortadd();
        textcolor(RED);
        gotoxy(20,16);
        cprintf("YOUR SCORE:%d",score);
        gotoxy(20,17);
        cputs("GAME OVER!");
        sortadd();
        gamecontrol();
}//gameover

status changediraction()    //改变蛇头方向函数
{  direction key;
   if(bioskey(1)){
      key=bioskey(0);
      key=key>>8;
      if(key==Esc){
         gameover();
         return error;
      }
      switch(din)
         {  case up:  if(key==left||key==right)
                            din=key;
                            break;
            case right:  if(key==up||key==down)
                                    din=key;
                                    break;
            case down:  if(key==left||key==right)
                                   din=key;
                                   break;
            case left:  if(key==up||key==down)
                                  din=key;
                                   break;
            default:  exit(error);
      }//switch
   }//if
   return ok;
}//changdiraction

status nextbody(snack *s)   //判断下一步是否蛇身
{  int dx,dy;
   linknode p;
   dx=s->head->x;
   dy=s->head->y;
   precompute(&dx,&dy);  //得到下一步的坐标
   p=s->tail;
   while(p)  //判断下一步的坐标是否与蛇身重复
   {  if(dx==p->x&&dy==p->y)
           return error;
      else
                  p=p->next;
   }
//  gotoxy(3,8);
//  printf("Next body return ok.\n");
   return ok;
}

status forward(snack *s)  //蛇前进一步函数
{ // linknode p;
   int dx,dy,i,_dx,_dy;
   _dx=dx=s->head->x; _dy=dy=s->head->y;
   precompute(&dx,&dy);
   textcolor(BLUE);   //消除蛇尾图表
   gotoxy(s->tail->x,s->tail->y);
   putch(219);
   textcolor(YELLOW);

   s->head->next=s->tail;
   s->head=s->tail;
   s->tail=s->tail->next;
   s->head->next=NULL;
   s->head->x=dx; s->head->y=dy;

   gotoxy(_dx,_dy);//clear the preview head
   putch(5);
   gotoxy(s->head->x,s->head->y);//打印蛇头
   putch(1);

   return ok;
}

status initsnack(snack *s)  //蛇的初始化函数
{  int i=0;
   linknode newbase[4];
//  textmode(C80);
   while(i<=3)
   {  newbase=(linknode)malloc(sizeof(snacknode));
      if(!newbase)  exit(overflow);
      newbase->x=i+3;  newbase->y=2;
      i++;
   }
   s->tail=newbase[0];
   for(i=1;i<4;i++)
           newbase[i-1]->next=newbase;
   newbase[3]->next=NULL;
   s->head=newbase[3];
   //打印蛇身
   textcolor(YELLOW);
   for(i=0;i<=3;i++){
       gotoxy(newbase->x,newbase->y);
       putch(5);
   }//for

   gotoxy(75,5);
   printf("%d",score);
   gotoxy(75,3);
   printf("%d",speed);

   return ok;
}

status nextwall(snack *s)   //判断在当前方向上下一步是否为墙壁
{  int dx,dy;
   dx=s->head->x;  dy=s->head->y;
   precompute(&dx,&dy);
   if(dx>63||dx<2||dy>23||dy<1)  //坐标越界
           return error;
//   gotoxy(30,22);
//   printf("Nextwall return ok.\n");
   return ok;
}

status layegg(snack *s)      //下蛋函数
{  int flag=1,m=0;//,t1,t2;
  // long t;
   linknode p;
   p=s->tail;
   while(p)        //判断蛇是否已经充满整个窗口
   {  m++;
      p=p->next;
          }
   if(m>=63*24)
           return error;
   while(flag){
   //  t=time(NULL);
        // t1=random(t)%60+1;
     // t2=random(t)%20+1;
      newegg.x=rand()%61+2;  //产生随机坐标
      newegg.y=rand()%20+2;
      p=s->tail ;
      while(p)
        {  if(newegg.x!=p->x&&newegg.y!=p->y)//  判断蛋的坐标是否为蛇身
                 p=p->next;
           else
              break;
        }
        if(p==NULL)
            flag=0;

   }

   gotoxy(newegg.x,newegg.y);  //打印蛋的图
   putch(3);

   return ok;
}

status eategg(snack *s)   //判断蛇在当前方向上下一步是否为蛋
{  int dx,dy,_dx,_dy;
   linknode p,q;
   if(s->head->x==newegg.x&&s->head->y==newegg.y)  //吃蛋后蛇身加长,并产生新蛋
   {  p=(linknode)malloc(sizeof(snacknode));
      if(!p)   exit(overflow);
      _dx=dx=s->head->x; _dy=dy=s->head->y;
      precompute(&dx,&dy);
      p->x=dx; p->y=dy;
      s->head->next=p;     //插入一个节点到蛇头
      s->head=p;
      p->next=NULL;

      score+=N;
      gotoxy(75,5);
      printf("%d",score);

      gotoxy(_dx,_dy);
      putch(5);
      gotoxy(s->head->x,s->head->y);//打印蛇头
      putch(1);

     // sound(100);

      layegg(s);
      return ok;
   }
   return error;
}

status nextegg(snack *s){           //next position is a egg or not
   if(s->head->x==newegg.x&&s->head->y==newegg.y){
       eategg(s);
       return ok;
   }
   else
      return error;
}//nextegg

void gamedrive(snack *s){
        status flag;
        while(1){
    //          getch();
          if(nextbody(s)==ok){
      //              getch();
              if(nextwall(s)==ok){
        //          getch();
                  if(nextegg(s)==error){
                      delay(50*(6-speed));
                      forward(s);
                  }//if
                  else
                      eategg(s);
              }//if
              else
                { gameover(); return;}

          }//if
          else
             {gameover(); return;}
         flag=changediraction();
         if(flag==error)
            break;
        }//while
}//gamedrive

void gamecontrol(){     //game begin or quit
   int m,f=0,flag=1;
   window(2,2,80,25);
   textbackground(GREEN);
   gotoxy(69,18);
   textcolor(YELLOW);
   cprintf("Menu");
   while(flag){
      while(!bioskey(1));
      m=bioskey(0);
      m=m>>8;
      switch(m){
         case 72:
         case 80: if(f==0){
                     textcolor(RED);
                     gotoxy(69,18);
                     cprintf("Menu");

                     textcolor(YELLOW);
                     gotoxy(69,22);
                     cputs("Quit");
                     textcolor(YELLOW);
                     f=1;
                  }//if
                  else{
                     textcolor(RED);
                     gotoxy(69,22);
                     cputs("Quit");

                     textcolor(YELLOW);
                     gotoxy(69,18);
                     cputs("Menu");
                     textcolor(YELLOW);
                     f=0;
                  }//else
                  break;
         case 28: if(f==0)   //menu
                     menuflag=ok;
                  else          //quit
                     menuflag=error;  //game over
      }//switch
      if(m==28)
         flag=0;
   }//while
}//gamecontrol
回复

使用道具 举报

发表于 2005-9-24 12:01 | 显示全部楼层
void initgame(){//initlize game
    status flag;
    snack s;
    gameground();
    window(2,2,80,25);
    initsnack(&s);
    layegg(&s);
    gamedrive(&s);
  //  return flag;
}//initgame

/**********MUEN OPERATION*********/
void menuground(){  //type the menu backgrond
   int i;
   textmode(C80);
   textbackground(BLUE);
   clrscr();
   window(35,8,45,19);
   textbackground(GREEN);
   clrscr();
   textcolor(RED);
   gotoxy(4,2);
   cprintf("Start");
   gotoxy(4,4);
   cputs("Speed");
   gotoxy(4,6);
   cputs("Sort");
   gotoxy(4,8);
   cputs("Help");
   gotoxy(4,10);
   cputs("Quit");
   textcolor(BLUE);
   for(i=1;i<=11;i+=2){
      gotoxy(1,i);
      cputs("-----------");
   }//for
}//menugrond

void outputmenu(int t){//output the need item
     switch(t){
         case 1: cputs("Start"); break;
         case 2: cputs("Speed"); break;
         case 3: cputs("Sort");  break;
         case 4: cputs("Help");  break;
         case 5: cputs("Quit");  break;
         default: exit(0);
     }//switch
}//outputmenu

int menucontrol(){ //choose a item from the menu
   int m,pos=1;
   menuground();
   textcolor(YELLOW);
   gotoxy(4,2);
   cputs("Start");

   while(1){
      while(!bioskey(1));
      m=bioskey(0);
      m=m>>8;
      switch(m){
         case 72: if(pos!=1){
                      textcolor(RED);
                      gotoxy(4,2*pos);
                      outputmenu(pos);

                      textcolor(YELLOW);
                      pos--;
                      gotoxy(4,2*pos);
                      outputmenu(pos);
                  }//if
                  break;

         case 80: if(pos!=5){
                     textcolor(RED);
                     gotoxy(4,2*pos);
                     outputmenu(pos);

                     textcolor(YELLOW);
                     pos++;
                     gotoxy(4,2*pos);
                     outputmenu(pos);
                  }//if
                  break;
         case 28: return pos;
      }//switch
   }//while
}//menucontrol

/**********SPEED CONTROL***********/
void speedground(){
   textmode(C80);
   textbackground(BLUE);
   clrscr();
   window(20,10,50,15);
   textbackground(GREEN);
   clrscr();
   textcolor(RED);
   gotoxy(1,1);
   cprintf("SPEED LEVEL:\n");
   textcolor(YELLOW);
   gotoxy(5,3);
   cputs("***1***2***3***4***5***\n");
   textcolor(RED);
   gotoxy(3,4);
   cprintf("****************************");
   gotoxy(3,5);
   cprintf("Easy<------------->Difficult");
   textcolor(YELLOW);
   gotoxy(2,6);
   cprintf("Choose the level...");
}//speedgrond
void outputspeed(int t){//output the need item
     switch(t){
         case 1: cputs("1"); break;
         case 2: cputs("2"); break;
         case 3: cputs("3");  break;
         case 4: cputs("4");  break;
         case 5: cputs("5");  break;
         default: exit(0);
     }//switch
}//outputmenu

void speedcontrol(){ //choose a item from the menu
   int m,pos=1;
   speedground();
   textcolor(RED);
   gotoxy(8,3);
   cputs("1");

   while(1){
      while(!bioskey(1));
      m=bioskey(0);
      m=m>>8;
      switch(m){
         case 75: if(pos!=1){
                      textcolor(YELLOW);
                      gotoxy(4*(pos+1),3);
                      outputspeed(pos);

                      textcolor(RED);
                      pos--;
                      gotoxy(4*(pos+1),3);
                      outputspeed(pos);
                  }//if
                  break;

         case 77: if(pos!=5){
                     textcolor(YELLOW);
                     gotoxy(4*(pos+1),3);
                     outputspeed(pos);

                     textcolor(RED);
                     pos++;
                     gotoxy(4*(pos+1),3);
                     outputspeed(pos);
                  }//if
                  break;
         case 28: speed=pos;
      }//switch
      if(m==28)
        break;
   }//while
}//menucontrol

/*****************END OF GAME**********/
void endgame(){
    int i,j,k;
    textmode(C80);
    textbackground(BLUE);
    clrscr();
    window(15,1,65,25);
    textbackground(YELLOW);
    clrscr();
    textcolor(GREEN);

    j=1;
    for(i=1;i<=4;i++){
       gotoxy(26-i,j);
       for(k=1;k<=2*i-1;k++)
          cputs("*");
       j++;
    }//for
    for(i=1;i<=6;i++){
       gotoxy(26-(i+1),j);
       for(k=1;k<=2*i+1;k++)
          cputs("*");
       j++;
    }//for
    for(i=1;i<=8;i++){
       gotoxy(26-(i+1),j);
       for(k=1;k<=2*i+1;k++)
          cputs("*");
       j++;
    }//for
    for(i=1;i<=4;i++){
       gotoxy(23,j);
       cputs("*****");
       j++;
    }//for

    textcolor(YELLOW);
    gotoxy(10,j);
    cprintf("To my belove:  Happy everyday!");
    textcolor(RED);
    gotoxy(40,j+1);
    cputs("Victor");
    getch();
    exit(0);
}//endgame

/*************SORT FUNCTIONS********/
void sort(){
        FILE *fp1,*fp2;
        int n,i;

        textmode(C80);
        textbackground(BLUE);
        clrscr();
        textbackground(GREEN);
        window(20,5,60,20);
        clrscr();
        textcolor(RED);
        gotoxy(2,1);
        cprintf("*****NUMBER*******NAME*******SCORE*****");

        if(!(fp1=fopen("n_value.lly","rb"))){  //open the file n_value.lly
             //        printf("Cannot open the file.\n");
                getch();
                return;
        }//if
        n=getw(fp1);
        fclose(fp1);//close file n_value.lly

        if(!(fp2=fopen("record.lly","rb"))){ //open the file record.lly
             printf("Can not open record.lly");
             getch();
             exit(0);
        }//if
        for(i=0;i<n;i++)                   //read the records
           fread(&record,sizeof(renode),1,fp2);
        fclose(fp2);//close file record.lly

        for(i=0;i<n;i++){
           gotoxy(7,i+3);
           cprintf("%d",record.number);
           gotoxy(20,i+3);
           cprintf("%s",record.name);
           gotoxy(31,i+3);
           cprintf("%d",record.score);
        }//for

        textcolor(YELLOW);
        gotoxy(2,16);
        cprintf("Press any key to return...");
        getch();
}//sort

void recordadd(){
    textbackground(GREEN);
    window(10,10,35,10);
    clrscr();
    textcolor(YELLOW);
    cprintf("Input your name:");
    gets(name);
    gotoxy(2,2);
}//recordadd
void sortadd(){
        int n,i;
        FILE *fp1,*fp2;

        if(!(fp1=fopen("n_value.lly","rb"))){  //n_value does not exit
            recordadd();
            if(name[0]=='\0')
               return;
            if(!(fp1=fopen("n_value.lly","wb"))){
                 printf("Can not open n_value with w.");
                 getch();
                 exit(0);
            }//if
            putw(1,fp1); //write n=1 to file n_value.lly
            fclose(fp1);//close the file n_value.lly
            record[0].score=score;
            strcpy(record[0].name,name);
            record[0].number=1;

            if(!(fp2=fopen("record.lly","wb"))){ //open file record.lly
                printf("Can not open record.lly with w");
                getch();
                exit(0);
            }//if
            fwrite(&record[0],sizeof(renode),1,fp2);//input new record
            fclose(fp2);//close record.lly
        }//if
        else{//n_value.lly has exited.
             n=getw(fp1);//get the record nember
             fclose(fp1);
             if(n<10){ //the record is not full
                 recordadd();
                 if(name[0]=='\0'){  //no name input
                     fclose(fp1);
                     return;
                 }//if
                 n++;
                 if(!(fp1=fopen("n_value.lly","wb"))){//open file n_value.lly
                     printf("Can not open n_value with w.");
                     getch();
                     exit(0);
                 }//if
                 putw(n,fp1);//updata the file n_value
                 fclose(fp1); //close file n_value
                 if(!(fp2=fopen("record.lly","rb"))){//open record.llly to add record
                      printf("Can not open record.lly");
                      getch();
                      exit(0);
                 }//if
                 for(i=0;i<n-1;i++)//read out the record
                     fread(&record,sizeof(renode),1,fp2);
                 for(i=n-2;i>=0;i--){          //choose the position to insert
                     if(record.score<score){
                         strcpy(record[i+1].name,record.name);
                         record[i+1].score=record.score;
                         record[i+1].number=record.number+1;
                     }//if
                     else
                         break;
                 }//for
                 record[i+1].score=score;   //insert the new record
                 strcpy(record[i+1].name,name);
                 record[i+1].number=i+2;
                 fclose(fp2);//close file record.lly

                 if(!(fp2=fopen("record.lly","wb"))){//open record.llly to add record
                      printf("Can not open record.lly");
                      getch();
                      exit(0);
                 }//if
                 for(i=0;i<n;i++)    //write the records
                    fwrite(&record,sizeof(renode),1,fp2);
                 fflush(fp2);
                 fclose(fp2); //close the file record
             }//if
             else{ // the record has full:n=10
                 if(!(fp2=fopen("record.lly","rb"))){//open record.llly to add record
                      printf("Can not open record.lly");
                      getch();
                      exit(0);
                 }//if
                 for(i=0;i<10;i++) //read out all the record
                    fread(&record,sizeof(renode),1,fp2);
                 fclose(fp2);//close file record.lly

                 for(i=0;i<10;i++){  //updata the records
                    if(record.score<score){
                        recordadd();
                        if(name[0]=='\0')
                            return;
                        strcpy(record.name,name);
                        record.score=score;
                        break;
                    }//if
                 }//for

                 if(i<10){//a new record is inserted
                    if(!(fp2=fopen("record.lly","wb"))){//open record.llly to add record
                       printf("Can not open record.lly");
                       getch();
                       exit(0);
                    }//if
                    for(i=0;i<10;i++)//write the records to file
                        fwrite(&record,sizeof(renode),1,fp2);
                    fflush(fp2);   //clear the output path
                    fclose(fp2);//close the file reord.lly
                 }//if
             }// else
        }//else
}//sortadd

void main(){
   int t,flag=1;
   while(flag){
      t=menucontrol();
      switch(t){
         case 1: initgame(); break;
         case 2: speedcontrol(); break;
         case 3: sort(); break;
         case 4: help(); break;
         case 5: endgame();
      }//switch
      flag=menuflag;
      if(flag==0)
        endgame();
      din=right;//init the data
      score=0;
   }//while
}//main
回复

使用道具 举报

发表于 2005-9-27 21:04 | 显示全部楼层
住在你对面的自动化学院那里大把~~ 大二时的C课程设计很多人都有这个课题
回复

使用道具 举报

 楼主| 发表于 2005-10-1 01:16 | 显示全部楼层
寂寞高手  在 2005-9-24 03:55 发表:

网上找的..看起来好像可以跑
我是用java的.对c仅仅是了解而已


java!?不行啊,我不懂软件这么专业的东西,因为我大二那时学c,感觉是一知半解,似乎80多分但是不实用的。没做过实际的东西出来心里很不踏实。大三学了汇编,学得比较顺手,回头来看c明白了好多,现在想学vc,以后可能要编写一些通讯程序,所以觉得c还是做点东西出来比较像样点,我想你给个思路我,我自己编写不是要现成的
回复

使用道具 举报

 楼主| 发表于 2005-10-1 01:23 | 显示全部楼层
邑之鹤  在 2005-9-27 13:04 发表:

住在你对面的自动化学院那里大把~~ 大二时的C课程设计很多人都有这个课题


嘿嘿都说自动化厉害,早知道当初就不应该把自动化填第三个志愿,因为信工分数高啊,唉~~~~要好好向你们学习,我要关键技术的算法思想,不要现成的。我觉得我的汇编强啊,就是c没底,搞通信要vc比较实际,达人指点下啊
回复

使用道具 举报

发表于 2005-10-9 08:47 | 显示全部楼层
看明白人家的的程序,那就可以有思路。算法也自然出来啦
回复

使用道具 举报

发表于 2005-10-9 10:15 | 显示全部楼层
高手你好~~~~java不懂
回复

使用道具 举报

 楼主| 发表于 2005-10-9 17:36 | 显示全部楼层
寂寞高手  在 2005-10-9 00:47 发表:

看明白人家的的程序,那就可以有思路。算法也自然出来啦


thx,总觉得看着人家程序来写,就好像做题目看着答案来做一样,不过还是谢谢你的帮忙,我在看
回复

使用道具 举报

发表于 2005-10-30 23:53 | 显示全部楼层
连源代码也不好好分析一下就说什么什么,唉。。。。。。。。。。
回复

使用道具 举报

发表于 2005-10-31 20:49 | 显示全部楼层
使唔使甘复杂啊~~~~??
睇到个头都大埋...
甘长嘅程序有无运行过噶~?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-14 06:33

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表