WorldCoder, a Model-Based LLM Agent: Building World Models by Writing Code and Interacting with the Environment