12-24-2023, 10:49 AM
Browsing and moving things into the new subforum for games, I stumbled across this little tutorial I did ages ago, and I thought I'd share it here once again. This will teach you how to become an unbeatable player at Tic-Tac-Toe -- guaranteed!
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, lets you quit at any time.
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.
Code: (Select All)
$Color:32
Dim Shared ViewScreen As Long, Font As Long
ViewScreen = _NewImage(640, 480, 32)
Screen ViewScreen
$Resize:On
fontsize = 20
Font = _LoadFont("courbd.ttf", fontsize, "monospace")
_Font Font
Do
Resizer
Lessons num
k = _KeyHit
Select Case k
Case 27 'escape
System
Case 19200 'previous
If num > 0 Then num = num - 1
Case 18432 'up font
If fontsize < 128 Then
fontsize = fontsize + 2
temp = _LoadFont("courbd.ttf", fontsize, "monospace")
_Font temp
_FreeFont Font
Font = temp
End If
Case 20480 'down font
If fontsize > 6 Then
fontsize = fontsize - 2
temp = _LoadFont("courbd.ttf", fontsize, "monospace")
_Font temp
_FreeFont Font
Font = temp
End If
Case 19712 'next
If num < 123 Then num = num + 1
End Select
_Limit 30
_Display
Loop
Sub Lessons (num)
Static DoOnce
Select Case num
Case 0 'just print
Cls
WordWrap "Hello World."
WordWrap "This is Steve's Tuturial on Tic-Tac-Toe!"
Print
WordWrap "Press <RIGHT ARROW> to move forward in the lessons."
Case 1
Cls
WordWrap "How many places can one start at in Tic-Tac-Toe? Let's look at the board below to find out!"
DrawBoard ""
Case 2
Cls
WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
WordWrap "Current count = 1"
DrawBoard "X"
Case 3
Cls
WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
WordWrap "Current count = 2"
DrawBoard ".X"
Case 4
Cls
WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
WordWrap "Current count = 3"
DrawBoard "..X"
Case 5
Cls
WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
WordWrap "Current count = 4"
DrawBoard "...X"
Case 6
Cls
WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
WordWrap "Current count = 5"
DrawBoard "....X"
Case 7
Cls
WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
WordWrap "Current count = 6"
DrawBoard ".....X"
Case 8
Cls
WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
WordWrap "Current count = 7"
DrawBoard "......X"
Case 9
Cls
WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
WordWrap "Current count = 8"
DrawBoard ".......X"
Case 10
Cls
WordWrap "First, there's the top-right corner, which makes one spot, and we'll count as we go from there."
WordWrap "Current count = 9"
DrawBoard "........X"
Case 11
Cls
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!"
Print
WordWrap "BUT..."
Print
WordWrap "Tic-Tac-Toe is a game of SYMMETRY. There's really only THREE positions to consider in the game."
Case 12
Cls
WordWrap "The outer corners."
DrawBoard "X.X...X.X"
Case 13
Cls
WordWrap "The middle edges."
DrawBoard ".X.X.X.X."
Case 14
Cls
WordWrap "And the center of it all -- the center!"
DrawBoard "....X...."
Case 15
Cls
WordWrap "So, to start this tuturial out, let's start with X in one of the OUTER CORNERS."
DrawBoard "X"
Case 16
Cls
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."
WordWrap "First, O goes right beside X... X then goes to their opposite free corner."
DrawBoard "XO....X.."
Case 17
Cls
WordWrap "This is a losing position for O, as here would be the next 2 moves played out:"
WordWrap "O blocks the win, X takes the middle..."
DrawBoard "XO.OX.X.."
Case 18
Cls
WordWrap "So what if O would have went to the far corner instead?"
WordWrap "X then goes to their diagional free corner."
DrawBoard "X.O.....X"
Case 19
Cls
WordWrap "O is now forced to block in the center, and X then blocks O."
WordWrap "Guess who just lost?"
DrawBoard "X.O.O.X.X"
Case 20
Cls
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?"
WordWrap "Doesn't this set up look just like what we saw previously with O in the top middle?"
DrawBoard "X....OX.."
Case 21
Cls
WordWrap "Yep. Same predictible results. O loses."
DrawBoard "X..OXOX.."
Case 22
Cls
WordWrap "And if O were to move down to the next open position, it's another outer corner."
WordWrap "X, once again, just takes the opposite corner for the win."
DrawBoard "X.....X.O"
Case 23
Cls
WordWrap "Next space on the rotation would be the bottom middle... Which is just like the other two middles we've seen so far..."
WordWrap "Can you see why O loses here?"
DrawBoard "X.X....O."
Case 24
Cls
WordWrap "And around we go, with another corner, which plays like the other corners for O.."
DrawBoard "X.X...O.."
Case 25
Cls
WordWrap "And the last move around the edge is right below X, in the left middle... Which results in... The same as before!"
DrawBoard "X.XO....."
Case 26
Cls
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!"
WordWrap "The whole game is decided by the first move!"
WordWrap "O's *ONLY* possible move is into the middle, which will force a draw unless someone is honestly just REALLY stupid!"
DrawBoard "X...O...."
Case 27
Cls
WordWrap "So in conclusion here: IF X starts with any OUTSIDE CORNER, the *ONLY* place O can go -- without losing -- is to the center."
WordWrap "*ALL* other moves lets X manulate the board for a guaranteed win."
DrawBoard "X...O...."
Case 28
Cls
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."
WordWrap "IF X takes an OUTER CORNER, the *ONLY* move O can make is to take the center, or lose."
Case 29
Cls
WordWrap "Which then leads to the question: What if X takes the center on their first move?"
DrawBoard "....X...."
Case 30
Cls
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..."
WordWrap "O takes a middle, X takes an outer beside it."
DrawBoard ".OX.X...."
Case 31
Cls
WordWrap "O has to block, and now X can take the other corner. Guess who just won?"
DrawBoard ".OX.X.O.X"
Case 32
Cls
WordWrap "So if X takes the center, and O takes a middle, then X wins! First move determines the game once again."
WordWrap "IF O doesn't want to lose, all they can do is take an OUTER CORNER when X takes the center."
DrawBoard "O...X...."
Case 33
Cls
WordWrap "So that means the only possible place left to talk about is if X starts in a MIDDLE EDGE position."
DrawBoard ".X......."
Case 34
Cls
WordWrap "And honestly, there's not to much to say about this position, except *DON'T*."
WordWrap "There's *NO* move that O can make which guarantees X a win if they start off with an OUTER MIDDLE tile."
WordWrap "Unless someone just plays terribly on purpose, these games will end in a draw."
WordWrap "ONLY start there if you're playing with an elementry grade child."
Case 35
Cls
WordWrap "So, to basically sum up:"
WordWrap "Tic-Tac-Toe is basically a game that is won in the first move."
WordWrap "X's two possible moves are OUTER EDGE and CENTER."
WordWrap "O's counters are CENTER and OUTER EDGE."
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...)"
Case 36
Cls
WordWrap "AND THAT'S ALL THERE IS TO TIC-TAC-TOE!"
Case 37
Cls
Color &HFFFF0000, 0
WordWrap "X's two possible starting moves are OUTER EDGE and CENTER."
WordWrap "O's counters are CENTER and OUTER EDGE."
Case 38
System
End Select
End Sub
Sub DrawBoard (text$)
Static board, boardfont
yStart = CsrLin * _FontHeight(Font)
d = _Dest: s = _Source
If board = 0 Then
board = _NewImage(600, 600, 32)
boardfont = _LoadFont("courbd.ttf", 196, "monospace")
End If
_Dest board: _Source board
Cls
Line (190, 0)-Step(20, 600), Yellow, BF
Line (390, 0)-Step(20, 600), Yellow, BF
Line (0, 190)-Step(600, 20), Yellow, BF
Line (0, 390)-Step(600, 20), Yellow, BF
_Font boardfont
For i = 0 To 8
temp$ = Mid$(text$, i + 1, 1)
Select Case temp$
Case "X", "x"
Color Red, 0
_PrintString ((i Mod 3) * 200 + 40, (i \ 3) * 200 + 10), "X"
Case "O", "o", "0"
Color Green, 0
_PrintString ((i Mod 3) * 200 + 40, (i \ 3) * 200 + 10), "O"
End Select
Next
_Dest d: _Source s
_PutImage (0, yStart)-(_Width, _Height), board
End Sub
Sub WordWrap (text As String)
Dim BreakPoint As String
BreakPoint = ",./- ;:!" 'I consider all these to be valid breakpoints. If you want something else, change them.
w = _Width
pw = _PrintWidth(text)
x = Pos(0): y = CsrLin
If _PixelSize <> 0 Then x = x * _FontWidth
firstlinewidth = w - x + 1
If pw <= firstlinewidth Then
Print text
Else
'first find the natural length of the line
For i = 1 To Len(text)
p = _PrintWidth(Left$(text, i))
If p > firstlinewidth Then Exit For
Next
lineend = i - 1
t$ = RTrim$(Left$(text, lineend)) 'at most, our line can't be any longer than what fits the screen.
For i = lineend To 1 Step -1
If InStr(BreakPoint, Mid$(text, i, 1)) Then lineend = i: Exit For
Next
Print Left$(text, lineend)
WordWrap LTrim$(Mid$(text, lineend + 1))
End If
End Sub
Sub Speak (text$)
text$ = Chr$(34) + "(pause), " + text$ + Chr$(34)
Open "temp.ps1" For Output As #1
Print #1, "Add-Type -AssemblyName System.Speech; "
Print #1, "$voice = New-Object System.Speech.Synthesis.SpeechSynthesizer; "
Print #1, "$voice.SelectVoice(" + Chr$(34) + "Microsoft Zira Desktop" + Chr$(34) + "); "
Print #1, "$voice.Speak(" + text$ + "); "
Close #1
Shell _Hide "powershell ./temp.ps1"
End Sub
Sub Resizer
If _Resize Then
_Delay .5
x = _ResizeWidth
y = _ResizeHeight
temp = _NewImage(x, y, 32)
Screen temp
_FreeImage ViewScreen
ViewScreen = temp
_Font Font
End If
End Sub
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, lets you quit at any time.
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.