The coding is only for tutorial purpose. This is the simple Tic-Tac-Toe game written in ASP.
Option Explicit
' -- Set up images to use ---
Const IMGx = "x.jpg"
Const IMGo = "o.jpg"
Const IMGblank = "blank.jpg"
' -- Set up game States ---
Const END_Not_Yet = 0
Const END_You_Win = 1
Const END_Computer_Win = 2
Const END_Tie = 3
' Read in board or Initialise
Dim Gameboard
Dim wl1,wl2,wl3,wl4,wl5,wl6,wl7,wl8
If Session("GameBoard") & "" = "" OR Request("PlayAgain") = "Yes" Then
PlayAgain
end if
GameBoard = Split(Session("GameBoard"),"_")
function GameState()
wl1 = GameBoard(0) & GameBoard(1) & GameBoard(2)
wl2 = GameBoard(0) & GameBoard(3) & GameBoard(6)
wl3 = GameBoard(0) & GameBoard(4) & GameBoard(8)
wl4 = GameBoard(1) & GameBoard(4) & GameBoard(7)
wl5 = GameBoard(3) & GameBoard(4) & GameBoard(5)
wl6 = GameBoard(6) & GameBoard(7) & GameBoard(8)
wl7 = GameBoard(2) & GameBoard(5) & GameBoard(8)
wl8 = GameBoard(6) & GameBoard(4) & GameBoard(2)
if wl1="XXX" or wl2="XXX" or wl3="XXX" or wl4="XXX" or wl5="XXX" or wl6="XXX" or wl7="XXX" or wl8="XXX" Then
GameState = END_You_Win
elseif wl1="OOO" or wl2="OOO" or wl3="OOO" or wl4="OOO" or wl5="OOO" or wl6="OOO" or wl7="OOO" or wl8="OOO" Then
GameState = END_Computer_Win
elseif Instr ( wl1 & wl5 & wl6 ,"B" ) = 0 then
GameState = END_Tie
else
GameState = END_Not_Yet
end if
end function
Function Suggest()
If wl1 = "XXB" or wl1 = "OOB" Then
Suggest = 2
elseif wl1 = "XBX" or wl1 = "OBO" Then
Suggest = 1
elseif wl1 = "BXX" or wl1 = "BOO" Then
Suggest = 0
elseif wl2 = "XXB" or wl2 = "OOB" Then
Suggest = 6
elseif wl2 = "XBX" or wl2 = "OBO" Then
Suggest = 3
elseif wl2 = "BXX" or wl2 = "BOO" Then
Suggest = 0
elseif wl3 = "XXB" or wl3 = "OOB" Then
Suggest = 8
elseif wl3 = "XBX" or wl3 = "OBO" Then
Suggest = 4
elseif wl3 = "BXX" or wl3 = "BOO" Then
Suggest = 0
elseif wl4 = "XXB" or wl4 = "OOB" Then
Suggest = 7
elseif wl4 = "XBX" or wl4 = "OBO" Then
Suggest = 4
elseif wl4 = "BXX" or wl4 = "BOO" Then
Suggest = 1
elseif wl5 = "XXB" or wl5 = "OOB" Then
Suggest = 5
elseif wl5 = "XBX" or wl5 = "OBO" Then
Suggest = 4
elseif wl5 = "BXX" or wl5 = "BOO" Then
Suggest = 3
elseif wl6 = "XXB" or wl6 = "OOB" Then
Suggest = 8
elseif wl6 = "XBX" or wl6 = "OBO" Then
Suggest = 7
elseif wl6 = "BXX" or wl6 = "BOO" Then
Suggest = 6
elseif wl7 = "XXB" or wl7 = "OOB" Then
Suggest = 8
elseif wl7 = "XBX" or wl7 = "OBO" Then
Suggest = 5
elseif wl7 = "BXX" or wl7 = "BOO" Then
Suggest = 2
elseif wl8 = "XXB" or wl8 = "OOB" Then
Suggest = 2
elseif wl8 = "XBX" or wl8 = "OBO" Then
Suggest = 4
elseif wl8 = "BXX" or wl8 = "BOO" Then
Suggest = 6
else
Suggest = -1
end if
end function
sub yourChoice(Position)
if Session("State") = "Dead" Then
ReportEnded
Else
If GameBoard(Position) <> "B" Then
ReportTaken
else
GameBoard(Position) = "X"
end if
end if
end sub
sub ReportTaken()
Response.Write "
That square is already occupied. Please select another square.
"end sub
sub ReportEnded()
Response.Write "
The game has already ended. To play a new game click the Play Again button.
"end sub
sub myChoice()
Dim NewMove
NewMove = Suggest()
While NewMove = -1
Randomize
NewMove=int(rnd*9)
If GameBoard(NewMove) <> "B" Then
NewMove = -1
End If
wend
GameBoard(NewMove) = "O"
end sub
sub ProcessBoard()
If Session("State") = "Alive" Then
Select Case GameState()
Case END_You_Win
Response.Write "
You won, congratulations!
"
Session("you") =
Session("you") + 1
Session("State") = "Dead"
Case
END_Computer_Win
Response.Write "
Gotcha! I
win!
"
Session("computer") = Session("computer") +
1
Session("State") = "Dead"
Case END_Tie
Response.Write
"
We tied.
"
Session("ties") = Session("ties") +
1
Session("State") = "Dead"
end Select
End If
end
sub
sub playAgain()
Session("GameBoard") =
"B_B_B_B_B_B_B_B_B"
Session("State") = "Alive"
end sub
sub
Display(CellNum)
If GameBoard(CellNum) = "B" Then
Response.Write
"
"Session("you") = Session("you") + 1
Session("State") = "Dead"
Case END_Computer_Win
Response.Write "
Gotcha! I win!
"Session("computer") = Session("computer") + 1
Session("State") = "Dead"
Case END_Tie
Response.Write "
We tied.
"Session("ties") = Session("ties") + 1
Session("State") = "Dead"
end Select
End If
end sub
sub playAgain()
Session("GameBoard") = "B_B_B_B_B_B_B_B_B"
Session("State") = "Alive"
end sub
sub Display(CellNum)
If GameBoard(CellNum) = "B" Then
Response.Write "
elseif GameBoard(CellNum) = "O" Then
Response.Write ""
elseif GameBoard(CellNum) = "X" Then
Response.Write ""
end if
end sub
' Main Code
If Request("Pressed") & "" <> "" Then
YourChoice(Request("Pressed"))
ProcessBoard
If GameState() = END_Not_Yet Then
myChoice
End If
ProcessBoard
' Save Game State
Session("GameBoard") = Join(GameBoard,"_")
End If
%>
Welcome to Tic-Tac-Toe! You play as the X's and the computer is the O's. Select the square you want to put your X into by clicking them. You cannot occupy a square that is already occupied. The first player to get three squares in a row wins. Good Luck!!