I am trying to fix Karel so it doesn't shut down after traversing 3-4 hurdles

26 Views Asked by At

I'm having a problem with this assignment I'm doing. I have been tasked with programming karel to traverse 8 rows of hurdles ranging from 1-3 blocks. First, everything goes smooth, karel easily climbs the first few hurdles with ease.

My code:

class Steeplechaser(st: Int, ave: Int, dir: Direction, beepers: Int) : Racer(st, ave, dir, beepers) {

override fun jumpHurdle() 
{
  if (!this.frontIsClear()) 
  {
    this.jumpUp()
    this.move()         // karel moves east/right after climbing a hurdle between 1-3 blocks
    this.glideDown()
    } 
      else            // karel moves forward continuously if there are no hurdles
    {
      this.move()  
      }
 }

override fun jumpUp() 
{
  if (!this.frontIsClear()) // karel starts facing east, checks if the front is not clear
  {
    this.turnLeft()       // since it returns true that the front is not clear, karel turns left, facing north now
    this.move()          // karel moves 1 hurdle
    this.turnRight()     // karel turns right, and begins checking again if the front is not clear.         
                            // The code works for 
                            // hurdles of 1-3 blocks high
    }

      if (!this.frontIsClear()) 
      {
        this.turnLeft()
        this.move()
        this.turnRight()
        }
          if (!this.frontIsClear()) 
          {
            this.turnLeft()
            this.move()
            this.turnRight()
            }
              if (!this.frontIsClear()) 
              {
                this.turnLeft()
                if (this.frontIsClear()) 
                {
                  this.move()
                }
                this.turnRight()
              }
}

override fun glideDown() 
{
  this.turnRight()             // since karel moved after jumpUp(), karel turns right, to face south
  if (this.frontIsClear())     // karel checks if the front is clear
  {
    this.move()                 // since it is clear, karel moves, "gliding down" the hurdle. The  
                                   code should work for    
                                   hurdles of 1-3 blocks high
    }
      if (this.frontIsClear()) 
      {
        this.move() 
        }
          if (this.frontIsClear()) 
          {
            this.move() 
          }
            else                  // this is for when karel reaches the bottom.
                {
                  this.turnLeft() // since the front is not clear, karel should theoretically turn 
                                     // left in preparation to 
                                     // climb the next hurdle with jumpUp(). If there isn't then it  
                                     // moves until it encounters a 
                                     // hurdle as stated in jumpHurdle()
                }
}

          
override fun turnRight() 
{
  this.turnLeft()
  this.turnLeft()
  this.turnLeft()
  }
}

After executing jumpHurdle() for the fourth time, Karel refuses to turn left to face north to execute jumpUp() and glideDown() commands, and instead keeps facing left after the end of glideDown(), and attempts to move, and since there is a hurdle in front of it, it shuts down. Before executing jumpHurdle for a fourth time, karel is able to traverse 3-4 hurdles at most, depending on the randomized layout.

I've tried adding if statement at the end of jumpUp():

if (!this.frontIsClear()) 
              {
                this.turnLeft()
                if (this.frontIsClear()) 
                {
                  this.move()
                }
                this.turnRight()
              }
}

Basically, I hoped that karel would detect a wall in front of it after gliding down and encountering a hurdle afterwards. If it detected that it wasn't clear, karel would have turned left to face north, then maybe move and begin climbing the hurdle? Instead the problem persists. Karel still doesn't turn left to face north to begin executing jumpUp(), and as usual stays turned left, attempting to move and well... shuts off.

enter image description here (karel failing) enter image description here (karel's task)

It should be noted that loops, if-else-if-else, and recursion is forbidden for me to use. The teacher forbids it. So I am limited with purely if and else statements. and (&&) / or (||) are allowed but I don't see any use for them here. This is just to point out that I am well aware that using things like a simple loop could make this easier on me, and the code shorter/readable.

Here is the class Steeplechaser inherits if more context is needed :

open class Racer:Robot
{
  constructor(st:Int, ave:Int, dir:Direction, beepers:Int):super(st, ave, dir, beepers) {}
  
  fun raceStride()
  {
    if (this.frontIsClear())
    {
      this.move()
    }
    else
    {
      this.jumpHurdle()
    }
  }

  open fun jumpHurdle()
  {
    this.jumpUp()
    this.move()
    this.glideDown()
  }

  open fun jumpUp()
  {
    this.turnLeft()
    this.move()
    this.turnRight()
  }

  open fun glideDown()
  {
    this.turnRight()
    this.move()
    this.turnLeft()
  }

  open fun turnRight()
  {
    this.turnLeft()
    this.turnLeft()
    this.turnLeft()
  }
}

The main:

fun main() {
  initializeWorld("world${(0..3).random()}.kwld")
  // Create your robot objects here and tell them what to do!
  var karel:Racer = Steeplechaser(1, 1, East, 0);
  karel.jumpHurdle()
  karel.jumpHurdle()
  karel.jumpHurdle()
  karel.jumpHurdle() // error, not turning left. moves and shuts off...
  // karel.jumpHurdle()
}

Any help would be greatly appreciated. Maybe it's me being a beginner but I honestly do not see where things are going wrong. I apologize for the formatting, I'm new to the site so... sorry.

0

There are 0 best solutions below