Please visit our sponsor
UNKNOWN '************************************** ' for :ASP TicTacToe '************************************** Source is PD '************************************** ' Name: ASP TicTacToe ' Description:ASP version of TicTacToe. ' Try to beat the computer at getting 3 Xs ' in a row. ' By: Owen Cutajar ' ' ' Inputs:N/A ' ' Returns:N/A ' 'Assumes:Code is fully self-sufficient, ' incorporating both the display and game ' logic. It's interesting to note that the ' game was converted to ASP from Javascrip ' t. ' 'Side Effects:N/A 'This code is copyrighted and has limite ' d warranties. 'Please see http://www.Planet-Source-Cod ' e.com/xq/ASP/txtCodeId.6364/lngWId.4/qx/ ' vb/scripts/ShowCode.htm 'for details. '************************************** &lt;% '--------------------------------------- ' ----------------------- ' ASP TicTacToe v1.0 ' (x) Ugh!! 2000 - 18/10/2000 ' ' based on Javascript TicTacToe by ' Maximilian Stocker (maxstocker@reallyu ' sefulcomputing.com) ' ' Written for http://www.only-network.co ' m/games Any comments, ' flames, requests, postcards etc to owe ' n@cutajar.net '--------------------------------------- ' ----------------------- 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) &lt;&gt; "B" Then ReportTaken else GameBoard(Position) = "X" end if end if end sub sub ReportTaken() Response.Write "<h2>That square is already occupied. Please select another square.</h2>" end sub sub ReportEnded() Response.Write "<h2>The game has already ended. To play a new game click the Play Again button.</h2>" end sub sub myChoice() Dim NewMove NewMove = Suggest() While NewMove = -1 Randomize NewMove=int(rnd*9) If GameBoard(NewMove) &lt;&gt; "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 "<h2>You won, congratulations!<h2>" Session("you") = Session("you") + 1 Session("State") = "Dead" Case END_Computer_Win Response.Write "<h2>Gotcha! I win!</h2>" Session("computer") = Session("computer") + 1 Session("State") = "Dead" Case END_Tie Response.Write "<h2>We tied.</h2>" 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 "<form action="/" method="post">" Response.Write "<input type="hidden" name="pressed" value=" &amp; CellNum &amp; ">" Response.Write "<input type="image" src="http://www.exhedra.com/vb/scripts/ &amp; IMGblank &amp; " border="0" height="100" width="100">" Response.Write "</form>" elseif GameBoard(CellNum) = "O" Then Response.Write "" elseif GameBoard(CellNum) = "X" Then Response.Write "" end if end sub ' Main Code If Request("Pressed") & "" &lt;&gt; "" Then YourChoice(Request("Pressed")) ProcessBoard If GameState() = END_Not_Yet Then myChoice End If ProcessBoard ' Save Game State Session("GameBoard") = Join(GameBoard,"_") End If %&gt; <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> </head> <body> <center> 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!! <form name="game" action="/"> <table border="0"> <tr> <td> <table border="1"> <tr height="120"> <td>&lt;% Display(0) %&gt;</td> <td>&lt;% Display(1) %&gt;</td> <td>&lt;% Display(2) %&gt;</td> </tr> <tr height="120"> <td>&lt;% Display(3) %&gt;</td> <td>&lt;% Display(4) %&gt;</td> <td>&lt;% Display(5) %&gt;</td> </tr> <tr height="120"> <td>&lt;% Display(6) %&gt;</td> <td>&lt;% Display(7) %&gt;</td> <td>&lt;% Display(8) %&gt;</td> </tr> </table> </td> <td> <table> <tr><td><input type="text" size="5" name="you" value="<%=Session(&quot;you&quot;)%">&gt;</td><td>You</td></tr> <tr><td><input type="text" size="5" name="computer" value="<%=Session(&quot;computer&quot;)%">&gt;</td><td>Computer</td></tr> <tr><td><input type="text" size="5" name="ties" value="<%=Session(&quot;ties&quot;)%">&gt;</td><td>Ties</td></tr> </table> </td></tr> </table> <form action="/"> <input type="hidden" name="PlayAgain" value="Yes"> <input type="submit" value="Play Again"> </form> </center> </body> </html>