编写用于赋值的伪代码,并希望仔细检查它是否有意义

时间:2017-01-26 03:25:59

标签: c++ pseudocode

我是一名初级CS学生,非常乐意帮助我们查看真正的简单伪代码。

问题在于:

编写一个算法,用于播放"寻宝的机器人版本"游戏。游戏涉及机器人接收线索,引导他们到其他线索,直到他们最终到达"宝藏"。

线索由瓷砖的颜色表示,每条线索只需要机器人沿一个方向(左,右,前)移动一个瓷砖。移动后,机器人重新检查它所站立的瓷砖的颜色,以确定下一步行动等。

图块颜色的规则是:

  • 白色瓷砖=下一个线索是直接位于机器人前面的瓷砖
  • 蓝色瓷砖=下一个线索是机器人左侧的瓷砖
  • 绿色瓷砖=下一个线索是机器人右边的瓷砖
  • 黑色瓷砖=机器人向后移动两个瓷砖,然后根据新标题重新检查线索
  • 黄色瓷砖=宝藏,游戏结束

算法规则:

  • 机器人只能前进,左转或右转
  • 机器人能够读取它所站立的瓷砖的颜色

这是我的伪代码:

     //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!”

1 个答案:

答案 0 :(得分:0)

我知道这已经有几个星期了,但我现在也遇到了这个问题。

我不确定为什么要以这种方式声明这些变量。我理解这些是给出的条件/规则,但它们必须转化为实际的评估和行动。

因此,在一个新函数output()中,因为你在WHILE循环中使用了它,你实际上会有IF语句采用tileColor并相应地执行运动。

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

假设还有其他功能。

看着WHILE循环,IF不是多余的吗? while循环已经检查了tileColor的相同条件而不是黄色。并且在进入WHILE循环之前尚未检查tileColor。

//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"

这假设您将使用不同的函数,其中输出将采用tileColor并相应地移动。而且checkTileColor是另一个检查实际图块颜色并返回值(颜色)的函数。

我不是专家,因为我也是学生,但我一直在四处寻找,这是我认为我实际可以回答的问题。