Author Topic: Tic-Tac-Toe Tutorial  (Read 4537 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Tic-Tac-Toe Tutorial
« on: January 31, 2021, 03:09:39 pm »
The start of a simple little Tic-Tac-Toe Tutorial.  Folks have been writing little games for this in QB64, so I thought I'd take a moment to help showcase the logic of the game.

Text is resizable and auto-formats, with the up and down arrow keys.
Right arrows move forward a page/lesson.
Left arrows move back, so you can compare, if needed, to see what happened.
Escape, of course, let's you quit at any time.

Code: QB64: [Select]
  1.  
  2. DIM SHARED ViewScreen AS LONG, Font AS LONG
  3.  
  4. ViewScreen = _NEWIMAGE(640, 480, 32)
  5. SCREEN ViewScreen
  6. fontsize = 20
  7. Font = _LOADFONT("courbd.ttf", fontsize, "monospace")
  8. _FONT Font
  9.  
  10.     Resizer
  11.     Lessons num
  12.     k = _KEYHIT
  13.     SELECT CASE k
  14.         CASE 27 'escape
  15.             SYSTEM
  16.         CASE 19200 'previous
  17.             IF num > 0 THEN num = num - 1
  18.         CASE 18432 'up font
  19.             IF fontsize < 128 THEN
  20.                 fontsize = fontsize + 2
  21.                 temp = _LOADFONT("courbd.ttf", fontsize, "monospace")
  22.                 _FONT temp
  23.                 _FREEFONT Font
  24.                 Font = temp
  25.             END IF
  26.         CASE 20480 'down font
  27.             IF fontsize > 6 THEN
  28.                 fontsize = fontsize - 2
  29.                 temp = _LOADFONT("courbd.ttf", fontsize, "monospace")
  30.                 _FONT temp
  31.                 _FREEFONT Font
  32.                 Font = temp
  33.             END IF
  34.         CASE 19712 'next
  35.             IF num < 123 THEN num = num + 1
  36.     END SELECT
  37.     _LIMIT 30
  38.     _DISPLAY
  39.  
  40.  
  41.  
  42. SUB Lessons (num)
  43.     STATIC DoOnce
  44.     SELECT CASE num
  45.         CASE 0 'just print
  46.             CLS
  47.             WordWrap "Hello World."
  48.             WordWrap "This is Steve's Tuturial on Tic-Tac-Toe!"
  49.             PRINT
  50.             WordWrap "Press <RIGHT ARROW> to move forward in the lessons."
  51.         CASE 1
  52.             CLS
  53.             WordWrap "How many places can one start at in Tic-Tac-Toe?  Let's look at the board below to find out!"
  54.             DrawBoard ""
  55.         CASE 2
  56.             CLS
  57.             WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
  58.             WordWrap "Current count = 1"
  59.             DrawBoard "X"
  60.         CASE 3
  61.             CLS
  62.             WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
  63.             WordWrap "Current count = 2"
  64.             DrawBoard ".X"
  65.         CASE 4
  66.             CLS
  67.             WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
  68.             WordWrap "Current count = 3"
  69.             DrawBoard "..X"
  70.         CASE 5
  71.             CLS
  72.             WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
  73.             WordWrap "Current count = 4"
  74.             DrawBoard "...X"
  75.         CASE 6
  76.             CLS
  77.             WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
  78.             WordWrap "Current count = 5"
  79.             DrawBoard "....X"
  80.         CASE 7
  81.             CLS
  82.             WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
  83.             WordWrap "Current count = 6"
  84.             DrawBoard ".....X"
  85.         CASE 8
  86.             CLS
  87.             WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
  88.             WordWrap "Current count = 7"
  89.             DrawBoard "......X"
  90.         CASE 9
  91.             CLS
  92.             WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
  93.             WordWrap "Current count = 8"
  94.             DrawBoard ".......X"
  95.         CASE 10
  96.             CLS
  97.             WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
  98.             WordWrap "Current count = 9"
  99.             DrawBoard "........X"
  100.         CASE 11
  101.             CLS
  102.             WordWrap "There's only 9 spots on a Tic-Tac-Toe board, so our first move has to be in one of those nine spaces!"
  103.             PRINT
  104.             WordWrap "BUT..."
  105.             PRINT
  106.             WordWrap "Tic-Tac-Toe is a game of SYMMETRY.  There's really only THREE positions to consider in the game."
  107.         CASE 12
  108.             CLS
  109.             WordWrap "The outer corners."
  110.             DrawBoard "X.X...X.X"
  111.         CASE 13
  112.             CLS
  113.             WordWrap "The middle edges."
  114.             DrawBoard ".X.X.X.X."
  115.         CASE 14
  116.             CLS
  117.             WordWrap "And the center of it all -- the center!"
  118.             DrawBoard "....X...."
  119.         CASE 15
  120.             CLS
  121.             WordWrap "So, to start this tuturial out, let's start with X in one of the OUTER CORNERS."
  122.             DrawBoard "X"
  123.         CASE 16
  124.             CLS
  125.             WordWrap "Now, O has 8 different places they can move. Let's look at all those possible moves, and I'll show you what X's next move should be."
  126.             WordWrap "First, O goes right beside X...  X then goes to their opposite free corner."
  127.             DrawBoard "XO....X.."
  128.         CASE 17
  129.             CLS
  130.             WordWrap "This is a losing position for O, as here would be the next 2 moves played out:"
  131.             WordWrap "O blocks the win, X takes the middle..."
  132.             DrawBoard "XO.OX.X.."
  133.         CASE 18
  134.             CLS
  135.             WordWrap "So what if O would have went to the far corner instead?"
  136.             WordWrap "X then goes to their diagional free corner."
  137.             DrawBoard "X.O.....X"
  138.         CASE 19
  139.             CLS
  140.             WordWrap "O is now forced to block in the center, and X then blocks O."
  141.             WordWrap "Guess who just lost?"
  142.             DrawBoard "X.O.O.X.X"
  143.         CASE 20
  144.             CLS
  145.             WordWrap "So, let's just continue on around the edges from there.  What happens when O is placed in the right middle edge?  Remember what I said about SYMMETRY before?"
  146.             WordWrap "Doesn't this set up look just like what we saw previously with O in the top middle?"
  147.             DrawBoard "X....OX.."
  148.         CASE 21
  149.             CLS
  150.             WordWrap "Yep.  Same predictible results.  O loses."
  151.             DrawBoard "X..OXOX.."
  152.         CASE 22
  153.             CLS
  154.             WordWrap "And if O were to move down to the next open position, it's another outer corner."
  155.             WordWrap "X, once again, just takes the opposite corner for the win."
  156.             DrawBoard "X.....X.O"
  157.         CASE 23
  158.             CLS
  159.             WordWrap "Next space on the rotation would be the bottom middle... Which is just like the other two middles we've seen so far..."
  160.             WordWrap "Can you see why O loses here?"
  161.             DrawBoard "X.X....O."
  162.         CASE 24
  163.             CLS
  164.             WordWrap "And around we go, with another corner, which plays like the other corners for O.."
  165.             DrawBoard "X.X...O.."
  166.         CASE 25
  167.             CLS
  168.             WordWrap "And the last move around the edge is right below X, in the left middle... Which results in... The same as before!"
  169.             DrawBoard "X.XO....."
  170.         CASE 26
  171.             CLS
  172.             WordWrap "So, as we've seen, if X starts on an outer edge, if the next move by O is on an outside tile, then X wins!"
  173.             WordWrap "The whole game is decided by the first move!"
  174.             WordWrap "O's *ONLY* possible move is into the middle, which will force a draw unless someone is honestly just REALLY stupid!"
  175.             DrawBoard "X...O...."
  176.         CASE 27
  177.             CLS
  178.             WordWrap "So in conclusion here: IF X starts with any OUTSIDE CORNER, the *ONLY* place O can go -- without losing -- is to the center."
  179.             WordWrap "*ALL* other moves lets X manulate the board for a guaranteed win."
  180.             DrawBoard "X...O...."
  181.         CASE 28
  182.             CLS
  183.             WordWrap "And, since the game is SYMMETRICAL, as we've seen so far, the *EXACT* same pattern plays out if X starts in *ANY* of the OUTER CORNERS."
  184.             WordWrap "IF X takes an OUTER CORNER, the *ONLY* move O can make is to take the center, or lose."
  185.         CASE 29
  186.             CLS
  187.             WordWrap "Which then leads to the question: What if X takes the center on their first move?"
  188.             DrawBoard "....X...."
  189.         CASE 30
  190.             CLS
  191.             WordWrap "O basically only has two choices here: OUTER EDGE or MIDDLE EDGE.  Let's look at them both, starting with O taking a MIDDLE EDGE..."
  192.             WordWrap "O takes a middle, X takes an outer beside it."
  193.             DrawBoard ".OX.X...."
  194.         CASE 31
  195.             CLS
  196.             WordWrap "O has to block, and now X can take the other corner.  Guess who just won?"
  197.             DrawBoard ".OX.X.O.X"
  198.         CASE 32
  199.             CLS
  200.             WordWrap "So if X takes the center, and O takes a middle, then X wins!  First move determines the game once again."
  201.             WordWrap "IF O doesn't want to lose, all they can do is take an OUTER CORNER when X takes the center."
  202.             DrawBoard "O...X...."
  203.         CASE 33
  204.             CLS
  205.             WordWrap "So that means the only possible place left to talk about is if X starts in a MIDDLE EDGE position."
  206.             DrawBoard ".X......."
  207.         CASE 34
  208.             CLS
  209.             WordWrap "And honestly, there's not to much to say about this position, except *DON'T*."
  210.             WordWrap "There's *NO* move that O can make which guarantees X a win if they start off with an OUTER MIDDLE tile."
  211.             WordWrap "Unless someone just plays terribly on purpose, these games will end in a draw."
  212.             WordWrap "ONLY start there if you're playing with an elementry grade child."
  213.         CASE 35
  214.             CLS
  215.             WordWrap "So, to basically sum up:"
  216.             WordWrap "Tic-Tac-Toe is basically a game that is won in the first move."
  217.             WordWrap "X's two possible moves are OUTER EDGE and CENTER."
  218.             WordWrap "O's counters are CENTER and OUTER EDGE."
  219.             WordWrap "Remember those two things, and you have become almost unbeatable at Tic-Tac-Toe.  (Unless just pure exhaustion or disinterest causes you to make a mismove after that...)"
  220.         CASE 36
  221.             CLS
  222.             WordWrap "AND THAT'S ALL THERE IS TO TIC-TAC-TOE!"
  223.         CASE 37
  224.             CLS
  225.             COLOR &HFFFF0000, 0
  226.             WordWrap "X's two possible starting moves are OUTER EDGE and CENTER."
  227.             WordWrap "O's counters are CENTER and OUTER EDGE."
  228.         CASE 38
  229.             SYSTEM
  230.     END SELECT
  231.  
  232.  
  233. SUB DrawBoard (text$)
  234.     STATIC board, boardfont
  235.     yStart = CSRLIN * _FONTHEIGHT(Font)
  236.     d = _DEST: s = _SOURCE
  237.     IF board = 0 THEN
  238.         board = _NEWIMAGE(600, 600, 32)
  239.         boardfont = _LOADFONT("courbd.ttf", 196, "monospace")
  240.     END IF
  241.     _DEST board: _SOURCE board
  242.     CLS
  243.     LINE (190, 0)-STEP(20, 600), Yellow, BF
  244.     LINE (390, 0)-STEP(20, 600), Yellow, BF
  245.     LINE (0, 190)-STEP(600, 20), Yellow, BF
  246.     LINE (0, 390)-STEP(600, 20), Yellow, BF
  247.     _FONT boardfont
  248.     FOR i = 0 TO 8
  249.         temp$ = MID$(text$, i + 1, 1)
  250.         SELECT CASE temp$
  251.             CASE "X", "x"
  252.                 COLOR Red, 0
  253.                 _PRINTSTRING ((i MOD 3) * 200 + 40, (i \ 3) * 200 + 10), "X"
  254.             CASE "O", "o", "0"
  255.                 COLOR Green, 0
  256.                 _PRINTSTRING ((i MOD 3) * 200 + 40, (i \ 3) * 200 + 10), "O"
  257.         END SELECT
  258.     NEXT
  259.     _DEST d: _SOURCE s
  260.     _PUTIMAGE (0, yStart)-(_WIDTH, _HEIGHT), board
  261.  
  262.  
  263. SUB WordWrap (text AS STRING)
  264.     DIM BreakPoint AS STRING
  265.     BreakPoint = ",./- ;:!" 'I consider all these to be valid breakpoints.  If you want something else, change them.
  266.     w = _WIDTH
  267.     pw = _PRINTWIDTH(text)
  268.     x = POS(0): y = CSRLIN
  269.     IF _PIXELSIZE <> 0 THEN x = x * _FONTWIDTH
  270.     firstlinewidth = w - x + 1
  271.     IF pw <= firstlinewidth THEN
  272.         PRINT text
  273.     ELSE
  274.         'first find the natural length of the line
  275.         FOR i = 1 TO LEN(text)
  276.             p = _PRINTWIDTH(LEFT$(text, i))
  277.             IF p > firstlinewidth THEN EXIT FOR
  278.         NEXT
  279.         lineend = i - 1
  280.         t$ = RTRIM$(LEFT$(text, lineend)) 'at most, our line can't be any longer than what fits the screen.
  281.         FOR i = lineend TO 1 STEP -1
  282.             IF INSTR(BreakPoint, MID$(text, i, 1)) THEN lineend = i: EXIT FOR
  283.         NEXT
  284.         PRINT LEFT$(text, lineend)
  285.         WordWrap LTRIM$(MID$(text, lineend + 1))
  286.     END IF
  287.  
  288. SUB Speak (text$)
  289.     text$ = CHR$(34) + "(pause), " + text$ + CHR$(34)
  290.     OPEN "temp.ps1" FOR OUTPUT AS #1
  291.     PRINT #1, "Add-Type -AssemblyName System.Speech; "
  292.     PRINT #1, "$voice = New-Object System.Speech.Synthesis.SpeechSynthesizer; "
  293.     PRINT #1, "$voice.SelectVoice(" + CHR$(34) + "Microsoft Zira Desktop" + CHR$(34) + "); "
  294.     PRINT #1, "$voice.Speak(" + text$ + "); "
  295.     CLOSE #1
  296.     SHELL _HIDE "powershell ./temp.ps1"
  297.  
  298. SUB Resizer
  299.         _DELAY .5
  300.         x = _RESIZEWIDTH
  301.         y = _RESIZEHEIGHT
  302.         temp = _NEWIMAGE(x, y, 32)
  303.         SCREEN temp
  304.         _FREEIMAGE ViewScreen
  305.         ViewScreen = temp
  306.         _FONT Font
  307.     END IF

Try it out and let me know how it looks.  ;)


Side note:  Screen is also fully resizable, and auto-formats itself to adjust to the size you make it.
« Last Edit: January 31, 2021, 09:59:27 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Tic-Tac-Toe Tutorial
« Reply #1 on: January 31, 2021, 03:15:47 pm »
If this is teaching material, I strongly strongly suggest targeting a medium more readable. HTML with code blocks? A Word doc you save to PDF? Anything but a monolith BAS file. Just my humble opinion.

Okay, let's run it...

Looks great. But way too linear. Coders refer to things like reference books - flip to the section you want, glance, and move on. But this is a slideshow.

Suggestion:

Finish making the graphics, screenshot each one, and then make this a real document. Even a PPT presentation so it can read EXACTLY the same. Keep the BAS code as an optional reference.
« Last Edit: January 31, 2021, 03:20:12 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Tic-Tac-Toe Tutorial
« Reply #2 on: January 31, 2021, 10:04:41 pm »
If this is teaching material, I strongly strongly suggest targeting a medium more readable. HTML with code blocks? A Word doc you save to PDF? Anything but a monolith BAS file. Just my humble opinion.

Okay, let's run it...

Looks great. But way too linear. Coders refer to things like reference books - flip to the section you want, glance, and move on. But this is a slideshow.

Suggestion:

Finish making the graphics, screenshot each one, and then make this a real document. Even a PPT presentation so it can read EXACTLY the same. Keep the BAS code as an optional reference.

Bah!  Who wants to write a boring old PDF?  Tic-Tac-Toe is a fairly boring subject to start with.  If I didn't have fun jazzing it up with some bells and whistles (such as the resizing routines and word wrap), I'd fall asleep working on it.

And this isn't really anything a person needs as a reference tool.  Run it from start to finish, take in the simple lesson, and you're done with it forever.  The rule of Tic-Tac-Toe is just like the rule for spelling: I before E, except after C.  See it.  Learn it.  Be done with it.  You don't need to go back and look it up again in the future.  ;P
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Tic-Tac-Toe Tutorial
« Reply #3 on: January 31, 2021, 10:46:00 pm »
Understood, just looking out for your legacy, that's all. :-)

Plus Tutorial.pdf is a lot less suspicious than Tutorial.exe.

The code in itself it a tutorial, that's a pretty tight way to draw the 3x3 grid!

You're not done when it works, you're done when it's right.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Tic-Tac-Toe Tutorial
« Reply #4 on: January 31, 2021, 10:59:34 pm »
Understood, just looking out for your legacy, that's all. :-)

Plus Tutorial.pdf is a lot less suspicious than Tutorial.exe.

The code in itself it a tutorial, that's a pretty tight way to draw the 3x3 grid!

If you notice, there’s also a routine for text-to-speech in there.  I’d considered having it read the lesson aloud for you, but hit a snag.  My speakers are connected via bluetooth, so powershell seems to take a half second before it actually starts speaking.

I type Speak “Hello World”, my speakers say, “World.”

So, for personal use, I tack on a “(pause), “ to all speech.  The hello world becomes Speak “(pause), Hello World”, which ends up actually saying, “Hello World.”

Since I didn’t know if this initial lag holds true for all users, I left the speech calls out.  I figured nothing would be any more annoying than having it read “pause” at the start of every call.

Feel free to test it out and let me know how/if it works for you (as long as you’re on a windows machine).
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Tic-Tac-Toe Tutorial
« Reply #5 on: January 31, 2021, 11:30:24 pm »
Thanks Steve, I never knew this. Although playing my game a bunch of times lead me to believe these kinds of things, although I never contemplated them in my mind completely. Mostly, I just made Tic-Tac-Toe back in 2019 for fun, nothing else. All my life I've known it wasn't a challenging game really. So the last couple days I just made it look better mostly.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Tic-Tac-Toe Tutorial
« Reply #6 on: January 31, 2021, 11:38:33 pm »


Oh ha! well I'll be...

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Tic-Tac-Toe Tutorial
« Reply #7 on: January 31, 2021, 11:47:47 pm »


Oh ha! well I'll be...

And that’s basically my example turned into a video, except he didn’t cover the way to win if you start in the center. ;)

Coding an *unbeatable* Tic-Tac-Toe engine is actually only a few simple lines.  As I said before, it’s not a game which you WIN at — it’s a game where the next guy screws up and makes a mistake.



The best Tic-Tac-Toe game is the following, where both win:

X goes top left.

X..
...
...

O goes to the right.
XO.
...
...

X goes to the right.
XOX
...
...

Continue around:
XOX
..O
...

XOX
..O
..X

XOX
..O
.OX

XOX
..O
XOX

XOX
O.O
XOX

XOX
OXO
XOX

Game is over, and now you can draw a giant X for X’s win, and a giant O for O’s win just by connecting their tiles.  ;D
« Last Edit: January 31, 2021, 11:58:12 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Tic-Tac-Toe Tutorial
« Reply #8 on: February 01, 2021, 12:00:44 am »
Man I have to go back and fix my AI program! Learned something new...
« Last Edit: February 01, 2021, 12:25:39 am by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Tic-Tac-Toe Tutorial
« Reply #9 on: February 01, 2021, 12:13:31 am »
And who knew so many people would be interested in such a simple little program!  O_O!
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Tic-Tac-Toe Tutorial
« Reply #10 on: February 01, 2021, 12:19:10 am »
Awww, a computer that wins every time is no fun ;D

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Tic-Tac-Toe Tutorial
« Reply #11 on: February 01, 2021, 02:17:19 am »
I’ll have to modify this a little bit tomorrow.  I wrote wrong when I said there was NO way for X to win from an outer middle tile...

The rule of Tic-Tac-Toe is “CORNER and CENTER”.  Control those to win.  Control those to block.  The only way you can win by starting in the middle edge, is if your opponent ignores those rules as well.

=X=
===
===

=X=
O==
===

=X=
OX=
===

=X=
OX=
=O=

=XX
OX=
=O=

It still involves the same basic rule — corner + center — but the only way you’ll ever win with this set-up is basically if the other guy just lets you.

The rule to win is Corner and Center.  The rule to block is Center and Edge.  If both players are ignoring that, who knows what might happen!

XX=
OO=
X=O
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Tic-Tac-Toe Tutorial
« Reply #12 on: February 01, 2021, 03:30:21 am »
.
« Last Edit: February 01, 2021, 03:42:27 am by NOVARSEG »