Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More Fun With JavaScript.... 3

Status
Not open for further replies.

mwolf00

Programmer
Nov 5, 2001
4,177
0
0
US
OK - So I must be bored...

<title>Javascript Tile Puzzle</title>
<script>
function mixEm(){
document.getElementById(&quot;spanPlace&quot;).innerHTML = &quot;&quot;
tempArr = new Array()
for (x=0; x<dLevel*dLevel; x++){
tempArr[x] = x

if (x==0){
newSpans = &quot;<span class=tile id=t0></span>&quot;
}
else{
newSpans += &quot;<span class=tile id=t&quot; + x + &quot; onClick='moveEm(this)'>&quot; + x + &quot;</span>&quot;
}
}
document.getElementById(&quot;spanPlace&quot;).innerHTML = newSpans
window.tileArr = new Array()
//now mix them up
numElems = tempArr.length
for (x=0; x<numElems; x++){
ranNum = Math.floor(Math.random() * tempArr.length)
tileArr.push(tempArr[ranNum])
tempArr.splice(ranNum,1)
}
}

function showEm(){
allInputs = document.getElementsByTagName(&quot;input&quot;)
for (x=0; x<allInputs.length; x++){
if (allInputs[x].checked){
window.dLevel = parseInt(allInputs[x].value)
break;
}
}

mixEm()
window.numClicks = 0
curRow = 0
curCol = 0
for (x=0; x<tileArr.length; x++){
if ( x % dLevel == 0 ){
curRow = 1
curCol ++
}
thisTile = document.getElementById(&quot;t&quot; + tileArr[x])
thisTile.style.top = curCol * 50
thisTile.style.left = curRow * 50
curRow ++
if (tileArr[x] > 0){
tileArr[x] % 2 == 0 ? color = &quot;yellow&quot; : color = &quot;green&quot;
thisTile.style.backgroundColor = color
}
}
checkEm()
}

function moveEm(inObj){
zeroTile = document.getElementById(&quot;t0&quot;)
zeroXpos = parseInt(zeroTile.style.left)
zeroYpos = parseInt(zeroTile.style.top)
inXpos = parseInt(inObj.style.left)
inYpos = parseInt(inObj.style.top)
if (zeroXpos == inXpos){
if (Math.abs(zeroYpos - inYpos) == 50){
zeroTile.style.top = inYpos
inObj.style.top = zeroYpos
numClicks ++
}
}
else if (zeroYpos == inYpos){
if (Math.abs(zeroXpos - inXpos) == 50){
zeroTile.style.left = inXpos
inObj.style.left = zeroXpos
numClicks ++
}
}
checkEm()
}

function checkEm(){
curRow = 0
curCol = 0
numWrong = 0
for (x=1; x<tileArr.length; x++){
if ((x-1) % dLevel == 0 ){
curRow = 1
curCol ++
}
thisTile = document.getElementById(&quot;t&quot; + x)
if (parseInt(thisTile.style.top) == curCol * 50 && parseInt(thisTile.style.left) == curRow * 50){
thisTile.style.color = &quot;black&quot;
}
else{
thisTile.style.color = &quot;red&quot;
numWrong ++
}

curRow ++
}
window.status = numWrong + &quot; wrong -- &quot; + numClicks + &quot; moves.&quot;
if (numWrong == 0){
alert(&quot;Solved in &quot; + numClicks + &quot; clicks&quot;)
}
}

</script>
<style>
body {font-family: arial;}
span {text-align:center;}
.tile {font-size: 26pt; border: solid blue 3px; width: 50px; height: 50px; position:absolute; }
</style>

<body onLoad=&quot;showEm()&quot; id=&quot;bodyNode&quot;>
<div id=&quot;spanPlace&quot;>
<span class=tile id=t0></span>
</div>
<div style=&quot;position: absolute; top: 50; left:400;&quot;>
<form name=&quot;gameForm&quot;>
<P><input type=radio name=&quot;levelPicker&quot; value=3 onClick=&quot;showEm()&quot;> Easy (3x3)</P>
<P><input type=radio name=&quot;levelPicker&quot; value=4 onClick=&quot;showEm()&quot; Checked> Medium (4x4)</P>
<P><input type=radio name=&quot;levelPicker&quot; value=5 onClick=&quot;showEm()&quot;> Hard (5x5)</P>
<P><input type=radio name=&quot;levelPicker&quot; value=6 onClick=&quot;showEm()&quot;> Impossible (6x6)</P>
<input type=button onClick=&quot;showEm()&quot; value=&quot;Start Over&quot;>
</form>
</div>
</body>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Anyone finish &quot;Impossible&quot;?

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
I wish that I could finish &quot;easy&quot; [hammer]
 
great, now I'm going to be clicking on this damn thing all day [lol]

I'm up to &quot;hard&quot; though

_____________________________________________________________________
onpnt2.gif
[sub]
I have the resolution perfect in my mind, but it never seems to fit on the screen quite the same way
[/sub]
 
I gave up on an &quot;impossible&quot; one yesterday after 2500 clicks - I couldn't figure how to flip 2 numbers [3eyes][dazed]

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
gr8!!!!!

any cheats???

Known is handfull, Unknown is worldfull
 
typical programmer. Any cheats???? [rofl2]

_____________________________________________________________________
onpnt2.gif
[sub]
I have the resolution perfect in my mind, but it never seems to fit on the screen quite the same way
[/sub]
 
makes life easy u know... ;)

Known is handfull, Unknown is worldfull
 
2500 clicks? Anyone see that Orbitz commercial? &quot;Digititus&quot; [lol]
 
mwolf,

It's too bad there's not a place for cool stuff like
this. I guess there could be a category in the FAQ's
called &quot;site gismos&quot;. I know another thing I'd like to
see in it is clarkin's clock.

That's all some people do at lunch time is
find a site with a game they like.

Would you guys mind if I posted my tic tac toe
program and see if you can help me &quot;polish it&quot;
I am not that great with style yet.



2b||!2b
 
Sounds like a good post - do you pay the computer in tic-tac-toe?

The domain &quot;scriptgames.com&quot; is available.... Anyone interested in building a site to keep ppl from getting work done?

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Is the impossible level really impossible?
 
I don't think so nerram - I've gotten to within 2 tiles of completing it (took me just over 1000 clicks). I am going to try again soon....

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
I'm not completely for sure, but I think the radomization of the grid causes unsolvable puzzles. Don't know if any of you are familiar with this, but if you take a corner piece out of a rubik's cube, rotate it 66% and put it back in, the cube is then unsolvable. I'm kinda wondering if this puzzle is the same way. I ran into a 4x4 puzzle that was the same way. I had 2 tiles switched and I spent about 15 mins trying to unswitch them. Back on Final Fantasy I (the original one for nintendo) there was a special code where you could press A+B simultaneously 55 times and you could play the same puzzle (in 4x4 mode) I never ran into a puzzle then that took me more than 15 mins to solve, and I don't remember the 2-tiles-switched case ever coming up either. So.... maybe the random generation is causing unsolvable puzzles, maybe not. Gives ya something to think about though.

-kaht
 
Impossibility? Seems unlikely... then again...

When I was little (6 or 7, I suppose) I had one of those tile-sliding puzzles with some cartoon characters on it. The characters could, by arranging the tiles, appear in different positions... like: 1,2,3,4,5 or 5,4,2,1,3 or... you get the idea.

A pretty cool little game... the box the game came in had several configurations pictured on the lid, along with relative &quot;difficulty&quot;. I finished all the configurations... except for the one rated &quot;impossible&quot;.

How could it be impossible?!? On the face of it, the problem just doesn't seem logical. How can certain configurations of semi-free floating elements be more &quot;possible&quot; than others.

I had that game for years, and I never finished that one. Weird. Still bugs me, 30+ years later.
 
I believe kaht may be correct in that the game can generate impossible scenarios. The size of the game doesn't seem to matter. Here's what seems an impossible 3x3 game:
[tt]
|1|2|3|
|4|6|5|
|7|8| |[/tt]

Success in switching the 6 and 5 will just result in two other numbers being switched. If any one can solve this pattern, there's no reason you couldn't do the same for a 50x50 game.

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life--};
 
hmm - I will try 3x3 games and see if I can replicate that scenario - my guess is that you just need to work on more than just the 2 - think outside the box as it were...

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Here's another impossible pattern:

[tt]_______
|1|2|3|
|8| |4|
|7|6|5|[/tt]

I'm sure there's some mathematical equation you could use to determine why some games are impossible and some are not, but if I had to guess I'd have to say it's for the same reason you could never solve this game:

[tt]_____
|1|3|
|2| |[/tt]

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life--};
 
This is really cool. Thanks mwolf :-D
 
hmm - you might be right - I think that I will have to start with a valid solution and randomly shuffle the tiles x number of times by clicking them with the program - this could be cool...

tomorrow - version 2....

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top