【中山大学20级计算机类第十五周作业题】【模拟】[Algorithm]Simple Game ---- Escape From the Hell

Simple Game ---- Escape From the Hell

前言:在做作业的时候觉得这题蛮有意思的,所以po上来供大家学习。
题目描述
Let’s play a game. The description may be very long so I don’t want to pretend B.

有一位大熊弟因为秀恩爱而遭到FFF团的制裁——被扔进了下界,而你,正以上帝视角暗中观察这一切。(但你只能观察)
观察到的地图如下

细节
遇到地形时的行为:

*边界
不会阻挡大熊弟(下面简称A)的移动,当遇到边界时,会从其对面边界穿出
如上图情况,若A向左走,下一地图应该如下

*********
*   $   *
*  #    *
*#   #  *
*       *
*A      *
*   @   *
*@    $ *
*********

A --- 大熊弟
* --- 边界
# --- 墙
@ --- 水
$ --- 火

#墙
会阻挡A的移动,假设A上方有#,而又尝试向上移动,则A的位置不变,输出原地图
@水
当A移动到@,其失去一次移动机会(即下一次移动尝试不会移动成功)
输出时,在此处输出A;但A离开后,此处还会是@ (即重叠时输出A)
$ 火
当A移动到$,A死亡,不输出地图,输出Game Over!并结束程序

输入
第一行是整数n(6 <= n <= 12),表示地图中活动空间的大小(如上面的示例中n == 7)
第2~n+3行接下来是一张地图,表示地图初始状态
之后是一组尝试移动的方向(EOF结束)

a:向左
d:向右
w:向上
s:向下
输出

每次接收到尝试移动的指令后都输出当前状态的地图 或 Game Over!

样例输入

7
*********
*   $   *
*  #    *
*#   #  *
*       *
*A@     *
* $ @   *
*@    $ *
*********
a
d
w
w
s
d
d
s

样例输出

*********
*   $   *
*  #    *
*#   #  *
*       *
* @    A*
* $ @   *
*@    $ *
*********
*********
*   $   *
*  #    *
*#   #  *
*       *
*A@     *
* $ @   *
*@    $ *
*********
*********
*   $   *
*  #    *
*#   #  *
*A      *
* @     *
* $ @   *
*@    $ *
*********
*********
*   $   *
*  #    *
*#   #  *
*A      *
* @     *
* $ @   *
*@    $ *
*********
*********
*   $   *
*  #    *
*#   #  *
*       *
*A@     *
* $ @   *
*@    $ *
*********
*********
*   $   *
*  #    *
*#   #  *
*       *
* A     *
* $ @   *
*@    $ *
*********
*********
*   $   *
*  #    *
*#   #  *
*       *
* A     *
* $ @   *
*@    $ *
*********
Game Over!

题目给定头文件:utility.h

#ifndef UTILITY_H
#define UTILITY_H
#include <stdlib.h>

/*
 * read the map from stdin.
 * @param map_size  size of the map.
 */
void readMap(const size_t map_size);

/*
 * print the map while human is alive.
 * @param map_size  size of the map.
 */
void printMap(const size_t map_size);

/*
 * move the human in map
 * @param map_size  the size of the map
 * @param is_alive  the human is alive or not.
 *                  If the human die in this turn, set it 0.
 * @param is_stop   stop in this turn or not. If he stop in next turn, set it 1.
 */
void move(size_t map_size, int *is_alive, int *is_stop);

char map[15][15] = {};

// position of the human
int human_x = 0, human_y = 0;

#endif

题解:

#include"utility.h"
#include<stdio.h>
void readMap(const size_t map_size){
    getchar();
    for (int i=0; i<map_size+2; i++) {
        for (int j=0; j<map_size+2; j++) {
            map[i][j]=getchar();
            if (map[i][j]=='A') {
                human_y=i;
                human_x=j;
                map[i][j]=' ';
            }
        }
        getchar();
    }
}

void printMap(const size_t map_size){
    for (int i=0; i<map_size+2; i++) {
        for (int j=0; j<map_size+2; j++) {
            if (i==human_y&&j==human_x) {
                printf("A");
            }else{
                printf("%c",map[i][j]);
            }
        }
        printf("\n");
    }
}

void move1(size_t map_size, int *is_alive, int *is_stop){
    char move=0;
    move=getchar();
    if (*is_stop) {
        *is_stop=0;
        return;
    }
    switch (move) {
        case 'a':
            if(human_x==1){
                if(map[human_y][map_size]!='#'){
                    human_x=(int)map_size;
                }
            }else human_x--;
            if(map[human_y][human_x]=='@'){
                *is_stop=1;
            }else if(map[human_y][human_x]=='$'){
                *is_alive=0;
            }else if(map[human_y][human_x]=='#'){
                human_x++;
            }
            break;
        case 'd':
            if(human_x==map_size){
                if(map[human_y][1]!='#'){
                    human_x=1;
                }
            }else human_x++;
            if(map[human_y][human_x]=='@'){
                *is_stop=1;
            }else if(map[human_y][human_x]=='$'){
                *is_alive=0;
            }else if(map[human_y][human_x]=='#'){
                human_x--;
            }
            break;
        case 'w':
            if(human_y==1){
                if(map[map_size][human_x]!='#'){
                    human_y=(int)map_size;
                }
            }else human_y--;
            if(map[human_y][human_x]=='@'){
                *is_stop=1;
            }else if(map[human_y][human_x]=='$'){
                *is_alive=0;
            }else if(map[human_y][human_x]=='#'){
                human_y++;
            }
            break;
        case 's':
            if(human_y==map_size){
                if(map[1][human_x]!='#'){
                    human_y=1;
                }
            }else human_y++;
            if(map[human_y][human_x]=='@'){
                *is_stop=1;
            }else if(map[human_y][human_x]=='$'){
                *is_alive=0;
            }else if(map[human_y][human_x]=='#'){
                human_y--;
            }
            break;
        default:
            break;
    }
    return;
}


int main()
{
    int num=0;
    int alive=1,stop=0;
    scanf("%d",&num);
    readMap(num);
    while(1){
        move1(num, &alive, &stop);
        if(getchar()==EOF||!alive)break;
        printMap(num);
    }
    if(!alive)printf("Game Over!\n");
    return 0;
}

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值