Welcome Guest ( Log In | Register )




                Web Hosting Guide

 
Reply to this topicNew Topic
[javascript] Sliding Tile Game ... Help?
xboxrulz
post Apr 8 2009, 02:18 AM
Post #1


Colonel Panic
Group Icon

Group: [MODERATOR]
Posts: 3,051
Joined: 25-March 05
From: Toronto, Ontario, Canada
Member No.: 3,233
myCENTs:38.62


Hey guys,

I really need some help here and I'd appreciate if you guys can help me.

Alright, so I'm suppose to design a sliding tiles game. However, if you play around with it, it obviously doesn't work as you can see:



URL: http://matrix.senecac.on.ca/~akwok9/bti220/assign3/

To replicate the bug above, all you need to do is to start playing from bottom up and then the tiles starts disappearing.

Also, another issue is that I don't quite know how to tackle checking when you win. I understand that it must match 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15. Can you give me a hint? My implementation is definitely flawed and is shown in the code below.

CODE
<script type="text/javascript">
    //<![CDATA[
    var rows = new Array(4)
    rows[0] = new Array (4)
    rows[1] = new Array (4)
    rows[2] = new Array (4)
    rows[3] = new Array (4)
    
    function checkWin() {
        var winner = false
        var checker = new Array(4)
        checker[0] = new Array (1, 2, 3, 4)
        checker[1] = new Array (5, 6, 7, 8)
        checker[2] = new Array (9, 10, 11, 12)
        checker[3] = new Array (13, 14, 15, 0)

        for (i = 0; i < 4; i++){
            for (j = 0; j < 4; j++){
                if (rows[i][j] == checker[i][j]){
                    winner = true
                }                    
            }
        }
        if (winner){
            alert("Congratulations! You've Won!")
            return true
        }
        return false
    }
                

    function move(tile){
        var obj = document.getElementById('tile' + tile)
        var win = false
        for (i = 0; i < 4; i++){
            for (j = 0; j < 4; j++){
                if (rows[i][j] == tile){
                    if (j > 0 && rows[i][j - 1] == 0){
                        obj.style.left = (j - 2) * 100 + 'px'
                        rows[i][j - 1] = tile
                        rows[i][j] = 0                        
                    }else if (j < 3 && rows[i][j + 1] == 0){
                        obj.style.left = (j + 2) * 100 + 'px'
                        rows[i][j + 1] = tile
                        rows[i][j] = 0
                    }else if (i > 0 && rows[i - 1][j] == 0){
                        obj.style.top = (i - 2) * 100 + 'px'
                        rows[i - 1][j] = tile
                        rows[i][j] = 0
                    }else if (i < 3 && rows[i + 1][j] == 0){
                        obj.style.top = (i + 2) * 100 + 'px'
                        rows[i + 1][j] = tile
                        rows[i][j] = 0
                    }
                    win = checkWin()
                    if (win){
                        break
                    }
                    return    
                }
            }
        }
                            
    }

    function initialize(){
        var check = new Array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
        for (i = 0; i < 4; i++) {
            for (j = 0; j < 4; j++) {
                if (i == 3 && j == 3){
                                        rows[i][j] = 0
                                } else {
                    var n = Math.ceil(Math.random() * 15)
                    while (check[n - 1] == 0){
                        n = Math.ceil(Math.random() * 15)
                    }
                    rows[i][j] = n
                    check[n - 1] = 0
                    document.getElementById('tile' + n).style.left = (j + 1) * 100 + 'px'
                    document.getElementById('tile' + n).style.top = (i + 1) * 100 + 'px'
                }
            }
        }             
    }
    // ]]>
   </script>


Many thanks,
xboxrulz
Go to the top of the page
 
+Quote Post
HannahI
post Nov 5 2009, 08:29 PM
Post #2


Don't Worry, I'm here
Group Icon

Group: Members
Posts: 271
Joined: 3-October 09
From: Northeast, United States of America
Member No.: 43,278
myCENTs:24.29


I'm guessing that you need to check if you can't move that area and if you can't do something like this:
CODE
alert("Sorry, Invalid Move");

Hope this helps! biggrin.gif

This post has been edited by HannahI: Nov 5 2009, 08:29 PM
Go to the top of the page
 
+Quote Post

Reply to this topicNew Topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   5 Curt200518 259 11th November 2009 - 06:29 PM
Last post by: yordan
No New Posts   0 HannahI 40 8th November 2009 - 01:18 AM
Last post by: HannahI
No New Posts   0 michellezum 81 5th November 2009 - 09:57 PM
Last post by: michellezum
No New Posts   8 unclebob 2,237 5th November 2009 - 08:38 PM
Last post by: HannahI
No New Posts 4 rahid 918 4th November 2009 - 12:27 AM
Last post by: HannahI
No New Posts   9 Sizux 7,674 3rd November 2009 - 11:28 PM
Last post by: iG-MomaNana2008
No New Posts   0 HannahI 24 3rd November 2009 - 09:43 PM
Last post by: HannahI
No New Posts 3 astdesy 499 8th October 2009 - 08:23 PM
Last post by: HannahI
No New Posts   11 Daming 10,356 6th October 2009 - 12:00 PM
Last post by: iG-
No New Posts   8 kc8ual 2,582 5th October 2009 - 08:56 PM
Last post by: HannahI
No New Posts   9 dragoneye 8,071 5th October 2009 - 09:15 AM
Last post by: starscream
No New Posts   2 Unknown_99 191 2nd October 2009 - 10:52 PM
Last post by: willielwgg
No new   34 vstxy 6,633 30th September 2009 - 11:47 AM
Last post by: iG-xxRAZORxx
No New Posts 8 kingdomtroy 1,801 27th September 2009 - 12:49 AM
Last post by: Darowen
No New Posts 17 Z-0-M 5,189 25th September 2009 - 10:02 AM
Last post by: iG-Dharamveer Singh Chouhan


Web Hosting Powered by ComputingHost.com.