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.
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