为赋值编写伪代码并想仔细检查它是否有意义
Writing pseudo-code for assignment and want to double check if it makes sense
我是一名 CS 初学者,希望得到一些帮助来检查非常简单的伪代码。
问题是:
编写一个算法来玩 "treasure hunt" 游戏的机器人版本。该游戏涉及机器人接收线索,这些线索会引导他们找到其他线索,直到他们最终到达 "treasure"。
线索由方块的颜色表示,每条线索只需要机器人在一个方向(左、右、前)移动一个方块。走完一步后,机器人会重新检查所站方块的颜色,以判断下一步的走法等等
瓷砖颜色的规则是:
- 白色方块 = 下一条线索是机器人正前方的方块
- 蓝色方块 = 下一个线索是机器人左边的方块
- 绿色方块 = 下一个线索是机器人右边的方块
- 黑色方块 = 机器人向后移动两块方块,然后根据新方向重新检查线索
- 黄色方块 = 宝藏,游戏结束
算法规则:
- 机器人只能前进、左转、右转
- 机器人能够读取它所站立的方块的颜色
这是我的伪代码:
//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!”
我知道这已经有几周了,但现在我也是新来的,所以现在才看到这个。
我不确定您为什么以这种方式声明这些变量。我知道这些是 conditions/rules 给出的,但必须将它们转化为实际的评估和行动。
因此,在一个新函数 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 是另一个检查实际图块颜色和 returns 值(颜色)的函数。
我不是专家,因为我也是一名学生,但我一直在四处寻找,这是我认为我可以真正回答的问题。
我是一名 CS 初学者,希望得到一些帮助来检查非常简单的伪代码。
问题是:
编写一个算法来玩 "treasure hunt" 游戏的机器人版本。该游戏涉及机器人接收线索,这些线索会引导他们找到其他线索,直到他们最终到达 "treasure"。
线索由方块的颜色表示,每条线索只需要机器人在一个方向(左、右、前)移动一个方块。走完一步后,机器人会重新检查所站方块的颜色,以判断下一步的走法等等
瓷砖颜色的规则是:
- 白色方块 = 下一条线索是机器人正前方的方块
- 蓝色方块 = 下一个线索是机器人左边的方块
- 绿色方块 = 下一个线索是机器人右边的方块
- 黑色方块 = 机器人向后移动两块方块,然后根据新方向重新检查线索
- 黄色方块 = 宝藏,游戏结束
算法规则:
- 机器人只能前进、左转、右转
- 机器人能够读取它所站立的方块的颜色
这是我的伪代码:
//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!”
我知道这已经有几周了,但现在我也是新来的,所以现在才看到这个。
我不确定您为什么以这种方式声明这些变量。我知道这些是 conditions/rules 给出的,但必须将它们转化为实际的评估和行动。
因此,在一个新函数 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 是另一个检查实际图块颜色和 returns 值(颜色)的函数。
我不是专家,因为我也是一名学生,但我一直在四处寻找,这是我认为我可以真正回答的问题。