hellpig/tic-tac-toe
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Repository files navigation
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<style type="text/css">
p{font-family:monospace;white-space:pre-wrap}
</style>
<title>readme.html</title>
</head>
<body>
<p>
There are three standalone files that can be run using Python 3 in a terminal (on Windows, in PowerShell or the Command Prompt).
_____________
my goal for TicTacToe.py
¯¯¯¯¯¯¯¯¯¯¯¯¯
I output all winning games to the 12 ways to start a Tic-tac-toe game. Neither player is allowed to ignore an immediate win, and, after checking for a win, neither player is allowed to ignore blocking an immediate win. I prevent the most obvious symmetric solutions from appearing more than once by preventing "repeated" boards in the first three moves. Here are the 12 ways to start a Tic-tac-toe game...
(1) middle → side
(2) middle → corner
(3) corner → middle
(4) corner → adjacent side
(5) corner → opposite side
(6) corner → adjacent corner
(7) corner → opposite corner
(8) side → middle
(9) side → adjacent corner
(10) side → opposite corner
(11) side → adjacent side
(12) side → opposite side
A game of Tic-tac-toe is short enough to simply remember what to do for various cases. You can verify by running the code that...
• if Player 1 moves into the center, Player 2 should move into a corner (else Player 1 can force a win), then Player 1 should choose the opposite corner
• if Player 1 moves into the corner, Player 2 should choose center (else Player 1 can force a win), then Player 1 should choose the opposite corner
However, these two start moves get boring very quickly, so the main goal is to explore the more complex and interesting situation when Player 1 begins on the side. If you're Player 1, I recommend sometimes starting on the side because your opponent may not know what to do!
See my comments at the top of the code to get started!
_____________
my goal for TicTacToe_MakeSingleMove.py
¯¯¯¯¯¯¯¯¯¯¯¯¯
This file takes the idea of the previous file to help you with which choice to make next given a Tic-tac-toe board.
See my comments at the top of the code to get started!
This code sets the stage for the next file, TicTacToe_AI.py, which copies over the exact same analyzeMoves() function.
_____________
my goal for TicTacToe_AI.py
¯¯¯¯¯¯¯¯¯¯¯¯¯
Play Tic-tac-toe with the computer, but the computer will never let you win.
My goal is to make the experience as interesting as possible, so the computer will not necessarily greedily try to win. That is, the computer will not necessarily keep trying the best moves each game. Depending on how the Greediness variable is set, you will get different experiences...
• Greediness = 0: computer prevents you from having the opportunity to force it to lose
• Greediness = 1: computer will also avoid situations that are a draw
• Greediness = 2: computer will always favor situations that guarantee that it can win
I suppose you could set Greediness to -1 to have the computer do more random moves, while still always completing any almost-complete line or blocking your almost-complete line.
If the computer is not avoiding a couple types of situations, to keep gameplay interesting, it will randomly choose between the allowed locations. If the computer is avoiding a couple types of situations, it will avoid the worse situation more.
_____________
more info
¯¯¯¯¯¯¯¯¯¯¯¯¯
• If you find bugs or have questions, leave me a message at www.BradleyKnockel.com
• This code is under the MIT license, so you can mostly do what you want with it (just make sure you give credit to me, Bradley Knockel)
</p>
</body>
</html>