I have two entries, since I have no life anyways
First entry:
(Almost) Unbeatable Tic-Tac-Toe Player
This program's main AI works by looking at each possible play it can make in any given instance, and then calculating through a recursive function every possible move that could be made after that, and then selects the play that has the most net possible gain. There are a few tweaks to this general idea, to make it a slightly better player, but the recursive function is the backbone of its AI.
Here's the code:
rem (almost) Invincible TicTacToe Player
rem by H4ck1d
global boardSize as float: boardSize = 400
global human: human = -1
global computer: computer = 1
dim strategicValue(9)
dim boardValue(9)
player = -1
do
clearBoard()
drawBoard()
mainLoop(player)
player = player*-1
loop
function mainLoop(player)
do
makePlay(player)
cls
if drawBoard() then exitfunction
player = player*-1
loop
endfunction
function makePlay(player)
if player = computer
boardValue(getCompMove(computer)) = computer
else
humanInput(human)
endif
endfunction
function humanInput(player)
humanTop:
repeat
okay = mousex()<boardSize and mousey()<boardSize
until mouseclick() and okay
xpos = int(mousex()/(boardSize/3.0))+1
ypos = int(mousey()/(boardSize/3.0))+1
checkOK = getValue(xpos,ypos)
if checkOK = 0
setValue(xpos,ypos,player)
else
goto humanTop
endif
while mouseclick()
endwhile
endfunction
function getCompMove(player)
rem loop through and check each possible move for either a win or for its strategic value
for loopVals = 1 to 9
strategicValue(loopVals) = -999999
if boardValue(loopVals) = 0
if loopVals = 5 then exitfunction 5
boardValue(loopVals) = player
if checkWin() <> player
rem if the move is not an instant win, check for its strategic value
strategicValue(loopVals) = getSumOutcomes(player*-1,1)
boardValue(loopVals) = 0
else
rem make the winning move!
boardValue(loopVals) = 0
Value = loopVals
exitfunction Value
endif
endif
next
rem select the highest value strategy for the computer and return it
maxIndex = 1
for loopAll = 1 to 9
if strategicValue(loopAll) > strategicValue(maxIndex) then maxIndex = loopAll
next
endfunction maxIndex
function clearBoard()
for loopSpots = 1 to 9
boardValue(loopSpots) = 0
next
endfunction
function getSumOutcomes(nextPlayer,generation)
sum = 0
for loopSpots = 1 to 9
if boardValue(loopSpots) = 0
boardValue(loopSpots) = nextPlayer
winner = checkWin()
if winner = 0
inc sum, getSumOutcomes(nextPlayer*-1,generation+1)
boardValue(loopSpots) = 0
else
boardValue(loopSpots) = 0
rem prevent opponent from winning next turn by avoiding a move that makes it possible
wnr2 = winner*9999
if generation=1 then exitfunction wnr2
exitfunction winner
endif
boardValue(loopSpots) = 0
endif
next
endfunction sum
function checkWin()
rem first check horizontally
for loopY = 1 to 3
valu = 0
for loopX = 1 to 3
inc valu, getValue(loopX,loopY)
winnerVal = abs(valu)/valu
if abs(valu) = 3 then exitfunction winnerVal
next
next
rem next check vertically
for loopX = 1 to 3
valu = 0
for loopY = 1 to 3
inc valu, getValue(loopX,loopY)
winnerVal = abs(valu)/valu
if abs(valu) = 3 then exitfunction winnerVal
next
next
rem check diagonals
value = getValue(1,1)+getValue(2,2)+getValue(3,3)
winnerVal = abs(value)/value
if abs(value) = 3 then exitfunction winnerVal
value = getValue(1,3)+getValue(2,2)+getValue(3,1)
winnerVal = abs(value)/value
if abs(value) = 3 then exitfunction winnerVal
rem okay, no winner - is the board full?
`remstart
for loopCheck = 1 to 9
if boardValue(loopCheck) = 0 then exitfunction 0
next
exitfunction 2
endfunction 0
function drawBoard()
ink rgb(255,255,255),rgb(0,0,0)
rem draw the lines
line 0,(boardSize/3),boardSize,(boardSize/3)
line 0,(boardSize*(2.0/3.0)),boardSize,(boardSize*(2.0/3.0))
line (boardSize/3),0,(boardSize/3),boardSize
line (boardSize*(2.0/3.0)),0,(boardSize*(2.0/3.0)),boardSize
rem draw the marks
for loopx = 1 to 3
for loopy = 1 to 3
ink rgb(0,0,0),rgb(0,0,0)
`if boardValue(loopx+3*(loopy-1)) = 1 then ink rgb(0,0,255),rgb(0,0,0)
`if boardValue(loopx+3*(loopy-1)) = -1 then ink rgb(255,0,0),rgb(0,0,0)
if getValue(loopx,loopy) = 1 then ink rgb(0,0,255),rgb(0,0,0)
if getValue(loopx,loopy) = -1 then ink rgb(255,0,0),rgb(0,0,0)
circle (loopx*(boardSize/3))-(boardSize/6), (loopy*(boardSize/3))-(boardSize/6), (boardSize/6)-5
next
next
rem check for a winner
winner = checkWin()
if winner <> 0
if winner = 1 then wnr$ = "Computer is again triumphant."
if winner = -1 then wnr$ = "Human cheated by messing with the code and barely won."
if winner = 2 then wnr$ = "Tie game. Computer still had the upper hand."
ink rgb(255,255,255),rgb(0,0,0)
text 0,boardSize+10,wnr$+" Press any key to play again."
wait key
exitfunction 1
endif
endfunction 0
function getValue(x,y)
valu = boardValue(x+3*(y-1))
endfunction valu
function setValue(x,y,valu)
boardValue(x+3*(y-1)) = valu
endfunction
Entry 2:
Sierpinski's Triangle
I'm not entirely sure if this counts as a recursive function or not, but considering that it calls itself many times over I believe it does. This program creates a very cool fractal by randomly selecting an endpoint on a triangle and creating a new point halfway between its starting point and the endpoint of the triangle. It repeats this process over and over again, theoretically forever, but I've capped it at 100,000 repititions. Take a look! When it's done, press space to close the window.
rem Sierpinski - Recursive Function
rem by H4ck1d
maxPoints = 100000
type endPointType
x as float
y as float
endtype
dim endPoints(3) as endPointType
triSize# = 800
height#=sqrt(triSize#^2-(triSize#/2.0)^2)
endPoints(1).x = triSize#/2
endPoints(1).y = 0
endPoints(2).x = 0
endPoints(2).y = height#
endPoints(3).x = triSize#
endPoints(3).y = height#
cls
ink rgb(255,255,255),rgb(0,0,0)
doSierpinski(105,105,1,maxPoints)
repeat
until spacekey()
end
function doSierpinski(x as float,y as float,currentIteration,maxIterations)
gotoPoint = rnd(2)+1
drawPtX = (endPoints(gotoPoint).x-x)/2.0+x
drawPtY = (endPoints(gotoPoint).y-y)/2.0+y
dot drawPtX,drawPtY
if currentIteration >= maxIterations
exitfunction
endif
doSierpinski(drawPtX,drawPtY,currentIteration+1,maxIterations)
endfunction