Saturday, July 22, 2017

Tic-Tac-Toe Haskell with Rule-Based AI

So recently I wanted to get back into programming, so I decided to dive in head first with Haskell, as it is one of my favorite languages. The aesthetic of the code with the style works great for me most of the time. After about four projects, I ran out of ideas. There was the obvious "Hello, world!", fibonacci sequence, quick sort, and a binary tree search. While these seemed great, they had no real immediate practical use for me to interact with. So I decided I should make a game. But what game would have seemingly simple enough rules to make a computer based player? Well, my two choices are Blackjack and Tic-Tac-Toe. Now Blackjack is super simple when it comes to a computer looking in, just statistics and seeing who's got the better hand. So Tic-Tac-Toe was the winner.

Now when a game of Tic-Tac-Toe is played perfectly, it's always a draw. Not necessarily fun for people to play, however it's good to know that if I get it perfectly that there is an expected outcome. With that in mind, I set out on my journey and created this monstrosity.

I chose to do rule based analysis simply because I wanted to break down the logic that way. Other methods I read about used minmax, which uses heuristics. My concern was if I mapped out every possibility as a means to the end, it's just simply path finding. I may try to adapt the rules to something a bit closer to real AI at some point in the future.

The way the rule based decisions work are as follows:
  1. Check for a move that it can win
  2. Check for any opponent wins to block
  3. If the center is available, take it
  4. If the center is taken, use the edges, otherwise use the corners
Now if we were to swap the roles, the rule for three and four would probably be different. I just haven't taken the time to work that out because it seemed like more effort than I wanted to do right now. For now, this is it.

No comments:

Post a Comment

Tag Cloud

.NET (2) A+ (5) ad ds (1) addon (4) Android (4) anonymous functions (1) application (9) arduino (1) artificial intelligence (1) backup (1) bash (6) camera (2) certifications (3) comptia (5) css (2) customize (11) encryption (3) error (13) exploit (5) ftp (1) funny (4) gadget (4) games (3) GUI (5) hardware (16) haskell (6) help (14) HTML (3) imaging (2) irc (1) it (1) java (2) javascript (13) jobs (1) Linux (19) lua (1) Mac (4) malware (1) math (6) msp (1) network (13) perl (2) php (3) plugin (2) powershell (8) privacy (2) programming (24) python (10) radio (2) regex (3) repair (2) security (16) sound (2) speakers (2) ssh (1) story (5) Techs from the Crypt (5) telnet (1) tools (13) troubleshooting (11) tutorial (9) Ubuntu (4) Unix (2) virtualization (2) web design (6) Windows (16) world of warcraft (1) wow (1) wx (1)