切换风格
开启辅助访问 切换到窄版

[提问] exercise 8

[复制链接]
作者:xwh 
版块:
Discuz!The Future 编程 c语言 发布时间:2018-11-19 23:01:29
19821
xwh 发表于 2018-11-19 22:48:08 | 显示全部楼层 |阅读模式
本帖最后由 thefuture 于 2018-11-19 22:50 编辑

Back in the good old days, there were no fancy graphics in computer games; we used text to represent everything. Your task is to draw a room from a family of games called Roguelikes -- the player (represented by the @ symbol) must explore a dungeon.

2x2 room, player at 0,1:  +--+  
|..|  
|@.|
+--+   
5x3 room, player at 1,2:  
+-----+  
|.....|  
|.....|  
|.@...|  
+-----+   
14x8 room, player at 8,5:  
+--------------+  
|..............|  
|..............|  
|..............|  
|..............|  
|..............|  
|........@.....|  
|..............|  
|..............|  
+--------------+  

Your task is to write a function (with extra "helper" functions) to draw such a room.
• How big are the rooms? What is the coordinate system?
• Your int main() must contain only:
  1. int main() {   
  2. drawRoom(2,2,0,1);      
  3. drawRoom(5,3,1,2);   
  4. drawRoom(14,8,8,5);
  5. getchar();     
  6. return 0;  }  


复制代码

• In addition to drawRoom(), write three extra functions. The first draw a horizontal +---+ line, the second draws a line without the player |....|, and the third draws the line with the player |..@.|. The drawRoom function should call those other helper functions when appropriate.  (examples of the three extra functions are not drawn to scale) • Use for loops. You may not use if or while in this exercise.

(optional: combine this exercise with keyboard input -- let the player move around in the room, bump into walls, etc. Ask the user to turn on the numlock key and to press enter after every move, then you can read his moves by reading int from the keyboard. Use a while loop for this movement.)


thefuture 发表于 2018-11-19 23:01:29 | 显示全部楼层
本帖最后由 thefuture 于 2018-11-19 23:23 编辑

解题思路:这题我们需要使用for循环语句来完成题目,并且由于题目规定不能使用if和while所以在书写程序时要注意避免使用if和while
本题要求我们打印一个棋盘,既然这样我们就可以用循环来打印每一行的每一个字符
简单的循环示例代码:

  1. #include <stdio.h>

  2. int main(){
  3. int i,e,b;

  4. //打印第一行,也就是框架
  5. printf("+--+\n");
  6. //打印中间的部分,包括|和.
  7. for(e=0;e<2;e++){//此处循环控制中间部分的行数,e<2代表中间有两行,e<2可根据棋盘所需大小修改为e<3(三行)或4(四行)或5(五行)等等
  8. printf("|");
  9. for (b=0;b<2;b++){//此处循环控制中间每一行的字符数,e<2代表每一行有2个.,b<2可根据棋盘所需大小修改为b<3(3个.)或4(4个.)或5(5个.)等等
  10. printf(".");
  11. }
  12. printf("|\n");}
  13. //打印最后一行,也就是框架
  14. printf("+--+");//仅作简单示例,程序真正实现时需要使用循环来输出最后一句
  15. }
复制代码

输出结果
+--+
|..|
|..|
+--+
以上代码仅作思路参考,想要真正完成此题还需稍加改进,包括循环的次数即修改e<2,b<2等部分,还需注意的是,题目要求加入@,所以循环到需要加@的那一行,则需要使用另外的循环语句,这里给出示范代码:
//输出@的一行的示例代码
  1. #include <stdio.h>

  2. int main()
  3. {
  4.         int b;
  5.   printf("|");
  6. for (b=0;b<1;b++){
  7. printf(".");
  8. }
  9. printf("@");
  10. for (b=2;b<3;b++){
  11. printf(".");
  12. }
  13. printf("|");
  14. }
复制代码


以上代码输出结果:
|.@.|
可参考进行@符号的输出
最后提醒一点,题目要求使用三个额外的函数,建议可以将输出@写成一个函数,输出头部的“+------+"写成一个函数,输出中间的”|......|"写成一个函数然后调用,这样会方便许多!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表