squirellplaying
04-29-2004, 11:23 PM
I have to create a battleship type game but the math seams to be a little off. It adds 5 instead of 1!
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:
<html>
<head>
<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>
</head>
<body onload = "writeTable()">
<div id="userInput">Row:<input type = 'text' id="xcoord"/> 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>
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:
<html>
<head>
<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>
</head>
<body onload = "writeTable()">
<div id="userInput">Row:<input type = 'text' id="xcoord"/> 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>