Solved

Techniques for displaying game board

Posted on 2004-03-23
5
263 Views
Last Modified: 2012-05-04
Right now I am using an ASP page to create a 10 row  X  10 column table.

Then I am filling that table with images (as I read through a dataset from an Access Table):


http://www.knowltonfamily.com/robotzweb/game.asp



My question is.....do I need a HTML table structure to do all of this?


Here is the full ASP source (below).  The only reason I am "doing it this way" is because I am a complete novice at this....and I don't know any better.

If there is a better way to do what I am trying to do, then please give me some pointers.  :)

TIA,

Tom


~~~~~~~~~~~~~~~~~~~



<!--#include File="connections.asp" -->
<%


Dim sInitGame
'Dim moveState
'squareOne = "One"
'squareTwo = "Two"
sInitGame = Request.Form("newgame")
'sInitGame = "New Game"

if request("subform") = "Yes" then
      Response.Write "Subform value was YES"
       strUp = "UPDATE tblGameBoard, tblGameBoard tb SET tblGameBoard.[Image] = tb.[Image] WHERE tblGameBoard.SquareID="&request("NewSqID")&" AND tb.SquareID = "&request("OldSqID")&";"
      strUp2 = "Update tblGameBoard set [Image]='blank' Where SquareID = "&request("OldSqID")
      Conn.execute(strUp)
      Conn.execute(strUp2)  
end if
%>

<form name="GameForm" action="game.asp" method=get>

<%
call sbInitGame()

Sub sbInitGame()
      response.Write "Initializing Game"
      response.Write "<br><br><br>"
      response.Write "Game board:   Click a game piece to select it, then click new square to move game piece to"
      response.Write "<br><br><br>"
       set rs=Server.CreateObject("ADODB.recordset")
      rs.CursorType = 3
      sql="select [Row], [Column], [Image], [Value], [SquareID] from tblGameBoard where GameNumber='1' Order by [Row], [Column]"
        rs.Open sql, Conn
      oldRow=""
      %><table><%
      if (rs.EOF or rs.BOF) then                
          response.write "No Row in the table<br>"
      else
            do until rs.eof    
                  currentRow = rs("row")
                  currentImage = rs("Image")
                  ValueReadIn = rs("Value")
                if not(currentRow = oldRow) then
                      %><tr><%                    
                end if%>
                  <td onclick="OnSquareClick('<%=rs("SquareID")%>','<%=currentImage%>','<%=ValueReadIn%>')">
              <img src="images\<%=currentImage%>.jpg">
                 </td>  
                 <%oldRow = currentRow
                 rs.MoveNext     'This tells the rows to move to the next row
            loop          'This tells the 'DO UNTIL' to move on and do it all over again.
            rs.close
          set rs=nothing     'Its a really good idea to close recordsets and connections when your done.
            %></tr><%
      end if                
      %></table><%
End Sub
%>


<!--#include File="closeconnections.asp" -->


Old Square ID:&nbsp;&nbsp;<input name="OldSqID" value="None">
<br>
New Square ID:&nbsp;&nbsp;<input name="NewSqID" value="None">
<br>
Item Value First:&nbsp;&nbsp;<input name="ItemValueFirst" value="None">
<br>
Item Value Second:&nbsp;&nbsp;<input name="ItemValueSecond" value="None">
<br>
Sub Form:&nbsp;&nbsp;<input name="SubForm" value="No">
<br>

<br>
<br>
</form>


<script type="text/javascript"  language="Javascript">

function OnSquareClick(square,imagename,ValueOfItem)
{
  Origin=document.GameForm.OldSqID;
  Dest=document.GameForm.NewSqID;
  SubForm=document.GameForm.SubForm;
  VOI_First=document.GameForm.ItemValueFirst;
  VOI_Second=document.GameForm.ItemValueSecond;
 
  if(Origin.value == Dest.value)
  {
    Origin.value = square;
      VOI_First.value= ValueOfItem;
    SubForm.value = "No";
  }
  else if(Origin.value != Dest.value)
  {
    Dest.value = square
    SubForm.value = "Yes";
      VOI_Second.value=ValueOfItem;
    document.forms[0].submit();
  }
}//End of OnSquareClick



</script>
0
Comment
Question by:knowlton
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:sforcier
ID: 10662155
You're offering a lot of points for code that looks generally good to me. Only thing I see that looks amiss is that I think you're not closing your table's rows (there's no </tr>).

As for pointers, here are my two cents (these are mostly coding habit comments so they're opinons only):

- You are presumably dim'ing and opening your connection in the include file (since I don't see that anywhere else). I don't like to use variables from outside the current "scope". This means that I'd actually dim the connection object in the page, and use the include to store a connection function, like so:

'In the include file (connection.inc)
Function GetDBConnection()
   Set GetDBConnection = Server.CreateObject("ADODB.Connection")
   GetDBConnection.Open "some string..."
End Function

'In the page itself
<!-- #include file="connection.inc" -->
<%
Dim objCon
Set objCon = GetDBConnection()
...

This just keeps, in my opinion, the code cleaner and easier to read.

- In the same vein, I'd modify the sbInitGame() function declaration to be sbInitGame(ByRef objCon) and I'd pass in the valid connection object. The ByRef (ByReference) cuts down on overhead by using the actual object that is passed in (as opposed to ByVal [ByValue] which is a copy). Again, this is for readability/portability, nothing earth shattering.

- I think you get the idea as to what I'd say about the close connection... same as the first comment.

All in all, I wouldn't be able to tell you're a novice by looking at the code, so you're not in bad shape!
0
 
LVL 5

Author Comment

by:knowlton
ID: 10662334
@ sforcier:

<quote>
All in all, I wouldn't be able to tell you're a novice by looking at the code, so you're not in bad shape!
</quote>

LOL.  Thanks.  But to be fair...the only reason the code looks as good as it does is because I have gotten A LOT OF HELP from the kind folks here in EE.

This project is a PURE learning project for me.  I picked a game as my first "big" ASP project......simply because I hoped that the fact that it was a game would add some intrinsic interest finishing the project just for the sake of the "FUN" it would be to play once it was up and running.



So as far as the game board itself is concerned.......if you were faced with creating a chess board, etc......what would be your approach?  Would you also use a table with rows and columns to house the squares?
0
 
LVL 6

Accepted Solution

by:
sforcier earned 500 total points
ID: 10663053
Yeah, I'd do pretty much exactly what you've got there. If you want to add another "niceity" you could update your looping to resemble this:

          dim strStyle, i
          i = 0
          do until rs.eof    
               if (i Mod 2) = 0 then
                  strStyle = "background-color: #990000;" 'change the color here, this is just red
               else
                  strStyle = "background-color: #009900;" 'change the color here, this is just green
               end if
               currentRow = rs("row")
               currentImage = rs("Image")
               ValueReadIn = rs("Value")
              if not(currentRow = oldRow) then
                   %><tr><%                    
              end if%>
               <td onclick="OnSquareClick('<%=rs("SquareID")%>','<%=currentImage%>','<%=ValueReadIn%>')" style="<%=strStyle%>">
             <img src="images\<%=currentImage%>.jpg">
               </td>  
               <%oldRow = currentRow
               i = i + 1
               rs.MoveNext     'This tells the rows to move to the next row
          loop          'This tells the 'DO UNTIL' to move on and do it all over again.

This will alternate the color of the squares, like in a chess board. If you do this, then you'll have to use gifs with transparencies in them to see the squares change color. Since your game (I'm presuming) isn't a standard board game, you may not want to use the alternating colors, but if you do, there you go.

The code you posted looks great to me, I wouldn't do anything differently, other than what I've suggested already. As a note about this last chunk of code, and coding habits, I recommend putting all Dim's (declarations) at the top of the page/function for readability's sake. Oh, that reminds me of something I would *definately* add. The top of your page should look like this:

<%@ LANGUAGE="VBScript" %>
<%
Option Explicit
...

I'm not sure technically why the first line is needed. It's a compiler directive for the server telling it to use VBScript, but I can't think of a web server configuration that doesn't use VBScript by default. The second line though, "Option Explicit" is *very* useful. Not only does it increase (slightly) the speed of your pages, but it will reduce troubleshooting time for some difficult to spot bugs. The Option Explict *forces* you to declare every variable that you use. If you don't declare a variable, it will cause an error (the bonus is it tells you exactly which line of code). Take the following code as an example:

Dim EasilyMisspelled
EasilyMisspelled = 5
Response.Write(EasilyMisspelled + 10)

With Option Explicit off, your output will be 10 (not 15 which is expected), because the variable used in the response.write is misspelled (ironically, I'm not sure if I'm spelling misspell correctly, but that's not the point). When you use a misspelled variable without Option Explicit, the compiler just makes a new variable and initializes it. This can cause some really hard to troubleshoot bugs because it won't always cause an error, it will just make your code behave wierd.

With Option Explicit on, you'll get an error on the specific line number saying something like "undeclared variable" which will let you easily see where the problem is.

How's that for long winded!
0
 
LVL 6

Expert Comment

by:sforcier
ID: 10663083
Ok, so even more ironically, I meant to change the spelling of the variable in the Response.Write statement in my little Option Explicit example. ;)
So it should be:

Dim EasilyMisspelled
EasilyMisspelled = 5
Response.Write(EasilyMispelled + 10)
0
 
LVL 5

Author Comment

by:knowlton
ID: 10663097
This is a lot of info to digest, but thanks!

Tom
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now