Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

program C++ Write an algorithm for playing a robotic version of the “treasure hu

ID: 3880042 • Letter: P

Question

program C++

Write an algorithm for playing a robotic version of the “treasure hunt” game. The “treasure hunt” game involves players receiving clues that guide them to other clues, until eventually the last clue leads them to a “treasure.” For example, at the beginning of the game the players might receive a slip of paper that says “go to the big oak tree,” at the oak tree they might find another slip of paper that says “try swimming,” so they go to the swimming pool to look for a third clue, and so forth.

Robots can’t read slips of paper, so the “clues” for a robot treasure hunt are represented by the colors of tiles. Furthermore, robots aren’t smart enough to think of (or find) oak trees or swimming pools, so every clue in a robot treasure hunt just requires a robot to move one tile in some direction. After doing so, the robot examines the color of the tile it is then standing on to figure out where to go next, and so forth.

Here are the specific rules for interpreting tile colors as clues:

· White tile means that the next clue is the tile directly in front of the robot · Blue tile means that the next clue is the tile to the robot’s left
· Green tile means that the next clue is the tile to the robot’s right

7.

· Black tile means that the robot should move two tiles backward, then continue interpreting clues based on its new heading

· Yellow tile is a treasure — it marks the end of a part of a treasure hunt

Rules for your algorithm:

· Robot can only move forward, turn left, or turn right.

· The robot can detect the color of the position it is current on.

Explanation / Answer

Answer:

Environment:

Let assume the environment is a grid of colored tiles(WHITE,BLUE,GREEN,BLACK,YELLOW).Robot is standing on a tile.Every tile has a(X,Y) Position

We use a function getTileColor(x,y) to know color of current tile(x,y position).

ALGORITHM:

TRESUREHUNT(x,y)

1. color=getColor(x,y)

2. IF color=YELLOW

3. Return "tiles"+x+y+" Is a tresure"

4. ELSE IF color=WHITE

5. TRESUREHUNT(x,y-1) // move to front tile by decrementing y by 1   

6 ELSE IF color=BLUE

7. TRESUREHUNT(x-1,y) // move to left bydecrementing x by 1   

8. ELSE IF color=GREEN

9. TRESUREHUNT(x+1,y) // move to right tile by incrementing x by 1   

10. ELSE IF color=BLACK

11. TRESUREHUNT(x,y+2) // move to front tile by incrementint y by 2   

12. ELSE

13. Return "Unsucessfull"  

12.END iF

C++ CODE

//for simplicity i represent entite grid in a 2;D array .Each cell is viewed as a tile.It contains arbitary color values,0-BLACK

//1-BLUE,2;GREEN,14;YELLOW,15-WHITE

//accordingly the x,y value is changed. if current cell(tile)=15(WHITE) robot moves to front tile.

// so x=x-1 and y=y

#include<iostream.h>
#include<graphics.h>
void tresurehunt(int x,int y);
int grid[][7]={ {1, 1, 1, 14,2, 0, 15},
       {0, 1, 1, 1, 0, 0, 2},
       {2, 2 ,1, 0, 15,14,1},
       {15,15,14,0, 1, 2, 1},
       {15,0, 0, 1, 2, 2, 14},
       {14,14,14,15,0, 2, 2},
       {15,15,15,1, 1, 2, 1}};
int main()
   {
   tresurehunt(4,4);
   return 0;
   }
void tresurehunt(int x,int y)
   {
   //int color=getpixel(x,y);
   int color=grid[x][y];
   if(color==YELLOW)
       {
       cout<<"Tresure is in:"<<x<<","<<y<<"tiles";
       }
   else if(color==WHITE)
       {
       tresurehunt(x-1,y);
       }
   else if(color==BLUE)
       {
       tresurehunt(x,y-1);
       }
   else if(color==GREEN)
       {
       tresurehunt(x,y+1);
       }

   else if(color==BLACK)
       {
       tresurehunt(x-2,y);
       }
   else
       {
       cout<<"U r unsuccessful";
       }
   }

Please provide your valuable feedback.