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

Stanford CS106A 1#Assignment1讨论

🔗
JessicaVans 2013-7-29 13:34:06 | 只看该作者
全局:
昨天剛學,自己的 CollectNewspaperKarel.java
回复

使用道具 举报

🔗
JessicaVans 2013-7-29 13:35:32 | 只看该作者
全局:
昨天剛學,自己的 Assignment1# CollectNewspaperKarel.java
  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. /*
  11. * Name: Van.S  jessicawyf@163.com
  12. */
  13. import stanford.karel.*;

  14. public class CollectNewspaperKarel extends Karel {
  15.                
  16.     public void run() {  // order level 0.0
  17.             pickNewspaper();
  18.             backToInitial();
  19.     }
  20.    
  21.     private void pickNewspaper()  {  // order level 1.0     
  22.             while (frontIsClear()) move();
  23.         turnRight();
  24.         outTheDoor();
  25.             pickBeeper();
  26.     }
  27.    
  28.         private void outTheDoor() {  // order level 1.1
  29.                 while (leftIsBlocked()) move();   
  30.                 turnLeft();
  31.                 move();
  32.         }
  33.    
  34.     private void backToInitial() {  // order level 2.0
  35.             turnAround();
  36.             while (frontIsClear()) move();
  37.             turnRight();
  38.             while (frontIsClear()) move();
  39.             turnRight();
  40.     }
  41.         
  42.         private void turnAround() {  // order level 2.1
  43.                     turnLeft();
  44.                     turnLeft();
  45.             }
  46.          
  47.         private void turnRight() {  // order level 2.1
  48.                     turnLeft();
  49.                     turnLeft();
  50.                     turnLeft();
  51.             }
  52. }
复制代码
回复

使用道具 举报

🔗
JessicaVans 2013-7-29 13:39:26 | 只看该作者
全局:
  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. /*
  11. * Name: Van.S  jessicawyf@163.com
  12. */
  13. import stanford.karel.*;

  14. public class CollectNewspaperKarel extends Karel {
  15.                
  16.     public void run() {  // order level 0.0
  17.             pickNewspaper();
  18.             backToInitial();
  19.     }
  20.    
  21.     private void pickNewspaper()  {  // order level 1.0     
  22.             while (frontIsClear()) move();
  23.         turnRight();
  24.         outTheDoor();
  25.             pickBeeper();
  26.     }
  27.    
  28.         private void outTheDoor() {  // order level 1.1
  29.                 while (leftIsBlocked()) move();   
  30.                 turnLeft();
  31.                 move();
  32.         }
  33.    
  34.     private void backToInitial() {  // order level 2.0
  35.             turnAround();
  36.             while (frontIsClear()) move();
  37.             turnRight();
  38.             while (frontIsClear()) move();
  39.             turnRight();
  40.     }
  41.         
  42.         private void turnAround() {  // order level 2.1
  43.                     turnLeft();
  44.                     turnLeft();
  45.             }
  46.          
  47.         private void turnRight() {  // order level 2.1
  48.                     turnLeft();
  49.                     turnLeft();
  50.                     turnLeft();
  51.             }
  52. }
复制代码
回复

使用道具 举报

🔗
nibuxing 2013-8-29 09:26:43 | 只看该作者
全局:
/*
* File: CollectNewspaperKarel.java
* --------------------------------
* At present, the CollectNewspaperKarel subclass does nothing.
* Your job in the assignment is to add the necessary code to
* instruct Karel to walk to the door of its house, pick up the
* newspaper (represented by a beeper, of course), and then return
* to its initial position in the upper left corner of the house.
*/

import stanford.karel.*;

public class CollectNewspaperKarel extends SuperKarel {

        public void run(){
                moveToNewspaper();
                pickBeeper();
                returnToStartpoint();
        }
                private void moveToNewspaper(){
                        move();
                        move();
                        turnRight();
                        move();
                        turnLeft();
                        move();
                }
                private void returnToStartpoint(){
                        turnAround();
                        move();
                        turnRight();
                        move();
                        turnLeft();
                        move();
                        move();
                        turnAround();
                }
}
回复

使用道具 举报

🔗
nibuxing 2013-8-29 22:05:02 | 只看该作者
全局:
/*
* 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(){
                fixTheLine();
                while(frontIsClear()){
                        move();
                        move();
                        move();
                        move();
                        fixTheLine();
                }
        }
        private void fixTheLine(){
                goAction();
                turnAround();
                returnAction();
                turnLeft();
        }
        private void goAction(){
                turnLeft();
                while(frontIsClear()){
                        if(beepersPresent()){
                                move();
                        } else {
                                putBeeper();
                                move();
                        }
                }
        }
        private void returnAction(){
                while(frontIsClear()){
                        move();
                }
        }
}
回复

使用道具 举报

🔗
nibuxing 2013-9-3 07:25:35 | 只看该作者
全局:
/*
* File: CheckerboardKarel.java
* ----------------------------
* When you finish writing it, the CheckerboardKarel class should draw
* a checkerboard using beepers, as described in Assignment 1.  You
* should make sure that your program works for all of the sample
* worlds supplied in the starter folder.
*/

import stanford.karel.*;

public class CheckerboardKarel extends SuperKarel {

        public void run(){
                oneLineAction();
                while(leftIsClear()){
                        repositionToTheWest();
                        oneLineAction();
                                if(rightIsClear()){
                                        repositonToTheEast();
                                        oneLineAction();
                                } else {
                                        turnAround();
                                }
                }
        }
        private void oneLineAction(){
                putBeeper();
                while(frontIsClear()){
                        move();
                        if(frontIsClear()){
                                move();
                                putBeeper();
                        }       
                }
        }
        private void repositionToTheWest(){
                if(beepersPresent()){
                        facingNorth();
                        move();
                        turnLeft();
                        move();
                        putBeeper();
                } else {
                        facingNorth();
                        move();
                        turnLeft();
                        putBeeper();
                }
        }
        private void repositonToTheEast(){
                if(beepersPresent()){
                        facingNorth();
                        move();
                        turnRight();
                        move();
                        putBeeper();
                } else {
                        facingNorth();
                        move();
                        turnRight();
                        putBeeper();
                }
        }
}

点评

请阅读版规,不要大段粘贴代码  发表于 2013-9-3 08:13
回复

使用道具 举报

🔗
nibuxing 2013-9-3 10:54:16 | 只看该作者
全局:
nibuxing 发表于 2013-9-3 07:25
/*
* File: CheckerboardKarel.java
* ----------------------------

应该怎么样,给注释吗。
回复

使用道具 举报

🔗
jby1797 2013-12-19 16:29:17 | 只看该作者
全局:
wasabi_akira 发表于 2013-7-23 17:06
/*
* File: MidpointFindingKarel.java
* -------------------------------

我觉得你引入了i这个变量来解这道题算是开挂的。
因为老师明确说了,Karel是不懂得计数的,你这样就教会了Karel计数了

点评

表示同意。使用count算是一个很简洁的思路,但不符合题意。  发表于 2013-12-21 07:33
回复

使用道具 举报

🔗
jby1797 2013-12-19 16:35:35 | 只看该作者
全局:
这里相应号召,不贴大段代码,只提供思路
第一题,取报纸;这个方法应该是适合任意长短的房子的

课本里有一个走迷宫的题,个人感觉是一个方法,右手一直放在墙上,空了的那一个点,就是出口,转身,往前走,就取到报纸了。
然后turnAround(); move(); turnRight();右手一直放在墙上,前面挡住了就左转,当走到一个点,前面和右手都有墙的时候,左转,走到第二个点,前面和右手都有墙的时候,就是起点
回复

使用道具 举报

🔗
jby1797 2013-12-19 16:41:36 | 只看该作者
全局:
第二题,修房子



这一题和下一题,都是handout “09-karel-examples.pdf”里面的题目的延伸
如果只用学过的知识,不用&&,||,可以用while里面加if来实现

while(rightIsClear()){
                        fillTheLine();
                        moveFaceSouth();
                        if(leftIsClear()){
                                fillTheLine();
                                moveFaceNorth();
                        }
                        else{
                                turnAround();
                        }
fillTheLine()就是填充一行,只要前面没有墙,则填,碰到墙了就移动要用到faceingSouth();这类superKarel里的指令来实现
第三题其实是一样的。。就不贴了
回复

使用道具 举报

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

本版积分规则

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