欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

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

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

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

思路分析:

小人及箱子的移動就是小人或者箱子和路的交換;

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

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

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

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

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

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

   3). 刷新地圖

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

下面是實現(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);
  //記錄小人及箱子位置
  //定義路的標志變量street
  int personX = 2,personY = 1,boxX = 2, boxY = 2;
  char street = ' ';
  //提示用戶輸入移動方向
  printf("請輸入小人移動方向:(w-上,s-下,a-左,d-右)\n");
  //定義記錄用戶輸入的方向
  char direction;
  //根據(jù)方向定義循環(huán)語句
  while(1)
  {
    scanf("%c",&direction);
    getchar();//接收鍵盤回車符
    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é)束標志
    if (boxY == 9) {
      printf("恭喜你,游戲過關(guān)!\n");
      return 0;
    }
  }
 
  return 0;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持我們。

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有