Stanford编程方法学公开课作业 2 ---- Karel中生成棋盘的代码
来源:互联网 发布:阿里巴巴服装淘宝网 编辑:程序博客网 时间:2024/05/17 23:43
解决如下图所示问题:(生成间隔棋盘状图案)
要求程序可以在 各类形状的地图中正常运行(如1x8 8x1 5x6 6x6 7x7 等等)
代码如下:
/*
* File: CheckerboardKarel.java
* ----------------------------
* The CheckerboardKarel class is able to 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() {
goToOrigin();
genReferenceRow(); //generate a reference row, choose 1st row as reference
toFaceNorth(); //to face north
while ( frontIsClear() ) {
if ( beepersPresent() ) {
moveToNextRow();
if ( frontIsClear() ) {
move();
genReferenceRow();
}//to deal with world with only one column
} else {
moveToNextRow();
genReferenceRow();
}
toFaceNorth();
} //end of while loop
} //end of main method
/*
* Make Karel move to the next row without changing column number
* Karel is always moving toward north part
* Precondition: facing north
* Postcondition: facing east/west depending on Karel is standing on
* left end or right end
*/
private void moveToNextRow() {
if ( leftIsBlocked() ) { //at left end wall of the world
move();
turnRight(); //to face east
} else { //at right end wall of the world
move();
turnLeft(); //to face west
} // end of if-else
} // end of moveToNextRow()
/*
* Is defined to generate one row of beepers, beepers are exactly 2 units apart
* Precondition: any case
* Postcondition: arrive at the end of one row
*/
private void genReferenceRow() {
if ( frontIsBlocked() ) {
putBeeper(); //dealing with the world with only one column
}
while ( frontIsClear() ) {
putBeeper();
move(); //enter while means can move forward at least one step
if ( frontIsClear() ) {
//if width is even, second move could not be made
move(); //second move
//if width is odd, second move should make, one beeper should be added
//in front of wall
if ( frontIsBlocked() ) {
putBeeper();
}
}
}
}
/*Move Karel back to origin of map (1,1), make it facing east
* Precondition: can be any case
* Postcondition: standing on (1,1) facing east
*/
private void goToOrigin() {
while (notFacingSouth()) {
turnLeft();
}
moveToWall();
while (notFacingWest()) {
turnLeft();
}
moveToWall();
while (notFacingEast()) {
turnLeft(); //Karel will face east
}
}
private void moveToWall() { //move Karel to wall
while (frontIsClear()) {
move();
}
}
private void toFaceNorth() {
while ( notFacingNorth() ) {
turnLeft();
}
}
}
0 0
- Stanford编程方法学公开课作业 2 ---- Karel中生成棋盘的代码
- Stanford编程方法学公开课作业 1 ----关于Karel 中点寻找问题的讨论 (Midpoint finding)
- 斯坦福大学公开课:编程方法学——Karel作业的第四题
- Stanford公开课《Programming Methodology》里面的Karel机器人的配置方法
- Stanford公开课《Programming Methodology》里面的Karel机器人的配置方法。
- Stanford公开课《Programming Methodology》里面的Karel机器人的配置方法。
- 斯坦福大学公开课《编程方法学》之笔记二——任务三:Karel与Java
- 斯坦福大学公开课 编程方法学 Karel the Robot 寻找中点问题解决
- Stanford : cs106a 编程方法学课程 Karel程序 编译环境配置
- 斯坦福《编程方法学》Karel环境搭建
- 斯坦福《编程方法学》Karel环境搭建
- stanford Java公开课 Karel机器人开发环境eclipse安装与配置
- 斯坦福《编程方法学》Karel环境搭建问题一:运行时出现jpanel面板只有file选项 没有karel机器人的现象
- 斯坦福大学-公开课-编程方法学
- 斯坦福《编程方法学》公开课
- Stanford公开课--神经网络的表示
- Stanford公开课--神经网络的学习
- [斯坦福开放课程.编程方法].作业.1-4.Karel将Beeper放在中间
- 108. Convert Sorted Array to Binary Search Tree
- 泛型以及泛型擦除
- Handler机制分析之一
- 位图与布隆过滤器 的说明 介绍
- 线段树例题5
- Stanford编程方法学公开课作业 2 ---- Karel中生成棋盘的代码
- Java提高篇---HashMap
- Autonomous RC Car
- 较简单的修改和添加功能(链接数据库)
- C primer plus---第一章:概览
- height、clientHeight、scrollHeight、offsetHeight区别
- 登录校园网
- java中创建对象的5种方式
- 每日小记2017.2.22