Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## Fishy Math

I have to create a battleship type game but the math seams to be a little off. It adds 5 instead of 1!
Code:
```var hits = 0
var iGuesses = 0

function checkForHit(x,y){    //start checking for hits
x = x.value;
y = y.value;
z = 'a' + x + y
for(c = 0; c<=4; c++){
if((shipLocation[c][0] == x)&&(shipLocation[c][1]==y)){
document.getElementById(z).innerHTML = 'H'
hits = hits + 1
iGuesses = iGuesses + 1
document.getElementById('hits').innerHTML = 'You have hit' + hits + 'times.'
document.getElementById('guesses').innerHTML =  'You have guessed' + iGuesses + 'times.'
shipLocation[x][y] = 'hit'
return (true)
}
document.getElementById(z).innerHTML = 'M'
iGuesses = iGuesses + 1
document.getElementById('guesses').innerHTML = 'You have guessed  ' + iGuesses + '  times.'
}

}     //end hit check```
This worked before I added the shipLocation[x][y] = 'hit' .
I need this so that you can't enter the same place twice and get another hit.

Here is the complete code:
Code:
```<html>

<script>
var hits = 0
var iGuesses = 0

function checkForHit(x,y){    //start checking for hits
x = x.value;
y = y.value;
z = 'a' + x + y
for(c = 0; c<=4; c++){
if((shipLocation[c][0] == x)&&(shipLocation[c][1]==y)){
document.getElementById(z).innerHTML = 'H'
hits = hits + 1
iGuesses = iGuesses + 1
document.getElementById('hits').innerHTML = 'You have hit' + hits + 'times.'
document.getElementById('guesses').innerHTML =  'You have guessed' + iGuesses + 'times.'
shipLocation[x][y] = 'hit'
return (true)
}
document.getElementById(z).innerHTML = 'M'
iGuesses = iGuesses + 1
document.getElementById('guesses').innerHTML = 'You have guessed  ' + iGuesses + '  times.'
}

}     //end hit check

function writeTable(){
var numArrays = 20;
var numCells = 20;
arrays = new Array();
sTable =''
sTable= sTable + "<table>"

for(var i = 1; i <= numArrays; i++) {    //Start to write table to div to be displayed
arrays[i] = new Array();
sTable= sTable + "<tr>"
for(var j = 1; j <= numCells; j++) {
arrays[i][j] = i + "," +j;
sTable= sTable + "<td id='a" + i + j +"'>"+ arrays[i][j] +"</td>";
}
sTable= sTable + "</tr>"

}  //End table writing

sTable= sTable + "</table>"
document.getElementById('tableDisplay').innerHTML = sTable     //write the table to the div
}

var N1 = 1
var N2 = 20

//create ship location array

function cheat(){
cheatWindow = window.open('','Cheater','')
sWindowText = ""
for (m = 0;m<5; m++){
for (n = 0; n<=1; n++){
if(n == 1){
sWindowText = sWindowText + shipLocation[m][n] + "<br>"
}
else{
sWindowText = sWindowText + shipLocation[m][n] + ","
}
}
}
cheatWindow.document.write(sWindowText)
}

var shipLocation = new Array()
for (a = 0; a<5; a++){
shipLocation[a] = new Array()
for (b = 0; b<=1; b++){
shipLocation[a][b] = Math.floor((N2 - N1 + 1)*Math.random() + N1) //assign each location in shipLocation array a random number.

}
}
//end creating ship location array

</script>
<style>
td{
height:52px;
width:52px;
text-align:center;
v-align:middle;
color:blue;
}
#userInput{
text-align:center;

}
</style>
<title>New Page 2</title>
<div id="userInput">Row:<input type = 'text' id="xcoord"/>&nbsp;&nbsp;&nbsp;Column:<input type = 'text' id="ycoord"/><br />
<input type="button" value="Fire Away!" onclick="checkForHit(document.getElementById('xcoord'),document.getElementById('ycoord'))" /><input type="button" onclick = "cheat()" value="Cheat" /></div>
<div><span id="hits" style="float:right"></span><span id="guesses" style="float:left"></span></div>
<br />
<div id="tableDisplay"></div>

</body>
</html>```

• its adding 5 times because the loop is executed 5 times.

I did not have enough time to look through your code fully, but this might work.
Code:
```var hits = 0
var iGuesses = 0

function checkForHit(x,y){    //start checking for hits
x = x.value;
y = y.value;
z = 'a' + x + y;
iGuesses = iGuesses + 1;  // you need to count the check only once hence moved out of loop.
document.getElementById(z).innerHTML = 'M'; // assume you always will miss. if you hit, you will update this in the loop to H.
for(c = 0; c<=4; c++){
if((shipLocation[c][0] == x)&&(shipLocation[c][1]==y)){
document.getElementById(z).innerHTML = 'H';
hits = hits + 1;
document.getElementById('hits').innerHTML = 'You have hit' + hits + 'times.';
shipLocation[x][y] = 'hit';
return (true);
}
}
document.getElementById('guesses').innerHTML =  'You have guessed' + iGuesses + 'times.'

}     //end hit check```

• Thanks. Works good. But it still lets you enter in a hit twice. It does not change the value of shipLocation[x][y] to equal something other then a number after it was used.

• Ah, I must have overlooked that one.

The problem is in this line
Code:
`shipLocation[x][y] = 'hit';`
As you can see, our shipLocation format is like shipLocation[0][0]=x and shipLocation[0][1]=y; so you were marking it at the wrong spot. see if you can correct it yourself.

If not, I messed with the code and changed what I felt like.. and I can't be bothered to change it back, so that's what you get . anyway, hope that it helps you in understanding it better. Post back if you have something that you dont understand. Highlighted my changes in red.

Code:
```<html>

<script>
var hits = 0
var iGuesses = 0

function checkForHit(x,y)
{
z = 'a' + x.value + y.value;
s = '' + x.value + '-' + y.value;
slot = document.getElementById(z);
if(slot.innerHTML == 'M' || slot.innerHTML == 'H')  // see if the user already tried this guess.
return;
slot.innerHTML = 'M'; // assume we missed.

iGuesses = iGuesses + 1;
document.getElementById('guesses').innerHTML =  'You have guessed ' + iGuesses + ' times.'

if(shipLocation[s]==1)  // ship is hit
{
slot.innerHTML = 'H';
hits = hits + 1;
document.getElementById('hits').innerHTML = 'You have hit ' + hits + ' times.';
shipLocation[s] = 0;   // mark that we are hit.
return;
}
}

function writeTable(){
var numArrays = 20;
var numCells = 20;
arrays = new Array();
var sTable= "<table>"

for(var i = 1; i <= numArrays; i++) {    //Start to write table to div to be displayed
arrays[i] = new Array();
sTable= sTable + "<tr>"
for(var j = 1; j <= numCells; j++) {
arrays[i][j] = i + "," +j;
sTable= sTable + "<td id='a" + i + j +"'>"+ arrays[i][j] +"</td>";
}
sTable= sTable + "</tr>"

}  //End table writing

sTable= sTable + "</table>"
document.getElementById('tableDisplay').innerHTML = sTable     //write the table to the div
}

var N1 = 1
var N2 = 20

//create ship location array

function cheat(){
cheatWindow = window.open('','Cheater','')
sWindowText = ""
for (var ship in shipLocation) {
sWindowText = sWindowText + ship + "<BR>";
}
cheatWindow.document.write(sWindowText)
}

var shipLocation = new Array()
for (a = 0; a<5; a++)
{
// shiplocation of the form shipLocation[x-y]=1
shipLocation[''+ (Math.floor((N2 - N1 + 1)*Math.random() + N1)) +  '-'  + Math.floor((N2 - N1 + 1)*Math.random() + N1)]=1;
}
//end creating ship location array

</script>
<style>
td{
height:52px;
width:52px;
text-align:center;
v-align:middle;
color:blue;
}
#userInput{
text-align:center;

}
</style>
<title>New Page 2</title>
<div id="userInput">Row:<input type = 'text' id="xcoord"/>&nbsp;&nbsp;&nbsp;Column:<input type = 'text' id="ycoord"/><br />
<input type="button" value="Fire Away!" onclick="checkForHit(document.getElementById('xcoord'),document.getElementById('ycoord'))" /><input type="button" onclick = "cheat()" value="Cheat" /></div>
<div><span id="hits" style="float:right"></span><span id="guesses" style="float:left"></span></div>
<br />
<div id="tableDisplay"></div>

</body>
</html>```

• Oh! Light bulb! I see now. I think I can fix it my self. Thanks.

• You might want to make it similar to my script such that guessing the same spot twice does not do anything like incrementing the number of guesses or hits. Try running my version of the script to see what I mean.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•