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

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

本文关键字:检查 有意义 是否 希望 赋值 伪代码      更新时间:2023-10-16

我是一名刚开始学习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!”

我知道这是几周前的事了,但现在我也是新来的。

我不知道你为什么要用这种方式声明这些变量。我理解这些是所给出的条件/规则,但必须将其转化为实际的评估和行动。

因此,在一个新函数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是另一个检查实际瓷砖颜色并返回值(color)的函数。

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