计算机有哪些语言 介绍计算机编程中常见语言的种类和应用场景?


引自强大而免费的微信小程序:皆我百晓生。
任务1:import java.util.*;
public class Agent {
// 定义代理的状态
enum State {
// 中心面向北,东,南或西
CN, CE, CS, CW,
// 在边界处始终面向边界方向
NB, EB, SB, WB,
// 胜利状态和失败状态
WIN, LOSE
}
// 定义代理的感知
enum Perception {
// 代表位置中心,北边界,东边界,南边界或西边界
C, NB, EB, SB, WB,
// 代表朝向北,东,南或西
N, E, S, W,
// 代表墙,坑或目标
WALL, PIT, GOAL
}
// 定义代理的动作
enum Action {
F, B, TR, TL, STOP
}
// 定义状态、感知和动作的转换模型
private Map<State, Map<List<Perception>, List<Action>>> transitionModel;
public Agent() {
// 初始化转换模型
transitionModel = new HashMap<>();
// 添加状态、感知和动作的转换关系
// 中心面向北状态
Map<List<Perception>, List<Action>> centerNorth = new HashMap<>();
centerNorth.put(Arrays.asList(Perception.C, Perception.PIT, Perception.GOAL), Arrays.asList(Action.STOP));
centerNorth.put(Arrays.asList(Perception.C, Perception.NO_PIT, Perception.GOAL), Arrays.asList(Action.STOP));
centerNorth.put(Arrays.asList(Perception.WALL, Perception.PIT, Perception.GOAL), Arrays.asList(Action.STOP));
transitionModel.put(State.CN, centerNorth);
// 中心面向东状态
Map<List<Perception>, List<Action>> centerEast = new HashMap<>();
centerEast.put(Arrays.asList(Perception.C, Perception.PIT, Perception.GOAL), Arrays.asList(Action.STOP));
centerEast.put(Arrays.asList(Perception.C, Perception.NO_PIT, Perception.GOAL), Arrays.asList(Action.STOP));
centerEast.put(Arrays.asList(Perception.WALL, Perception.PIT, Perception.GOAL), Arrays.asList(Action.STOP));
transitionModel.put(State.CE, centerEast);
// 中心面向南状态
Map<List<Perception>, List<Action>> centerSouth = new HashMap<>();
centerSouth.put(Arrays.asList(Perception.C, Perception.PIT, Perception.GOAL), Arrays.asList(Action.STOP));
centerSouth.put(Arrays.asList(Perception.C, Perception.NO_PIT, Perception.GOAL), Arrays.asList(Action.STOP));
centerSouth.put(Arrays.asList(Perception.WALL, Perception.PIT, Perception.GOAL), Arrays.asList(Action.STOP));
transitionModel.put(State.CS, centerSouth);
// 中心面向西状态
Map<List<Perception>, List<Action>> centerWest = new HashMap<>();
centerWest.put(Arrays.asList(Perception.C, Perception.PIT, Perception.GOAL), Arrays.asList(Action.STOP));
centerWest.put(Arrays.asList(Perception.C, Perception.NO_PIT, Perception.GOAL), Arrays.asList(Action.STOP));
centerWest.put(Arrays.asList(Perception.WALL, Perception.PIT, Perception.GOAL), Arrays.asList(Action.STOP));
transitionModel.put(State.CW, centerWest);
// 边界面向北状态
Map<List<Perception>, List<Action>> northBoundary = new HashMap<>();
northBoundary.put(Arrays.asList(Perception.C, Perception.PIT, Perception.GOAL), Arrays.asList(Action.F, Action.TR, Action.TL));
northBoundary.put(Arrays.asList(Perception.C, Perception.NO_PIT, Perception.GOAL), Arrays.asList(Action.F, Action.TR, Action.TL));
northBoundary.put(Arrays.asList(Perception.WALL, Perception.PIT, Perception.GOAL), Arrays.asList(Action.STOP));
transitionModel.put(State.NB, northBoundary);
// 边界面向东状态
Map<List<Perception>, List<Action>> eastBoundary = new HashMap<>();
eastBoundary.put(Arrays.asList(Perception.C, Perception.PIT, Perception.GOAL), Arrays.asList(Action.F, Action.TR, Action.TL));
eastBoundary.put(Arrays.asList(Perception.C, Perception.NO_PIT, Perception.GOAL), Arrays.asList(Action.F, Action.TR, Action.TL));
eastBoundary.put(Arrays.asList(Perception.WALL, Perception.PIT, Perception.GOAL), Arrays.asList(Action.STOP));
transitionModel.put(State.EB, eastBoundary);
// 边界面向南状态
Map<List<Perception>, List<Action>> southBoundary = new HashMap<>();
southBoundary.put(Arrays.asList(Perception.C, Perception.PIT, Perception.GOAL), Arrays.asList(Action.F, Action.TR, Action.TL));
southBoundary.put(Arrays.asList(Perception.C, Perception.NO_PIT, Perception.GOAL), Arrays.asList(Action.F, Action.TR, Action.TL));
southBoundary.put(Arrays.asList(Perception.WALL, Perception.PIT, Perception.GOAL), Arrays.asList(Action.STOP));
transitionModel.put(State.SB, southBoundary);
// 边界面向西状态
Map<List<Perception>, List<Action>> westBoundary = new HashMap<>();
westBoundary.put(Arrays.asList(Perception.C, Perception.PIT, Perception.GOAL), Arrays.asList(Action.F, Action.TR, Action.TL));
westBoundary.put(Arrays.asList(Perception.C, Perception.NO_PIT, Perception.GOAL), Arrays.asList(Action.F, Action.TR, Action.TL));
westBoundary.put(Arrays.asList(Perception.WALL, Perception.PIT, Perception.GOAL), Arrays.asList(Action.STOP));
transitionModel.put(State.WB, westBoundary);
}
// 根据当前状态和感知返回下一步的行动
public Action chooseAction(State currentState, Perception perception) {
List<Action> possibleActions = transitionModel.get(currentState).get(Arrays.asList(perception));
if (possibleActions == null) {
return Action.STOP;
}
Random random = new Random();
return possibleActions.get(random.nextInt(possibleActions.size()));
}
}
任务2:public static void main(String[] args) {
Agent agent = new Agent();
// 测试easy_layout
System.out.println("============== Testing easy_layout ==============");
Object easyLayout = loadObject("easy_layout.pickle");
runGame(agent, easyLayout);
// 测试hard_layout
System.out.println("============== Testing hard_layout ==============");
Object hardLayout = loadObject("hard_layout.pickle");
runGame(agent, hardLayout);
}
public static Object loadObject(String filename) {
// 从文件中加载对象
// ...
}
public static void runGame(Agent agent, Object layout) {
// 运行游戏
// ...
}
任务3:public static void main(String[] args) {
Agent agent = new Agent();
int totalSteps = 0;
int totalGames = 30;
int winCount = 0;
int maxSteps = Integer.MIN_VALUE;
int minSteps = Integer.MAX_VALUE;
for (int i = 0; i < totalGames; i++) {
Object layout = generateRandomLayout();
int steps = runGame(agent, layout);
totalSteps += steps;
maxSteps = Math.max(maxSteps, steps);
minSteps = Math.min(minSteps, steps);
if (steps > 0) {
winCount++;
}
}
double averageSteps = (double) totalSteps / (double) totalGames;
double winPercentage = (double) winCount / (double) totalGames;
System.out.println("Total games: " + totalGames);
System.out.println("Total steps: " + totalSteps);
System.out.println("Average steps: " + averageSteps);
System.out.println("Win count: " + winCount);
System.out.println("Max steps: " + maxSteps);
System.out.println("Min steps: " + minSteps);
System.out.println("Win percentage: " + (winPercentage * 100) + "%");
}
/**
* 生成随机布局
*/
private static Object generateRandomLayout() {
// 实现随机布局生成的代码
}
/**
* 运行游戏并返回完成游戏时的步数
*/
private static int runGame(Agent agent, Object layout) {
// 使用agent和layout运行游戏的代码,并返回完成游戏时的步数
}
以上是完整的任务三代码,其中generateRandomLayout()函数和runGame()函数需要根据实际情况实现具体逻辑。这些函数的功能是生成随机布局和运行游戏,并返回完成游戏时的步数。在main()函数中,我们使用一个循环来运行30个游戏。每个游戏都会生成一个随机布局,然后使用生成的布局和代理运行游戏。在每个游戏结束后,我们会将该游戏的步数累加到总步数中,并更新最大步数和最小步数。如果游戏的步数大于0,表示代理赢得了游戏,我们会增加赢得游戏的计数。最后,我们计算平均步数和赢得游戏的百分比,并输出这些统计数据。}

我要回帖

更多关于 计算机编程中常见 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信