Search code examples
c++pseudocode

Writing pseudo-code for assignment and want to double check if it makes sense


I'm a beginning CS student and would love for some help checking REALLY SIMPLY pseudo-code.

Here is the problem:

Write an algorithm for playing a robotic version of the "treasure hunt" game. The game involves robots receiving clues that guide them to other clues until they eventually reach a "treasure".

Clues are represented by colors of tiles and each clue just requires the robot to move one tile in a direction (left, right, forward). After making a move, the robot re-examines the color of the tile it is standing on to figure out the next move, etc.

The rules for the tile colors are:

  • White tile = next clue is the tile directly in front of the robot
  • Blue tile = next clue is the tile to the robot's left
  • Green tile = next clue is the tile to the robot's right
  • Black tile = robot moves back TWO tiles then re-examines clues based on its new heading
  • Yellow tile = treasure and game is over

The algorithm rules:

  • The robot can only move forward, turn left, or turn right
  • The robot is able to read the color of the tile it is standing on

Here is my pseudo-code:

     //assign movements and actions to variables

     whiteTile = step forward one space
     blueTile = turn left and step forward one space
     greenTile = turn right and step forward one space
     blackTile = step backwards two spaces without changing directions
     yellowTile = treasure found and game over
     tileColor = check color of tile 

     //initiate while loop to run continously until robot reaches yellow tile

     While tile under feet does not equal yellowTile
    // initiates the movements of the robot to start if loop
        Check color of tile
    // this if loop should run continously as long as tileColor isn’t yellow
        if tile color does not equal yellowTile
          output(tileColor)
          check color of tile


    // once robot is on top of yellow tile, it exits the while loop and game is over
     output “Game over, you win!”

Solution

  • I know this is a few weeks old but came across this now as I am new here as well.

    I am not sure why you are declaring those variables in that way. I understand that those were the conditions/rules given but they would have to be translated into actual evaluations and actions.

    So within a new function, output() since that's what you're using in the WHILE loop, you would actually have IF statements taking tileColor and executing the movements accordingly.

    BEGIN output
       IF tileColor = white THEN
           CALL moveForward
       ELSE IF tileColor = blue THEN
           CALL moveLeft
       ..
       .
       .
    

    Assuming there's another function for the movements.

    And looking at the WHILE loop, isn't the IF redundant? The while loop is already checking the same condition of the tileColor not yellow. And the tileColor hasn't been checked before entering the WHILE loop.

    //initialize first and check for case of robot spawning on yellow
    tileColor = checkTileColor    
    
    WHILE NOT(tileColor = yellow)
         CALL output(tileColor)
         tileColor = checkTileColor
    END WHILE
    
    DISPLAY "You win"
    

    This is assuming you would have different functions where output would take the tileColor and move accordingly. And also checkTileColor being another function that checks the actual tile colors and returns a value (color).

    I'm not an expert as I'm a student as well but I have been looking around and this is something I thought I could actually answer.