#SingleInstance force #NoTrayIcon #NoEnv SetBatchLines -1 ; Default Settings: Players= 1 ;2|1 Number of people to play. TitleBar= 1 ;1|0 Show or hide the GUI title bar. AutoStrt= 0 ;1|0 Automatically start new games with no clicking. RsltMsg= 1 ;1|0 Show the result of each game in a Message Box. Level= 1 ;1|2|3|4 Starting Level Color= Default ;One of the following background colors. ; Colors = Default|Aqua|Blue|Fuchsia|Gray|Green|Lime|Maroon|Navy|Olive|Purple|Red|Silver|Teal|White|Yellow ;_____________________________________ Win1 = 1|2|3 Win2 = 1|4|7,1|2|4|7,1|3|4|7,1|4|5|7,1|4|6|7 Win3 = 1|5|9,1|2|5|9,1|3|5|9,1|4|5|9,1|5|6|9,1|5|7|9,1|5|8|9,1|2|4|5|9,1|3|4|5|9,1|5|6|7|9,1|5|6|8|9 Win4 = 2|5|8,2|3|5|8,2|4|5|8,2|5|6|8,2|5|7|8,2|3|4|5|8,2|5|6|7|8 Win5 = 3|5|7,3|4|5|7,3|5|6|7 Win6 = 3|6|9,3|4|6|9,3|5|6|9,3|6|7|9,3|6|8|9 Win7 = 4|5|6 Win8 = 7|8|9 Block1 = 2|3 ,4|7,4|5|7,4|6|7 ,5|9,5|6|9,5|7|9,5|8|9,5|6|7|9,5|6|8|9 Block2 = 1|3 ,5|8,5|6|8,5|7|8,5|6|7|8 Block3 = 1|2 ,5|7,5|6|7 ,6|9,6|7|9,6|8|9 Block4 = 1|7,1|2|7,1|3|7,1|5|7,1|6|7,1|2|5|7,1|2|6|7,1|3|5|7,1|3|6|7 ,5|6 Block5 = 1|9,1|2|9,1|3|9,1|4|9,1|6|9,1|7|9,1|8|9,1|2|4|9,1|2|6|9,1|2|7|9,1|2|8|9,1|3|4|9,1|3|7|9,1|3|8|9,1|4|6|9,1|4|8|9,1|6|7|9,1|6|8|9 ,2|8,2|3|8,2|4|8,2|6|8,2|7|8,2|3|4|8,2|3|6|8,2|3|7|8,2|4|6|8,2|4|7|8,2|6|7|8 ,3|7,3|4|7,3|6|7,3|4|6|7 ,4|6 Block6 = 3|9,3|4|9,3|5|9,3|7|9,3|8|9,3|4|5|9,3|4|7|9,3|4|8|9,3|5|8|9 ,4|5 Block7 = 1|4,1|2|4,1|3|4 ,3|5,3|4|5 ,8|9 Block8 = 2|5,2|3|5,2|4|5,2|3|4|5 ,7|9 Block9 = 1|5,1|2|5,1|3|5,1|4|5,1|2|4|5,1|3|4|5 ,3|6,3|4|6,3|5|6 ,7|8 X=0 O=0 T=0 Corners = 1|3|7|9 player = X PC = O Turn = 1 LastTurn = 1 Gui, Margin, 10,10 Gui, Font, s100 Menu, Menu1, Add, New Game, NewGame Menu, Menu1, Add, Clear Score, ClrScore Menu, Menu1, Add, About..., About Menu, Menu1, Add, Exit ( Esc ), GuiClose Menu, Menu2, Add, Level 1, Level Menu, Menu2, Add, Level 2, Level Menu, Menu2, Add, Level 3, Level Menu, Menu2, Add, Level 4, Level Menu, Menu3, Add, Show Title Bar, TitleBar Menu, Menu3, Add, Show Result Message, RsltMsg Menu, Menu3, Add, Auto-Start New Game, AutoStrt Loop, Parse, Colors, | Menu, Menu4, Add, %A_LoopField%, Color Menu, Menu5, Add, 1 Player, Players Menu, Menu5, Add, 2 Player, Players Menu, Menu5, Add, 0 Player, Players Menu, MenuBar1, Add, Tic-Tac-Toe, :Menu1 Menu, MenuBar1, Add, Level, :Menu2 Menu, MenuBar1, Add, Options, :Menu3 Menu, MenuBar1, Add, Background, :Menu4 Menu, MenuBar1, Add, Players, :Menu5 Menu, Menu2, Check, Level %Level% If TitleBar Menu, Menu3, Check, Show Title Bar If RsltMsg Menu, Menu3, Check, Show Result Message If AutoStrt Menu, Menu3, Check, Auto-Start New Game Menu, Menu4, Check, %Color% Menu, Menu5, Check, %Players% Player Gui, Menu, MenuBar1 Gui, Add, Text, w125 h125 Center gClick vBox1, * Gui, Add, Text, wp hp Center gClick vBox2, * Gui, Add, Text, wp hp Center gClick vBox3, * Gui, Add, Text, wp hp Center gClick ym vBox4, * Gui, Add, Text, wp hp Center gClick vBox5, * Gui, Add, Text, wp hp Center gClick vBox6, * Gui, Add, Text, wp hp Center gClick ym vBox7, * Gui, Add, Text, wp hp Center gClick vBox8, * Gui, Add, Text, wp hp Center gClick vBox9, * Gui, Font, s15 Gui, Add, GroupBox, w395 h100 xm, X-O-Tie Gui, Font, s50 Gui, Add, Text, w385 xp+5 yp+20 Center vScore, 0-0-0 Gui, Font, s100 Gui, Color, %Color% Menu, MenuBar1, Color, %Color% If !TitleBar Gui, -Caption +Border Gui, Show,,Tic-Tac-Toe return GuiEscape: GuiClose: ExitApp Click: If !Turn return Turn = 0 GuiControlGet, used,,%A_GuiControl% If used != * { Turn = 1 return } GuiControl,,%A_GuiControl%, %player% StringRight, num, A_GuiControl, 1 %player%_has .= num "|" Sort, %player%_has, N D| GameOver := Win(%player%_has,player) If GameOver { If AutoStrt { Sleep, 1000 GoTo, NewGame } return } If Players = 2 { player := player="X" ? "O":"X" Turn = 1 return } PC: If Players = 0 Level := PC="X" ? LevelX:LevelO Loop { Random, random, 1,9 GuiControlGet, used,,Box%random% If used = * { If (Level > 2) AND (StrLen(%PC%_has) > 2) Loop,9 { GuiControlGet, used,,Box%A_Index% If used = * If %PC%_has contains % Block%A_Index% { random := A_Index GoTo, AddBox } } If (Level > 1) AND (StrLen(%player%_has) > 2) Loop,9 { GuiControlGet, used,,Box%A_Index% If used = * If %player%_has contains % Block%A_Index% { random := A_Index GoTo, AddBox } } If Level > 3 { If (StrLen(%PC%_has) > 2) OR (StrLen(%player%_has) > 2) { unused= Loop,9 { GuiControlGet, used,,Box%A_Index% If used = * unused .= A_Index "," } StringTrimRight, unused, unused, 1 Loop,2 { letter := A_Index=1 ? "X":"O" %letter%forks= Loop, Parse, unused, `, { temp%letter% := A_LoopField temp%letter%_has := %letter%_has A_LoopField Sort, temp%letter%_has, N D| wins= Loop, Parse, unused, `, { If A_LoopField = % temp%letter% continue If temp%letter%_has contains % Block%A_LoopField% wins++ } If wins > 1 %letter%forks .= temp%letter% "," } ;MsgBox,% letter " can fork with these boxes:`n`n" %letter%forks } If %PC%forks Loop { Random, random, 1,9 If random in % %PC%forks GoTo, AddBox } Else If %player%forks { If (StrLen(%player%forks) = 2) { StringLeft, temp%player%, %player%forks, 1 temp%player%_has := %player%_has temp%player% Sort, temp%player%_has, N D| Blocks= Loop, Parse, unused, `, { If A_LoopField = % temp%player% continue If temp%player%_has contains % Block%A_LoopField% Blocks .= A_LoopField "," } Blocks .= temp%player% ;MsgBox, %PC% can block a fork with these boxes:`n`n%Blocks% } Else { Blocks= Loop, Parse, unused, `, { temp%PC% := A_LoopField temp%PC%_has := %PC%_has A_LoopField Sort, temp%PC%_has, N D| Loop, Parse, unused, `, { If A_LoopField = % temp%PC% continue If A_LoopField in % %player%forks continue If temp%PC%_has contains % Block%A_LoopField% Blocks .= temp%PC% "," } } ;MsgBox, %PC% can block multiple forks with these boxes:`n`n%Blocks% } Loop { Random, random, 1,9 If random in %Blocks% GoTo, AddBox } } } GuiControlGet, used,,Box5 If used = * random = 5 Else If %player%_has = 1| random = 9 Else If %player%_has = 3| random = 7 Else If %player%_has = 7| random = 3 Else If %player%_has = 9| random = 1 Else { corner= Loop, Parse, Corners, | { GuiControlGet, used,,Box%A_LoopField% If used = * corner .= A_LoopField "," } If corner Loop { Random, random, 1,9 If random in %corner% break } } } AddBox: GuiControl,,Box%random%, %PC% %PC%_has .= random "|" Sort, %PC%_has, N D| GameOver := Win(%PC%_has,PC) If GameOver { If Players = 0 { turns-- PC := player="X" ? "X":"O" player := player="X" ? "O":"X" GoTo, NewGame } If AutoStrt { Sleep, 1000 GoTo, NewGame } } If Players = 0 { PC := player="X" ? "X":"O" player := player="X" ? "O":"X" GoTo, PC } Else Turn = 1 return } } NewGame: If turns = 0 { turns= Level := OldLevel return } Gui, Font, cBlack Loop,9 { GuiControl, Font, Box%A_Index% GuiControl,,Box%A_Index%, * } X_has= O_has= If turns > 0 GoTo, PC If Players = 2 { Turn = 1 player := LastTurn=1 ? "O":"X" LastTurn := LastTurn=1 ? 0:1 return } If Players = 0 { Turn = 0 If !Oldturns Oldturns = 100 Gui, 2:+LastFoundExist IfWinExist return Gui, 2:+Owner1 Gui, 2:Font, s10 Gui, 2:Add, GroupBox, r2, Levels Gui, 2:Add, Edit, w32 xm+15 yp+15 number limit1 vLevelX Gui, 2:Add, UpDown, left Range1-4, %LevelX% Gui, 2:Add, Text, x+10 yp+2, Strength of PC "X" Gui, 2:Add, Edit, w32 xm+15 yp+25 number limit1 vLevelO Gui, 2:Add, UpDown, left Range1-4, %LevelO% Gui, 2:Add, Text, x+10 yp+2, Strength of PC "O" Gui, 2:Add, GroupBox, r2 xm, # of Games Gui, 2:Add, Edit, w60 xp+10 yp+28 number limit5 vTurns Gui, 2:Add, UpDown, left Range1-999, %Oldturns% Gui, 2:Add, Text, x+7 yp-12 ,How many`nrounds will the`ncomputer play? Gui, 2:Font Gui, 2:Add, Button, w70 xm+9 Default, OK Gui, 2:Add, Button, w70 x+15 g2GuiClose, Cancel Gui, 2:Color, %Color% If !TitleBar Gui, 2:-Caption +Border Gui, 2:Show,,PC vs PC GuiControl, 2:Focus, OK return 2GuiClose: Gui, 2:Destroy return 2ButtonOK: Gui, 2:Submit Gui, 2:Destroy If turns is not digit NonDigit = 1 Else NonDigit = 0 If (turns < 1) OR (NonDigit) { turns = -1 return } If turns > 10000 turns = 10000 Oldturns := turns OldLevel := Level If LevelX > 4 LevelX = 4 Else If LevelX < 1 LevelX = 1 If LevelO > 4 LevelO = 4 Else If LevelO < 1 LevelO = 1 Gui, 1:Default GoSub, PC return } Turn := LastTurn=1 ? 0:1 LastTurn := LastTurn=1 ? 0:1 If !Turn GoTo, PC return ClrScore: clipboard := X "-" O "-" T X=0 O=0 T=0 GuiControl,,Score, %X%-%O%-%T% return About: MsgBox,,About: Tic-Tac-Toe , Composed in AutoHotkey by:`njaco0646`nv2.2`nhttp://autohotkey.net/~jaco0646/ return Level: Loop,4 { If A_ThisMenuItem = Level %A_Index% Menu, Menu2, Check, %A_ThisMenuItem% Else Menu, Menu2, UnCheck, Level %A_Index% } StringRight, Level, A_ThisMenuItem, 1 return TitleBar: Menu, Menu3, ToggleCheck, %A_ThisMenuItem% TitleBar := TitleBar=1 ? 0:1 Gui, 2:+LastFoundExist If TitleBar { Gui, +Caption IfWinExist Gui, 2:+Caption } Else { Gui, -Caption +Border IfWinExist Gui, 2:-Caption +Border } Gui, Show, AutoSize IfWinExist Gui, 2:Show, AutoSize return RsltMsg: Menu, Menu3, ToggleCheck, %A_ThisMenuItem% RsltMsg := RsltMsg=1 ? 0:1 return AutoStrt: Menu, Menu3, ToggleCheck, %A_ThisMenuItem% AutoStrt := AutoStrt=1 ? 0:1 return Color: Loop, Parse, Colors, | { If A_ThisMenuItem = %A_LoopField% Menu, Menu4, Check, %A_ThisMenuItem% Else Menu, Menu4, UnCheck, %A_LoopField% } Menu, MenuBar1, Color, %A_ThisMenuItem% Gui, Color, %A_ThisMenuItem% Color := A_ThisMenuItem Gui, 2:+LastFoundExist IfWinExist Gui, 2:Color, %A_ThisMenuItem% return Players: Loop,3 { num := A_Index - 1 If A_ThisMenuItem = %num% Player Menu, Menu5, Check, %A_ThisMenuItem% Else Menu, Menu5, UnCheck, %num% Player } StringLeft, Players, A_ThisMenuItem, 1 player = X PC = O turns= GoTo, NewGame return Win(XorO_has, XorO) { global Loop,8 { If XorO_has contains % Win%A_Index% { Gui, +OwnDialogs If (RsltMsg) AND (Players) MsgBox,,Tic-Tac-Toe, Game Over.`n%XorO% Wins! %XorO% +=1 Gui, Font, cWhite Loop, Parse, Win%A_Index%,,| { If A_Index = 4 break GuiControl, Font, Box%A_LoopField% } GuiControl,,Score, %X%-%O%-%T% Return, 1 } } If (StrLen(X_has) > 8) OR (StrLen(O_has) > 8) { Gui, +OwnDialogs If (RsltMsg) AND (Players) MsgBox,,Tic-Tac-Toe, Game Over.`nTie! T +=1 GuiControl,,Score, %X%-%O%-%T% Return, 1 } }