Game

J

jar13861

Create a game in a 3x3 HTML table that works as follows:

* The goal of the game is to beat the computer by scoring more
points than the computer.
* The game starts when a hidden random number between 1 and 9 is
placed in each cell.
* The user and computer take turns by "clicking" on table cells.
When a cell is "clicked" by the user, the user scores the number of
hidden points in the cell. When the computer takes a turn, it "clicks
on" a random cell that has not been clicked yet and adds the hidden
points in the clicked cell to its scor
* The user always goes first, so they should have a better chance
of winning since they will get 5 turns and the computer 4 turns.
* The user wins if they score more points than the computer. The
computer wins if it scores more than the user.



Hints:

* Have your script first generate a set of random numbers from 1- 9
and place them randomly in an array. The numbers in the array will
need to be unique, so you will need a loop to go through and check that
each new random number has not already been used.
* Use the ONCLICK="clicked(cellNum)" in each cell(TD tag) to call
the a function called clicked(cellNum), passing in the cell
number(0,1,2,...,9) as the parameter. That way, when the function
named "clicked()" gets cellNum it will know which cell was clicked and
perform scoring based on that.
* Use a P or DIV tags to hold the scores for the user and computer.




Please help out anyway you can. I know I'm new, but I really really
need help.
 
T

Thomas 'PointedEars' Lahn

Create a game in a 3x3 HTML table that works as follows:
[...]
Hints:
[...]
Please help out anyway you can. I know I'm new, but I really really
need help.

You really really should have attended the corresponding lessons.


PointedEars
 
R

Randy Webb

(e-mail address removed) said the following on 2/28/2006 2:27 PM:
Create a game in a 3x3 HTML table that works as follows:

How much of your grade do I get for doing your homework?
 
E

Evertjan.

Create a game in a 3x3 HTML table that works as follows:

* The goal of the game is to beat the computer by scoring more
[....]

Please help out anyway you can. I know I'm new, but I really really
need help.

You should do your school assignment yourself.

If you get into specific trouble, perhaps, ... I mean certainly, someone
here on this usenet NG will help you if you show the details of where the
trouble is.
 
L

Lee

(e-mail address removed) said:
Create a game in a 3x3 HTML table that works as follows:

* The goal of the game is to beat the computer by scoring more
points than the computer.
* The game starts when a hidden random number between 1 and 9 is
placed in each cell.
...
Please help out anyway you can. I know I'm new, but I really really
need help.

Does your instructor have office hours?
Is it too late to drop the class?
 
J

jar13861

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.


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!-- HW 6 -->

<HEAD>
<TITLE>Jonathan Riehm CIS 201-01 HW 6</TITLE>


<SCRIPT LANGUAGE = "JavaScript">

var holder,
num,
userScore,
compScore,
x,
y,
flag;



compScore = 0;
userScore = 0;
holder = new Array ( 9 );
flag = new Array ( 9 );


for (var i=0; i <=8; i++) {

holder = Math.floor( 1 + Math.random() * 9 );

flag = 0;
}


document.writeln(holder);
document.writeln(flag);
window.alert("User turn. Click on a cell inside the table");




function test (num) {


if (num == 1){
cell1.innerText = holder [0];
flag[num] = 1;
}
else if (num == 2) {
cell2.innerText = holder [1];
flag[num] = 1;
}
else if (num == 3) {
cell3.innerText = holder [2];
flag[num] = 1;
}
else if (num == 4) {
cell4.innerText = holder [3];
flag[num] = 1;
}
else if (num == 5) {
cell5.innerText = holder [4];
flag[num] = 1;
}
else if (num == 6) {
cell6.innerText = holder [5];
flag[num] = 1;
}
else if (num == 7) {
cell7.innerText = holder [6];
flag[num] = 1;
}
else if (num == 8) {
cell8.innerText = holder [7];
flag[num] = 1;
}
else if (num == 9) {
cell9.innerText = holder [8];
flag[num] = 1;
}

}

for (var j = 1; j <= 9; j++) {
if ( j%2 != 0) {
window.alert("User turn. Click on a cell inside the table");
y = num - 1
userScore = (userScore + holder [y]);
}
else {
x = Math.floor( 1 + Math.random() * 9);
if (flag [x] = 1) {
j--;
}
else compScore = (compScore + holder [y]);
}
}

if (userScore > compScore)
window.alert("User Wins!");
else window.alert("Computer Wins!");










</SCRIPT>
</HEAD>
<BODY>


<TABLE BORDER="1" WIDTH = "100%">
<TR>
<TD ONCLICK = "test (1)"><P ID = "cell1">Click me!</P></TD>
<TD ONCLICK = "test (2)"><P ID = "cell2">Click me!</P></TD>
<TD ONCLICK = "test (3)"><P ID = "cell3">Click me!</P></TD>
</TR>

<TR>
<TD ONCLICK = "test (4)"><P ID = "cell4">Click me!</P></TD>
<TD ONCLICK = "test (5)"><P ID = "cell5">Click me!</P></TD>
<TD ONCLICK = "test (6)"><P ID = "cell6">Click me!</P></TD>
</TR>

<TR>
<TD ONCLICK = "test (7)"><P ID = "cell7">Click me!</P></TD>
<TD ONCLICK = "test (8)"><P ID = "cell8">Click me!</P></TD>
<TD ONCLICK = "test (9)"><P ID = "cell9">Click me!</P></TD>
</TR>

</TABLE>






</BODY>
</HTML>
 
L

Lasse Reichstein Nielsen

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:
holder = 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
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Tue, 28 Feb 2006 11:27:20 remote, seen in
news:comp.lang.javascript said:
* Have your script first generate a set of random numbers from 1- 9
and place them randomly in an array. The numbers in the array will
need to be unique, so you will need a loop to go through and check that
each new random number has not already been used.

So your instructor is also of inadequate ability.
His/Her "you will need a loop ..." is a false statement.

Such a loop can be used, but that is an inefficient method. The need is
to deal the numbers, in random order, into an array. There is an
efficient algorithm for that; it is in Knuth, and can also be found
/via/ the newsgroup FAQ.
* Use the ONCLICK="clicked(cellNum)" in each cell(TD tag) to call
the a function called clicked(cellNum), passing in the cell
number(0,1,2,...,9) as the parameter. That way, when the function
^
Either your instructor cannot count or you are a careless copier.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top