Re: Game



"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.'
.



Relevant Pages

  • Re: Help with Homework
    ... There were 14 students that did not do this assignment. ... You must print a sentence with the I.D. numbers of the students who did not take the test. ... Any empty cells that are found must be counted and the number of them declared in the output statement. ... Where Scores is the variable name that stores the matrix from the Excel file. ...
    (comp.soft-sys.matlab)
  • Re: Calculate
    ... in the AdventureWorks everything is working fine, ... writes in the empty cell. ... Because the main query uses the Non Empty operator all empty cells (leaf ... The assignment for a leaf-cell is it persisted ...
    (microsoft.public.sqlserver.olap)
  • RE: open / close files and display names
    ... Change the cells .Value assignment to ... and change those single quotes to double quotes (remember you're in ... That should fix the immediate problem. ...
    (microsoft.public.excel.programming)
  • Re: VLOOKUP #N/A Sometimes but not always
    ... Probably not all C cells are numbers, although they may look like numbers. ... Format a blank cell as Number. ... In the assignment, we're looking up a discount rate based on the ... > customer's credit score. ...
    (microsoft.public.excel.misc)