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

ASP Programming for the Absolute Beginner

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

176

ASP Programming for the Absolute Beginner

The computer wins

 

 

Y

the battle. With two

 

 

Aces and an 8, the

 

 

computer’s total

 

 

score is 36 (each

 

 

 

 

 

Ace is worth 14

 

L

points, and the 8

 

 

F

card equals 36).

 

 

 

M

 

3. If you scroll down the page, underneath the COMPUTER WINS THE BATTLE! message, you

 

A

 

will see a prompt to enter the number of armies you want to gamble in the next

battle (see Figure 8.9). Note how the total of your available armies is automati-

 

E

 

 

T

 

 

cally computed, based on the results of the current round. In this example, the player gambled 100 of his 200 armies. Because the computer won the battle, he now has only 100 armies left (200 - 100 = 100). The computer, whenever it wins, adds to its total the number of armies the player gambled.

FIGURE 8.9

Enter the number of armies you want to gamble for the next round.

Click Proceed to next battle!

Team-Fly®

FIGURE 8.10

The player wins this round. Note how the current round is tracked at the top of the screen.

4.As the game continues, note how the current round is tracked (in Figure 8.10, the game is in round 2). Also, in this figure, the player has won the battle with a total score of 32 (Ace = 14, King = 13, and a 5).

5.Scrolling to the bottom of this screen (refer to Figure 8.10), the player has a total of 150 armies, but this time he forgets the rule that he must gamble at least half his armies and enters only 50. When he clicks the submit button, he is presented with an error message, shown in Figure 8.11.

FIGURE 8.11

You must gamble at

least half your

available armies to

fight each battle.

177

C h a p te r

8

E s s en t i a l

P r og r a m m i n g

L o g i P c, ar I t I

178 6. The game is also smart enough to know whether you are trying to gamble more armies than you have available. If you do, you see the message shown in Figure 8.12.

ASP Programming for the Absolute Beginner

7.The game continues through 10 rounds. The highest score at the end of the tenth round wins the game, as shown in Figure 8.13.

FIGURE 8.12

Don’t try to gamble

more armies than

you have available!

FIGURE 8.13

The war is over! Can you fight the evil computer and bring peace to Silicon Valley?

Analyzing the Code

The game consists of two ASP pages:

ASPWar_Home.asp. This displays the opening text and asks you to enter your name and the initial number of armies you want to fight with.

ASPWar_Play.asp. This is where all the action of the game occurs, including the display of the cards, determining who wins each round, the tracking of the round and score, and the final determination of who wins the war (the game).

The War.mdb database. This is used to track the randomly selected cards from hand to hand so that the player and the computer are not dealt the same card(s).

Listing 8.13 is the ASPWar_Home.asp page.

Listing 8.13 ASPWar_Home.asp

<html>

<head>

<title>ASP War!</title> </head>

<body>

<p align="center"><font face="Courier New" size="7" color="#FF0000"> <b>ASP WAR</b></font></p>

<hr>

<p align="left"><font size="3">So private, you think you got what it takes to be a general?  You better be ready, because your computer just might have other plans!  Gamble your armies on the turn of a card: if you get lucky, you'll gain reinforcements allowing you to mount even bigger offensives.  Have a bad day, and you're going to be very unpopular on the home front. So, Dr. Strangelove, enter your name below and get ready for...WAR!</font></p>

<p align="center"><b><font size="3" color="#008000">BEGIN THE WAR WITH 200 ARMIES</font></b></p>

<p align="center"><b><font size="3" color="#008000">YOU MUST ENGAGE AT LEAST HALF OF YOUR AVAILABLE ARMIES IN EACH BATTLE!</font></b></p>

<hr>

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

<p align="left">Enter your name here: <input type="text" name="Player" size="20"></p>

<p align="left">Enter number of armies for first battle: <input type="text" name="Armies" size="10"></p>

<p align="center"><input type="submit" value="Begin the War!" name="B1"></p> <input type="hidden" name="TotalArmies" value="200">

<input type="hidden" name="CTotalArmies" value="100"> <input type="hidden" name="Round" value="1">

</form>

</body>

</html>

179

C h a p te r

8

E s s en t i a l

P r og r a m m i n g

L o g i P c, ar I t I

180

ASP Programming for the Absolute Beginner

This is a very simple form, but you should take note of the specific hidden form fields set and passed along with the initial number of armies to be used (form element Armies), as well as the player’s name (form element Player). Specifically, three hidden form fields are initially set on this page:

TotalArmies. Tracks the total number of armies you gain or lose throughout the course of the game. As you progress through each round, TotalArmies tracks the total number of armies you have available to fight with. To begin the game, you have 200 armies at your disposal.

CTotalArmies. Functions in exactly the same way as the TotalArmies variable but tracks the armies available to the computer. Note that the initial value of this variable is set to 100.

Round. Tracks the current round of the game. After the tenth round, the TotalArmies and CTotalArmies variables are compared, with the highest one indicating who wins the game.

TRICK

Why make the TotalArmies, CTotalArmies, and Round variables hidden? They are used

 

 

to track specific game components, so they can remain behind the scenes because

 

they require no direct manipulation by the user.

Listing 8.14 is the ASPWar_Play.asp page, where all the game action takes place. Although at first glance this might seem complicated, it’s quite straightforward and easy to understand.

Listing 8.14 ASPWar_Play.asp

<html>

<head>

<title>ASP War!</title> </head>

<body>

<%

ArmyCheck=Request.Form("TotalArmies")

ArmyCheck=CInt(ArmyCheck)

ArmyCheckStatic=ArmyCheck

ArmyCheck=ArmyCheck / 2

ArmyBet=Request.Form("Armies")

ArmyBet=CInt(ArmyBet)

'Check to see if game is over 'Only 10 rounds are played

IF Request.Form("Round")=11 THEN

%>

<hr>

<b><center>The War Is Over!</center></b> <hr>

<%

'Check to see who won the game

IF Request.Form("TotalPoints") > Request.Form("CTotalPoints") THEN %>

<b>YOU WIN THE BATTLE!</B><p>

Your score: <%=Request.Form("TotalArmies")%><p> Computer score: <%=Request.Form("CTotalArmies")%> <%

ELSE %>

<b>SORRY! THE COMPUTER HAS WON THE BATTLE!</b><p> Your score: <%=Request.Form("TotalArmies")%><p> Computer score: <%=Request.Form("CTotalArmies")%> <%END IF%>

<%

'Check to see number of armies. Remember, the player must 'gamble at least half his or her available armies

'Check to see if player has gambled more armies 'than she or he has available

ELSEIF ArmyBet > ArmyCheckStatic THEN

'Player needs to reduce number of gambled armies

%>

<b>Sorry, but you don't have that many armies!</b><p> <form method="POST" action="ASPWar_Play.asp">

<b>Enter number of armies for next battle (Available: <%=Request.Form ("TotalArmies")%>): <input type="text" name="Armies" size="10"></b> <p>

<center><input type="submit" value="Click here to gamble more armies"></center> <input type="hidden" name="player" value="<%=Request.Form("Player")%>">

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

<input type="hidden" name="round" value="<%=Request.Form("Round")%>"> <input type="hidden" name="CTotalArmies" value="<%=Request.Form ("CTotalArmies")%>">

</p>

</form>

<p>

<%

ELSEIF ArmyCheck > ArmyBet THEN 'Player needs to gamble more armies.

181

C h a p te r

8

E s s en t i a l

P r og r a m m i n g

L o g i P c, ar I t I

182

ASP Programming for the Absolute Beginner

%>

<b>Sorry, but you need to gamble at least half of your available armies!</b><p> <form method="POST" action="ASPWar_Play.asp">

<b>Enter number of armies for next battle (Available: <%=Request.Form ("TotalArmies")%>): <input type="text" name="Armies" size="10"></b> <p>

<center><input type="submit" value="Click here to gamble more armies"></center> <input type="hidden" name="player" value="<%=Request.Form("Player")%>">

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

<input type="hidden" name="round" value="<%=Request.Form("Round")%>"> <input type="hidden" name="CTotalArmies" value="<%=Request.Form ("CTotalArmies")%>">

</p>

</form>

<p>

<%

'If player has gambled at least half of his or her available armies, 'then the game may continue

ELSE

'Define Arrays

Dim PlayerCards(3)

Dim ComputerCards(3)

Dim ComputerScore(3)

Dim PlayerScore(3)

'Set player name Player=Request.Form("Player") %>

<hr>

<b><center>BATTLE No.<%=Request.Form("Round")%></center></b> <hr>

<table border="0" width="100%" bgcolor="#C0C0C0"> <tr>

<td width="50%" colspan="2">

<p align="center"><b><font color="#008000">                </font><font color="#008000"><u> <%=Request.Form("Player")%></u> </font></b></td>

<td width="50%" colspan="2">

<p align="left"><b><font color="#008000">                                    

<u>COMPUTER</u>

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

</tr>

<%

'Randomly select the cards from the database, for this battle 'We will need six cards, so the loop will repeat six times

'As each card is randomly selected, the Selected column is marked in the 'database, and the card name is placed into one of the two arrays

'for retrieval during the battle!

'Determine Player Cards For a=1 to 3

CheckVar=0

'Check to see if this card has already been selected from the database DO UNTIL CheckVar=1

CheckVar=0

RANDOMIZE

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

set CardCheck=Server.CreateObject("ADODB.Recordset") sqlstmt="SELECT * FROM Cards WHERE ID="& RndCheck CardCheck.Open sqlstmt, "DSN=War", 3, 3, 1

IF CardCheck("Selected")=0 THEN

CheckVar=1

PlayerCards(a)=CardCheck("CardName")

PlayerScore(a)=CardCheck("CardValue")

'Update Database

set CardUpdate=Server.CreateObject("ADODB.Recordset") sqlstmt="UPDATE Cards SET Selected=1 WHERE ID="&RndCheck CardUpdate.Open sqlstmt, "DSN=War", 3, 3, 1

END IF

LOOP

Next

'Determine Computer Cards For a=1 to 3

CheckVar=0

'Check to see if this card has already been selected from the database DO UNTIL CheckVar=1

CheckVar=0

RANDOMIZE

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

183

C h a p te r

8

E s s en t i a l

P r og r a m m i n g

L o g i P c, ar I t I

set CardCheck=Server.CreateObject("ADODB.Recordset")

184

sqlstmt="SELECT * FROM Cards WHERE

ID="&

RndCheck

 

CardCheck.Open sqlstmt, "DSN=War",

3, 3,

1

ASP Programming for the Absolute Beginner

IF CardCheck("Selected")=0 THEN

CheckVar=1

ComputerCards(a)=CardCheck("CardName")

ComputerScore(a)=CardCheck("CardValue")

'Update Database

set CardUpdate=Server.CreateObject("ADODB.Recordset") sqlstmt="UPDATE Cards SET Selected=1 WHERE ID="&RndCheck CardUpdate.Open sqlstmt, "DSN=War", 3, 3, 1

END IF

LOOP

Next

'Now, begin the battle! %>

<tr>

<td width="11%"><font color="#FF0000"><b>BANG!</b></font></td> <td width="39%">

<p align="center"><img border="0" src="Cards/<%=PlayerCards(1)%>.gif" width="73" height="97"></td>

<td width="41%">

<p align="center"><img border="0" src="Cards/<%=ComputerCards(1)%>.gif" width="73" height="97"></td>

</tr>

<tr>

<td width="11%"><font color="#FF0000"><b>BANG!</b></font></td> <td width="39%">

<p align="center"><img border="0" src="Cards/<%=PlayerCards(2)%>.gif" width="73" height="97"></td>

<td width="41%">

<p align="center"><img border="0" src="Cards/<%=ComputerCards(2)%>.gif" width="73" height="97"></td>

</tr>

<tr>

<td width="11%"><font color="#FF0000"><b>BANG!</b></font></td> <td width="39%">

<p align="center"><img border="0" src="Cards/<%=PlayerCards(3)%>.gif" width="73" height="97"></td>

<td width="41%">

<p align="center"><img border="0" src="Cards/<%=ComputerCards(3)%>.gif" width="73" height="97"></td>

</tr>

</table>

<%

'Determine who won this battle PScore=PlayerScore(1)+PlayerScore(2)+PlayerScore(3) CScore=ComputerScore(1)+ComputerScore(2)+ComputerScore(3)

IF PScore > CScore THEN

'Set Armies

A=Request.Form("TotalArmies")

B=Request.Form("Armies")

A=CInt(A)

B=CInt(B)

TotalArmies=A+B

CTotalArmies=Request.Form("CTotalArmies")

%>

<p align="center"><b><font color="#FF0000" size="5">PLAYER WINS THE BATTLE!</font></b></p>

<%END IF%> <%

IF CScore > PScore THEN

A=Request.Form("TotalArmies")

B=Request.Form("Armies")

TotalArmies=A-b

'When the computer wins, they get armies added to their total C=Request.Form("CTotalArmies")

C=CInt(C)

CTotalArmies=C+B

%>

<p align="center"><b><font color="#FF0000" size="5">COMPUTER WINS THE BATTLE!</font></b></p>

<%END IF%>

<%IF PScore=CScore THEN TotalArmies=Request.Form("TotalArmies") CTotalArmies=Request.Form("CTotalArmies") %>

<p align="center"><b><font color="#FF0000" size="5">STALEMATE!</font></b></p> <%

END IF

%>

<%

'Reset the database

set CardReset=Server.CreateObject("ADODB.Recordset") sqlstmt="UPDATE Cards SET Selected=0"

CardReset.Open sqlstmt, "DSN=War", 3, 3, 1

185

C h a p te r

8

E s s en t i a l

P r og r a m m i n g

L o g i P c, ar I t I