C language code to implement 2048 game

  • 2020-06-23 01:39:38
  • OfStack

I am a freshman in college. In the winter vacation training class, the teacher asked us to program a small game with c language in groups, and our group selected "2048". Due to our group have a great god, and we do most of the work to the great god, but I feel useless after training completed, learn nothing, their understanding of the c language is limited to books, can think of the great god have basic programming 1 small game, the in the mind is very worried. So this winter vacation, I independently completed the "2048" game programming.

I wrote the game code with Xcode, and I am IOS white. Some header files cannot be used in Xcode and I cannot find a substitute. Therefore, some functions cannot be realized, such as screen clearing function, data storage function and music function. I hope to have a great god can guide me, very grateful!


#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<curses.h>
/* The header file */
int i,j,a[4][4]={};/*2 Dimensional array a[4][4] For all 0*/
void kaishi()/* Generate start interface */
{
  printf("Welcome to 2048\n");
  printf("  1). A new game \n");
  printf("  2). help \n");
}
int defen(int a[][4])/* The scoring function */
{
  int max=a[0][0];
  int static sum=0;
  for(i=0;i<4;i++)
    for(j=0;j<4;j++)
      if(a[i][j]>max)
        max=a[i][j];
  sum+=max;
  return sum;/* Take the sum of the largest number as the score */
}
void jiemian(int a[][4])
{
  int b[2]={2,4},c[4]={0,1,2,3},sum,n,m;
  do
  {
    srand((int)time(NULL));/* Take time as a random number seed */
    m=c[rand()%4];
    n=c[rand()%4];
    /* Randomly generated coordinates */
    if(a[m][n]==0)/* Determines whether it is a space */
    {
      a[m][n]=b[rand()%2];/* Randomly generated 2 or 4*/
      break;/* Jump out of the loop */
    }
  }while(1);
  printf("%d %d %d\n",m,n,a[m][n]);
  for(i=0;i<4;i++)
  {
    printf("-------------------------\n");
    for(j=0;j<4;j++)
      {
        if((m==i)&&(n==j))
          printf("| %d ",a[m][n]);/* The output is randomly generated 2 or 4*/
        else
        {
          if(a[i][j]>0)
            printf("| %d ",a[i][j]);/* if a[i][j] If there are Numbers, output them */
          else
            printf("|   ");/* if a[i][j] No Numbers, output Spaces */
        }
      }
    printf("|\n");
  }
    printf("-------------------------\n");
    printf("      score :%d\n",sum=defen(a));
}
void hebingup(int a[][4])/* Up to merge */
{
  int m;
  for(j=0;j<4;j++)
    for(i=0;i<3;i++)
      if(a[i][j]>0)/* if a[i][j] Don't for 0 Judge, otherwise jump straight down 1 item */
        if(a[i][j]==a[i+1][j]&&a[i][j]!=0)/* To determine whether two adjacent terms are equal */
        {
          a[i][j]=2*a[i][j];/* The above 1 This term is going to double */
          if(i==2)
            a[i+1][j]=0;
          else
            for(m=i+1;m<3;m++)
            {
              a[m][j]=a[m+1][j];
              a[m+1][j]=0;
            }
          /* Move the following Numbers up as a whole and recycle */
        }
}
void hebingdown(int a[][4])/* Merge down */
{
  int m;
  for(j=0;j<4;j++)
    for(i=3;i>0;i--)
      if(a[i][j]>0)
        if(a[i][j]==a[i-1][j])
        {
          a[i][j]=2*a[i][j];
          if(i==1)
            a[i-1][j]=0;
          else
            for(m=i-1;m>0;m--)
            {
              a[m][j]=a[m-1][j];
              a[m-1][j]=0;
            }
        }
}
void hebingleft(int a[][4])/* To the left to merge */
{
  int m;
  for(i=0;i<4;i++)
    for(j=0;j<3;j++)
      if(a[i][j]>0)
        if(a[i][j]==a[i][j+1])
        {
          a[i][j]=2*a[i][j];
          if(j==2)
            a[i][j+1]=0;
          else
            for(m=j+1;m<3;m++)
            {
              a[i][m]=a[i][m+1];
              a[i][m+1]=0;
            }
        }
}
void hebingright(int a[][4])/* Merge to the right */
{
  int m;
  for(i=0;i<4;i++)
    for(j=3;j>0;j--)
      if(a[i][j]>0)
        if(a[i][j]==a[i][j-1])
        {
          a[i][j]=2*a[i][j];
          if(j==1)
            a[i][j-1]=0;
          else
            for(m=j-1;m>0;m--)
            {
              a[i][m]=a[i][m-1];
              a[i][m-1]=0;
            }
        }
}
void yidong(char b)
{
  int x,m;
  switch(b)
  {
    case 'w' :
      for(j=0;j<4;j++)
        for(i=1;i<=3;i++)
          for(x=i,m=i;x>0;x--,m--)/*x Is the number of cycles, m alternative i Keep the cycle going */
          {
            if(a[m-1][j]>0)
              break;/* If the 1 A non 0 And jump down 1 position */
            else
            {
              a[m-1][j]=a[m][j];
              a[m][j]=0;
            }
          }
      hebingup(a);
      break;
    case 's' :
      for(j=0;j<4;j++)
        for(i=2;i>=0;i--)
          for(x=3-i,m=i;x>0;x--,m++)
          {
            if(a[m+1][j]>0)
              break;
            else
            {
              a[m+1][j]=a[m][j];
              a[m][j]=0;
            }
          }
      hebingdown(a);
      break;
    case 'a' :
      for(i=0;i<4;i++)
        for(j=1;j<=3;j++)
          for(x=j,m=j;x>0;x--,m--)
          {
            if(a[i][m-1]>0)
              break;
            else
            {
              a[i][m-1]=a[i][m];
              a[i][m]=0;
            }
          }
      hebingleft(a);
      break;
    case 'd' :
      for(i=0;i<4;i++)
        for(j=2;j>=0;j--)
          for(x=3-j,m=j;x>0;x--,m++)
          {
            if(a[i][m+1]>0)
              break;
            else
            {
              a[i][m+1]=a[i][m];
              a[i][m]=0;
            }
          }
      hebingright(a);
      break;
  }
}
int main(int argc,char *argv[])
{
  int flag=1;
  char c,k;
  system("stty -icanon");
outloop:;
  kaishi();
  k=getchar();
  printf("\n");
  if(k=='1')
  {
    while(flag==1)
    {
      jiemian(a);
      c=getchar();
      printf("\n");
      yidong(c);
      for(i=0;i<4;i++)
        for(j=0;j<4;j++)
        {
          if(a[i][j]==0)
          {
            flag=1;
            goto end;/* If you have a checkerboard 1 Jump out of the judgment and continue to generate random Numbers */
          }
          else
            flag=0;
        }
       end:;
    }
  }
  if(k=='2')
  {
    do
    {
      printf(" Rules of the game: by clicking <w>,<s>,<a>,<d> Key to move the Numbers up, down, left and right. Add the same Numbers. The sum of the Numbers in each cell will be scored. \n");
      printf("  3). return \n");
      k=getchar();
    }while(k!=3);
    goto outloop;/* Jump to the start screen */
  }
  printf("      Game over \n");
  return 0;
}

Related articles: