Sunday, April 13, 2014

Weekly Reflection #10

This week I got to take what I learned last week and apply it to the creation of a tic tac toe game. This tic tac toe game was different from the hangman game where you have to guess the word. This chapter ventures into simple artificial intelligence. An AI(Artificial intelligence) is a computer program that can intelligently respond to the player’s moves. This game didn’t introduce any complicated new concepts. As we already know tic tac toe is a simple pencil and paper game played between two people, one is X and the other is O. On a simple nine square grid, the players can take turns placing their X or O on the board. If a player gets three of their marks on the board in a row(whether it be straight or diagonally), they win. Most games of tic tac toe end in a draw with neither players having three marks in a row. In this chapter, instead of having a second human the AI made moves against the user.


In the previous chapter I learned about ascii art, along with various string methods, and elif statements. Before I could actually utilize the AI I had to define a function called drawBoard. This function does exactly what the name implies. What good is tic tac toe without a board? I had to use ascii art to draw up the board.



The ascii art in code












After coding in the art you'd get a board that looks like this:
   |   |
   |   |
   |   |
-----------
   |   |
   |   |
   |   |
-----------
   |   |
   |   |
   |   |




But before I get ahead of myself, just like a couple chapters back, I actually had to use a flowchart, because programming an AI no matter how simple, isn't something that should be done without some sort of organization, you'll likely end up with bugs later on.












After I had that established, the reason I had to draw the board first was because I needed some sort of interface. Next I had to make the board represent a variable. With pen and paper playing tic tac toe, you'd just put the X or O in the empty space, in python, that empty space is represented by a list of strings. Each string represents one of the nine positions on the board. To make it easier to remember the location of each position the number pad was used. 

After I had the board layout figured out. The rest was pretty much just me following the flowchart layout. Ask a player's letter, randomly generate who goes first, wait for the move to be made, and after that constant checks of which player won. Creating a program that can play a game really comes down to carefully considering all the possible situations and outcomes. It's not so much the AI is thinking for itself it's really just using predetermined situations which you code for it. Compare to something like chess or checkers, tic tac toe is simple because there aren't too many outcomes. This AI just blocks the players move if the player is about to win, and tries to win if put in a position to. The AI chooses any empty space on the board, then the center, then the side.



No comments:

Post a Comment