Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ASP Programming for the Absolute Beginner

.pdf
Скачиваний:
64
Добавлен:
17.08.2013
Размер:
7.66 Mб
Скачать

126

4. When all 16 records in the MatchData table are assigned a unique random num-

 

ber between 1 and 16, the code is complete.

ASP Programming for the Absolute Beginner

Understanding the Entire

Memory Game Code

Whew! Feeling overwhelmed by the Random.asp code discussion? Well, you can relax. The rest of the Memory Game code is quite easy to understand and highlights the ways you can use forms to gather, manipulate, and insert data, either directly on a Web page or in conjunction with a database connection.

The Memory_Home.asp Page

The Memory Game consists of two pages: Memory Home.asp and Memory_Process.asp.

Memory_Home.asp is the opening screen of the game, greeting the players and asking

 

 

 

 

Y

them to enter their name. Figure 6.14 illustrates this opening screen.

Take a moment to review the code for the

L

emory Home.asp page. From the CD-ROM,

open the file Memory_Home.asp. Here isFthe code for this page:

Listing 6.4 Memory_Home.asp

M

 

 

 

 

<html>

A

 

 

<head>

 

 

<title>The Memory Game!</title>E

 

 

</head>

T

 

 

 

 

 

 

 

<%

'Reset the game database

Set GameReset=Server.CreateObject("ADODB.Recordset")

sqlstmt="UPDATE MatchData SET Table_Location=0, Rnd_Assigned=0, Selected=0" GameReset.Open sqlstmt, "DSN=Memory", 3, 3, 1

%>

<body>

<p align="left"><font face="Century Gothic" color="#008000"><i> <b>Welcome to...</b></i></font></p>

<p align="center"><font face="Century Gothic><hr></font></p>

<p align="right"><b><font face="Century Gothic" size="5" color="#800000">The Memory Game!</font></b></p>

<p><b>Let's

Play!</b></p>

<form method="POST" action="Memory_Process.asp">

<p>What is your name? <input type="text" name="Name" size="20"></p> <p align="center"><input type="submit" value="Submit your name,

and begin play!" name="B1"></p> </form>

<p> </p>

</body>

</html>

Team-Fly®

FIGURE 6.14

The opening screen of the Memory Game, utilizing

a one-line text box form element to gather the user’s name.

TRICK

Take a look at the code here, and notice the following line:

 

 

GameReset.Open sqlstmt, "DSN=Memory", 3, 3, 1

 

What does the 3, 3, 1 mean? This is a specific ADO configuration (where each num-

 

ber corresponds to a specific data access setting). For all the data access examples

 

used within this book, don’t forget to include this line of code, and leave it as 3, 3, 1.

Notice the ASP code at the beginning of this page. Every time this page loads, the MatchData table within the Memory database is reset, ready to begin another game. Take a look at the SQL statement in the GameReset connection. For each record in the database, the Rnd_Assigned and Selected fields are set to 0.

The rest of this page is straightforward HTML. In addition to minimal text formatting, a form is used to gather the player’s name. Notice the action of the form (action = "Memory_Process.asp"), which points to the Memory_Process.asp page. This is where the real action is. Also notice the name given to the one-line text box used within the form (Name). This will be used on the Memory_Process.asp page so that the user’s name can be both displayed on-screen and inserted into the History table of the database when the game is completed.

The Memory_Process.asp Page

After the user provides his or her name on the Memory_Home.asp page, the Memory_Process.asp page takes command, allowing the game to be played. Although the code for this page might seem overwhelming, it’s quite easy to understand. Remember, too, that you already encountered the central—and most difficult—section of the Memory_Process.asp page when I discussed random number generation (the preceding code will be used verbatim in the Memory_Process.asp page).

127

C h a p te r

6

U s i n F g o r m s

128

ASP Programming for the Absolute Beginner

Another interesting element of this page is that to provide the maximum amount of functionality with the minimum number of Web pages, the Memory_Process.asp page submits to itself. I’ll explain this important concept of programming in a few moments.

For now, open the page within Notepad so that you can see the entire code:

Listing 6.5 Memory_Process.asp

<html>

<head>

<title>The Memory Game!</title> </head>

<body>

<p align="left"><b><font face="Century Gothic" size="5" color="#800000">The Memory Game</font></b></p>

<%

'Check to see if gameboard has been generated

IF Request.Form("GameStart")=1 THEN%>

<b><center>Player: <%=Request.Form("Player")%></center></b> <%

'Add to the number of guesses Guesses=Request.Form("Guesses") Guesses=CInt(Guesses) Guesses=Guesses+1

'Gameboard has been generated -- check for matches

Set MatchCheckA=Server.CreateObject("ADODB.Recordset")

sqlstmt="SELECT * FROM MatchData WHERE Table_Location="&Request.Form("MatchA") MatchCheckA.Open sqlstmt, "DSN=Memory", 3, 3, 1 CheckA=MatchCheckA("Table_Name")

Set MatchCheckB=Server.CreateObject("ADODB.Recordset")

sqlstmt="SELECT * FROM MatchData WHERE Table_Location="&Request.Form("MatchB") MatchCheckB.Open sqlstmt, "DSN=Memory", 3, 3, 1 CheckB=MatchCheckB("Table_Name")

'Check to see if a match has been made

IF CheckA=CheckB AND Request.Form("MatchA") <> Request.Form("MatchB") THEN

'A match is made -- update the MatchData table to reflect this

Set MatchInsert=Server.CreateObject("ADODB.Recordset")

sqlstmt="UPDATE MatchData SET Selected=1 WHERE Table_Name='" & CheckA & "'" MatchInsert.Open sqlstmt, "DSN=Memory", 3, 3, 1

'Pause briefly to allow for update to be completed FOR Z=1 to 750000

NEXT %> <hr>

<b><center>That's a match!</center></b> <hr>

<table border="1" width="100%"> <tr>

<%

TableCount=0

FOR a=1 to 16

TableCount=TableCount+1

'Check to see if a match is made for this table location Set TableDisplay=Server.CreateObject("ADODB.Recordset") sqlstmt="SELECT * FROM MatchData WHERE Table_Location=" & a TableDisplay.Open sqlstmt, "DSN=Memory", 3, 3, 1

%>

<td width="25%" align="center">

<%IF TableDisplay("Selected")=1 THEN%>

<p align="center"><font face="Century Gothic" color="#008000"> <b><%=TableDisplay("Table_Name")%></b></font>

</td>

<%ELSE%>

<p align="center"><font face="Century Gothic" color="#008000"> <b><%=a%></b></font>

</td> <%END IF%> <%

IF TableCount=4 THEN

TableCount=0

%>

</tr>

<tr>

<%

END IF

NEXT %>

</table>

129

C h a p te r

6

U s i n F g o r m s

<%

130

ASP Programming for the Absolute Beginner

ELSE %> <hr>

<b><center>I'm sorry, but that is not a match!</center></b> <hr>

<table border="1" width="100%"> <tr>

<%

FormCheckA=Request.Form("MatchA")

FormCheckA=CInt(FormCheckA)

FormCheckB=Request.Form("MatchB")

FormCheckB=CInt(FormCheckB)

TableCount=0

FOR a=1 to 16

TableCount=TableCount+1

'Check to see if a match is made for this table location Set TableDisplay=Server.CreateObject("ADODB.Recordset") sqlstmt="SELECT * FROM MatchData WHERE Table_Location=" & a TableDisplay.Open sqlstmt, "DSN=Memory", 3, 3, 1

%>

<td width="25%" align="center">

<%IF TableDisplay("Selected")=1 OR FormCheckA=a OR FormCheckB=a THEN%> <p align="center"><font face="Century Gothic" color="#008000"> <b><%=TableDisplay("Table_Name")%></b></font>

</td>

<%ELSE%>

<p align="center"><font face="Century Gothic" color="#008000"> <b><%=a%></b></font>

</td> <%END IF%> <%

IF TableCount=4 THEN

TableCount=0

%>

</tr>

<tr>

<% END IF NEXT %>

</table>

<%END IF ELSE

'Execute the code to randomly build the matches 'Comment this

Guesses=0

Dim CheckVar

For i=1 to 16

set DataCheck=Server.CreateObject("ADODB.Recordset") sqlstmt="SELECT * FROM MatchData WHERE ID=" & i DataCheck.Open sqlstmt, "DSN=Memory", 3, 3, 1 B=DataCheck("Rnd_Assigned")

IF B=0 THEN

CheckVar=0

ELSE

CheckVar=1

END IF

DO UNTIL CheckVar = 1

RANDOMIZE

RndCheck=INT((16-1+1) * RND + 1)

For Index = 1 to 750000

Next

set Datacheck2=Server.Createobject("ADODB.Recordset") sqlstmt2="SELECT * FROM MatchData WHERE Table_Location="&RndCheck DataCheck2.Open sqlstmt2, "DSN=Memory", 3, 3, 1

If DataCheck2.recordcount = 0 THEN

set InsertCheck=Server.Createobject("ADODB.Recordset") sqlinsert="UPDATE MatchData SET Table_Location=" & RndCheck & ", Rnd_Assigned=1 " & " WHERE ID="& i

InsertCheck.Open sqlinsert, "DSN=Memory"

CheckVar=1

END IF

DataCheck2.close

Set DataCheck2=nothing

131

C h a p te r

6

U s i n F g o r m s

132

ASP Programming for the Absolute Beginner

LOOP

Datacheck.close

Set Datacheck=nothing

Next

%>

<table border="1" width="100%"> <tr>

<%

TableCount=0

FOR a=1 to 16

TableCount=TableCount+1

%>

<td width="25%" align="center">

<p align="center"><font face="Century Gothic" color="#008000"> <b><%=a%></b></font>

</td>

<%

IF TableCount=4 THEN

TableCount=0

%>

</tr>

<tr>

<% END IF NEXT %>

</table> <%END IF%>

<%

'Now, scan the database to see if all matches have been made, and 'the game has been won

Set WinCheck=Server.CreateObject("ADODB.Recordset") sqlstmt="SELECT * FROM MatchData WHERE Selected=1" WinCheck.Open sqlstmt, "DSN=Memory", 3, 3, 1

IF WinCheck.Recordcount=16 THEN

'Insert player information into the database Player=Request.form("Player") Guesses=Guesses

sqlstmt="INSERT INTO History (Name, Guesses) VALUES

('"& Player & "'," & Guesses & ")"

Set GameInsert=Server.CreateObject("ADODB.Recordset") GameInsert.Open sqlstmt, "DSN=Memory", 3, 3, 1

%>

<hr>

<b><center>YOU WIN THE GAME!</center></b><p> You won the game in <%=guesses%> guesses! <hr>

<form method="POST" action="Memory_Home.asp">

<center><input type="submit" value="Play again" name="b2"></center> </form>

<% ELSE %>

<p><b>Make your  match selections:</b></p> <form method="POST" action="Memory_Process.asp">

<table border="1" width="100%"> <tr>

<td width="50%" align="center">

<p><font face="Century Gothic" size="4"><b>Match A: <select size="1" name="MatchA">

<%

FOR i=1 to 16 %>

<option value="<%=i%>"><%=i%></option> <%

NEXT %>

</select></b></font></p>

</td>

<td width="50%" align="center">

<font face="Century Gothic" size="4"><b>MatchB: <select size="1" name="MatchB">

<%

FOR i=1 to 16 %>

<option value="<%=i%>"><%=i%></option> <%

NEXT %>

</select></b></font></td>

</tr>

133

C h a p te r

6

U s i n F g o r m s

134

ASP Programming for the Absolute Beginner

</table>

<p align="center"><input type="submit" value="Make a Match!" name="B1"></p>

<input type="hidden" name="GameStart" value="1">

<input type="hidden" name="Guesses" value="<%=Guesses%>"> <%

IF Request.Form("GameStart")=1 THEN%>

<input type="hidden" name="Player" value="<%=Request.Form("Player")%>"> <%ELSE%>

<input type="hidden" name="Player" value="<%=Request.Form("Name")%>"> <%END IF%>

</form>

<p> </p> <%END IF%> </body>

</html>

I will explain this code in sections to ensure that you understand not only the basic script but also how it integrates with the Memory database, which it does extensively.

The Memory_Process.asp Page: Setting Up the Game

Take another look at the following excerpt:

<html>

<head>

<title>The Memory Game!</title> </head>

<body>

<p align="left"><b><font face="Century Gothic" size="5" color="#800000">The Memory Game</font></b></p>

<%

'Check to see if gameboard has been generated

IF Request.Form("GameStart")=1 THEN%>

<b><center>Player: <%=Request.Form("Player")%></center></b> <%

'Add to the number of guesses Guesses=Request.Form("Guesses") Guesses=CInt(Guesses)

Guesses=Guesses+1

'Gameboard has been generated -- check for matches

Set MatchCheckA=Server.CreateObject("ADODB.Recordset")

sqlstmt="SELECT * FROM MatchData WHERE Table_Location="&Request.Form("MatchA") MatchCheckA.Open sqlstmt, "DSN=Memory", 3, 3, 1 CheckA=MatchCheckA("Table_Name")

Set MatchCheckB=Server.CreateObject("ADODB.Recordset")

sqlstmt="SELECT * FROM MatchData WHERE Table_Location="&Request.Form("MatchB") MatchCheckB.Open sqlstmt, "DSN=Memory", 3, 3, 1 CheckB=MatchCheckB("Table_Name")

'Check to see if a match has been made

IF CheckA=CheckB AND Request.Form("MatchA") <> Request.Form("MatchB") THEN

'A match is made -- update the MatchData table to reflect this

Set MatchInsert=Server.CreateObject("ADODB.Recordset")

sqlstmt="UPDATE MatchData SET Selected=1 WHERE Table_Name='" & CheckA & "'" MatchInsert.Open sqlstmt, "DSN=Memory", 3, 3, 1

'Pause briefly to allow for update to be completed FOR Z=1 to 750000

NEXT %>

This initial section of code checks whether the game has already started, and if it has, checks to determine whether a match has been made. Remember how I said that the Memory_Process.asp page submits back to itself? When the game begins, the player selects two possible table locations for a match. When the player clicks Submit, the form has its action method set to Memory_Process.asp. This type of coding is convenient for keeping all the action confined to one place, so to speak, because you can put a lot of code in one container; that is, one Web page.

In the preceding code, the first task is to see whether the game board has been generated. The game board generation code is the random number generator code, as you will soon see. For each game, you want the game board to be generated only once, so the matching pairs are assigned a random number between 1 and 16—their unique hidden location on the game board—and they keep that location throughout the game. If the game has already started, the name of the player is captured, and the current guess is added to the overall number of guesses.

Assuming that the game board has been generated, which is determined by the IF Request.form ("GameStart") = 1 THEN) statement, the code checks whether a match has been made. The database is queried twice (note the connections MatchCheckA and MatchCheckB).

135

C h a p te r

6

U s i n F g o r m s