Avatar billede lordofmankind Nybegynder
06. januar 2005 - 14:17 Der er 11 kommentarer og
2 løsninger

Dette virker ikke? Mangler kode?

Hvorfor kan jeg ikke få dette til at virke?

Første del er min rettelse, og anden del er original koden.
Jeg har selvfølgelig oploadet alle .gif filerne.

Et sted i originalen har jeg ændret et af navnene på en gif fra pole.gif til post.gif
Mit eget gæt er at det er en fejl fra deres side.

Min del:



<html>

<head>
  </head>
<center>
<body>


<SCRIPT LANGUAGE="JavaScript">


//  http://www.vege.dk/side6.html/disk1.gif
//  http://www.vege.dk/side6.html/disk1h.gif
//  http://www.vege.dk/side6.html/disk2.gif
//  http://www.vege.dk/side6.html/disk2h.gif
//  http://www.vege.dk/side6.html/disk3.gif
//  http://www.vege.dk/side6.html/disk3h.gif
//  http://www.vege.dk/side6.html/disk4.gif
//  http://www.vege.dk/side6.html/disk4h.gif
//  http://www.vege.dk/side6.html/disk5.gif
//  http://www.vege.dk/side6.html/disk5h.gif
//  http://www.vege.dk/side6.html/disk6.gif
//  http://www.vege.dk/side6.html/disk6h.gif
//  http://www.vege.dk/side6.html/disk7.gif
//  http://www.vege.dk/side6.html/disk7h.gif
//  http://www.vege.dk/side6.html/post.gif
//  http://www.vege.dk/side6.html/posttop.gif




// change this to where you upload the images to your site
imgdir = "http://www.vege.dk/side6.html/";


function preload() {
this.length = preload.arguments.length;
for (var i = 0; i < this.length; i++) {
this[i] = new Image();
this[i].src = imgdir + preload.arguments[i];
  }
}

var pics = new preload("disk1.gif","disk2.gif",
"disk3.gif","disk4.gif","disk5.gif","disk6.gif",
"disk7.gif","post.gif", "disk1h.gif","disk2h.gif",
"disk3h.gif","disk4h.gif","disk5h.gif","disk6h.gif",
"disk7h.gif");

var selectedr = null;
var selectedc = null;
var maxposts = 3;
var maxdisks = 7;
var all_posts = 3;
var startpost = 1;
var endpost = (startpost-1 < 0 ? maxposts-1 : startpost-1);
var disks = 7;
var imgwidth = 160;
var imgheight = 14;
var game_is_over = false;
var show_messages = false;
var board = new Array(maxposts);
board[0] = new Array(maxdisks + 1);
board[1] = new Array(maxdisks + 1);
board[2] = new Array(maxdisks + 1);

function initboard(startpost, disks) {
var len = board[0].length;
selectedc = null;
selectedr = null;
game_is_over = false;
endpost = (startpost-1 < 0 ? maxposts-1 : startpost-1);

for (i = 0; i < len; i++) {
board[0][i] = 0;
board[1][i] = 0;
board[2][i] = 0;
}
for (i = len-disks, j = 0; i < len; i++, j++) {
board[startpost][i] = len - j - 1;
  }
}

function drawall() {
for (j=0; j<board.length; j++) {
for (i=0; i<board[j].length; i++) {
draw(j,i, getName( board[j][i]));
  }
}
message("You may begin! Select a piece to move.");
}

function restart(start) {
startpost = start;
disks = document.forms[0].disc.options[document.forms[0].disc.selectedIndex].text;
initboard(startpost,disks);
drawall();
theAnim = new Animation();
}
initboard(startpost, disks);

function getName( num ) {
if (num == 0) return "post.gif";
return "disk" + num + ".gif";
}

function message(str, force) {
if (force || !game_is_over && !show_messages)
document.disp.message.value = str;
}

function messageadd(str) {
if (!game_is_over)
document.disp.message.value = document.disp.message.value + "\n" + str;
}

function isempty(num) {
for (i = 0; i < board[num].length; i++) {
if ( board[num][i] != 0) return false;
}
return true;
}

function topmost(num) {
for (i = 0; i < board[num].length; i++) {
if (board[num][i] != 0) return  i;
}
return -1;
}

function ispost(i,j) {
return (board[j][i] == 0);
}

function istopdisk(i,j) {
return (board[j][i-1] == 0);
}

function drawboard() {
document.writeln("<h2>The Towers of Hanoi</h2><p>");
document.writeln("<table cellspacing=0 cellpadding=0 border=0>");
document.write("<tr>");
for (j = 0; j < board.length; j++) {
document.write("<td>");
document.write("<a href='java script:clicked("+0+","+j+")'><img src='" + imgdir + "posttop.gif' border=0></a><br>");
for (i=0; i< board[0].length; i++) {
document.write("<a href='java script:clicked("+i+","+j+")'>");
document.write("<img src='" + imgdir + getName(board[j][i]) + "' name='pos"+ j + i + "' border=0><br>");
document.write("</a>");
}
document.writeln("</td>");
}
document.write("</tr></table>");
document.write("<form name='disp'><textarea name='message' wrap=virtual rows=2 cols=40></textarea><br>" +
"Disks: <select name=\"disc\" size=1><option>3<option>4<option>5<option>6<option selected>7</select><input "
+"type=button value=\"Start the Game Over\" onClick=\"restart(startpost);\"><input "
+"type=button value=\"Solve It!\" onClick=\"restart(startpost);setTimeout('hanoi(disks,startpost,endpost)',300)\"></form>");
}

function draw(x,y,name) {
document.images["pos"+x+""+y].src = imgdir + name;
}

function animate(x,y,name) {
theAnim.addFrame( "pos"+x+""+y, imgdir + name);
}

function clicked(i,j) {
document.forms[0].message.focus(); // get rid of annoying outline in MSIE
document.forms[0].message.blur();

if (game_is_over)  restart(startpost = endpost);
if (!isselection() && ispost(i,j)) { message("Select a piece to move."); return; }
if (!ispost(i,j)) { toggle(j); return; };
if (ispost(i,j) && selectedc == j) { message("Move the piece to a different post."); return; }
if (!legalmove(j)) { message("That is not a legal move. Try again."); return; }
move(j); return;
}

function legalmove(j) {
if (isempty(j)) return true;
return (board[j][topmost(j)] < board[selectedc][selectedr]);
}

function isselection() {
return selectedc != null;
}

function toggle( num ) {
var toppos = topmost(num);

if (selectedc == num && selectedr == toppos) {
selectedc = null; selectedr = null;
animate(num,toppos,"disk" + board[num][toppos] + ".gif");
message("Select a piece to move.");
return;
}
if (isselection()) {
animate(selectedc,selectedr,"disk" + board[selectedc][selectedr] + ".gif");
}
selectedc = num; selectedr = toppos;
animate(num,toppos,"disk" + board[num][toppos] + "h.gif");
message("Click on the post to which you want to move the disk.");
}

function move( num ) {
var toppos = (!isempty(num) ? topmost(num) : board[num].length);
board[num][toppos-1] = board[selectedc][selectedr];
board[selectedc][selectedr] = 0;
animate(selectedc,selectedr,"post.gif");
animate(num,toppos-1,"disk" + board[num][toppos-1] + ".gif");
selectedc = null; selectedr = null;
message("Select a piece to move.");
game_over();
}

function hanoi(no_of_disks, start_post, goal_post) {
if (no_of_disks > 0) {
var free_post = all_posts - start_post - goal_post;
hanoi (no_of_disks - 1, start_post, free_post);
show_messages = true;
toggle(start_post);
move(goal_post);
show_messages = false;
hanoi (no_of_disks - 1 , free_post, goal_post);
game_over(true);
  }
}

function game_over(forceMsg) {
var filledpost = null;
var val = 0;
for (k = 0; k < board.length; k++)  {
val += ( isempty(k) ? 1 : 0 );
if (!isempty(k)) filledpost = k;
}

if (val == 2 && isempty(startpost)) {
message("You won!", forceMsg);
game_is_over = true;
endpost = filledpost;
}
return game_is_over;
}

//
// Animation functions
//

function Animation() {
this.imageNum = new Array();  // Array of indicies document.images to be changed
this.imageSrc = new Array();  // Array of new srcs for imageNum array
this.frameIndex = 0;          // the frame to play next
this.alreadyPlaying = false;  // semaphore to ensure we play smoothly

this.getFrameCount = getframecount;  // the total numebr of frame so far
this.moreFrames = moreframes;        // tells us if there are more frames to play
this.addFrame = addframe;            // add a frame to the animation
this.drawNextFrame = drawnextframe;  // draws the next frame
this.startAnimation = startanimation; // start the animation if necessary
}

function getframecount() {  return this.imageNum.length; }
function moreframes() {  return this.frameIndex < this.getFrameCount(); }
function startanimation() {
if (!this.alreadyPlaying) {
theAnim.alreadyPlaying = true;
setTimeout('theAnim.drawNextFrame()',5);
  }
}

function addframe(num, src) {
var theIndex = theAnim.imageNum.length;
theAnim.imageSrc[theIndex] = src;
theAnim.imageNum[theIndex] = num;
theAnim.startAnimation();
}

function drawnextframe() {
if (theAnim.moreFrames()) {
document.images[ theAnim.imageNum[theAnim.frameIndex] ].src = theAnim.imageSrc[theAnim.frameIndex];
theAnim.frameIndex++;
setTimeout('theAnim.drawNextFrame()', 30);
} else {
theAnim.alreadyPlaying = false;
  }
}

drawboard();
var theAnim = new Animation();
message("You may begin! Select a piece to move.");
document.disp.message.value = "";
//  End -->
</script>



</body>
</ceter>



</html>





Deres del:


<!-- TWO STEPS TO INSTALL TOWERS OF HANOI:

  1.  Copy the coding into the BODY of your HTML document
  2.  Save all the hanoi images to your website directory  -->

<!-- STEP ONE: Paste this code into the BODY of your HTML document  -->

<BODY>

<SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Adam Stock (adam@digital-biz.com) -->

<!-- (c) Copyright 1998-99 Adam L. Stock. All Rights Reserved -->
<!-- You have permission to republish this code provided -->
<!-- that you do not remove this copyright notice -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin

<!-- STEP TWO: Save these images to your website server directory  -->

//  http://www.yourdomain.com/hanoi-img-directory/disk1.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk1h.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk2.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk2h.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk3.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk3h.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk4.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk4h.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk5.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk5h.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk6.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk6h.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk7.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk7h.gif
//  http://www.yourdomain.com/hanoi-img-directory/post.gif
//  http://www.yourdomain.com/hanoi-img-directory/posttop.gif


// change this to where you upload the images to your site
imgdir = "http://www.your-web-site-address-here.com/images/your-hanoi-image-directory/";


function preload() {
this.length = preload.arguments.length;
for (var i = 0; i < this.length; i++) {
this[i] = new Image();
this[i].src = imgdir + preload.arguments[i];
  }
}

var pics = new preload("disk1.gif","disk2.gif",
"disk3.gif","disk4.gif","disk5.gif","disk6.gif",
"disk7.gif","pole.gif", "disk1h.gif","disk2h.gif",
"disk3h.gif","disk4h.gif","disk5h.gif","disk6h.gif",
"disk7h.gif");

var selectedr = null;
var selectedc = null;
var maxposts = 3;
var maxdisks = 7;
var all_posts = 3;
var startpost = 1;
var endpost = (startpost-1 < 0 ? maxposts-1 : startpost-1);
var disks = 7;
var imgwidth = 160;
var imgheight = 14;
var game_is_over = false;
var show_messages = false;
var board = new Array(maxposts);
board[0] = new Array(maxdisks + 1);
board[1] = new Array(maxdisks + 1);
board[2] = new Array(maxdisks + 1);

function initboard(startpost, disks) {
var len = board[0].length;
selectedc = null;
selectedr = null;
game_is_over = false;
endpost = (startpost-1 < 0 ? maxposts-1 : startpost-1);

for (i = 0; i < len; i++) {
board[0][i] = 0;
board[1][i] = 0;
board[2][i] = 0;
}
for (i = len-disks, j = 0; i < len; i++, j++) {
board[startpost][i] = len - j - 1;
  }
}

function drawall() {
for (j=0; j<board.length; j++) {
for (i=0; i<board[j].length; i++) {
draw(j,i, getName( board[j][i]));
  }
}
message("You may begin! Select a piece to move.");
}

function restart(start) {
startpost = start;
disks = document.forms[0].disc.options[document.forms[0].disc.selectedIndex].text;
initboard(startpost,disks);
drawall();
theAnim = new Animation();
}
initboard(startpost, disks);

function getName( num ) {
if (num == 0) return "post.gif";
return "disk" + num + ".gif";
}

function message(str, force) {
if (force || !game_is_over && !show_messages)
document.disp.message.value = str;
}

function messageadd(str) {
if (!game_is_over)
document.disp.message.value = document.disp.message.value + "\n" + str;
}

function isempty(num) {
for (i = 0; i < board[num].length; i++) {
if ( board[num][i] != 0) return false;
}
return true;
}

function topmost(num) {
for (i = 0; i < board[num].length; i++) {
if (board[num][i] != 0) return  i;
}
return -1;
}

function ispost(i,j) {
return (board[j][i] == 0);
}

function istopdisk(i,j) {
return (board[j][i-1] == 0);
}

function drawboard() {
document.writeln("<h2>The Towers of Hanoi</h2><p>");
document.writeln("<table cellspacing=0 cellpadding=0 border=0>");
document.write("<tr>");
for (j = 0; j < board.length; j++) {
document.write("<td>");
document.write("<a href='java script:clicked("+0+","+j+")'><img src='" + imgdir + "posttop.gif' border=0></a><br>");
for (i=0; i< board[0].length; i++) {
document.write("<a href='java script:clicked("+i+","+j+")'>");
document.write("<img src='" + imgdir + getName(board[j][i]) + "' name='pos"+ j + i + "' border=0><br>");
document.write("</a>");
}
document.writeln("</td>");
}
document.write("</tr></table>");
document.write("<form name='disp'><textarea name='message' wrap=virtual rows=2 cols=40></textarea><br>" +
"Disks: <select name=\"disc\" size=1><option>3<option>4<option>5<option>6<option selected>7</select><input "
+"type=button value=\"Start the Game Over\" onClick=\"restart(startpost);\"><input "
+"type=button value=\"Solve It!\" onClick=\"restart(startpost);setTimeout('hanoi(disks,startpost,endpost)',300)\"></form>");
}

function draw(x,y,name) {
document.images["pos"+x+""+y].src = imgdir + name;
}

function animate(x,y,name) {
theAnim.addFrame( "pos"+x+""+y, imgdir + name);
}

function clicked(i,j) {
document.forms[0].message.focus(); // get rid of annoying outline in MSIE
document.forms[0].message.blur();

if (game_is_over)  restart(startpost = endpost);
if (!isselection() && ispost(i,j)) { message("Select a piece to move."); return; }
if (!ispost(i,j)) { toggle(j); return; };
if (ispost(i,j) && selectedc == j) { message("Move the piece to a different post."); return; }
if (!legalmove(j)) { message("That is not a legal move. Try again."); return; }
move(j); return;
}

function legalmove(j) {
if (isempty(j)) return true;
return (board[j][topmost(j)] < board[selectedc][selectedr]);
}

function isselection() {
return selectedc != null;
}

function toggle( num ) {
var toppos = topmost(num);

if (selectedc == num && selectedr == toppos) {
selectedc = null; selectedr = null;
animate(num,toppos,"disk" + board[num][toppos] + ".gif");
message("Select a piece to move.");
return;
}
if (isselection()) {
animate(selectedc,selectedr,"disk" + board[selectedc][selectedr] + ".gif");
}
selectedc = num; selectedr = toppos;
animate(num,toppos,"disk" + board[num][toppos] + "h.gif");
message("Click on the post to which you want to move the disk.");
}

function move( num ) {
var toppos = (!isempty(num) ? topmost(num) : board[num].length);
board[num][toppos-1] = board[selectedc][selectedr];
board[selectedc][selectedr] = 0;
animate(selectedc,selectedr,"post.gif");
animate(num,toppos-1,"disk" + board[num][toppos-1] + ".gif");
selectedc = null; selectedr = null;
message("Select a piece to move.");
game_over();
}

function hanoi(no_of_disks, start_post, goal_post) {
if (no_of_disks > 0) {
var free_post = all_posts - start_post - goal_post;
hanoi (no_of_disks - 1, start_post, free_post);
show_messages = true;
toggle(start_post);
move(goal_post);
show_messages = false;
hanoi (no_of_disks - 1 , free_post, goal_post);
game_over(true);
  }
}

function game_over(forceMsg) {
var filledpost = null;
var val = 0;
for (k = 0; k < board.length; k++)  {
val += ( isempty(k) ? 1 : 0 );
if (!isempty(k)) filledpost = k;
}

if (val == 2 && isempty(startpost)) {
message("You won!", forceMsg);
game_is_over = true;
endpost = filledpost;
}
return game_is_over;
}

//
// Animation functions
//

function Animation() {
this.imageNum = new Array();  // Array of indicies document.images to be changed
this.imageSrc = new Array();  // Array of new srcs for imageNum array
this.frameIndex = 0;          // the frame to play next
this.alreadyPlaying = false;  // semaphore to ensure we play smoothly

this.getFrameCount = getframecount;  // the total numebr of frame so far
this.moreFrames = moreframes;        // tells us if there are more frames to play
this.addFrame = addframe;            // add a frame to the animation
this.drawNextFrame = drawnextframe;  // draws the next frame
this.startAnimation = startanimation; // start the animation if necessary
}

function getframecount() {  return this.imageNum.length; }
function moreframes() {  return this.frameIndex < this.getFrameCount(); }
function startanimation() {
if (!this.alreadyPlaying) {
theAnim.alreadyPlaying = true;
setTimeout('theAnim.drawNextFrame()',5);
  }
}

function addframe(num, src) {
var theIndex = theAnim.imageNum.length;
theAnim.imageSrc[theIndex] = src;
theAnim.imageNum[theIndex] = num;
theAnim.startAnimation();
}

function drawnextframe() {
if (theAnim.moreFrames()) {
document.images[ theAnim.imageNum[theAnim.frameIndex] ].src = theAnim.imageSrc[theAnim.frameIndex];
theAnim.frameIndex++;
setTimeout('theAnim.drawNextFrame()', 30);
} else {
theAnim.alreadyPlaying = false;
  }
}

drawboard();
var theAnim = new Animation();
message("You may begin! Select a piece to move.");
document.disp.message.value = "";
//  End -->
</script>

<p><center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

<!-- Script Size:  8.91 KB -->






Hvad kan jeg have gjort galt eller virker deres spil ikke?
Avatar billede flintstone Novice
06. januar 2005 - 17:20 #1
Prøv lige i Java kategorien
Avatar billede flintstone Novice
06. januar 2005 - 18:00 #2
Du skal selv lige rette dit image dir.

<HTML>
<HEAD>
<TITLE>The JavaScript Source:  Games:  Towers of Hanoi</TITLE>
<META HTTP-EQUIV="The JavaScript Source" CONTENT = "no-cache">
<META NAME="date" CONTENT="2000-09-09">
<META NAME="channel" CONTENT="Web Developer">
<META NAME="author" CONTENT="Adam Stock (adam@digital-biz.com)">
<META NAME="section" CONTENT="Games">
<META NAME="description" CONTENT="See how you do in this wonderful game of skill and logic.  Try to move all the disks onto another pole.  Thing is, you can only move one disk at a time and you must follow size order (a bigger disk can't go on a smaller disk).  Good luck!">
</HEAD>

<BODY BGCOLOR=#ffffff vlink=#0000ff>

<BR>
<center>
<script language="JavaScript" type="text/javascript"><!--
function openDescription(ID) {
var url = "http://www.ppcforhosts.com/public/util/description.cfm?id=" + ID;
link = window.open(url,"newWin","directories=0,height=250,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0,width=450");
}
//--></script>
<FONT SIZE="+2" FACE="Helvetica,Arial"></font>
<table BORDER=1 WIDTH=486 CELLPADDING=3 CELLSPACING=0>
<tr>
<td><font FACE="helvetica,arial,geneva">
<!-- Description --><!--content_start-->
See how you do in this wonderful game of skill and logic.  Try to move all the disks onto another pole.  Thing is, you can only move one disk at a time and you must follow size order (a bigger disk can't go on a smaller disk).  Good luck!
</td>
</tr>
</table>
<!-- Demonstration -->
<SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Adam Stock (adam@digital-biz.com) -->
<!-- (c) Copyright 1998-99 Adam L. Stock. All Rights Reserved -->
<!-- You have permission to republish this code provided -->
<!-- that you do not remove this copyright notice -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
<!-- STEP ONE: STEP TWO: Download this zip file with all the images
// change this to where you upload the images to your site

imgdir = "http://javascript.internet.com/img/hanoi/";

function preload() {
this.length = preload.arguments.length;
for (var i = 0; i < this.length; i++) {
this[i] = new Image();
this[i].src = imgdir + preload.arguments[i];
  }
}

var pics = new preload("disk1.gif","disk2.gif",
"disk3.gif","disk4.gif","disk5.gif","disk6.gif",
"disk7.gif","pole.gif", "disk1h.gif","disk2h.gif",
"disk3h.gif","disk4h.gif","disk5h.gif","disk6h.gif",
"disk7h.gif");

var selectedr = null;
var selectedc = null;
var maxposts = 3;
var maxdisks = 7;
var all_posts = 3;
var startpost = 1;
var endpost = (startpost-1 < 0 ? maxposts-1 : startpost-1);
var disks = 7;
var imgwidth = 160;
var imgheight = 14;
var game_is_over = false;
var show_messages = false;
var board = new Array(maxposts);
board[0] = new Array(maxdisks + 1);
board[1] = new Array(maxdisks + 1);
board[2] = new Array(maxdisks + 1);

function initboard(startpost, disks) {
var len = board[0].length;
selectedc = null;
selectedr = null;
game_is_over = false;
endpost = (startpost-1 < 0 ? maxposts-1 : startpost-1);

for (i = 0; i < len; i++) {
board[0][i] = 0;
board[1][i] = 0;
board[2][i] = 0;
}
for (i = len-disks, j = 0; i < len; i++, j++) {
board[startpost][i] = len - j - 1;
  }
}

function drawall() {
for (j=0; j<board.length; j++) {
for (i=0; i<board[j].length; i++) {
draw(j,i, getName( board[j][i]));
  }
}
message("You may begin! Select a piece to move.");
}

function restart(start) {
startpost = start;
disks = document.forms[0].disc.options[document.forms[0].disc.selectedIndex].text;
initboard(startpost,disks);
drawall();
theAnim = new Animation();
}
initboard(startpost, disks);

function getName( num ) {
if (num == 0) return "post.gif";
return "disk" + num + ".gif";
}

function message(str, force) {
if (force || !game_is_over && !show_messages)
document.disp.message.value = str;
}

function messageadd(str) {
if (!game_is_over)
document.disp.message.value = document.disp.message.value + "\n" + str;
}

function isempty(num) {
for (i = 0; i < board[num].length; i++) {
if ( board[num][i] != 0) return false;
}
return true;
}

function topmost(num) {
for (i = 0; i < board[num].length; i++) {
if (board[num][i] != 0) return  i;
}
return -1;
}

function ispost(i,j) {
return (board[j][i] == 0);
}

function istopdisk(i,j) {
return (board[j][i-1] == 0);
}

function drawboard() {
document.writeln("<h2>The Towers of Hanoi</h2><p>");
document.writeln("<table cellspacing=0 cellpadding=0 border=0>");
document.write("<tr>");
for (j = 0; j < board.length; j++) {
document.write("<td>");
document.write("<a href='java script:clicked("+0+","+j+")'><img src='" + imgdir + "posttop.gif' border=0></a><br>");
for (i=0; i< board[0].length; i++) {
document.write("<a href='java script:clicked("+i+","+j+")'>");
document.write("<img src='" + imgdir + getName(board[j][i]) + "' name='pos"+ j + i + "' border=0><br>");
document.write("</a>");
}
document.writeln("</td>");
}
document.write("</tr></table>");
document.write("<form name='disp'><textarea name='message' wrap=virtual rows=2 cols=40></textarea><br>" +
"Disks: <select name=\"disc\" size=1><option>3<option>4<option>5<option>6<option selected>7</select><input "
+"type=button value=\"Start the Game Over\" onClick=\"restart(startpost);\"><input "
+"type=button value=\"Solve It!\" onClick=\"restart(startpost);setTimeout('hanoi(disks,startpost,endpost)',300)\"></form>");
}

function draw(x,y,name) {
document.images["pos"+x+""+y].src = imgdir + name;
}

function animate(x,y,name) {
theAnim.addFrame( "pos"+x+""+y, imgdir + name);
}

function clicked(i,j) {
document.forms[0].message.focus(); // get rid of annoying outline in MSIE
document.forms[0].message.blur();

if (game_is_over)  restart(startpost = endpost);
if (!isselection() && ispost(i,j)) { message("Select a piece to move."); return; }
if (!ispost(i,j)) { toggle(j); return; };
if (ispost(i,j) && selectedc == j) { message("Move the piece to a different post."); return; }
if (!legalmove(j)) { message("That is not a legal move. Try again."); return; }
move(j); return;
}

function legalmove(j) {
if (isempty(j)) return true;
return (board[j][topmost(j)] < board[selectedc][selectedr]);
}

function isselection() {
return selectedc != null;
}

function toggle( num ) {
var toppos = topmost(num);

if (selectedc == num && selectedr == toppos) {
selectedc = null; selectedr = null;
animate(num,toppos,"disk" + board[num][toppos] + ".gif");
message("Select a piece to move.");
return;
}
if (isselection()) {
animate(selectedc,selectedr,"disk" + board[selectedc][selectedr] + ".gif");
}
selectedc = num; selectedr = toppos;
animate(num,toppos,"disk" + board[num][toppos] + "h.gif");
message("Click on the post to which you want to move the disk.");
}

function move( num ) {
var toppos = (!isempty(num) ? topmost(num) : board[num].length);
board[num][toppos-1] = board[selectedc][selectedr];
board[selectedc][selectedr] = 0;
animate(selectedc,selectedr,"post.gif");
animate(num,toppos-1,"disk" + board[num][toppos-1] + ".gif");
selectedc = null; selectedr = null;
message("Select a piece to move.");
game_over();
}

function hanoi(no_of_disks, start_post, goal_post) {
if (no_of_disks > 0) {
var free_post = all_posts - start_post - goal_post;
hanoi (no_of_disks - 1, start_post, free_post);
show_messages = true;
toggle(start_post);
move(goal_post);
show_messages = false;
hanoi (no_of_disks - 1 , free_post, goal_post);
game_over(true);
  }
}

function game_over(forceMsg) {
var filledpost = null;
var val = 0;
for (k = 0; k < board.length; k++)  {
val += ( isempty(k) ? 1 : 0 );
if (!isempty(k)) filledpost = k;
}

if (val == 2 && isempty(startpost)) {
message("You won!", forceMsg);
game_is_over = true;
endpost = filledpost;
}
return game_is_over;
}

//
// Animation functions
//

function Animation() {
this.imageNum = new Array();  // Array of indicies document.images to be changed
this.imageSrc = new Array();  // Array of new srcs for imageNum array
this.frameIndex = 0;          // the frame to play next
this.alreadyPlaying = false;  // semaphore to ensure we play smoothly

this.getFrameCount = getframecount;  // the total numebr of frame so far
this.moreFrames = moreframes;        // tells us if there are more frames to play
this.addFrame = addframe;            // add a frame to the animation
this.drawNextFrame = drawnextframe;  // draws the next frame
this.startAnimation = startanimation; // start the animation if necessary
}

function getframecount() {  return this.imageNum.length; }
function moreframes() {  return this.frameIndex < this.getFrameCount(); }
function startanimation() {
if (!this.alreadyPlaying) {
theAnim.alreadyPlaying = true;
setTimeout('theAnim.drawNextFrame()',5);
  }
}

function addframe(num, src) {
var theIndex = theAnim.imageNum.length;
theAnim.imageSrc[theIndex] = src;
theAnim.imageNum[theIndex] = num;
theAnim.startAnimation();
}

function drawnextframe() {
if (theAnim.moreFrames()) {
document.images[ theAnim.imageNum[theAnim.frameIndex] ].src = theAnim.imageSrc[theAnim.frameIndex];
theAnim.frameIndex++;
setTimeout('theAnim.drawNextFrame()', 30);
} else {
theAnim.alreadyPlaying = false;
  }
}

drawboard();
var theAnim = new Animation();
message("You may begin! Select a piece to move.");
document.disp.message.value = "";
//  End -->
</script>
</center>
</body>
</html>
Avatar billede lordofmankind Nybegynder
07. januar 2005 - 00:11 #3
Skal det her med?:



//  http://www.yourdomain.com/hanoi-img-directory/disk1.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk1h.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk2.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk2h.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk3.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk3h.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk4.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk4h.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk5.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk5h.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk6.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk6h.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk7.gif
//  http://www.yourdomain.com/hanoi-img-directory/disk7h.gif
//  http://www.yourdomain.com/hanoi-img-directory/post.gif
//  http://www.yourdomain.com/hanoi-img-directory/posttop.gif

Og skal det se sådan ud?:


//  http://www.vege.dk/side6.html/disk1.gif
//  http://www.vege.dk/side6.html/disk1h.gif
//  http://www.vege.dk/side6.html/disk2.gif
//  http://www.vege.dk/side6.html/disk2h.gif
//  http://www.vege.dk/side6.html/disk3.gif
//  http://www.vege.dk/side6.html/disk3h.gif
//  http://www.vege.dk/side6.html/disk4.gif
//  http://www.vege.dk/side6.html/disk4h.gif
//  http://www.vege.dk/side6.html/disk5.gif
//  http://www.vege.dk/side6.html/disk5h.gif
//  http://www.vege.dk/side6.html/disk6.gif
//  http://www.vege.dk/side6.html/disk6h.gif
//  http://www.vege.dk/side6.html/disk7.gif
//  http://www.vege.dk/side6.html/disk7h.gif
//  http://www.vege.dk/side6.html/post.gif
//  http://www.vege.dk/side6.html/posttop.gif

Og det her:

imgdir = "http://javascript.internet.com/img/hanoi/";

Skal det se sådan her ud?:

imgdir = "http://www.vege.dk/side6.html/";


Og hvor skal det her ind hvis det skal ind?:


//  http://www.vege.dk/side6.html/disk1.gif
//  http://www.vege.dk/side6.html/disk1h.gif
//  http://www.vege.dk/side6.html/disk2.gif
//  http://www.vege.dk/side6.html/disk2h.gif
//  http://www.vege.dk/side6.html/disk3.gif
//  http://www.vege.dk/side6.html/disk3h.gif
//  http://www.vege.dk/side6.html/disk4.gif
//  http://www.vege.dk/side6.html/disk4h.gif
//  http://www.vege.dk/side6.html/disk5.gif
//  http://www.vege.dk/side6.html/disk5h.gif
//  http://www.vege.dk/side6.html/disk6.gif
//  http://www.vege.dk/side6.html/disk6h.gif
//  http://www.vege.dk/side6.html/disk7.gif
//  http://www.vege.dk/side6.html/disk7h.gif
//  http://www.vege.dk/side6.html/post.gif
//  http://www.vege.dk/side6.html/posttop.gif
Avatar billede lordofmankind Nybegynder
07. januar 2005 - 00:15 #4
Skal det her med+:    img/hanoi/";

efter det her?:        http://www.vege.dk/side6.html/";


Altså?:                http://www.vege.dk/side6.html/img/hanoi/";


Eller bare?:          http://www.vege.dk/side6.html/";



Du må virkeliig sige til hvis du ikke gider bruge tid på det.
Ved godt det kun er et "spil" og mindre vigtigt end mange andre ting herinde....

Men jeg vil jo bare lære hvordan man bruger det:-)
Avatar billede lordofmankind Nybegynder
07. januar 2005 - 00:21 #5
Sidste ting.

Skal det her med:?

//  http://www.vege.dk/side6.html/hanoi-img-directory/disk1.gif
//  http://www.vege.dk/side6.html/hanoi-img-directory/disk1h.gif
//  http://www.vege.dk/side6.html/hanoi-img-directory/disk2.gif
//  http://www.vege.dk/side6.html/hanoi-img-directory/disk2h.gif
//  http://www.vege.dk/side6.html/hanoi-img-directory/disk3.gif
//  http://www.vege.dk/side6.html/hanoi-img-directory/disk3h.gif
//  http://www.vege.dk/side6.html/hanoi-img-directory/disk4.gif
//  http://www.vege.dk/side6.html/hanoi-img-directory/disk4h.gif
//  http://www.vege.dk/side6.html/hanoi-img-directory/disk5.gif
//  http://www.vege.dk/side6.html/hanoi-img-directory/disk5h.gif
//  http://www.vege.dk/side6.html/hanoi-img-directory/disk6.gif
//  http://www.vege.dk/side6.html/hanoi-img-directory/disk6h.gif
//  http://www.vege.dk/side6.html/hanoi-img-directory/disk7.gif
//  http://www.vege.dk/side6.html/hanoi-img-directory/disk7h.gif
//  http://www.vege.dk/side6.html/hanoi-img-directory/post.gifp
//  http://www.vege.dk/side6.html/hanoi-img-directory/posttop.gif



Eller var det rigtigt som jeg gjorde her?:

//  http://www.vege.dk/side6.html/disk1.gif
//  http://www.vege.dk/side6.html/disk1h.gif
//  http://www.vege.dk/side6.html/disk2.gif
//  http://www.vege.dk/side6.html/disk2h.gif
//  http://www.vege.dk/side6.html/disk3.gif
//  http://www.vege.dk/side6.html/disk3h.gif
//  http://www.vege.dk/side6.html/disk4.gif
//  http://www.vege.dk/side6.html/disk4h.gif
//  http://www.vege.dk/side6.html/disk5.gif
//  http://www.vege.dk/side6.html/disk5h.gif
//  http://www.vege.dk/side6.html/disk6.gif
//  http://www.vege.dk/side6.html/disk6h.gif
//  http://www.vege.dk/side6.html/disk7.gif
//  http://www.vege.dk/side6.html/disk7h.gif
//  http://www.vege.dk/side6.html/post.gif
//  http://www.vege.dk/side6.html/posttop.gif
Avatar billede lordofmankind Nybegynder
07. januar 2005 - 00:25 #6
Jeg giver dig 100 ekstra når det virker, i en henvisningstråd..
Avatar billede flintstone Novice
07. januar 2005 - 12:45 #7
Klap lige hesten........Du har downloadet nogle giffer. Dem har du så uploadet til dit eget site. De filer ligger i en folder/bibliotek. Det er den folder/bibliotek, som du skal henvise til i dit image dir. Ikke noget med "side6.html", men derimod http://www.vege.dk/images, hvis det er der dine giffer ligger. Så simpelt er det c",)
Avatar billede flintstone Novice
07. januar 2005 - 23:25 #8
Her har du en dansk udgave:

<HTML>
<HEAD>
<TITLE>The JavaScript Source:  Games:  Towers of Hanoi</TITLE>
<META HTTP-EQUIV="The JavaScript Source" CONTENT = "no-cache">
<META NAME="date" CONTENT="2000-09-09">
<META NAME="channel" CONTENT="Web Developer">
<META NAME="author" CONTENT="Adam Stock (adam@digital-biz.com)">
<META NAME="section" CONTENT="Games">
<META NAME="description" CONTENT="Se hvordan du klarer dig i dette spil om færdighed og logisk tænkning. Prøv at flytte alle skiverne til en anden pind. Finten er, at du må kun flytte en skive af gangen og du skal se på størrelsen (en stor skive kan IKKE ligge ovenpå en mindre). God fornøjelse !">
</HEAD>

<BODY BGCOLOR=#ffffff vlink=#0000ff>

<BR>
<center>
<script language="JavaScript" type="text/javascript"><!--
function openDescription(ID) {
var url = "http://www.ppcforhosts.com/public/util/description.cfm?id=" + ID;
link = window.open(url,"newWin","directories=0,height=250,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0,width=450");
}
//--></script>
<FONT SIZE="+2" FACE="Helvetica,Arial"></font>
<table BORDER=1 WIDTH=486 CELLPADDING=3 CELLSPACING=0>
<tr>
<td><font FACE="helvetica,arial,geneva">
<!-- Description --><!--content_start-->
Se hvordan du klarer dig i dette spil om færdighed og logisk tænkning. Prøv at flytte alle skiverne til en anden pind. Finten er, at du må kun flytte en skive af gangen og du skal se på størrelsen (en stor skive kan IKKE ligge ovenpå en mindre). God fornøjelse !
</td>
</tr>
</table>
<!-- Demonstration -->
<SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Adam Stock (adam@digital-biz.com) -->
<!-- (c) Copyright 1998-99 Adam L. Stock. All Rights Reserved -->
<!-- You have permission to republish this code provided -->
<!-- that you do not remove this copyright notice -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
<!-- STEP ONE: STEP TWO: Download this zip file with all the images
// change this to where you upload the images to your site

imgdir = "http://javascript.internet.com/img/hanoi/";

function preload() {
this.length = preload.arguments.length;
for (var i = 0; i < this.length; i++) {
this[i] = new Image();
this[i].src = imgdir + preload.arguments[i];
  }
}

var pics = new preload("disk1.gif","disk2.gif",
"disk3.gif","disk4.gif","disk5.gif","disk6.gif",
"disk7.gif","pole.gif", "disk1h.gif","disk2h.gif",
"disk3h.gif","disk4h.gif","disk5h.gif","disk6h.gif",
"disk7h.gif");

var selectedr = null;
var selectedc = null;
var maxposts = 3;
var maxdisks = 7;
var all_posts = 3;
var startpost = 1;
var endpost = (startpost-1 < 0 ? maxposts-1 : startpost-1);
var disks = 7;
var imgwidth = 160;
var imgheight = 14;
var game_is_over = false;
var show_messages = false;
var board = new Array(maxposts);
board[0] = new Array(maxdisks + 1);
board[1] = new Array(maxdisks + 1);
board[2] = new Array(maxdisks + 1);

function initboard(startpost, disks) {
var len = board[0].length;
selectedc = null;
selectedr = null;
game_is_over = false;
endpost = (startpost-1 < 0 ? maxposts-1 : startpost-1);

for (i = 0; i < len; i++) {
board[0][i] = 0;
board[1][i] = 0;
board[2][i] = 0;
}
for (i = len-disks, j = 0; i < len; i++, j++) {
board[startpost][i] = len - j - 1;
  }
}

function drawall() {
for (j=0; j<board.length; j++) {
for (i=0; i<board[j].length; i++) {
draw(j,i, getName( board[j][i]));
  }
}
message("Begynd nu ! Vælg den skive du vil flytte.");
}

function restart(start) {
startpost = start;
disks = document.forms[0].disc.options[document.forms[0].disc.selectedIndex].text;
initboard(startpost,disks);
drawall();
theAnim = new Animation();
}
initboard(startpost, disks);

function getName( num ) {
if (num == 0) return "post.gif";
return "disk" + num + ".gif";
}

function message(str, force) {
if (force || !game_is_over && !show_messages)
document.disp.message.value = str;
}

function messageadd(str) {
if (!game_is_over)
document.disp.message.value = document.disp.message.value + "\n" + str;
}

function isempty(num) {
for (i = 0; i < board[num].length; i++) {
if ( board[num][i] != 0) return false;
}
return true;
}

function topmost(num) {
for (i = 0; i < board[num].length; i++) {
if (board[num][i] != 0) return  i;
}
return -1;
}

function ispost(i,j) {
return (board[j][i] == 0);
}

function istopdisk(i,j) {
return (board[j][i-1] == 0);
}

function drawboard() {
document.writeln("<h2>Pindene fra Paraguay</h2><p>");
document.writeln("<table cellspacing=0 cellpadding=0 border=0>");
document.write("<tr>");
for (j = 0; j < board.length; j++) {
document.write("<td>");
document.write("<a href='java script:clicked("+0+","+j+")'><img src='" + imgdir + "posttop.gif' border=0></a><br>");
for (i=0; i< board[0].length; i++) {
document.write("<a href='java script:clicked("+i+","+j+")'>");
document.write("<img src='" + imgdir + getName(board[j][i]) + "' name='pos"+ j + i + "' border=0><br>");
document.write("</a>");
}
document.writeln("</td>");
}
document.write("</tr></table>");
document.write("<form name='disp'><textarea name='message' wrap=virtual rows=2 cols=40></textarea><br>" +
"Disks: <select name=\"disc\" size=1><option>3<option>4<option>5<option>6<option selected>7</select><input "
+"type=button value=\"Start spillet forfra\" onClick=\"restart(startpost);\"><input "
+"type=button value=\"Løsning !\" onClick=\"restart(startpost);setTimeout('hanoi(disks,startpost,endpost)',300)\"></form>");
}

function draw(x,y,name) {
document.images["pos"+x+""+y].src = imgdir + name;
}

function animate(x,y,name) {
theAnim.addFrame( "pos"+x+""+y, imgdir + name);
}

function clicked(i,j) {
document.forms[0].message.focus(); // get rid of annoying outline in MSIE
document.forms[0].message.blur();

if (game_is_over)  restart(startpost = endpost);
if (!isselection() && ispost(i,j)) { message("Vælg den skive du vil flytte."); return; }
if (!ispost(i,j)) { toggle(j); return; };
if (ispost(i,j) && selectedc == j) { message("Flyt skiven til en anden pind."); return; }
if (!legalmove(j)) { message("Det kan man ikke !! Prøv igen."); return; }
move(j); return;
}

function legalmove(j) {
if (isempty(j)) return true;
return (board[j][topmost(j)] < board[selectedc][selectedr]);
}

function isselection() {
return selectedc != null;
}

function toggle( num ) {
var toppos = topmost(num);

if (selectedc == num && selectedr == toppos) {
selectedc = null; selectedr = null;
animate(num,toppos,"disk" + board[num][toppos] + ".gif");
message("Vælg den skive du vil flytte.");
return;
}
if (isselection()) {
animate(selectedc,selectedr,"disk" + board[selectedc][selectedr] + ".gif");
}
selectedc = num; selectedr = toppos;
animate(num,toppos,"disk" + board[num][toppos] + "h.gif");
message("Vælg den pind hvor du vil flytte skiven hen på.");
}

function move( num ) {
var toppos = (!isempty(num) ? topmost(num) : board[num].length);
board[num][toppos-1] = board[selectedc][selectedr];
board[selectedc][selectedr] = 0;
animate(selectedc,selectedr,"post.gif");
animate(num,toppos-1,"disk" + board[num][toppos-1] + ".gif");
selectedc = null; selectedr = null;
message("Vælg en skive du vil flytte.");
game_over();
}

function hanoi(no_of_disks, start_post, goal_post) {
if (no_of_disks > 0) {
var free_post = all_posts - start_post - goal_post;
hanoi (no_of_disks - 1, start_post, free_post);
show_messages = true;
toggle(start_post);
move(goal_post);
show_messages = false;
hanoi (no_of_disks - 1 , free_post, goal_post);
game_over(true);
  }
}

function game_over(forceMsg) {
var filledpost = null;
var val = 0;
for (k = 0; k < board.length; k++)  {
val += ( isempty(k) ? 1 : 0 );
if (!isempty(k)) filledpost = k;
}

if (val == 2 && isempty(startpost)) {
message("DU VANDT !!", forceMsg);
game_is_over = true;
endpost = filledpost;
}
return game_is_over;
}

//
// Animation functions
//

function Animation() {
this.imageNum = new Array();  // Array of indicies document.images to be changed
this.imageSrc = new Array();  // Array of new srcs for imageNum array
this.frameIndex = 0;          // the frame to play next
this.alreadyPlaying = false;  // semaphore to ensure we play smoothly

this.getFrameCount = getframecount;  // the total numebr of frame so far
this.moreFrames = moreframes;        // tells us if there are more frames to play
this.addFrame = addframe;            // add a frame to the animation
this.drawNextFrame = drawnextframe;  // draws the next frame
this.startAnimation = startanimation; // start the animation if necessary
}

function getframecount() {  return this.imageNum.length; }
function moreframes() {  return this.frameIndex < this.getFrameCount(); }
function startanimation() {
if (!this.alreadyPlaying) {
theAnim.alreadyPlaying = true;
setTimeout('theAnim.drawNextFrame()',5);
  }
}

function addframe(num, src) {
var theIndex = theAnim.imageNum.length;
theAnim.imageSrc[theIndex] = src;
theAnim.imageNum[theIndex] = num;
theAnim.startAnimation();
}

function drawnextframe() {
if (theAnim.moreFrames()) {
document.images[ theAnim.imageNum[theAnim.frameIndex] ].src = theAnim.imageSrc[theAnim.frameIndex];
theAnim.frameIndex++;
setTimeout('theAnim.drawNextFrame()', 30);
} else {
theAnim.alreadyPlaying = false;
  }
}

drawboard();
var theAnim = new Animation();
message("Begynd nu! Vælg en skive du vil flytte.");
document.disp.message.value = "";
//  End -->
</script>
</center>
</body>
</html>
Avatar billede lordofmankind Nybegynder
08. januar 2005 - 02:38 #9
Nice. Tusind tak flintstone.-)
Avatar billede lordofmankind Nybegynder
08. januar 2005 - 02:40 #10
Avatar billede lordofmankind Nybegynder
08. januar 2005 - 02:42 #11
Mærkeligt at jeg ikke engang behøver at ændre mit img. directory?
Avatar billede lordofmankind Nybegynder
08. januar 2005 - 02:44 #12
Hmm. måske skal jeg alligevel gøre det. Det virkede lige da jeg oploadede det, men ikke nu.
Men det virker ret nemt herfra.
Avatar billede flintstone Novice
08. januar 2005 - 11:52 #13
Du må hellere ændre dit img.library, for ellers henter spillet jo gifferne fra en anden server.
Henvis til det bibliotek hvor du har dine giffer, så skulle det også virke uden problemer.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester