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

Stanford CS106A 1#Assignment1讨论

🔗
 楼主| yiyiyaya 2012-7-26 22:29:09 | 只看该作者
全局:
能拜托大家一件事儿吗?首先,希望积极贴代码,方便互相学习。其次,我们这周在学习的一共有5个人,把作业完成的童鞋最好在这里吼一声,方便我安排后面的进度。最后,关于上课以及作业中不明白的点可以提出来,大家讨论。谢谢!
回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-7-26 22:31:42 | 只看该作者
全局:
nprotect同学的assignment1的作业我帮忙转到这里,希望nprotect不要介意
No1 CollectNewspaperKarel

  1. import stanford.karel.*;

  2. /*
  3. * Name: songy6@uci.edu
  4. * Section Leader:  ICS department
  5. */

  6. public class CollectNewspaperKarel extends SuperKarel {
  7.          
  8.         /*
  9.           * this is my first decomposition program of karel the robot
  10.           * public class include karel's basic movement methods
  11.           */
  12.          
  13.         public void run() {
  14.                  moveToNewspaper();
  15.                  pickBeeper();
  16.                  turnAround();
  17.                  returnToStartpoint();
  18.             }
  19.          
  20.         //this is move to newspaper's decomposition
  21.                  private void moveToNewspaper() {
  22.                          move();
  23.                          move();
  24.                          turnRight();
  25.                          move();
  26.                          turnLeft();
  27.                          move();
  28.             }
  29.                  
  30.         //this is return to startpoint's decomposition        
  31.                 private void returnToStartpoint() {
  32.                          move();
  33.                          turnRight();
  34.                          move();
  35.                          turnLeft();
  36.                          move();
  37.                          move();
  38.                          turnAround();
  39.             }
  40. }
复制代码

回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-7-26 22:32:59 | 只看该作者
全局:
同样是nprotect的代码,No.2: StoneMasonKarel

  1. import stanford.karel.*;

  2. /*
  3. * Name: songy6@uci.edu
  4. * Section Leader: ICS department
  5. */

  6. public class StoneMasonKarel extends SuperKarel {

  7.         //this is the basic idea for karel's action, decomposition shows later on
  8.          public void run() {
  9.                  oneLineAction();
  10.                  while(frontIsClear()) {
  11.                  move();
  12.                  move();
  13.                  move();
  14.                  move();
  15.                  oneLineAction();
  16.                  }
  17.          }
  18.          
  19.         //this is the basic idea for one line action for karel, decomposition shows later on
  20.                  private void oneLineAction() {               
  21.                    turnLeft();
  22.                     goAction();
  23.                     turnAround();
  24.                     returnAction();
  25.                     turnLeft();
  26.          }
  27.                  
  28.         //this is the basic idea for goAction        
  29.                 private void goAction() {
  30.                     while(frontIsClear()) {
  31.                     if(noBeepersPresent()) {
  32.                     putBeeper();
  33.                     }
  34.                     move();
  35.                     }
  36.                     if(noBeepersPresent()) {
  37.                             putBeeper();
  38.                     }
  39.                  }
  40.                  
  41.         //this is the basic idea for returenAction
  42.                  private void returnAction() {
  43.                          while(frontIsClear()) {
  44.                                  move();
  45.                          }
  46.                  }                  
  47. }
复制代码

回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-7-26 22:34:16 | 只看该作者
全局:
nprotect的代码,No.3: CheckerboardKarel

  1. import stanford.karel.*;

  2. /*
  3. * Name: songy6@uci.edu
  4. * Section Leader: ICS department
  5. */

  6. /*basic idea for karel to fill the world, decomposition shows later on*/
  7. public class CheckerboardKarel extends SuperKarel {
  8.         public void run() {
  9.                  oneLineAction();   //karel's first line action. pre-condition, post-condition both facing east
  10.                  while(frontIsClear()){         
  11.           if(beepersPresent()){
  12.                move();                    //2,4,6,8,10.... line action
  13.                turnLeft();
  14.                move();
  15.                turnRight();
  16.                oneLineAction();
  17.        }
  18.                else {                        
  19.                      move();                  //3,5,7,9,11.... line action
  20.                      oneLineAction();  
  21.                }
  22.        }
  23.          }

  24.         /*oneLineAction decomposition*/
  25.          private void oneLineAction() {
  26.                  turnLeft();
  27.                  goAction();                   //karel's one line go action
  28.                  turnAround();
  29.                  returnAction();               //karel's one line return action
  30.                  turnLeft();
  31.          }
  32.          
  33.         /*goAction decomposition, key idea for this question*/
  34.          private void goAction() {
  35.                  while(frontIsClear()) {       //while row high is odd number,"while" ends the circulation, it also cause off by one bug
  36.                      putBeeper();              //while circulation ends, try to go back one row and test to decide if put one beeper or not
  37.                      move();               
  38.                     if(frontIsClear()) {      //while row high is even,"if" ends the circulation
  39.                              move();
  40.                      }
  41.                  }
  42.                  
  43.                 turnAround();                 //go back one row and test
  44.                  if(frontIsClear()){
  45.                  move();
  46.                  if(noBeepersPresent()) {
  47.                          turnAround();
  48.                          move();
  49.                          putBeeper();
  50.                  }
  51.                  else {                        //此else对应if(noBeepersPresent())这个循环
  52.                              turnAround();
  53.                                  move();
  54.                           }
  55.                         
  56.                 }
  57.                  else {
  58.                          oneLineAction();          //此else对应if(frontIsClear())这个循环
  59.              }                             //加此句的目的是为了防止当只有一行的时候,退一格检测奇偶的方法会报错
  60. }                                     //注意oneLineAction定义的初始方向与实际的goAction呈90度

  61.          /*returnAction decomposition*/
  62.          private void returnAction() {
  63.                  while(frontIsClear()){
  64.                  move();
  65.          }
  66.          }
  67. }
复制代码

回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-7-26 22:35:56 | 只看该作者
全局:
nprotect的代码,No.4: MidpointFindingKarel

  1. import stanford.karel.*;

  2. /*
  3. * Name:    songy6@uci.edu
  4. * Section Leader:  ICS department
  5. */

  6. public class MidpointFindingKarel extends SuperKarel  {

  7.         public void run() {
  8.                  oneLineAction();             //karel在第一横行的行为,期望可以在本行最后一格,放上相等于本行列数的beepers
  9.                  secondAndThirdLineAction();  //karel在第二,三横行的行为,期望可以从第一横行中取beepers
  10.                                               //并交替放在第二,三横行的最后一格上,先放第二横行的最后一格
  11.                  secondAction();              //把第二横行最后一格的所有beepers从第二横行第一格开始,每一格放一个
  12.                  thirdAction();               //把第三横行最后一格的所有beepers从第三横行第一格开始,每一格放一个
  13.                  testing();                   //检测第三行和第二行是否beeper数量一致,若第二行多一个,对应位置则为多出
  14.                                           //那个beeper的正下方,若两行beepers数量一致,对应位置为最后一个beeper
  15.                                               //的正下方,以及右边的一个
  16.          }                          
  17.         
  18.         private void oneLineAction()  {
  19.                 while (frontIsClear()) {
  20.                 putBeeper();
  21.                 move();
  22.                   }
  23.             putBeeper();
  24.             turnAround();
  25.             move();
  26.             while (frontIsClear()){
  27.              if (beepersPresent()){
  28.                      pickBeeper();
  29.                      turnAround();
  30.                      while (frontIsClear()){
  31.                       move();
  32.                      }
  33.                      if (frontIsBlocked()){
  34.                              putBeeper();
  35.                              turnAround();
  36.                              move();
  37.                   }
  38.              }
  39.              else {                            //对应if(beepersPresent())
  40.                   move();
  41.              }
  42.         }
  43.             pickBeeper();                      //当搜集到了第一行第一个时,因为被边框档住了,前面的
  44.             turnAround();                      //while (frontIsClear())不能继续下去,所以必须再单独写
  45.             while (frontIsClear()) {           //一次循环
  46.                     move();
  47.             }
  48.             if (frontIsBlocked()){
  49.                     putBeeper();
  50.             }
  51.     }                                          //至此,oneLineAction结束。第一行最后一格被放上了相等于
  52.                                                //本行列数的beepers

  53.      private void secondAndThirdLineAction(){ //交替分给第二,第三行beepers
  54.                 turnLeft();
  55.                 while (beepersPresent()){          //第二行的最后一格
  56.                   pickBeeper();
  57.                   move();
  58.                   putBeeper();
  59.                   turnAround();
  60.                   move();
  61.                   turnAround();
  62.                   if (beepersPresent()){           //第三行的最后一格,注意while和if的嵌套使用
  63.                     pickBeeper();
  64.                     move();
  65.                     move();
  66.                     putBeeper();
  67.                     turnAround();
  68.                     move();
  69.                     move();
  70.                     turnAround();
  71.               }
  72.                }                                  //至此,secondAndThirdLineAction结束。第二,三横行
  73.         }                                      //的最后一格上,被分别交替放上第一行中的beepers

  74.      private void secondAction(){             //排列第二行beepers
  75.               move();                              //secondAndThirdLineAction结束,karel面朝北方
  76.               turnLeft();
  77.               while(frontIsClear()){               //把第一个beeper带至最左边
  78.               if (beepersPresent()){               //如果从第一个beeper开始循环,找不到通用的使karel返回的条件
  79.                       pickBeeper();
  80.               }
  81.               move();
  82.               }
  83.               if (frontIsBlocked()){               //行至最左边
  84.                       putBeeper();                     //放下beeper
  85.                       turnAround();
  86.                       while(frontIsClear()){           //返回起始位置
  87.                               move();
  88.                       }
  89.                       if (frontIsBlocked()){
  90.                               turnAround();                //转身
  91.                       }
  92.               }
  93.               while (beepersPresent()){            //第二个beeper开始,程序可以循环
  94.                       pickBeeper();
  95.                       move();
  96.                       while (noBeepersPresent()){      //循环开始
  97.                       move();
  98.                 }
  99.                       if (beepersPresent()){           //从第二个beeper开始,返回的判断依据改为前方是否有beeper存在
  100.                               turnAround();
  101.                               move();
  102.                               putBeeper();
  103.                               while(frontIsClear()){
  104.                                       move();
  105.                               }
  106.                               if (frontIsBlocked()){
  107.                                       turnAround();            //至此,karel把第二行的所有beeper排列起来
  108.                               }
  109.                        }
  110.           }
  111.       }
  112.       
  113.       private void thirdAction(){              //排列第三行beepers,原理同第二行,不重复了
  114.               turnRight();                         //使karel面朝北面
  115.               move();
  116.               turnLeft();
  117.               while(frontIsClear()){               //把第一个beeper带至最左边
  118.                   if (beepersPresent()){           //如果从第一个beeper开始循环,找不到通用的使karel返回的条件
  119.                           pickBeeper();
  120.                   }
  121.                   move();
  122.                   }
  123.                   if (frontIsBlocked()){           //行至最左边
  124.                           putBeeper();                 //放下beeper
  125.                           turnAround();
  126.                           while(frontIsClear()){       //返回起始位置
  127.                                   move();
  128.                           }
  129.                           if (frontIsBlocked()){
  130.                                   turnAround();            //转身
  131.                           }
  132.                   }
  133.                   while (beepersPresent()){        //第二个beeper开始,程序可以循环
  134.                           pickBeeper();
  135.                           move();
  136.                           while (noBeepersPresent()){    //循环开始
  137.                           move();
  138.                     }
  139.                           if (beepersPresent()){         //从第二个beeper开始,返回的判断依据改为前方是否有beeper存在
  140.                                   turnAround();
  141.                                   move();
  142.                                   putBeeper();
  143.                                   while(frontIsClear()){
  144.                                           move();
  145.                                   }
  146.                                   if (frontIsBlocked()){
  147.                                           turnAround();          //至此,karel把第三行的所有beeper排列起来
  148.                                   }
  149.                            }
  150.               }
  151.       }
  152.       
  153.       private void testing(){        //检测第二行和第三行的差值,注意结果:1)两行相等 2)第二行比第三行多一个
  154.               turnLeft();
  155.               move();
  156.               turnRight();
  157.               while (noBeepersPresent()){
  158.                       move();
  159.               }
  160.               
  161.               if (beepersPresent()){         //接触到第二行最右边的beeper了     
  162.                       turnRight();
  163.                       move();                    //向上跑一格
  164.               }
  165.               
  166.            if (noBeepersPresent()){      //第三行最右边如果没有beeper,此时宽度为奇数  
  167.                       turnAround();
  168.                       move();
  169.                       pickBeeper();
  170.                       move();
  171.                       putBeeper();               //中点已经被确定,并放置了一个beeper
  172.                       turnAround();
  173.                       move();
  174.                       turnLeft();                //karel在第二行最右边,面朝西
  175.                       while (frontIsClear()){    //开始收集余下的无用beepers
  176.                               if (beepersPresent()){
  177.                                       pickBeeper();
  178.                               }
  179.                               move();
  180.                           }
  181.               if (frontIsBlocked()){
  182.                       pickBeeper();            //第二行的左起第一个beeper,上面的循环无法捡起来,所以加一个
  183.                   turnRight();
  184.                   move();
  185.                   turnRight();             //此时,karel在第三行的第一格,面朝东面
  186.                   }                           
  187.               while (frontIsClear()){
  188.                  if (beepersPresent()){
  189.                      pickBeeper();
  190.                  }
  191.                   move();
  192.               }
  193.               if (frontIsBlocked()){        //到达第三行的最右边
  194.                       turnRight();
  195.                       move();
  196.                       move();
  197.                       turnRight();              //此时karel在第一行最右边,面朝左边
  198.               }
  199.               while (noBeepersPresent()){
  200.                       move();
  201.               }
  202.            }                  
  203.                  
  204.           else if (beepersPresent()) {     //偶数的时候,对应   if (noBeepersPresent()){
  205.                       turnAround();             //注意不能两个if并列连用,因为if, else if后面均可以接条件
  206.                       pickBeeper();
  207.                       move();
  208.                       pickBeeper();
  209.                       move();
  210.                       putBeeper();
  211.                       turnLeft();
  212.                       move();
  213.                       putBeeper();               //此时,两个中心点上均有一个beeper
  214.                   turnAround();
  215.                   move();
  216.                   turnRight();
  217.                   move();
  218.                   turnLeft();                 //karel此时在第二行中间,面朝西
  219.                           while (frontIsClear()){     //开始收集余下的无用beepers
  220.                                   if (beepersPresent()){
  221.                                           pickBeeper();
  222.                                   }
  223.                                   move();
  224.                           }
  225.                   if (frontIsBlocked()){
  226.                           pickBeeper();            //第二行的左起第一个beeper,上面的循环无法捡起来,所以加一个
  227.                       turnRight();
  228.                       move();
  229.                       turnRight();             //此时,karel在第三行的第一格,面朝西面
  230.                   }                           
  231.                   while (frontIsClear()){
  232.                      if (beepersPresent()){
  233.                          pickBeeper();
  234.                      }
  235.                       move();
  236.                   }
  237.                   if (frontIsBlocked()){
  238.                           turnRight();
  239.                           move();
  240.                           move();
  241.                           turnRight();
  242.                   }
  243.                   while (noBeepersPresent()){
  244.                           move();
  245.                   }
  246.                       }                 
  247.            }//检测差值总括号         
  248. }//程序总括号
复制代码

点评

这个太牛了,放在eclipse里面运行了一遍,愣是没看明白,经历了无数步骤,最终回到中点…… 虽然算法不是最简便的,不过这思维过程也太缜密啦  发表于 2012-10-2 17:49
回复

使用道具 举报

🔗
ericalingyuan 2012-7-27 12:51:54 | 只看该作者
全局:
练习一:pick newspaper
/*
* 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(){
  move();
  move();
  turnRight();
  move();
  turnLeft();
  move();
  pickBeeper();
  turnAround();
  move();
  move();
  move();
  turnRight();
  move();
  turnRight();  
}
}
先完成这个简单的,争取今天把四个assignment都完成。

评分

参与人数 2大米 +1 学分 +1 收起 理由
Brianlin + 1 回答的很好!
yiyiyaya + 1

查看全部评分

回复

使用道具 举报

🔗
ericalingyuan 2012-7-27 13:36:10 | 只看该作者
全局:
第二个作业:修补缺失的Beepers
感觉很有成就感哦

/*
* File: StoneMasonKarel.java
* --------------------------
* dive this stone mason job into three parts
*
*/import stanford.karel.*;public class StoneMasonKarel extends SuperKarel {
public void run(){
  turnLeft();
  while(rightIsClear()){  
  repairALine();
  turnBack();
  goNextLine();
  }
  repairALine();
  turnBack();
}
// to repair a single line when Karel face north
private void repairALine(){
  while (frontIsClear()){
   while(noBeepersPresent()){
    putBeeper();
   }
   move();
  }
     if(noBeepersPresent()){
   putBeeper();
  }
}
// after repair a single line,Karel go back to the start of the line, face north
private void turnBack(){
  turnAround();
  while (frontIsClear()){
   move();
  }
  turnLeft();
}
// after repair a single line,Karel go to the next line, face north
private void goNextLine(){
  move();
  move();
  move();
  move();
  turnLeft();
}
}

回复

使用道具 举报

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

本版积分规则

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