473,326 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Help with online crossword puzzle

Hi! I'm new to JavaScript and I'm having trouble getting my program to work. Could someone take at a look at my code and help me try to figure out what the problem is?

EDIT: I guess I shouldn't post my entire code so I'll revise it. I'll post where I think I'm having issues.


This program is supposed to generate an online crossword puzzle for a newspaper. I cannot get any of my functions to work and would greatly appreciate some help figuring out what is going on. Users should be able to directly type their answers into the puzzle and navigate the puzzle by pressing the arrow keys on their keyboard. Typing a letter should move the cursor to the next cell. A user should also be able to toggle whether typing is entered vertically or horizontally by pressing the spacebar. The current cell in the puzzle should be yellow. If a user enters a correct letter into a cell, the background should be changed to light green. If an incorrect letter is entered, a light red background should be displayed. Blank puzzle cells should be displayed with a white background.

Here is the main part of the code. If anyone needs the other files, let me know;

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <!-- 
  4.  
  5.    Filename:             cross.htm
  6.    Supporting files: across.gif, down.gif, functions.js, makepuzzle.js,
  7.                                          pcglogo.jpg, styles.css
  8. -->
  9. <title>Crossword Puzzle</title>
  10. <link href="styles.css" rel="stylesheet" type="text/css" />
  11.  
  12. <script type="text/javascript" src="makepuzzle.js"></script>
  13. <script type="text/javascript" src="functions.js"></script>
  14.  
  15. <script type="text/javascript">
  16.  
  17.  
  18.  
  19.  
  20. //capture the key code of the pressed key and then call the appropriate function for that key
  21. function getKey(e){
  22.         if (IE) keyNum=event.keyCode; 
  23.                 else if (DOM) keyNum=e.keyCode;
  24.         if (keyNum = 32) 
  25.         toggleDirection(); //if the user presses the spacebar run the toggleDirection function
  26.         //if the user presses one of the four arrow keys run the moveCursor function
  27.         if (keyNum = 37) {
  28.         moveCursor(); }
  29.         if (keyNum = 38){
  30.         moveCursor(); }
  31.         if (keyNum = 39) {
  32.         moveCursor(); }
  33.         if (keyNum = 40) {
  34.         moveCursor();
  35.         } else { 
  36.         writeGuess();
  37.         }                                //otherwise run the writeGuess function
  38.         //return the value false from the function so that the safari browssers treats the arrows keys as single keystrokes
  39. }
  40.  
  41. //allow a user to press the spacebar to toggle the typing direction from across to down and vice versa, swap the point hand image on the page
  42. //to match the typing direction
  43. function toggleDirection() {
  44. //if across = true change the value to false, change the second image in the document to the source of the first image in the handImage array
  45. //if across = false change the value to true, change the second image in the document to the source of the first image in the handImage array
  46. if (across == true) { 
  47.         across = false;
  48.         document.images[1]=handImage[1].src;
  49.         } else {
  50.         if (across == false) {
  51.                 across=true;
  52.                 document.images[1]=hamdImage[0].src;
  53.         }
  54. }
  55.  
  56. //next four function will be used to update the values of the currentX and currentY variables as the cursor moves around the puzzle grid
  57.  
  58. //decrease the value of currentX by one, if currentX is less than zero change the value to four
  59. function moveLeft() {
  60.         curentX--;
  61.         if (currentX < 0) {
  62.                 currentX = 4
  63.         }
  64. }
  65.  
  66. //increase the value of currentX by one, if currentX is greater than four change the value to zero
  67. function moveRight() {
  68.         currentX++;
  69.         if (currentX > 4) {
  70.                 currentX = 0;
  71.         }
  72. }
  73.  
  74. //decrease the value of currentY  by one, if currentY is less than zero change value to four 
  75. function moveUp() {
  76.         currentY--;
  77.         if (currentY< 0) {
  78.                 currentY = 4;
  79.         }
  80. }
  81.  
  82. //increase the value of currentY by one, if currentY is greater than four change the value to zero
  83. function moveDown() {
  84.         currentY++;
  85.         if (currentY > 4) {
  86.                 currentY = 0;
  87.         }
  88. }
  89.  
  90. //move the active cell in response to a user presing the arrow keys on the keyboard
  91. function moveCursor() {
  92.         currentCell.style.backgroundColor = currentColor; //set the background color of the currentCell to the value of the currentColor variable
  93.         if (keyNum = 37) 
  94.                 moveLeft();                                                                     //if the value of the keyNum indicates a left arrow run the moveLeft function
  95.         if (keyNum = 38) 
  96.                 moveUp();                                                                               //if the value of the keyNum indicates an up arrow run the moveUp function
  97.         if (keyNum = 39) 
  98.                 moveRight();                                                                    //if the value of the keyNum indicates a right arrow run the moveRight function
  99.         if (keyNum =40) 
  100.                 moveDown();                                                                             //if the value of the keyNum indicates a down arrow run the moveDown function
  101.  
  102.         currentCell = document.getElementById("grid" + currentX + currentY);    
  103.         currentColor = currentCell.style.backgroundColor;                               //store the background color of the currentCell in the currentColor variable
  104.         currentCell.style.backgroundColor = "yellow";                                   //change the background color of the currentCell to yellow
  105. }
  106.  
  107. //write the lettr typed by the user into the current cell and change the background color to indicate whether the user typed a correct letter
  108. //after the letter has been written teh current cell should move either to the right or down 
  109. function writeGuess() {
  110.         var outChar = String.fromCharCode(keyNum); //use the fromCharCode method to extract the letter corresponding to the value of the keyNum variable
  111.         outChar = string.toUpperCase(); //use the toUpperCase function to change the outChar variable to an uppercase letter
  112.         document.write(writeText(outChar)); //use the writeText function to write outChart to the current cell
  113.         if (words[(currentY * 5) + currentX] == outChar) {
  114.                 currentCell.style.backgroundColor="lightgreen";
  115.                 } else {
  116.                 currentCell.style.backgroundColor="pink";
  117.                 }
  118.         //if across equals true run the moveRight function else run the moveDown function
  119.         if (across == true) {
  120.         moveRight();
  121.         } else {
  122.         moveDown();
  123.         }
  124.         //point the currentCell object to the element with the id gridxy, where x is the value of currentX and y is the value of currenY
  125.         currentCell = document.getElementById("grid" + currentX + currentY);
  126.         currentColor = currentCell.style.backgroundColor; //store the value of the current cell background color in currentColor
  127.         currentCell.style.backgroundColor = "yellow"; //change the background color of the current cell to yellow
  128. }
  129.  
  130.  
  131. </script>
Mar 16 '10 #1
1 4378
gits
5,390 Expert Mod 4TB
that is a quite complex question without having a sample page ... is there a link where we might have a look at the working/non-working page? ... or at least a sample that could be used to see all that in action/non-action?

kind regards
Mar 20 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: potm | last post by:
I hope you'll allow me a few moments to tell you about a programming contest that may be of interest to you. It's completely for fun, unsponsored, and pressure-free. It's called the POTM, and we...
4
by: Charlie Cosse | last post by:
Asymptopia Crossword Builder is a JavaScript application. A robust GUI allows users to submit arbitrary length lists of key-hint pairs which can be regenerated an unlimited number of times until...
6
by: Asymptopia | last post by:
Hi, if anyone is interested I've released an opensource crossword builder at http://www.asymptopia.org. It should work with IE/Mozilla/Netscape and hopefully others. Please send any feedback you...
2
by: Aaron | last post by:
Hey There~ I am a newer in VB.NET from HK.~ i am dealing with a School Project of a Crossword Puzzle..... But i dont know how to work with a Text validation in the Text Boxes.... The algorithm...
0
by: potm | last post by:
I hope you'll allow me a few moments to tell you about a programming contest that may be of interest to you. It's completely for fun, unsponsored, and pressure-free. It's called the POTM, and we...
3
by: potm | last post by:
C/C++/Perl/Python/PHP/Shell/awk/Ruby/TCL/etc ... Given a list of words, your program must create a crossword puzzle. Deadline: February 28, 2005. http://dinsights.com/POTM is the home of the...
3
by: Richard | last post by:
I have tried to create a javascript puzzle where by the grid is filled with numbers from 1 to 26 and another grid listed 1 to 26 where each number relates to a letter. Once you figure out the...
0
by: potm | last post by:
I hope you'll allow me a few moments to tell you about a programming contest that may be of interest to you. It's completely for fun, unsponsored, and pressure-free. It's called the POTM, and we...
2
by: NickPomp | last post by:
Hi, I have to write a slide puzzle program for class. I have the program finished and working except that I can not get the blank space to print out. I wrote code that would find the number I used...
4
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.