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
You run the code by typing
where choice is one of the above integers. Read the comments in the beginning of the function if the output is unclear, which can be done by typing
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!
• Release date of this version: August 2015
• Version of MATLAB I used: R2011b
• If you find bugs or have questions, leave me a message at www.BradleyKnockel.com
• This code is under the BSD license, so you can mostly do what you want with it (just make sure you give credit to me, Bradley Knockel)
Bradley Knockel (2020). Explore Winning Games of Tic-tac-toe (https://www.mathworks.com/matlabcentral/fileexchange/52720-explore-winning-games-of-tic-tac-toe), MATLAB Central File Exchange. Retrieved .
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!