One AI Done… Mostly.
On my Ruby Uno clone called One…
I've now completed the AI part of the project. There were some bits that were harder than others and some bits that were easier. I also managed to include the full functionality of the previous version so now you can play a pickup 2 over the top of another pickup 2 (same with pickup 4).
I think the hardest part of designing the AI was figuring out how to start. I wrote and rewrote pages of pseudo code and diagrams over and over. It wasn’t until I actually started coding properly that I had the ah-ha moment and figured out how to do it reasonably concisely. I’ve got 3 difficulty levels. The easy setting actively tries to lose, the medium setting is random and the hard setting actively tries to win. The middle difficulty was by far the easiest — randomly picking a card from the available options. One line of code and I was done.
Its kind of disheartening when I get the computer to play itself and the random one wins…
The hardest one was the hard setting. Sounds pretty obvious doesn’t it.
I wanted to assess the card to play based on a score of the final hand after that card was played but I realised that it was a lot simpler to give the cards a score and choose based on that rather than calculating a score for the entire hand.
The easy difficulty was easy once the hard was done because it just required copying, pasting and then changing one line of code from a greater than symbol to a less than symbol. I could have put in some more subtle additions to the AI decision making but I wasn’t even sure if it would make it that much better and it would have been a lot of coding for very little result.
So the guts of the AI card scoring for the hard setting I can't really show you but once all the possible cards are scored, the computer simply chooses the highest value and plays that.
There is a bunch more if statements than I wanted but in the end it was hard to avoid them.
Left to do: getting the computer to play itself automatically, a set number of times and record the percentages of wins for each level of difficulty. Might have to leave this for another day. Please download and enjoy the game as it is here:One via Github