有一个包含1000X1000个格子的地面,上面有n个由程序控制的人物,每个格子有四种可能 1)工作地点 2)普通路面 3)障碍物 4)休息地点 游戏规则 规则1:人物可在普通路面移动,可进入工作地点工作,可进入休息地点休息,但不可移动穿过工作地点和休息地点,不可移动到障碍物 规则2:人物移动一个格子需要的时间为1,进入工作地点工作需要时间为10,进入休息地点休息需要时间为5 规则3:人物在工作地点工作一次,获得1点收入,工作一次后必须进行一次休息才可以继续工作 规则4:每个人物的休息地点是固定的,工作地点可任意 规则5:每个工作地点同时只能容纳一个人物进行工作 程序逻辑 1、随机生成格子,其中工作地点为100个,障碍物为500个,休息地点,人物均为100个(人物需要与休息地点绑定),人物初始状态为可进行工作,人物初始位置为在对应休息地点内,其他为路面 2、使用伪随机数,将9999设置为伪随机数的初始值,需要每次同样的随机值生成的地图为一样的 3、执行1000000个工作时间,计算出获得总能量数,输出执行时间和获得总能量 写出java代码
时间: 2023-06-09 22:04:56 浏览: 111
```java
import java.util.*;
public class Game {
static final int GRID_SIZE = 1000;
static final int NUM_WORKPLACES = 100;
static final int NUM_OBSTACLES = 500;
static final int NUM_RESTAREAS = 100;
static final int NUM_PEOPLE = 100;
static final int WORK_DURATION = 10;
static final int REST_DURATION = 5;
static final int WORK_REWARD = 1;
static final int[] dx = { 0, 0, 1, -1 };
static final int[] dy = { 1, -1, 0, 0 };
static final int WORKPLACE = 1;
static final int ROAD = 2;
static final int OBSTACLE = 3;
static final int RESTAREA = 4;
static final int PERSON = 5;
static int[][] grid = new int[GRID_SIZE][GRID_SIZE];
static int[] restAreas = new int[NUM_PEOPLE];
static Person[] people = new Person[NUM_PEOPLE];
static Random rand = new Random(9999);
public static void main(String[] args) {
// 初始化地图
initMap();
// 执行工作时间
int totalEnergy = 0;
for (int i = 0; i < 1000000; i++) {
movePeople();
totalEnergy += work();
}
System.out.println("执行时间:" + 1000000);
System.out.println("获得总能量:" + totalEnergy);
}
// 初始化地图
public static void initMap() {
// 生成工作地点
for (int i = 0; i < NUM_WORKPLACES; i++) {
int x = rand.nextInt(GRID_SIZE);
int y = rand.nextInt(GRID_SIZE);
while (grid[x][y] != 0) {
x = rand.nextInt(GRID_SIZE);
y = rand.nextInt(GRID_SIZE);
}
grid[x][y] = WORKPLACE;
}
// 生成障碍物
for (int i = 0; i < NUM_OBSTACLES; i++) {
int x = rand.nextInt(GRID_SIZE);
int y = rand.nextInt(GRID_SIZE);
while (grid[x][y] != 0) {
x = rand.nextInt(GRID_SIZE);
y = rand.nextInt(GRID_SIZE);
}
grid[x][y] = OBSTACLE;
}
// 生成休息地点和人物,并将人物与休息地点绑定
for (int i = 0; i < NUM_RESTAREAS; i++) {
int x = rand.nextInt(GRID_SIZE);
int y = rand.nextInt(GRID_SIZE);
while (grid[x][y] != 0) {
x = rand.nextInt(GRID_SIZE);
y = rand.nextInt(GRID_SIZE);
}
grid[x][y] = RESTAREA;
restAreas[i] = x * GRID_SIZE + y;
int px = rand.nextInt(GRID_SIZE);
int py = rand.nextInt(GRID_SIZE);
while (grid[px][py] != 0 || hasPersonAt(px, py)) {
px = rand.nextInt(GRID_SIZE);
py = rand.nextInt(GRID_SIZE);
}
grid[px][py] = PERSON;
people[i] = new Person(px, py, i);
}
}
// 移动人物
public static void movePeople() {
for (Person p : people) {
if (p.status == Person.WORKING) {
// 工作状态不移动
continue;
}
int rx = restAreas[p.id] / GRID_SIZE;
int ry = restAreas[p.id] % GRID_SIZE;
if (p.x == rx && p.y == ry) {
// 在休息地点内,不移动
continue;
}
int minDist = Integer.MAX_VALUE;
int[] nearestPos = new int[2];
for (int i = 0; i < dx.length; i++) {
int nx = p.x + dx[i];
int ny = p.y + dy[i];
if (isValidPos(nx, ny)) {
int dist = Math.abs(nx - rx) + Math.abs(ny - ry);
if (dist < minDist) {
minDist = dist;
nearestPos[0] = nx;
nearestPos[1] = ny;
}
}
}
p.x = nearestPos[0];
p.y = nearestPos[1];
grid[nearestPos[0]][nearestPos[1]] = PERSON;
}
}
// 执行工作操作
public static int work() {
int energy = 0;
List<Person> workingPeople = new ArrayList<>();
for (Person p : people) {
if (p.status == Person.WORKING) {
workingPeople.add(p);
}
}
for (Person p : workingPeople) {
int wx = getWorkplaceX();
int wy = getWorkplaceY();
p.work(wx, wy);
energy += WORK_REWARD;
}
return energy;
}
// 获取工作地点的 x 坐标
public static int getWorkplaceX() {
int wx = rand.nextInt(GRID_SIZE);
int wy = rand.nextInt(GRID_SIZE);
while (grid[wx][wy] != WORKPLACE) {
wx = rand.nextInt(GRID_SIZE);
wy = rand.nextInt(GRID_SIZE);
}
grid[wx][wy] = 0;
return wx;
}
// 获取工作地点的 y 坐标
public static int getWorkplaceY() {
int wx = rand.nextInt(GRID_SIZE);
int wy = rand.nextInt(GRID_SIZE);
while (grid[wx][wy] != 0) {
wx = rand.nextInt(GRID_SIZE);
wy = rand.nextInt(GRID_SIZE);
}
grid[wx][wy] = WORKPLACE;
return wy;
}
// 判断指定位置是否为空位置
public static boolean isValidPos(int x, int y) {
if (x < 0 || x >= GRID_SIZE || y < 0 || y >= GRID_SIZE) {
return false;
}
if (grid[x][y] == OBSTACLE || grid[x][y] == WORKPLACE) {
return false;
}
return true;
}
// 判断指定位置是否已有人物
public static boolean hasPersonAt(int x, int y) {
return grid[x][y] == PERSON;
}
}
class Person {
static final int IDLE = 0;
static final int WORKING = 1;
int x;
int y;
int id;
int status;
int restCount;
public Person(int x, int y, int id) {
this.x = x;
this.y = y;
this.id = id;
this.status = IDLE;
this.restCount = REST_DURATION;
}
// 与指定坐标的工作地点匹配,开始工作
public void work(int wx, int wy) {
if (x == wx && y == wy) {
status = WORKING;
restCount = 0;
}
}
// 工作一段时间,返回是否工作完成
public boolean working() {
restCount--;
if (restCount == 0) {
status = Person.IDLE;
restCount = Person.REST_DURATION;
return true;
}
return false;
}
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)