📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
楼主: yiyiyaya
跳转到指定楼层
上一主题 下一主题
收起左侧

Stanford CS106A 1#Assignment1讨论

🔗
melissami 2012-10-2 11:58:47 | 只看该作者
全局:
CollectNewspaperKarel
  1. /*
  2. * File: CollectNewspaperKarel.java
  3. * --------------------------------
  4. * At present, the CollectNewspaperKarel subclass does nothing.
  5. * Your job in the assignment is to add the necessary code to
  6. * instruct Karel to walk to the door of its house, pick up the
  7. * newspaper (represented by a beeper, of course), and then return
  8. * to its initial position in the upper left corner of the house.
  9. */

  10. import stanford.karel.*;

  11. /*
  12. * Name:
  13. * Section Leader:
  14. */

  15. public class CollectNewspaperKarel extends SuperKarel {

  16.         public void run() {
  17.                 move();
  18.                 move();
  19.                 turnLeft();
  20.                 turnLeft();
  21.                 turnLeft();
  22.                 move();
  23.                 turnLeft();
  24.                 move();
  25.                 pickBeeper();
  26.                 goback();       
  27.         }
  28.         private void goback(){
  29.                 turnLeft();
  30.                 turnLeft();
  31.                 move();
  32.                 move();
  33.                 move();
  34.                 turnLeft();
  35.                 turnLeft();
  36.                 turnLeft();
  37.                 move();
  38.                
  39.         }

  40. }



复制代码



StoneMasonKarel
  1.                     
  2.                     /*
  3.                      * File: StoneMasonKarel.java
  4.                      * --------------------------
  5.                      * The StoneMasonKarel subclass as it appears here does nothing.
  6.                      * When you finish writing it, it should solve the "repair the quad"
  7.                      * problem from Assignment 1.  In addition to editing the program,
  8.                      * you should be sure to edit this comment so that it no longer
  9.                      * indicates that the program does nothing.
  10.                      */

  11.                     import stanford.karel.*;

  12.                     /*
  13.                      * Name:
  14.                      * Section Leader:
  15.                      */

  16.                     public class StoneMasonKarel extends SuperKarel {

  17.                             public void run() {
  18.                                    
  19.                                     while (frontIsClear()){
  20.                                     distributeBeeper();
  21.                                     goBack();
  22.                                     move();
  23.                                     move();
  24.                                     move();
  25.                                     move();}
  26.                                     distributeBeeper();
  27.                                     goBack();
  28.                             }
  29.                                    
  30.                             private void distributeBeeper(){
  31.                                     turnLeft();
  32.                                     while (frontIsClear()){
  33.                                     if(noBeepersPresent())putBeeper();
  34.                                 move();
  35.                                             }      
  36.                                             if(noBeepersPresent()) putBeeper();
  37.                             }
  38.                                                      
  39.                            
  40.                            
  41.                             private void goBack(){
  42.                                     turnLeft();
  43.                                     turnLeft();
  44.                                     move();
  45.                                     move();
  46.                                     move();
  47.                                     move();
  48.                                     turnLeft();
  49.                             }
  50.                                    
  51.                     }

复制代码


回复

使用道具 举报

🔗
melissami 2012-10-2 12:11:35 | 只看该作者
全局:
本帖最后由 melissami 于 2012-10-2 17:53 编辑

CheckerboardKarel
  1. /*
  2. * File: CheckerboardKarel.java
  3. * ----------------------------
  4. * When you finish writing it, the CheckerboardKarel class should draw
  5. * a checkerboard using beepers, as described in Assignment 1.  You
  6. * should make sure that your program works for all of the sample
  7. * worlds supplied in the starter folder.
  8. */

  9. import stanford.karel.*;

  10. /*
  11. * Name:
  12. * Section Leader:
  13. */

  14. public class CheckerboardKarel extends SuperKarel {

  15.         public void run() {
  16.                 while (leftIsClear()) {
  17.                 oddRow();
  18.                 turnLeft();
  19.                 move();
  20.                 turnLeft();
  21.                
  22.                
  23.                
  24.                 oddRow();
  25.                 turnRight();
  26.                 move();
  27.                 turnRight();
  28.                
  29.                 }
  30.                 }
  31.                         
  32.                
  33.                
  34.                 private void oddRow(){
  35.                         putBeeper();
  36.                         move();
  37.                         while (frontIsClear()){
  38.                                 move();
  39.                                 putBeeper();
  40.                                 move();
  41.                         }
  42.                 }
  43.                

  44.                
  45. }
复制代码
虽然完成了题目中8*8的棋牌,但是到最后居然被block了
而且奇数行不适用……哎



能否问问大家都是看了视频就做assignment的吗?有没有看老师推荐的教材和网站上所有的handout?总觉得做起来很吃力,而且lecture里面似乎是挑了重点讲,还有一些只是点没有涉及到。


才assignment1就这么困难,除了第一个小程序,似乎剩下每一个都超过三个小时了

第四题实在实现不了,参照前面几个人的完成了,就不贴代码了
回复

使用道具 举报

🔗
酸奶红茶 2012-11-11 10:33:07 | 只看该作者
全局:
yiyiyaya 发表于 2012-7-25 10:13
下面是CheckerboardKarel.java的程序:

这个好像只有当偶数行的时候才能运行出来相应的结果,如果Karel的世界是1*8或者是7*7的就不能运行出来对的结果了。。。
回复

使用道具 举报

🔗
咪崽 2012-11-21 06:28:15 | 只看该作者
全局:
这是A1-2题的代码... 求大神帮忙看看是不是general的...

/*
* File: StoneMasonKarel.java
* --------------------------
* The StoneMasonKarel subclass as it appears here does nothing.
* When you finish writing it, it should solve the "repair the quad"
* problem from Assignment 1.  In addition to editing the program,
* you should be sure to edit this comment so that it no longer
* indicates that the program does nothing.
*/

import stanford.karel.*;

public class StoneMasonKarel extends SuperKarel {

        public void run() {
                while(frontIsClear()) {
                turnLeft();
                fixOneColumn();
                moveToAnotherColumn();
                turnRight();
                }
                turnLeft();
                fixOneColumn();
                turnRight();
        }
        
        private void fixOneColumn() {
                while(frontIsClear()){
                        if(noBeepersPresent()){
                                putBeeper();
                        }
                        move();
                }
                if(noBeepersPresent()) {
                        putBeeper();
                }
                turnAround();
                while(frontIsClear()) {
                        move();
                }
                turnAround();
        }
        
        private void moveToAnotherColumn() {
                turnRight();
                for(int i=0; i < 4; i++) {
                        move();
                }
                turnLeft();
        }
}
回复

使用道具 举报

🔗
咪崽 2012-11-21 06:31:23 | 只看该作者
全局:
cooooolboy 发表于 2012-9-20 18:38
刚开始学,先贴第二个的代码吧,第三个还在纠结。
import stanford.karel.*;

我觉得要是把
                        fixIt();
                        while(frontIsClear()){
                                move();
                                fixIt();                                
                        }

这段换成一个 比如说 fixTheLine(){ }

会易读一些

评分

参与人数 1大米 +10 收起 理由
cooooolboy + 10 好久不看都不认识了,哈哈,多谢!

查看全部评分

回复

使用道具 举报

🔗
Irvin 2013-2-5 17:23:55 | 只看该作者
全局:
版主,偶问个小白问题啊,怎么导入assignment1呢,我点了import,不是先出现让我选择路径,而是先Choose import source.有6种

6种下面各有分支,我都试试了,失败了。
如果我选择open file 然后路径找到assignment 1文件夹中的karel,是出现失败的提示

我在安装eclipse的时候就安装了JDK,设置了那个什么环境。
现在正看到第2课完,
在eclipse里面不知道怎么找出来那个会跑的karel。
麻烦版主花几秒钟回答下偶的问题,偶小白,花了好久都没搞懂。对版主感激不尽啊
回复

使用道具 举报

🔗
asterid 2013-4-10 10:04:45 | 只看该作者
全局:
Irvin 发表于 2013-2-5 04:23
版主,偶问个小白问题啊,怎么导入assignment1呢,我点了import,不是先出现让我选择路径,而是先Choose im ...

你选的是“Import”,应该选择工具栏上的“Import Project”,就可以了。
回复

使用道具 举报

🔗
Irvin 2013-4-10 14:22:19 | 只看该作者
全局:
asterid 发表于 2013-4-10 10:04
你选的是“Import”,应该选择工具栏上的“Import Project”,就可以了。

亲,谢谢你,我找到了,import project是standford上面那个版本的eclipse才有,在standford的下拉菜单里面。我安装的是官网下载的eclipse,没有standford下拉菜单,不过在import里面试了很多遍也找到了怎么import了。总之谢谢你。不知道亲,你学到哪里了
回复

使用道具 举报

🔗
asterid 2013-4-11 00:06:07 | 只看该作者
全局:
Irvin 发表于 2013-4-10 01:22
亲,谢谢你,我找到了,import project是standford上面那个版本的eclipse才有,在standford的下拉菜单里面 ...

我上个礼拜刚刚开始上这门,才做完第一个assignment。
之前主攻C++和Python,为了刷CtCi,想补一下JAVA。
回复

使用道具 举报

🔗
Irvin 2013-4-18 08:56:36 | 只看该作者
全局:
asterid 发表于 2013-4-11 00:06
我上个礼拜刚刚开始上这门,才做完第一个assignment。
之前主攻C++和Python,为了刷CtCi,想补一下JAVA。 ...

哦,我刚在CS入门中,看来路子还很长
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

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