top of page

A Framework for Writing Code with just AI: Here’s how I coded a strategy game in python with ChatGPT

The point of this is showing how chatgpt4.0 can be used to create semi complex programs from scratch and is eerily similar to having a game programmer sit next to me and coding the project whilst I keep refining the game. The game runs on python, you can test it out here:



It’s a PVP game 50/50 chance of winning (so coin toss basically), with 5 power ups, of which the player can choose two for each game. Each game consists of multiple rounds until a player wins 5 rounds. If there is a tie break at 5, it’s whoever gets one point ahead after 5:5 tie that wins. Players may choose the same side of the coin. Once consumed for a particular round, the power up cannot be re-used for that game. The power ups are:


1. Double Points (Scoring 2 points for winning one round)

2. Reverse (Reversing the opponent’s decision)

3. Peak (Knowing the future outcome of the toss result of the round before you make your choice)

4. Swap (Swapping your opponent’s points with your points)

5. Freeze (Opponent unable to gain point in that round even if they guessed right)


As you can see this makes for very interesting combinations of power ups (e.g. double points together with peak coin), match ups and strategies.


So now that we have summarized the game, these are my major learnings for using ChatGPT for coding more complex programs:


1. The prompt engineering tutorials on Youtube and Twitter giving you a “killer prompt” asking ChatGPT working step by step and asking clarifying questions etc, yes that works, and still must be used, but its much more complex than that for complex tasks.

2. Give ChatGPT its role, either software engineer or author or researcher or data analyst etc. Keep reminding it of its role every now and then.

3. Break the code into different sections. ChatGPT has limitations into how many lines of code it can interpret, and how many lines of code it can give you. For me ultimately, it was breaking the code into 3 different sections. The definitions and setup, the main game loop, and the ending loop (calculating the winner). For coding complex projects, you may wish to have some trial chats on bits and pieces of different functions with ChatGPT before forming a solid strategy in your mind on how to separate the sections.

4. For every step forward, remind ChatGPT of the newest versions of the code, section by section, everytime something new implemented works. ChatGPT is like a baby in terms of its memory. For easy tasks its ok. To write a complex game, after a few iterations it starts giving you complete nonsense, new lines of code to some random version of the code that was 5 iterations or so before the latest one or the 6th or the 7th (you just don’t know) and you basically have to start everything again. To completely avoid the confusion, its easy. Every time it gives you successful piece of code and you have tested it and run it smoothly, re-update the sections of the code to ChatGPT4.0. Copy the lines of code by section, paste it back to ChatGPT, and tell it that this is now the most updated version, remember it, and we will be updating the code from these new sections from now on. DO that every single time there is success with the code without fail, otherwise things will turn very confusing and messy, fast.

5. For every single iteration, update, change and addition to the code that ChatGPT writes for you, run it, test it, debug it then confirm the change with chatGPT by doing (4). There are two types of debugging. 1.) Actual programming errors or syntax errors. That’s easy. Just copy back the code that was tested together with the result that you tested and the bug and paste it inside the chatGPT chatbox, and it will help you debug. 2.) The other type is not programming errors but things that you have decided you need to change. Maybe rules of the game, the game logic or the sequencing of events. For those types of changes use step (6) below.

6. Have a process for brainstorming of task, confirmation of task, further clarification and analysis of task, ChatGPT writing the code, explaining the code, you testing the code, debugging the code, successfully running the code and reminding chatGPT of the latest iteration section by section of the code. That’s mainly the loop that I used for adding new power-ups or changes to the logic of the game.

7. Lastly, every now and then, remind (copy and paste) your framework and process of working (step 6) to ChatGPT again. Its keeps forgetting after a while, and you constantly need to remind it.


Using these steps, ChatGPT will be your own software engineer, or at least assist you quite well in your programming tasks.


You can see the full script here, but its long:


Comments


bottom of page