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

Stanford CS106A 1#Assignment1讨论

🔗
ericalingyuan 2012-7-28 00:08:33 | 只看该作者
全局:
疯了,第三题总是多走了两步!
有人帮我看看没
多谢啊

/*
* File: CheckerboardKarel.java
* ----------------------------

*/
import stanford.karel.*;
public class CheckerboardKarel extends SuperKarel {
public void run(){
  if (frontIsClear()){
   setOneLine();
   while (rightIsClear()||leftIsClear()){
    if (leftIsClear()&&noBeepersPresent()){
        eastToWest();
              setOneLine();
       }
    if (rightIsClear()&&noBeepersPresent()){
           westToEast();
           setOneLine();
       }
   }
  }
}
  // setOneLine, means to finish set a line
private void setOneLine(){
  while (frontIsClear()&&noBeepersPresent()){
   putBeeper();
   move();
      if(frontIsClear()&&noBeepersPresent()){
       move();
      }
  }
}
  // precondition:face east, postcondition:face west direction
private void eastToWest(){
  turnLeft();
  if(noBeepersPresent()){
   move();
   turnLeft();
  }
}
  // precondition:face west, postcondition:face east direction
private void westToEast(){
  turnRight();  
  if(noBeepersPresent()){
   move();
   turnRight();
  }

}

}


回复

使用道具 举报

🔗
ericalingyuan 2012-7-28 00:27:25 | 只看该作者
全局:
第三题:
参照前面的改好了,虽然还是有点不能接受
/*
* File: CheckerboardKarel.java
* ----------------------------

*/

import stanford.karel.*;

public class CheckerboardKarel extends SuperKarel {
        public void run(){
                if (frontIsClear()){
                        setOneLine();
                        while (leftIsClear()){
                                    eastToWest();
                            setOneLine();
                                if (rightIsClear()){
                                westToEast();
                                setOneLine();
                            }
                                else
                                {
                                        turnAround();
                                }
                        }
                }

        }
        // setOneLine, means to finish set a line
        private void setOneLine(){
                while (frontIsClear()&&noBeepersPresent()){
                        putBeeper();
                        move();
                    if(frontIsClear()&&noBeepersPresent()){
                            move();
                    }
                }
        }
        // precondition:face east, postcondition:face west direction
        private void eastToWest(){
                turnLeft();

                        move();
                        turnLeft();       


        }
        // precondition:face west, postcondition:face east direction
        private void westToEast(){
                turnRight();               

                        move();
                        turnRight();       

       
        }
       
}
回复

使用道具 举报

🔗
ericalingyuan 2012-7-28 00:59:45 | 只看该作者
全局:
第四题实在想不出了,放弃了,参照楼主的答案完成了
回复

使用道具 举报

🔗
zhuolinliu8 2012-7-30 01:30:48 | 只看该作者
全局:
以前看过没做作业,所以java还是不太会。。。。跟着做一个。。。不知道break这种东西能不能用CheckerboardKarel
  1. import stanford.karel.*;

  2. public class CheckerboardKarel extends SuperKarel {

  3.          public void run() {
  4.                  
  5.                  while(true){
  6.                         //put in the odd line
  7.                         turnLeft();
  8.                         putBeeper();
  9.                         while(frontIsClear()){
  10.                                 move();
  11.                                 if( frontIsClear() ){
  12.                                         move();
  13.                                         putBeeper();       
  14.                                 }
  15.                         }
  16.                        
  17.                         turnRight();
  18.                         if(frontIsBlocked()) break;
  19.                        
  20.                         //put in the even line
  21.                         if(noBeepersPresent()){
  22.                                 move();
  23.                                 putBeeper();
  24.                          }else move();       
  25.                         turnRight();
  26.                        
  27.                         while(frontIsClear()){
  28.                                 if(noBeepersPresent()){
  29.                                         move();
  30.                                         putBeeper();                                       
  31.                                 }else move();
  32.                         }
  33.                        
  34.                         turnLeft();
  35.                         if(frontIsBlocked()) break;
  36.                         move();
  37.                  }
  38.      }
  39. }
复制代码
CollectNewspaperKarel
  1. import stanford.karel.*;

  2. public class CollectNewspaperKarel extends SuperKarel {

  3.      public void run() {
  4.                       towardBeeper();
  5.              pickBeeper();
  6.              turnAround();
  7.              backwardBeeper();
  8.         }
  9.      
  10.      public void towardBeeper(){
  11.              move();
  12.                   move();
  13.                   turnRight();
  14.                   move();
  15.                   turnLeft();
  16.                   move();             
  17.      }
  18.      
  19.      public void backwardBeeper(){
  20.              move();
  21.                   turnRight();
  22.                   move();
  23.                   turnLeft();
  24.                   move();   
  25.                   move();
  26.                   turnAround();
  27.      }

  28. }
复制代码

MidpointFindingkarel

  1. import stanford.karel.*;

  2. public class MidpointFindingKarel extends SuperKarel {

  3.         public void run(){               
  4.                 putBeeper();
  5.                 if (frontIsClear()){
  6.                         //put first two beepers
  7.                         while(frontIsClear()){
  8.                                 move();
  9.                         }
  10.                         putBeeper();
  11.                         turnAround();
  12.                         move();
  13.                         //move around and around
  14.                         while(noBeepersPresent()){
  15.                                 while(noBeepersPresent()){
  16.                                 move();
  17.                                 }
  18.                                 pickBeeper();
  19.                                 turnAround();
  20.                                 move();
  21.                                 putBeeper();
  22.                                 move();
  23.                         }
  24.                         //odd line in total,keep one beeper and pick one beeper
  25.                         if(facingEast()) pickBeeper();
  26.                         turnAround();
  27.                         move();
  28.                 }               
  29.         }
  30. }
复制代码
StoneMasonKarel
  1. import stanford.karel.*;

  2. public class StoneMasonKarel extends SuperKarel {

  3.         public void run(){
  4.                
  5.                 while(frontIsClear()){
  6.                         turnLeft();
  7.                         putBeepers();
  8.                         turnRight();
  9.                        
  10.                         if (frontIsClear())        move();
  11.                         if (frontIsClear())        move();
  12.                         if (frontIsClear())        move();
  13.                         if (frontIsClear())        move();
  14.                        
  15.                         turnRight();
  16.                         putBeepers();
  17.                         turnLeft();       
  18.                        
  19.                         if (frontIsClear())        move();
  20.                         if (frontIsClear())        move();
  21.                         if (frontIsClear())        move();
  22.                         if (frontIsClear())        move();
  23.                 }
  24.                
  25.         }
  26.        
  27.         public void putBeepers(){
  28.                 while (frontIsClear()){
  29.                         if(noBeepersPresent())putBeeper();
  30.                         move();
  31.                 }       
  32.                 if(noBeepersPresent()) putBeeper();
  33.         }

  34. }
复制代码



回复

使用道具 举报

🔗
gotoo 2012-8-3 05:15:23 | 只看该作者
全局:
本帖最后由 gotoo 于 2012-8-3 07:46 编辑

大家看图就知道是哪到题了,我是从http://www.stanford.edu/class/cs106a/ 上面下载的作业,不知道和网易上面的老版本有没有什么不同。1,2,3,4我是按照顺序贴的,但是类名好像用错了,不好意思。

No2


  1. import stanford.karel.*;

  2. /*
  3. * Name:
  4. * Section Leader:
  5. */

  6. public class CollectNewspaperKarel extends SuperKarel {

  7.         public void run() {
  8.                 while (!isNextToColumn()) {
  9.                         processOneColumn();
  10.                         moveToNextColumn();
  11.                 }
  12.                 processOneColumn();
  13.                 moveToNextColumn();
  14.         }

  15.         private void processOneColumn() {
  16.                 turnNorth();
  17.                 if (frontIsBlocked()) {
  18.                         turnSouth();
  19.                 }
  20.                 while (!isNextToWall()) {
  21.                         if (!beepersPresent()) {
  22.                                 putBeeper();
  23.                                 move();
  24.                         } else {
  25.                                 move();
  26.                         }
  27.                 }
  28.                 if(!beepersPresent()){
  29.                         putBeeper();
  30.                 }
  31.         }

  32.         private void moveToNextColumn() {
  33.                 turnSouth();
  34.                 while(!isNextToWall()) {
  35.                         move();
  36.                 }
  37.                 turnEast();
  38.                 if (!isNextToColumn()) {
  39.                         moveFourSteps();
  40.                 }
  41.         }

  42.         private boolean isNextToColumn() {
  43.                 return frontIsBlocked();
  44.         }

  45.         private boolean isNextToWall() {
  46.                 return frontIsBlocked();
  47.         }

  48.         private void moveFourSteps() {
  49.                 move();
  50.                 move();
  51.                 move();
  52.                 move();
  53.         }

  54.         private void turnNorth() {
  55.                 if (facingNorth()) {

  56.                 }
  57.                 if (facingSouth()) {
  58.                         turnAround();
  59.                 }
  60.                 if (facingEast()) {
  61.                         turnLeft();
  62.                 }
  63.                 if (facingWest()) {
  64.                         turnRight();
  65.                 }

  66.         }

  67.         private void turnSouth() {
  68.                 if (facingNorth()) {
  69.                         turnAround();
  70.                 }
  71.                 if (facingSouth()) {

  72.                 }
  73.                 if (facingEast()) {
  74.                         turnRight();
  75.                 }
  76.                 if (facingWest()) {
  77.                         turnLeft();
  78.                 }

  79.         }

  80.         private void turnEast() {
  81.                 if (facingNorth()) {
  82.                         turnRight();
  83.                 }
  84.                 if (facingSouth()) {
  85.                         turnLeft();
  86.                 }
  87.                 if (facingEast()) {

  88.                 }
  89.                 if (facingWest()) {
  90.                         turnAround();
  91.                 }

  92.         }

  93. }
复制代码

No 3

  1. import stanford.karel.*;

  2. /*
  3. * Name:
  4. * Section Leader:
  5. */

  6. public class StoneMasonKarel extends SuperKarel {

  7.         public void run() {
  8.                 while (!isEnd()) {
  9.                                 putBeeperAndMove();
  10.                                 onlyMove();                                
  11.                 }
  12.         }

  13.         private void onlyMove() {
  14.                 if(frontIsClear()){
  15.                         move();
  16.                 }else if(!isEnd()){
  17.                         turnNorth();
  18.                         move();
  19.                         turnToCorrentDirection();
  20.                 }else{
  21.                         
  22.                 }
  23.         }

  24.         private void turnToCorrentDirection() {
  25.                 if(leftIsBlocked()){
  26.                         turnRight();
  27.                 }else{
  28.                         turnLeft();
  29.                 }
  30.         }

  31.         private void putBeeperAndMove() {
  32.                 putBeeper();
  33.                 if (frontIsClear()) {
  34.                         move();
  35.                 }else{
  36.                         turnNorth();
  37.                         move();
  38.                         turnToCorrentDirection();
  39.                 }
  40.         }

  41.         private boolean isEnd() {
  42.                 if (frontIsBlocked()){
  43.                         if(facingWest() && rightIsBlocked()){
  44.                                 return true;
  45.                         }else if(facingEast() && leftIsBlocked()){
  46.                                 return true;
  47.                         }else{
  48.                                 return false;
  49.                         }
  50.                 }
  51.                 return false;
  52.         }


  53.         private void turnNorth() {
  54.                 if (facingNorth()) {
  55.                 }

  56.                 if (facingSouth()) {
  57.                         turnAround();
  58.                 }

  59.                 if (facingEast()) {
  60.                         turnLeft();
  61.                 }

  62.                 if (facingWest()) {
  63.                         turnRight();
  64.                 }

  65.         }

  66. }
复制代码





回复

使用道具 举报

🔗
gotoo 2012-8-3 05:21:55 | 只看该作者
全局:
yiyiyaya 发表于 2012-7-26 22:29
能拜托大家一件事儿吗?首先,希望积极贴代码,方便互相学习。其次,我们这周在学习的一共有5个人,把作业完 ...

第三期都开始了 刚开始写第一期 争取今天把4道题都完成 贴出来
回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-8-4 19:48:12 | 只看该作者
全局:
gotoo 发表于 2012-8-3 05:21
第三期都开始了 刚开始写第一期 争取今天把4道题都完成 贴出来

加油!!!
回复

使用道具 举报

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

本版积分规则

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