溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C語(yǔ)言推箱子游戲?qū)崿F(xiàn)代碼

發(fā)布時(shí)間:2020-09-09 01:44:54 來(lái)源:腳本之家 閱讀:136 作者:zhudong10 欄目:編程語(yǔ)言

推箱子游戲的運(yùn)行規(guī)則:在街道上上小人推動(dòng)箱子移動(dòng),直到把箱子移動(dòng)到目的地。

思路分析:

小人及箱子的移動(dòng)就是小人或者箱子和路的交換;

1 定義二維字符數(shù)組,存儲(chǔ)地圖

2 顯示地圖,提示游戲玩法

3 記錄小人及箱子位置,并定義字符變量接收用戶輸入方向

4 循環(huán)判斷語(yǔ)句

   1).小人的下一步是否為路,如果為路,則移動(dòng)并記錄小人新位置信息

   2).小人的下一步如果不是路,在判斷是否為箱子,如果是箱子,在判斷箱子的下一個(gè)位置是否是路,如果是路,則移動(dòng)箱子和小人

   3). 刷新地圖

   4) .判斷箱子的位置,如果在指定位置,則游戲結(jié)束;

下面是實(shí)現(xiàn)代碼:

#include <stdio.h>
//交換字符數(shù)組元素
void swapPosition(char ch[][11],int oldX,int oldY,int newX,int newY)
{
 char temp;
 temp = *(*(ch + oldX) + oldY);
 *(*(ch + oldX) + oldY) = *(*(ch + newX) + newY);
 *(*(ch + newX) + newY) = temp;
 
}
//打印數(shù)組
void printArray(char (*ch)[11])
{
 for(int i = 0;i < 10;i++)
 {
  for(int j = 0;j < 10;j++)
  {
   printf("%c\t",*(*(ch + i) + j));
  }
  printf("\n");
 }
}
int main(int argc, char* argv[])
{
 //定義數(shù)組
 char ch[10][11] = {
  {'#','#','#','#','#','#','#','#','#','#','\0'},
  {'#','#','#',' ',' ','#','#','#','#','#','\0'},
  {'#','0','X',' ',' ','#','#','#','#','#','\0'},
  {'#','#','#','#',' ','#','#','#','#','#','\0'},
  {'#','#','#',' ',' ',' ',' ','#','#','#','\0'},
  {'#','#','#',' ',' ',' ',' ',' ','#','#','\0'},
  {'#','#','#','#','#',' ',' ',' ','#','#','\0'},
  {'#','#','#','#','#',' ',' ',' ',' ',' ','\0'},
  {'#','#','#','#','#','#','#','#',' ',' ','\0'},
  {'#','#','#','#','#','#','#','#','#','#','\0'}
 };
 //打印數(shù)組
 printArray(ch);
 //記錄小人及箱子位置
 //定義路的標(biāo)志變量street
 int personX = 2,personY = 1,boxX = 2, boxY = 2;
 char street = ' ';
 //提示用戶輸入移動(dòng)方向
 printf("請(qǐng)輸入小人移動(dòng)方向:(w-上,s-下,a-左,d-右)\n");
 //定義記錄用戶輸入的方向
 char direction;
 //根據(jù)方向定義循環(huán)語(yǔ)句
 while(1)
 {
  scanf("%c",&direction);
  getchar();//接收鍵盤(pán)回車符
  switch(direction)
  {
   case 'w':
   case 'W':
    if(*(*(ch+personX-1)+personY) == street)
    {
     swapPosition(ch,personX,personY,personX - 1,personY );
     personX--;
     
    }
    else if(*(*(ch+personX-1)+personY) == *(*(ch+boxX)+boxY))
    {
     if(*(*(ch+boxX-1)+boxY) == street)
     {
      swapPosition(ch,boxX,boxY,boxX - 1,boxY);
      boxX--;
      swapPosition(ch,personX,personY,personX - 1,personY);
      personX--;
     }
    }
  
    break;
   case 's':
   case 'S':
    if(*(*(ch+personX + 1) + personY) == street)
    {
     swapPosition(ch,personX,personY,personX + 1,personY );
     personX++;
     
    }
    else if(*(*(ch+personX+1)+personY) == *(*(ch+boxX)+boxY))
    {
     if(*(*(ch+boxX+1)+boxY) == street)
     {
      swapPosition(ch,boxX,boxY,boxX + 1,boxY);
      boxX++;
      swapPosition(ch,personX,personY,personX + 1,personY);
      personX++;
     }
    }
    break;
   case 'a':
   case 'A':
    if(*(*(ch+personX)+personY - 1) == street)
    {
     swapPosition(ch,personX,personY,personX ,personY - 1 );
     personY--;
     
    }
    else if(*(*(ch+personX)+personY - 1) == *(*(ch+boxX)+boxY))
    {
     if(*(*(ch+boxX)+boxY - 1) == street)
     {
      swapPosition(ch,boxX,boxY,boxX,boxY - 1);
      boxY--;
      swapPosition(ch,personX,personY,personX,personY - 1);
      personY--;
     }
    }
    break;
   case 'd':
   case 'D':
    
    if(*(*(ch+personX)+personY + 1) == street)
    {
     swapPosition(ch,personX,personY,personX ,personY + 1 );
     personY++;
     
    }
    else if(*(*(ch+personX)+personY + 1) == *(*(ch+boxX)+boxY))
    {
     if(*(*(ch+boxX)+boxY + 1) == street)
     {
      swapPosition(ch,boxX,boxY,boxX,boxY + 1);
      boxY++;
      swapPosition(ch,personX,personY,personX,personY + 1);
      personY++;
     }
    }
    break;
    case 'q':
    case 'Q':
    return 0;
   
  }
  printArray(ch);
  //定義結(jié)束標(biāo)志
  if (boxY == 9) {
   printf("恭喜你,游戲過(guò)關(guān)!\n");
   return 0;
  }
 }
 
 return 0;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI