C language realizes knight flying chess game

  • 2020-07-21 09:18:29
  • OfStack

This article shares the specific code of C language to realize knight flying chess for your reference. The specific content is as follows

Demand analysis

The rules of the game and the traditional flying chess 1, support two against

Use 100 squares small game board

Rules of the game: each player takes turns to roll the dice to control the advance or retreat of their cavalry

ordinary

mines

suspended

Time and space tunnel

Lucky roulette (offers two kinds of luck: position swapping and bombing)

Levels on the chessboard are only valid on the cavalry's first move encounter


#include<stdio.h>
#include<windows.h> // color 
#include<string.h>
#include<conio.h> // A function for data input and data output through the console 
#include<stdlib.h>
#include<time.h>  // Define the time function 
struct node // define 4 Personal things 
{
  char name[20];
} people[4];
int map[100]=
{
  0,0,0,0,0,2,1,0,0,3,0,0,0,2,0,0,0,2,0,0,4,0,0,1,0,4,0,3,0,0,
  0,0,0,2,0,
  0,0,0,2,0,1,0,0,0,0,4,0,0,0,0,2,0,0,0,0,1,0,0,0,0,3,0,0,4,2,
  0,0,0,0,1,
  0,0,4,0,0,0,0,0,0,0,2,0,0,1,0,0,0,0,4,0,4,0,0,3,2,0,0,0,0,0
}; // The digital code for the map 
int number1,number2; // The player 1 Select character number 
int numstep1,numstep2; // The player 1 Select character number 
int rand(void);  // Pseudo-random number generation function 
void srand(unsigned int n); // Seeds function 
void showmap(int map[])  // Print game map 
{
  int i,j,k;
  printf("\t\t\t Riding a   "   fly   line   chess  \n\n");
  printf("' ∷ ' Is the ground, ' because ' It's a lucky compass, ' u ' Is mine, ' s ' Is suspended, ' 〓 ' It's a time tunnel \n\n");
  for(i=0; i<31; i++)
  {
    if(map[i]==0)
      printf(" ∷ ");
    else if(map[i]==1)
      printf(" because ");
    else if(map[i]==2)
      printf(" u ");
    else if(map[i]==3)
      printf(" s ");
    else if(map[i]==4)
      printf(" 〓 ");
    else if(map[i]==10)
      printf(" a. ");
    else if(map[i]==20)
      printf(" B ");
    else if(map[i]==30)
      printf("@@");
  }
  printf("\n");
  for(k=0; k<4; k++)
  {
    for(j=0; j<30; j++)
    {
      printf(" ");
    }
    if(map[i]==0)
      printf(" ∷ ");
    else if(map[i]==1)
      printf(" because ");
    else if(map[i]==2)
      printf(" u ");
    else if(map[i]==3)
      printf(" s ");
    else if(map[i]==4)
      printf(" 〓 ");
    else if(map[i]==10)
      printf(" a. ");
    else if(map[i]==20)
      printf(" B ");
    else if(map[i]==30)
      printf("@@");
    i++;
    printf("\n");
  }
  for(i=65; i>=35; i--)
  {
    if(map[i]==0)
      printf(" ∷ ");
    else if(map[i]==1)
      printf(" because ");
    else if(map[i]==2)
      printf(" u ");
    else if(map[i]==3)
      printf(" s ");
    else if(map[i]==4)
      printf(" 〓 ");
    else if(map[i]==10)
      printf(" a. ");
    else if(map[i]==20)
      printf(" B ");
    else if(map[i]==30)
      printf("@@");
  }
  printf("\n");
  i=66;
  for(j=0; j<3; j++)
  {
    if(map[i]==0)
      printf(" ∷ ");
    else if(map[i]==1)
      printf(" because ");
    else if(map[i]==2)
      printf(" u ");
    else if(map[i]==3)
      printf(" s ");
    else if(map[i]==4)
      printf(" 〓 ");
    else if(map[i]==10)
      printf(" a. ");
    else if(map[i]==20)
      printf(" B ");
    else if(map[i]==30)
      printf("@@");
    i++;
    printf("\n");
  }
  for(i=69; i<100; i++)
  {
    if(map[i]==0)
      printf(" ∷ ");
    else if(map[i]==1)
      printf(" because ");
    else if(map[i]==2)
      printf(" u ");
    else if(map[i]==3)
      printf(" s ");
    else if(map[i]==4)
      printf(" 〓 ");
    else if(map[i]==10)
      printf(" a. ");
    else if(map[i]==20)
      printf(" B ");
    else if(map[i]==30)
      printf("@@");
  }
  printf("\n");
}
void cleana(int map[]) // Clear the marks on the map A And restore the map 
{
  int i;
  for(i=0; i<100; i++)
  {
    if(map[i]==10)
    {
      if(i==6||i==23||i==40||i==55||i==69||i==83) // 'because ' Location on the corresponding map 
        map[i]=1;
      else if(i==5||i==13||i==17||i==33||i==38||i==50||i==64||i==80||i==94) // Painted ' ' Location on the corresponding map 
        map[i]=2;
      else if(i==9||i==27||i==60||i==93)  // 's ' Location on the corresponding map 
        map[i]=3;
      else if(i==20||i==25||i==45||i==63||i==72||i==88||i==90)  // ' 〓  ' Location on the corresponding map 
        map[i]=4;
      else
        map[i]=0;
    }
  }
}
void cleanb(int map[])  // Clear the marks on the map B And restore the map 
{
  int i;
  for(i=0; i<100; i++)
  {
    if(map[i]==20)
    {
      if(i==6||i==23||i==40||i==55||i==69||i==83)  // 'because ' Location on the corresponding map 
        map[i]=1;
      else if(i==5||i==13||i==17||i==33||i==38||i==50||i==64||i==80||i==94) // Painted ' ' Location on the corresponding map 
        map[i]=2;
      else if(i==9||i==27||i==60||i==93)  // 's ' Location on the corresponding map 
        map[i]=3;
      else if(i==20||i==25||i==45||i==63||i==72||i==88||i==90)  // ' 〓  ' Location on the corresponding map 
        map[i]=4;
      else
        map[i]=0;
    }
  }
}
void showprocess(int map[]) // The process of the game 
{
  int flag1=2,flag2=2; // The marker variable that controls game pauses 
  numstep1=0;  // The player 1 Initial position 
  numstep2=0;  // The player 2 Initial position 
  int numtou;  // Number of dice rolled per roll 
  int t;    // As an intermediate variable when the lucky compass switches positions 
  int number;  // Variable as input to lucky compass selection 
  system("cls"); // Clear the screen 
  showmap(map);  // Call a function showmap(map) To output maps 
  printf("\n");
  while(numstep1<100&&numstep2<100) // The game start 
  {
    system("pause"); // "Press any key to continue  . .
    printf("\n");
    cleana(map);   // Clear the marks on the map A
    cleanb(map);   // Clear the marks on the map B
    if(flag1==2)   // Determine if it is paused 
    {
      if(flag2==0||flag2==1)
      {
        flag2++;
      }
      srand(time(NULL));  // Is the seed that sets the random number with the current time as the seed of the random number 
      numtou=rand()%6+1;  // Random number generation 
      numstep1+=numtou;
      Sleep(500);     // suspended 0.5 seconds 
      printf("\n The player 1 The number of rolls is zero  %d\n\n",numtou);
      if(numstep1>=100)  // Count is greater than 100 Break out of the loop. Game over 
      {
        map[99]=10;   // Make the map last 1 A position as A
        Sleep(1000);   // suspended 1 seconds 
        system("cls");  // Clear the screen 
        showmap(map);  // Call a function showmap(map) To output maps 
        printf("\n\n");
        printf(" Game over! \n");
        break;
      }
      else
      {
        printf(" The player 1%s, Your current position is  %d\n",people[number1-1].name,numstep1);
        Sleep(800);
        if(map[numstep1-1]==0)   // On the map, is ' ∷ '
        {
          map[numstep1-1]=10;
          if(numstep1==numstep2) // Judgment of the player 1 , the player 2 Is it in the same position 
          {
            printf("\n The player 2%s Get blown up !\n",people[number2-1].name);
            numstep2=0;
            printf(" The player 2%s, Your current position is %d\n",people[number2-1].name,numstep2);
          }
          if(numstep2!=0)
            map[numstep2-1]=20;
          Sleep(1000);   // suspended 1 seconds 
          system("cls");  // Clear the screen 
          showmap(map);  // Print the map 
          printf("\n");
        }
        else if(map[numstep1-1]==1)  // On the map, is ' because ' Fortune wheel 
        {
          printf("\n The player 1%s, Congratulations on your lucky compass! \n",people[number1-1].name);
          printf(" Please make a choice :\n");
          printf("1. Switch places with each other \n2. Bombing of the other party ( Fried back 6 step )\n");
          scanf("%d",&number); // Input selector 
          if(number==1)     // Switch player positions 
          {
            t=numstep1;
            numstep1=numstep2;
            numstep2=t;
            printf(" The player 1%s, Your current position is %d\n",people[number1-1].name,numstep1);
            printf("\n The player 2%s , your current position is %d\n",people[number2-1].name,numstep2);
            if(numstep1==0)
            {
              map[numstep2-1]=20;
            }
            else
            {
              map[numstep1-1]=10;
              map[numstep2-1]=20;
            }
            if(numstep1==numstep2&&numstep1!=0)
            {
              map[numstep1-1]=30;
            }
            Sleep(1800);  // suspended 1.8 seconds 
            system("cls"); // Clear the screen 
            showmap(map);  // Print the map 
            printf("\n");
          }
          else if(number==2)  // Each other back 6 step 
          {
            map[numstep1-1]=10;
            if(numstep2>=6)
            {
              numstep2-=6;
            }
            else numstep2=0;
            printf(" The player 2%s, Your current position is %d\n",people[number2-1].name,numstep2);
            if(numstep2!=0)
            {
              map[numstep2-1]=20;
            }
            if(numstep1==numstep2&&numstep1!=0)
            {
              map[numstep1-1]=30;
            }
            Sleep(1800);   // suspended 1.8 seconds 
            system("cls");  // Clear the screen 
            showmap(map);   // Print the map 
            printf("\n");
          }
        }
        else if(map[numstep1-1]==2)  // On the map, is ' u ' And mine 
        {
          printf("\nSORRY ,  You stepped on a mine   Going back 6 Step low � low \n");
          if(numstep1>=6)
            numstep1-=6;
          else numstep1=0;
          printf("\n The player 1%s, Your current position is %d\n",people[number1-1].name,numstep1);
          if(numstep1==0&&numstep2!=0)
          {
            map[numstep2-1]=20;
          }
          else if(numstep1!=0&&numstep2==0)
          {
            map[numstep1-1]=10;
          }
          else if(numstep1!=0&&numstep2!=0)
          {
            map[numstep1-1]=10;
            map[numstep2-1]=20;
          }
          if(numstep1==numstep2&&numstep1!=0)
          {
            map[numstep1-1]=30;
          }
          Sleep(1800);   // suspended 1.8 seconds 
          system("cls");  // Clear the screen 
          showmap(map);  // Print the map 
          printf("\n");
        }
        else if(map[numstep1-1]==3)   // On the map, is ' s ' , to suspend 1 time 
        {
          flag1=0;
          printf("\n~~>_<~~  To a truce 1 Bureau! \n");
          map[numstep1-1]=10;
          if(numstep2!=0)
          {
            map[numstep2-1]=20;
          }
          if(numstep1==numstep2&&numstep1!=0)
          {
            map[numstep1-1]=30;
          }
          Sleep(1800);   // suspended 1.8 seconds 
          system("cls");  // Clear the screen 
          showmap(map);   // Print the map 
          printf("\n");
        }
        else if(map[numstep1-1]==4)   // On the map, is ' 〓 ' , time tunnel 
        {
          printf("\nOh My God , It's a time warp!!   ! ^_^\n");
          numstep1+=10;
          if(numstep1>=100)
          {
            map[99]=10;
            Sleep(1000);
            system("cls");
            showmap(map);
            printf("\n\n");
            printf(" Game over! \n");
            break;
          }
          printf("\n The player 1%s, Your current position is %d\n",people[number1-1].name,numstep1);
          map[numstep1-1]=10;
          if(numstep2!=0)
          {
            map[numstep2-1]=20;
          }
          if(numstep1==numstep2&&numstep1!=0)
          {
            map[numstep1-1]=30;
          }
          Sleep(1800);   // suspended 1.8 seconds 
          system("cls");  // Clear the screen 
          showmap(map);   // Print the map 
          printf("\n");
        }
      }
    }
    else if(flag1!=2)  // When a player 1 Is suspended state 
    {
      flag1++;
    }
    system("pause");    // According to " Please press any key to continue ....."
    printf("\n");
    cleana(map);      // Clear the marks on the map A
    cleanb(map);      // Clear the marks on the map B
    if(flag2==2)    // Judgment of the player 2 Is it in a suspended state 
    {
      if(flag1==0||flag1==1)
      {
        flag1++;
      }
      srand(time(NULL));     // Is the seed that sets the random number with the current time as the seed of the random number 
      numtou=rand()%6+1;     // Random number generation 
      numstep2+=numtou;
      Sleep(500);         // suspended 0.5 seconds 
      printf("\n The player 2 The number of rolls is zero %d\n\n",numtou);
      if(numstep2>=100)      // Count is greater than 100 Break out of the loop. Game over 
      {
        map[99]=20;       // Make the map last 1 A position as B
        Sleep(1000);      // suspended 1 seconds 
        system("cls");     // Clear the screen 
        showmap(map);      // Print the map 
        printf("\n\n");
        printf(" Game over! \n");
        break;
      }
      else
      {
        printf(" The player 2%s, Your current position is %d\n",people[number2-1].name,numstep2);
        Sleep(1000);       // suspended 1 seconds 
        if(map[numstep2-1]==0)  // On the map, is ' ∷ '
        {
          map[numstep2-1]=20;
          if(numstep1==numstep2)
          {
            printf("\n The player 1%s Get blown up !\n",people[number1-1].name);
            numstep1=0;
            printf(" The player 1%s, Your current position is %d\n",people[number1-1].name,numstep1);
          }
          if(numstep1!=0)
            map[numstep1-1]=10;
          Sleep(1000);
          system("cls");
          showmap(map);
          printf("\n");
        }
        else if(map[numstep2-1]==1)  // The location on the map is ' because ' Fortune wheel 
        {
          printf("\n The player 2%s, Congratulations on your lucky compass! \n",people[number2-1].name);
          printf(" Please make a choice :\n");
          printf("1. Switch places with each other \n2. Bombing of the other party ( Fried back 6 step )\n");
          scanf("%d",&number);
          if(number==1) // Players swap places 
          {
            t=numstep1;
            numstep1=numstep2;
            numstep2=t;
            printf("\n The player 1%s, Your current position is %d\n",people[number1-1].name,numstep1);
            printf("\n The player 2%s , your current position is %d\n",people[number2-1].name,numstep2);
            if(numstep2==0)
            {
              map[numstep1-1]=10;
            }
            else
            {
              map[numstep1-1]=10;
              map[numstep2-1]=20;
            }
            if(numstep1==numstep2&&numstep1!=0)
            {
              map[numstep1-1]=30;
            }
            Sleep(1800);
            system("cls");
            showmap(map);
            printf("\n");
          }
          else if(number==2)  // Each other back 6 step 
          {
            map[numstep2-1]=20;
            if(numstep1>=6)
            {
              numstep1-=6;
            }
            else numstep1=0;
            printf(" The player 1%s, Your current position is %d\n",people[number1-1].name,numstep1);
            if(numstep1!=0)
            {
              map[numstep1-1]=10;
            }
            if(numstep1==numstep2&&numstep1!=0)
            {
              map[numstep1-1]=30;
            }
            Sleep(1800);
            system("cls");
            showmap(map);
            printf("\n");
          }
        }
        else if(map[numstep2-1]==2)  // The location on the map is ' u ', mines 
        {
          printf("\nSORRY ,  You stepped on a mine   Going back 6 Step low � low \n");
          if(numstep2>=6)
            numstep2-=6;
          else numstep2=0;
          printf("\n The player 2%s, Your current position is %d\n",people[number2-1].name,numstep2);
          if(numstep2==0&&numstep1!=0)
          {
            map[numstep1-1]=10;
          }
          else if(numstep2!=0&&numstep1==0)
          {
            map[numstep2-1]=20;
          }
          else if(numstep1!=0&&numstep2!=0)
          {
            map[numstep1-1]=10;
            map[numstep2-1]=20;
          }
          if(numstep1==numstep2&&numstep1!=0)
          {
            map[numstep1-1]=30;
          }
          Sleep(1800);
          system("cls");
          showmap(map);
          printf("\n");
        }
        else if(map[numstep2-1]==3)  // On the map, is ' s ' , to suspend 1 time 
        {
          flag2=0;
          printf("\n~~>_<~~  To a truce 1 Bureau of the \n");
          map[numstep2-1]=20;
          if(numstep1!=0)
          {
            map[numstep1-1]=10;
          }
          if(numstep1==numstep2&&numstep1!=0)
          {
            map[numstep1-1]=30;
          }
          Sleep(1800);
          system("cls");
          showmap(map);
          printf("\n");
        }
        else if(map[numstep2-1]==4)     // On the map, is ' 〓 ' , time tunnel 
        {
          printf("\nOh My God , It's a time warp!!   ! ^_^\n");
          numstep2+=10;
          if(numstep1>=100)  // Count is greater than 100 Jump out of the loop 
          {
            map[99]=10;
            Sleep(1000);
            system("cls");
            showmap(map);
            printf("\n\n");
            printf(" Game over! \n");
            break;
          }
          printf("\n The player 2%s, Your current position is %d\n",people[number2-1].name,numstep2);
          map[numstep2-1]=20;
          if(numstep1!=0)
          {
            map[numstep1-1]=10;
          }
          if(numstep1==numstep2&&numstep1!=0)
          {
            map[numstep1-1]=30;
          }
          Sleep(1800);
          system("cls");
          showmap(map);
          printf("\n");
        }
      }
    }
    else if(flag2!=0)
    {
      flag2++;
    }
  }
  if(numstep1>numstep2) // Judge whether a player wins or loses 
    printf("\n Congratulations to the player 1%s You win! !!!\n",people[number1-1].name);
  else printf("\n Congratulations to the player 2%s You win! !!!\n",people[number2-1].name);
}
void showready()
{
  int i;
  printf(" Map loading - ");
  for(i=0; i<15; i++)
  {
    printf(".");
    Sleep(100);
  }
  system("cls");
  showmap(map);
  printf("\n\n");
  printf(" The player 1%s, Your current position is  0\n",people[number1-1].name);
  printf(" The player 2%s, Your current position is  0\n\n",people[number2-1].name);
  system("pause");
  printf("\n Game on! \n Please the player 1 Start rolling the dice \n");
  Sleep(1000);
  showprocess(map);
}
void showstart()  // Show the game start screen 
{
  int i;
  int choose;
  system("color 71");
  printf("**************************************************\n");
  printf("//                       //\n");
  printf("//                       //\n");
  printf("//         Riding a   "   fly   line   chess          //\n");
  printf("//                       //\n");
  printf("//                       //\n");
  printf("**************************************************\n");
  for(i=0; i<5; i++)
  {
    printf("\n");
  }
  printf("~~~~~~~~~~~ two   people   right   war ~~~~~~~~~~~\n\n");
  printf(" Please select roles: ");
  strcpy(people[0].name," Charles DE gaulle ");
  strcpy(people[1].name," Eisenhower ");
  strcpy(people[2].name," MacArthur ");
  strcpy(people[3].name," patton ");
  for(i=1; i<=4; i++)
  {
    printf("%d.%s ",i,people[i-1].name);
  }
  printf("\n");
  printf(" Please the player 1 Select role (select serial number) : ");
  scanf("%d",&number1);
  printf(" Please the player 2 Select role (select serial number) : ");
  scanf("%d",&number2);
  printf("\n\n");
  printf("1. Just play the game   2. Read the rules of the game \n");
  scanf("%d",&choose);
  if(choose==1)
  {
    showready();
  }
  else if(choose==2)  // Show the rules of the game 
  {
    system("cls");
    printf("\n~~~~~~~~~~~~~~~~~~~ The rules are as follows ~~~~~~~~~~~~~~~~~~~~\n\n");
    printf("1. Two players take turns rolling the dice, and if the previous round reaches the pause level, stop rolling 1 time \n\n");
    printf("2. If the player walks to the wheel of fortune, they switch positions or move back 6 step \n\n");
    printf("3. If a player goes to a certain frame and the other player is in that frame, the other player goes back to the origin \n\n");
    printf("4. Retreat in case of a mine 6 step \n\n");
    printf("5. If a pause is encountered, this player will play 1 Turn stops rolling the dice \n\n");
    printf("6. If you encounter a time tunnel, then go forward 10 step \n\n");
    printf(" Note: the levels on the chessboard are for cavalry only 1 Submove is valid when encountered \n\n");
    Sleep(1800);
    system("pause");
    printf("\n");
    showready();
  }
}
int main()
{
  char str[10];
  showstart();
  printf("\n Whether or not to 1 Bureau? Please select:  (Yes/No)\n");  // Decide whether to come back 1 Bureau of 
  scanf("%s",str);
  if(strcmp(str,"Yes")==0)
  {
    system("cls");
    cleana(map);
    cleanb(map);
    showstart();
  }
  if(strcmp(str,"No")==0)
    return 0;
}

Related articles: