Recall our
Tic-Tac-Toe
assignment of BA371. Now, using the techniques you learned in
our
ASP.Net 2.0 lab and
LAMP lab port the game to
the
Web using ASP.Net 2.0.
This exercise is at once both 'easy' and 'difficult.' The 'easy' part
is that you can reuse much of the code you already wrote in BA371;
after all,
ASP's 'code-behind' facilities that we explored in the
ASP.Net 2.0 lab are meant to do just that.
The hard part
is that whereas in a Windows or command-line app, state is
automatically maintained in the app's memory, on the Web we have to
overcome
HTTP's statelessness using the methods we practiced in the
ASP.Net lab and and
LAMP lab.
Recall that in our
LAMP lab, we have
already ported our Tic-Tac-Toe game to the Web and we already had to
maintain state. Therefore, from that lab you should have developed a
good idea on which state variables to maintain. However, whereas in
our LAMP version of Tic-Tac-Toe we maintained all state variables
ourselves by storing then in
<input
type="hidden"> fields in the form, in the ASP.Net version
you must
use the ViewState()
and/or SessionState()
techniques demonstrated in the ASP.Net lab.
Click
here for a demo version.
Hints and tips:
- Your ASP.Net Web page should contain only four components:
- an asp:Literal
to display the board.
- an asp: Literal
to display the who's-on-turn prompt and the end of the game.
- an asp:TtextBox
where the user will type the move.
- an asp:Button
that first of the Web request.
- Use the code-behind facilities we encountered in our ASP.Net
lab. In other words, do not include any code but the
above-mentioned four components in your Default.aspx
page. Stick all your VB.Net code in the Default.aspx.vb
file.
- Recall from our ASP.Net
lab that the asp:Button
will be tied to an event handler; just as we have seen in standard
VB.Net. It's in this event handler that you'll process a move.
- Retain your two-dimensional array of shorts to represent the
board. The Tic-Tac-Toe version of the LAMP
lab represented the board in a one-dimensional array. Doing this,
however, would require you to rewrite some important sections of your
BA371 Tic-Tac-Toe code. Retaining the two-dimensional array of
shorts version saves you that work. However, the current version of
ASP.Net does not allow
a
two-dimensional array of shorts to be stored in ViewState().
Hence, you must(!!) use SessionState()
to store the array. You can use ViewState()
for all other state variables.
- Recall that in the ASP.Net 2.0 lab
we used the Page_Load()
subroutine. Page_Load()
gets automatically called when an ASP.Net Web page loads. This (the
time of loading the page) would be a good time to read all the state
variables back from a previously stored ViewState()
and SessionState().
Remember, HTTP is stateless, so whenever the state of the game changes,
this state must be stored somewhere, so that at the start of the next
move, the old state can first be restored. Restoring state this way
works great, except on the very first call because on the first call
there is no saved state yet; it's the first call who must establish the
state (make sure you understand
what you just read!). Fortunately, ASP.Net keeps a variable IsPostBack,
which is set to True on all
but the very first call to the ASP.Net page. By checking the value of
this variable, Page_Load()
can decide whether or nor to restore any previously stored states.
- Page_Prerender()
is another of those automatically called subroutines. It's called after
Page_Load()and
after any buttonclick event handlers, but before the page's Literals
are rendered. Hence, Page_Prerender()
is a good place to store state variables so that they are available at
the next invokation of the Web page. Please note!! PagePrerender()
will be called if and only if, the Asp.Net Web page has its AutoEventWireUp
directive set to true.
- In summary, your Default.aspx.vb
file contains only one class which itself contains only five subroutines:
- Page_Load()
- Page_Prerender()
- Your_Button_Event_Handler()
- DisplayBoard()
- CheckForWinner()