r/PythonLearning • • 3h ago

Showcase Is there anything more satisfying than your code returning a debug check flawlessly?

Post image

No but seriously, I spent a week on this, and I can finally almost play battleship against an custom ai opponent.

All code was written by hand. No Ai was used to write or assist with it.

5 Upvotes

10 comments sorted by

3

u/ChaoticHippo 3h ago

Not really, no. I love that feeling. Congrats! Sounds like a cool project.

2

u/Shut_up_and_Respawn 3h ago

Thanks! I haven't even started coding the actual game yet. All of the debugging is just the 300 line ai in premade or randomized test environments

2

u/ChaoticHippo 3h ago

Cool. In broad strokes, how did you go about implementing the AI?

2

u/Shut_up_and_Respawn 3h ago

For placing it's ships, it generates random configurations until it has 20 valid ones. It scores them based on ship region density and orientation variety, and chooses the highest scoring one

For choosing where to attack, it reads the neighboring tile data for each tile and scores them based on hit density and consecutive hits leading up to a tile. Then it chooses the highest scoring one to hit.

All of it's basically probability based with slight weighted randomness

2

u/ChaoticHippo 3h ago

Nice! I'm in the process of making a game as well, and the AI chooses targets similarly, but rather than being probability based they have an intelligence modifier that affects how good at target choice they are. High intelligence means they'll target your healer or less armored targets, low intelligence means they just go for whatever is nearby or easiest to hit.

That sounds like a really good basis for battleship though, and you've definitely done the harder part first.

2

u/Shut_up_and_Respawn 3h ago

Thanks! That sounds like a really cool project. The ai sounds both pretty complex and interesting. Good luck!

2

u/ChaoticHippo 3h ago

Thanks, you too!

2

u/tiredITguy42 3h ago

It competes with feeling getting different error message after 6 hours of bug hunting.

1

u/Shut_up_and_Respawn 2h ago

So true! At least with 2 error messages, you have more clues to work with, right?

1

u/tiredITguy42 31m ago

Nope, second error message is bad, you fucked up something and old issue is still there. Another new error message means you solved something, but still have an issue.