Re: Game
- From: Lasse Reichstein Nielsen <lrn@xxxxxxxxxx>
- Date: Tue, 28 Feb 2006 22:50:10 +0100
"jar13861@xxxxxxxxx" <jar13861@xxxxxxxxx> writes:
I have attended every class, and the teacher wont help on homework
assigned outside of class. I already have tried on numerous occasions
only to fall up short. I will submit the latest attempt in hopes that
it can be dissected and corrected.
I'll try to give a few hints.
You are missing the point of using events. Your loop, the one which
alternates between alerting that it is the user's turn and not doing
it, will run to completeness (including all five alerts) before any
other script is allowed to run, including the event handlers of the
table cells. Javascript in browsers isn't multithreaded or preemptive.
Busy-waiting on user actions will never work (it's a bad idea
everywhere else too, even when it "works").
You should do the calculations and set up the intial game state and
(perhaps) alert the user for his first turn. Then your initialization
script should end.
The next thing that happens is that the user clicks a cell and an
event fires. In this event handler you update your game state
accordingly. When five such events have fired (not counting any
attempts of pressing the same square again!) you should be done.
<SCRIPT LANGUAGE = "JavaScript">
For valid HTML, the type attribute is required. It's also all that
is needed, so you can do with:
<script type="text/javascript">
holder [i] = Math.floor( 1 + Math.random() * 9 );
Good! Far too often we see people doing random wrong. This is just
right.
if (num == 1){
cell1.innerText = holder [0];
flag[num] = 1;
}
else if (num == 2) {
cell2.innerText = holder [1];
flag[num] = 1;
}
....
this can be done *much* shorter.
Try to see what is the same in each, and what changes.
Also, you should not refer to a named element simply as its name.
Not all browsers creates a global variable pointing to the element
that way. The standard method for getting an element by its id is:
document.getElementById("idofelement");
Since the name is a string expression here, it can be calculated,
whereas a variable name must be fixed when the program is written.
The "innerText" property isn't widely supported either (but if your
assignment only need to run in IE, it's not a problem either).
x = Math.floor( 1 + Math.random() * 9);
if (flag [x] = 1) {
j--;
}
This is somewhat inefficient, eventually trying to guess one of the
two unpicked cells until you succede (average number of tries is only
4.5, but the potential is infinite :). For a small array (9 elements
is small) it won't matter, but keep in mind that it's not optimal.
if (userScore > compScore)
window.alert("User Wins!");
else window.alert("Computer Wins!");
Come on, show the score! I want to know HOW much I won by! :)
<TD ONCLICK = "test (1)"><P ID = "cell1">Click me!</P></TD>
You can put the id attribute directly on the td.
Good luck
/L
--
Lasse Reichstein Nielsen - lrn@xxxxxxxxxx
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
.
- Prev by Date: Re: XML & Javascript parser
- Next by Date: Click and Drag
- Previous by thread: Re: Game
- Next by thread: Click and Drag
- Index(es):
Relevant Pages
|