We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Techniques for displaying game board

Tom Knowlton
Tom Knowlton asked
on
Medium Priority
306 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>
Comment
Watch Question

Commented:
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!
Tom KnowltonWeb developer

Author

Commented:
@ 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?
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
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)
Tom KnowltonWeb developer

Author

Commented:
This is a lot of info to digest, but thanks!

Tom
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.