gpt-3.5-turbo-0125· openai
Suzhen/CodeChat
Create Java code for ai people crowd in game
- User
Create Java code for ai people crowd in game
- Assistant
Here is an example of Java code for creating an AI-controlled people crowd in a game:
import java.util.ArrayList; import java.util.Random; public class Crowd { private ArrayList<Person> people; public Crowd(int numPeople) { people = new ArrayList<>(); // Generate people with random positions Random rand = new Random(); for (int i = 0; i < numPeople; i++) { int x = rand.nextInt(100); int y = rand.nextInt(100); Person person = new Person(x, y); people.add(person); } } public void update() { // Move each person in the crowd for (Person person : people) { person.move(); } } // Class representing a person in the crowd private class Person { private int x; private int y; private int dx; private int dy; public Person(int x, int y) { this.x = x; this.y = y; this.dx = 1; // x direction this.dy = 1; // y direction } public void move() { // Update position x += dx; y += dy; // Change direction randomly Random rand = new Random(); if (rand.nextBoolean()) { dx = rand.nextInt(3) - 1; // -1, 0, or 1 } if (rand.nextBoolean()) { dy = rand.nextInt(3) - 1; // -1, 0, or 1 } } } public static void main(String[] args) { Crowd crowd = new Crowd(10); // Update crowd positions for a few iterations for (int i = 0; i < 5; i++) { crowd.update(); // Print updated positions for (Person person : crowd.people) { System.out.println("Person at position (" + person.x + ", " + person.y + ")"); } System.out.println(); } } }This code creates a Crowd class that contains a collection of Person objects. Each Person object has an x and y position, as well as a dx and dy direction for movement. The update method moves each person in the crowd and changes their direction randomly. The main method creates a crowd with 10 people and updates their positions for a few iterations.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.