View Full Version : Checkerboard karel java program help
ssgliberty
April 11th, 2011, 10:08 PM
i accidentally deleted this and do not have the complete knowledge of it but the code i have so far works as i intended so far but i cannot get past this
part, the java code i have written is
import stanford.karel.*;
public class CheckerboardKarel extends SuperKarel {
public void run () {
putBeeper () ;
move () ;
build () ;
}
private void build () {
if (beepersPresent() ) {
move () ;
buildCheckerboard () ;
}else{
buildCheckerboard () ;
}
}
private void buildCheckerboard () {
turnAround () ;
move () ;
turnAround () ;
if (beepersPresent() ) {
moveTwice () ;
putBeeper () ;
build () ;
}
}
private void moveTwice () {
move () ;
move () ;
}
}
thanks in advance for any help :) .
darkwoon
April 12th, 2011, 11:02 AM
Err, what is your problem exactly?
I guess you already read the documentation (http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBkQFjAA&url=http%3A%2F%2Fwww.stanford.edu%2Fclass%2Fcs106a%2Fbook%2Fkarel-the-robot-learns-java.pdf&rct=j&q=SuperKarel&ei=9XakTb-AAoaZOr7fpc0J&usg=AFQjCNHkmGq5531Dhl0Hwrg4h6LQpmP5ng&cad=rja)?
ssgliberty
April 12th, 2011, 03:55 PM
yes, i have , i can't figure out where / how to make it do every row , i have done it before but don't remember how.
here is what i tried today so far , it is supposed to turn left , move and turn left again. it turns left then moves 7 times then turns left.
import stanford.karel.*;
public class CheckerboardKarel extends SuperKarel {
public void run () {
putBeeper () ;
move () ;
build () ;
}
private void build () {
if (beepersPresent() ) {
move () ;
buildCheckerboard () ;
}else{
buildCheckerboard () ;
}
}
private void buildCheckerboard () {
turnAround () ;
move () ;
turnAround () ;
while (beepersPresent() ) {
moveTwice () ;
putBeeper () ;
build () ;
}
}
private void moveTwice () {
move () ;
while (frontIsBlocked() ) {
if (leftIsClear() ){
turnLeft () ;
move () ;
turnLeft () ;
}
if (rightIsClear() ) {
turnRight () ;
move () ;
turnRight () ;
}
}
move();
}
}
ssgliberty
April 12th, 2011, 09:15 PM
I got it to work.,
here is the working java code [/QUOTE]
import stanford.karel.*;
public class CheckerboardKarel extends SuperKarel {
public void run () {
putBeeper () ;
move () ;
build () ;
}
private void build () {
if (beepersPresent() ) {
move () ;
buildCheckerboard () ;
}else{
buildCheckerboard () ;
}
}
private void buildCheckerboard () {
turnAround () ;
move () ;
turnAround () ;
if (beepersPresent() ) {
moveTwice () ;
putBeeper () ;
build () ;
}
}
private void moveTwice () {
move () ;
while (facingEast() ) {
if (frontIsBlocked() ) {
turnLeft () ;
while(frontIsBlocked() ) {
turnLeft () ;
}
move () ;
turnLeft () ;
putBeeper () ;
move () ;
build () ;
}else{
move () ;
putBeeper () ;
build () ;
}
}
while (facingWest() ) {
if(frontIsBlocked() ) {
turnRight () ;
while (frontIsBlocked() ) {
stop () ;
}
move () ;
turnRight () ;
putBeeper ();
move () ;
build () ;
}else{
move () ;
putBeeper () ;
build () ;
}
}
}
}
[/QUOTE]
bernietmac1
February 2nd, 2012, 12:29 PM
I cannot figure out why karel won't follow the instruction while (frontIsClear()).
This is my code, need to know why when karel reaches the north wall why does it keep going and receive an error.
import stanford.karel.*;
public class CheckerboardKarel extends SuperKarel {
public void run() {
while (frontIsClear()){
makeRow();
nextRow();
}
}
private void makeRow() {
while (frontIsClear()) {
putBeeper();
move();
if (frontIsClear()){
move();
}
}
}
private void nextRow() {
if (leftIsClear()) {
moveUp();
turnBack();
}
}
private void turnBack() {
if (facingNorth()) {
turnLeft();
}
}
private void moveUp() {
if (facingEast()){
turnLeft();
move();
}
{
if (facingWest()) {
turnRight();
moveIfPosible();
turnRight();}
}
}
private void moveIfPosible(){
if (frontIsClear()) move();
}
}
TheMatrix
February 2nd, 2012, 08:31 PM
I cannot figure out why karel won't follow the instruction while (frontIsClear()).
This is my code, need to know why when karel reaches the north wall why does it keep going and receive an error.
-snip-
While I appreciate you are trying to help, this thread is older than 2 months. Please don not bump old threads.
:locked:
OP: If you want your thread reopened, please PM me.
EDIT: OP requested reopen.
ssgliberty
February 3rd, 2012, 03:08 AM
I cannot figure out why karel won't follow the instruction while (frontIsClear()).
This is my code, need to know why when karel reaches the north wall why does it keep going and receive an error.
import stanford.karel.*;
public class CheckerboardKarel extends SuperKarel {
public void run() {
while (frontIsClear()){
makeRow();
nextRow();
}
}
private void makeRow() {
while (frontIsClear()) {
putBeeper();
move();
if (frontIsClear()){
move();
}
}
}
private void nextRow() {
if (leftIsClear()) {
moveUp();
turnBack();
}
}
private void turnBack() {
if (facingNorth()) {
turnLeft();
}
}
private void moveUp() {
if (facingEast()){
turnLeft();
move();
}
{
if (facingWest()) {
turnRight();
moveIfPosible();
turnRight();}
}
}
private void moveIfPosible(){
if (frontIsClear()) move();
}
}
this is the code you gave me.
import stanford.karel.*;
public class CheckerboardKarel extends SuperKarel {
public void run() {
while (frontIsClear()){
makeRow();
nextRow();
}
}
private void makeRow() {
while (frontIsClear()) {
putBeeper();
move();
if (frontIsClear()){
move();
}
}
}
private void nextRow() {
if (leftIsClear()) {
moveUp();
turnBack();
}
}
private void turnBack() {
if (facingNorth()) {
turnLeft();
}
}
private void moveUp() {
if (facingEast()){
turnLeft();
move();
}
{
if (facingWest()) {
turnRight();
moveIfPosible();
turnRight();}
}
}
private void moveIfPosible(){
if (frontIsClear()) move();
}
}
your code is missing the else statement , try this and see if it works(id do it but cant get eclipse to import karel projects on win 7)
import stanford.karel.*;
public class CheckerboardKarel extends SuperKarel {
public void run() {
while (frontIsClear()){
makeRow();
nextRow();
}
}
private void makeRow() {
while (frontIsClear()) {
putBeeper();
move();
if (frontIsClear()){
move();
}
}
}
private void nextRow() {
if (leftIsClear()) {
moveUp();
turnBack();
}
}
private void turnBack() {
if (facingNorth()) {
turnLeft();
}
}
private void moveUp() {
if (facingEast()){
turnLeft();
move();
}
// note the "else" you did'nt have it before
else{
if (facingWest()) {
turnRight();
moveIfPosible();
turnRight();
}
}
}
private void moveIfPosible(){
if (frontIsClear()) move();
}
}
i put a comment above the fixed error for you , you must have deleted it :P
vBulletin® v3.8.9, Copyright ©2000-2021, vBulletin Solutions, Inc.