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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/233062.html
標籤:其他
上一篇:一文讓你徹底了解Redis基礎,史上最全【建議新手收藏】
下一篇:獲取小程式原始碼總結
