为赋值编写伪代码,并希望仔细检查它是否有意义
Writing pseudo-code for assignment and want to double check if it makes sense
我是一名刚开始学习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)的函数。
我不是专家,因为我也是一名学生,但我一直在四处寻找,这是我认为我可以真正回答的问题。
相关文章:
- 在C++中,使用带有 std::optional 参数的函数<T>来表示可选参数是否有意义?
- API 返回智能指针的 std::optional 以明确指定指针可能为 null 是否有意义?
- 从头开始为应用程序创建 docker 映像是否有意义?
- 在c++中,如果首先禁止默认构造,那么禁止复制构造有意义吗
- sizeof(函数)有意义吗?
- 二进制模式 + 格式化文本操作或文本模式 + 二进制数据操作 - 有意义吗?
- 插入向量时,使用lambda的返回而不是函数的返回是否有意义?
- 在 C++17 中使用 const std::string& 参数有意义吗?
- 基础类模板参数的有意义名称
- 内联这个函数,确实有意义
- 将 final 关键字添加到没有基类(未派生)的类中的虚函数是否有意义
- 左移负整数为零是否有意义?
- 是否有一个上下文表达式`a.b :: c`有意义
- 是否有一些有意义的统计数据来证明保持有符号整数算术溢出未定义是合理的
- 将可选与reference_wrapper相结合是否有意义?
- 函数返回rvalue参考是否有意义
- 将 [[noreturn]] 添加到主函数是否有意义
- 在shared_ptr的自定义删除器中检查 nullptr 是否有意义?
- YAML-CPP 节点诊断有意义的错误
- 为赋值编写伪代码,并希望仔细检查它是否有意义