A346/A348 Home Work 1 HTML/JavaScript |
Modified: |
Overview
The purpose of the first homework is to introduce some of the basic concepts of HTML and JavaScript.
HTML limits user interaction to a mostly static user interface; a scripting language adds dynamic interaction through programming to that possible in HTML alone. There are two approaches that use client-side scripting for user interaction: 1) define the user interface in HTML but change elements displayed as the user interacts, and 2) generate new HTML as the user interacts.
The following assignments are designed to practice interacting with the user using both HTML and the JavaScript scripting language.
Assignment - 2 parts
2. Memory Game
You probably played a memory game as a child where all tiles were initially face down and you attempted to match one tile with its pair; the tiles were removed on a match or again placed face down when no match. The object is, of course, to match all tiles in the fewest tries.
The following illustrates the start of the game with no tiles matched and after matching 3 pairs while attempting another match.
Initial
After 3 matches
Assignment
Implement the memory game to:
- arrange matching pairs of tiles randomly in a table of rows and columns,
- initially the backs of all tiles are exposed,
- when clicked, a tile is turned over exposing the front, no more than two tiles at a time can have the front exposed,
- the player indicates when to try again, when selected tiles fail to match the tiles are turned over exposing the back, when two exposed tiles match they are replaced by a special tile,
- the current number of tries is continually displayed,
- the player can choose to play a new game at any time.
Suggestions and Insights
- Random numbers - generate a random integer 0..11 by: Math.floor(Math.random()*12)
- Clickable images - below, the image back.jpg is first displayed, when clicked the function pick(1) is invoked with the parameter value 1:
<A HREF="" onClick="pick(1); return false;">
<img name="IMAGE1" src="back.jpg" alt="click to flip">
</a>
One important point is that the object is named IMAGE1 allowing attribute values to be changed in a script, to change the file displayed:
IMAGE1.src = "curly.jpg";
- Arrays - consider using arrays; they behave much as in C++ or Java but are not typed and automatically expand in size.
- An empty array is defined by:
var pics = new Array();
- An array initialized to three strings by:
var pics = new Array("larry.jpg", "moe.jpg", "curly.jpg");
or
var pics = ["larry.jpg", "moe.jpg", "curly.jpg"];
- An array initialized to reference (alias) three objects named IMAGE1, IMAGE2, IMAGE3 by:
var imgObjects = new Array(IMAGE1, IMAGE2, IMAGE3);
The src attribute of IMAGE1 can now be changed by either of the following:
IMAGE1.src = pics[2];
imgObjects[0].src = pics[2];
- Tiles - Use photos of friends or family, pets, cars, etc. Screen shots can also be captured and edited by:
Home - HTML and XML pages can be browsed in two ways, the simplest is given first.
Browser - The simplest approach is to copy the HTML or other files to your computer and enter the path to the file in the address bar of IE (or other browser). The address bar entry would be similar to:
- C:\A348\HW1.htm
OR
- C:\A346\HW1.htm
Internet Information Server (IIS) - Though not needed for this assignment, the IIS can be used on your home system to complete all assignments. The server is intended to be simple to install and administer. It is supplied on the Windows 2000 and XP CD-ROM. See Setting Up the IIS Web Server for detailed installation instructions for XP.
- Administration - The main IIS administrator function is to define virtual directories that are visible to the WWW, this will be examined in detail later in the course. For now, this function can be avoided simply by copying any directories to be visible over the Internet in the directory C:\InetPub\wwwroot created automatically when installing IIS. Unless your login has administrator rights, you will need to login in as Administrator to have access to the C:\InetPub\wwwroot directory.
- Browser access - The name of the local machine is localhost or 127.0.0.1 and can be used as part of the URL to access files stored on your local machine. The file path C:\InetPub\wwwroot\HW1\First.htm can be accessed from a browser on the same machine in at least two ways:
- http://localhost/HW1/First.htm
- http://127.0.0.1/HW1/First.htm
IUS - Files stored on the W: directory are accessible to the WWW.
The file path W:\A348\HTML\Syllabus.htm on my IUS directory can be accessed over the WWW by:
- http://homepages.ius.edu/RWISMAN/A348/HTML/Syllabus.htm