Creating the NumberGuess Server Response File


The user interface of your number-guessing game will consist of a simple text entry box, a Submit button, a Restart button, and plain text. The same SRF will be used throughout the user's session. This means that the same SRF will be used to play the game, display error messages, restart the game, and finally show the congratulations message when the user guesses the number correctly. To begin with, overwrite the current contents of your NumberGuess.srf file with the code from Listing 14.2.

Listing 14.2 Creating the NumberGuess.srf File
 1: {{ // This file implements the classic guess the number game }}  2: <html>  3: <script language="javascript">  4: <!--  5: function OnRestart()  6: {  7:     location.href = "NumberGuess.srf";  8: }  9: //--> 10: </script> 11: 12: <HEAD> 13: </HEAD> 14: <BODY> 15: <P>{{handler NumberGuess.dll/Default}}</P> 16: <FORM action="NumberGuess.srf"> 17: 18: {{if UserGuessedCorrectly}} 19: 20: Congratulations you guessed the correct number in {{GetNumGuesses}} steps! 21: <p> 22: <INPUT type="button" value="Restart"  23: NAME="Restart" onclick="OnRestart()"> 24: </p> 25: 26: {{else}} 27: 28: <p>{{GetLastError}}</p> 29: 30: <p>Enter a number between 1 and 100: 31: 32: <INPUT  type="text" maxLength="3" size="3" name="UserGuess"> 33: </p> 34: 35: <p> 36: <INPUT  type="submit" value="Submit" name="Submit1"> 37: <INPUT type="button" value="Restart"  38: NAME="Restart" onclick="OnRestart()"> 39: </p> 40: 41: {{endif}} 42: 43: </FORM> 44: </BODY> 45: </html> 

By looking at the structure based on the ATL Server tags, you can see the basic flow of the Web application. Starting on Line 18, you see the following conditional:

 {{ if UserGuessedCorrectly }} 

Replacement tags can contain either if or while loop conditional statements such as this one. The function UserGuessedCorrectly, in case you haven't guessed, is a function you will create within your Web application DLL. It returns either True or False based on the value of the UserGuess form variable on line 32. If the user does guess the correct value, the text congratulating the user is displayed and any rendering instructions after the {{else}} replacement tag is ignored.

However, if the user doesn't guess correctly, the SRF's contents past the {{else}} tag is rendered. In this case, the SRF tells the Web application to display the last error it encountered if there was one and then display the standard HTML for the input box and Submit and Restart buttons.


Sams Teach Yourself Visual C++. NET in 24 Hours
Sams Teach Yourself Visual C++.NET in 24 Hours
ISBN: 0672323230
EAN: 2147483647
Year: 2002
Pages: 237

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: