Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.
Battleship game in JavaScript
a simple battleship game written in javascript…
it's dirty coding because I only had about 1 month with full time school and had no prior javascript knowledge…
but it's fun to mass around… and it's fun to mass with javascript injection….
<body>
<title>Battleship</title>
<script language="JavaScript" type="text/JavaScript">
/*********************************************************************
Author : Alka_Vulcan
Real programmers don't document. If it was hard to write, it should
be hard to understand.... Just kidding.... I did some documentation
P.S. it's my first and last (so far...) JavaScript code I ever written
after studying so it's very newbish and lack alot of stuff so forgive me....
*********************************************************************/
var number= new Array() //random number array
var used = new Array() //used random number array
var ship = 0; //Player's ship count see if player sunk all ships
var cship = 0; //computer's ship count to see if player sunk all ships
var x = 0; //for loop controler
var n = 0; //sub number for random number array
var m = 0; //sub number for used random number array
var hit = false; //if a portion of ship is destroyed or not
var sunk = false; //if entire ship sunk or not
<!-- insult variables --!>
var miss = 0;
var shot = 0;
<!-- end --!>
<!-- to see all portion of ships has been destroyed or not --!>
var ms1 = false; //if a portion of player's mine sweeper got hit or not
var ms2 = false; //if a portion of player's mine sweeper got hit or not
var sb1 = false; //if a portion of player's submarine got hit or not
var sb2 = false; //if a portion of player's submarine got hit or not
var sb3 = false; //if a portion of player's submarine got hit or not
var fr1 = false; //if a portion of player's frigate got hit or not
var fr2 = false; //if a portion of player's frigate got hit or not
var fr3 = false; //if a portion of player's frigate got hit or not
var bs1 = false; //if a portion of player's battleship got hit or not
var bs2 = false; //if a portion of player's battleship got hit or not
var bs3 = false; //if a portion of player's battleship got hit or not
var bs4 = false; //if a portion of player's battleship got hit or not
var ca1 = false; //if a portion of player's carrier got hit or not
var ca2 = false; //if a portion of player's carrier got hit or not
var ca3 = false; //if a portion of player's carrier got hit or not
var ca4 = false; //if a portion of player's carrier got hit or not
var ca5 = false; //if a portion of player's carrier got hit or not
var cms1 = false; //if a portion of computer's mine sweeper got hit or not
var cms2 = false; //if a portion of computer's mine sweeper got hit or not
var csb1 = false; //if a portion of computer's submarine got hit or not
var csb2 = false; //if a portion of computer's submarine got hit or not
var csb3 = false; //if a portion of computer's submarine got hit or not
var cfr1 = false; //if a portion of computer's frigate got hit or not
var cfr2 = false; //if a portion of computer's frigate got hit or not
var cfr3 = false; //if a portion of computer's frigate got hit or not
var cbs1 = false; //if a portion of computer's battleship got hit or not
var cbs2 = false; //if a portion of computer's battleship got hit or not
var cbs3 = false; //if a portion of computer's battleship got hit or not
var cbs4 = false; //if a portion of computer's battleship got hit or not
var cca1 = false; //if a portion of computer's carrier got hit or not
var cca2 = false; //if a portion of computer's carrier got hit or not
var cca3 = false; //if a portion of computer's carrier got hit or not
var cca4 = false; //if a portion of computer's carrier got hit or not
var cca5 = false; //if a portion of computer's carrier got hit or not
<!-- end --!>
<!-- Coordinates --!>
var Minesweeper1 //coordinate for a portion of player's minesweeper
var Minesweeper2 //coordinate for a portion of player's minesweeper
var Submarine1 //coordinate for a portion of player's submarine
var Submarine2 //coordinate for a portion of player's submarine
var Submarine3 //coordinate for a portion of player's submarine
var Frigate1 //coordinate for a portion of player's frigate
var Frigate2 //coordinate for a portion of player's frigate
var Frigate3 //coordinate for a portion of player's frigate
var Battleship1 //coordinate for a portion of player's battleship
var Battleship2 //coordinate for a portion of player's battleship
var Battleship3 //coordinate for a portion of player's battleship
var Battleship4 //coordinate for a portion of player's battleship
var Carrier1 //coordinate for a portion of player's carrier
var Carrier2 //coordinate for a portion of player's carrier
var Carrier3 //coordinate for a portion of player's carrier
var Carrier4 //coordinate for a portion of player's carrier
var Carrier5 //coordinate for a portion of player's carrier
var cMinesweeper1 //coordinate for a portion of computer's minesweeper
var cMinesweeper2 //coordinate for a portion of computer's minesweeper
var cSubmarine1 //coordinate for a portion of computer's submarine
var cSubmarine2 //coordinate for a portion of computer's submarine
var cSubmarine3 //coordinate for a portion of computer's submarine
var cFrigate1 //coordinate for a portion of computer's frigate
var cFrigate2 //coordinate for a portion of computer's frigate
var cFrigate3 //coordinate for a portion of computer's frigate
var cBattleship1 //coordinate for a portion of computer's battleship
var cBattleship2 //coordinate for a portion of computer's battleship
var cBattleship3 //coordinate for a portion of computer's battleship
var cBattleship4 //coordinate for a portion of computer's battleship
var cCarrier1 //coordinate for a portion of computer's carrier
var cCarrier2 //coordinate for a portion of computer's carrier
var cCarrier3 //coordinate for a portion of computer's carrier
var cCarrier4 //coordinate for a portion of computer's carrier
var cCarrier5 //coordinate for a portion of computer's carrier
<!-- end --!>
<!-- function that start on load of page to set up player's and computer's table --!>
function load()
{
//Because i'm retarded and didn't have time at the moment I only have 4 possible table for computer and player each
var tableP = Math.round(Math.random()*4); //Player's table
var tableC = Math.round(Math.random()*4); //computer's table
if(tableP == 0)
{
Minesweeper1 = 50
Minesweeper2 = 60
Submarine1 = 31
Submarine2 = 41
Submarine3 = 51
Frigate1 = 24
Frigate2 = 25
Frigate3 = 26
Battleship1 = 53
Battleship2 = 54
Battleship3 = 55
Battleship4 = 56
Carrier1 = 38
Carrier2 = 48
Carrier3 = 58
Carrier4 = 68
Carrier5 = 78
}
if(tableP == 1)
{
Minesweeper1 = 85
Minesweeper2 = 86
Submarine1 = 28
Submarine2 = 38
Submarine3 = 48
Frigate1 = 13
Frigate2 = 23
Frigate3 = 33
Battleship1 = 14
Battleship2 = 15
Battleship3 = 16
Battleship4 = 17
Carrier1 = 61
Carrier2 = 62
Carrier3 = 63
Carrier4 = 64
Carrier5 = 65
}
if(tableP == 2)
{
Minesweeper1 = 13
Minesweeper2 = 14
Submarine1 = 26
Submarine2 = 36
Submarine3 = 46
Frigate1 = 35
Frigate2 = 34
Frigate3 = 33
Battleship1 = 88
Battleship2 = 87
Battleship3 = 86
Battleship4 = 85
Carrier1 = 10
Carrier2 = 20
Carrier3 = 30
Carrier4 = 40
Carrier5 = 50
}
if(tableP == 3)
{
Minesweeper1 = 90
Minesweeper2 = 91
Submarine1 = 28
Submarine2 = 38
Submarine3 = 48
Frigate1 = 65
Frigate2 = 66
Frigate3 = 67
Battleship1 = 40
Battleship2 = 41
Battleship3 = 42
Battleship4 = 43
Carrier1 = 74
Carrier2 = 64
Carrier3 = 54
Carrier4 = 44
Carrier5 = 34
}
if(tableP == 4)
{
Minesweeper1 = 42
Minesweeper2 = 43
Submarine1 = 15
Submarine2 = 25
Submarine3 = 35
Frigate1 = 17
Frigate2 = 18
Frigate3 = 19
Battleship1 = 11
Battleship2 = 12
Battleship3 = 13
Battleship4 = 14
Carrier1 = 88
Carrier2 = 78
Carrier3 = 68
Carrier4 = 58
Carrier5 = 48
}
if(tableC == 0)
{
cMinesweeper1 = 12
cMinesweeper2 = 13
cSubmarine1 = 35
cSubmarine2 = 45
cSubmarine3 = 55
cFrigate1 = 67
cFrigate2 = 68
cFrigate3 = 69
cBattleship1 = 81
cBattleship2 = 82
cBattleship3 = 83
cBattleship4 = 84
cCarrier1 = 31
cCarrier2 = 41
cCarrier3 = 51
cCarrier4 = 61
cCarrier5 = 71
}
if(tableC == 1)
{
cMinesweeper1 = 72
cMinesweeper2 = 73
cSubmarine1 = 03
cSubmarine2 = 13
cSubmarine3 = 23
cFrigate1 = 17
cFrigate2 = 16
cFrigate3 = 15
cBattleship1 = 08
cBattleship2 = 18
cBattleship3 = 28
cBattleship4 = 38
cCarrier1 = 11
cCarrier2 = 21
cCarrier3 = 31
cCarrier4 = 41
cCarrier5 = 51
}
if(tableC == 2)
{
cMinesweeper1 = 53
cMinesweeper2 = 54
cSubmarine1 = 20
cSubmarine2 = 21
cSubmarine3 = 22
cFrigate1 = 38
cFrigate2 = 48
cFrigate3 = 58
cBattleship1 = 37
cBattleship2 = 36
cBattleship3 = 35
cBattleship4 = 34
cCarrier1 = 89
cCarrier2 = 88
cCarrier3 = 87
cCarrier4 = 86
cCarrier5 = 85
}
if(tableC == 3)
{
cMinesweeper1 = 88
cMinesweeper2 = 87
cSubmarine1 = 75
cSubmarine2 = 76
cSubmarine3 = 77
cFrigate1 = 22
cFrigate2 = 32
cFrigate3 = 42
cBattleship1 = 51
cBattleship2 = 61
cBattleship3 = 71
cBattleship4 = 81
cCarrier1 = 08
cCarrier2 = 07
cCarrier3 = 06
cCarrier4 = 05
cCarrier5 = 04
}
if(tableC == 4)
{
cMinesweeper1 = 22
cMinesweeper2 = 32
cSubmarine1 = 02
cSubmarine2 = 03
cSubmarine3 = 04
cFrigate1 = 78
cFrigate2 = 77
cFrigate3 = 76
cBattleship1 = 14
cBattleship2 = 24
cBattleship3 = 34
cBattleship4 = 44
cCarrier1 = 60
cCarrier2 = 61
cCarrier3 = 62
cCarrier4 = 63
cCarrier5 = 64
}
tableSetting(); //call a function that show you player's map
for (X=0; x<100; x++)
{
number[x]=x //store 0 ~ 99 in random number array
}
for (x=0; x<10000; x++) //random number array location swapper
{
var randA = Math.round(Math.random()*99) //select random number 0 ~ 99
var randB = Math.round(Math.random()*99) //select random number 0 ~ 99
var temp = number[randA] //save a randomnumber to temporary variable
number[randA] = number[randB] //save a randomnumber to another randomnumber's location
number[randB] = temp //save temporary variable data to randomnumber that previously used
}
}
<!-- end of function that start on load of page --!>
<!-- Function that show player's map --!>
function tableSetting()
{
var a=new Array() //array holding player's map coordinate
a[0] = cMinesweeper1;
a[1] = cMinesweeper2;
a[2] = cSubmarine1;
a[3] = cSubmarine2;
a[4] = cSubmarine3;
a[5] = cFrigate1;
a[6] = cFrigate2;
a[7] = cFrigate3;
a[8] = cBattleship1;
a[9] = cBattleship2;
a[10] = cBattleship3;
a[11] = cBattleship4;
a[12] = cCarrier1;
a[13] = cCarrier2;
a[14] = cCarrier3;
a[15] = cCarrier4;
a[16] = cCarrier5;
var r; //row
var c; //colum
for(i = 0; i < 17; i++)
{
if(a[i] < 10) //if coordinate is < 10 (because coordinates are stored as '01', '04', or ect)
{
r = 0; //row is 0
c = a[i] / 1; //colum is coordinate / 1
}
else
{
r = (a[i] - (a[i] % 10)) / 10; //row is (coordinate - 2nd digit) / 10
c = a[i] % 10; //colum is remainder of coordinate
}
document.getElementById('computer').rows[r].cells[c].style.backgroundColor="#AAAAAA"; //color it to black
}
}
<!-- end --!>
<!-- functions that test if a portion of player's choice match computer's ship coordinate, and if a ship is fully destoryed or not --!>
function minesweeper(v)
{
var test=false; //ship demage initialized
if(!ms1) //if it's true, do not change it's data
{
if(v==Minesweeper1) //if coordinate matches ship's coordinate
{
ms1 = true; //a portion of ship is destroyed
test = true; //ship got hit
alert("It's a hit")
}
if(ms1 && ms2) //if all portions of ships destroyed
{
alert("You've sunk Minesweeper")
ship++; //increment ship counter
insultship();
}
}
if(!ms2) //if it's true, do not change it's data
{
if(v==Minesweeper2) //if coordinate matches ship's coordinate
{
ms2 = true; //a portion of ship is destroyed
test = true; //ship got hit
alert("it's a hit")
}
if(ms1 && ms2) //if all portions of ships destroyed
{
alert("You've sunk Minesweeper")
ship++; //increment ship counter
insultship();
}
}
return test;
}
function submarine(v)
{
var test=false;
if(!sb1)
{
if(v==Submarine1)
{
sb1 = true;
test = true;
alert("it's a hit")
}
if(sb1 && sb2 && sb3)
{
alert("You've sunk Submarine")
ship++;
insultship();
}
}
if(!sb2)
{
if(v==Submarine2)
{
sb2 = true;
test = true;
alert("it's a hit")
}
if(sb1 && sb2 && sb3)
{
alert("You've sunk Submarine")
ship++;
insultship();
}
}
if(!sb3)
{
if(v==Submarine3)
{
sb3 = true;
test = true;
alert("it's a hit")
}
if(sb1 && sb2 && sb3)
{
alert("You've sunk Submarine")
ship++;
insultship();
}
}
return test;
}
function frigate(v)
{
var test=false;
if(!fr1)
{
if(v==Frigate1)
{
fr1 = true;
test = true;
alert("it's a hit")
}
if(fr1 && fr2 && fr3)
{
alert("You've sunk Frigate")
ship++;
insultship();
}
}
if(!fr2)
{
if(v==Frigate2)
{
fr2 = true;
test = true;
alert("it's a hit")
}
if(fr1 && fr2 && fr3)
{
alert("You've sunk Frigate")
ship++;
insultship();
}
}
if(!fr3)
{
if(v==Frigate3)
{
fr3 = true;
test = true;
alert("it's a hit")
}
if(fr1 && fr2 && fr3)
{
alert("You've sunk Frigate")
ship++;
insultship();
}
}
return test;
}
function battleship(v)
{
var test=false;
if(!bs1)
{
if(v==Battleship1)
{
bs1 = true;
test = true;
alert("it's a hit")
}
if(bs1 && bs2 && bs3 && bs4)
{
alert("You've sunk battleship")
ship++;
insultship();
}
}
if(!bs2)
{
if(v==Battleship2)
{
bs2 = true;
test = true;
alert("it's a hit")
}
if(bs1 && bs2 && bs3 && bs4)
{
alert("You've sunk battleship")
ship++;
insultship();
}
}
if(!bs3)
{
if(v==Battleship3)
{
bs3 = true;
test = true;
alert("it's a hit")
}
if(bs1 && bs2 && bs3 && bs4)
{
alert("You've sunk battleship")
ship++;
insultship();
}
}
if(!bs4)
{
if(v==Battleship4)
{
bs4 = true;
test = true;
alert("it's a hit")
}
if(bs1 && bs2 && bs3 && bs4)
{
alert("You've sunk battleship")
ship++;
insultship();
}
}
return test;
}
function carrier(v)
{
var test=false;
if(!ca1)
{
if(v==Carrier1)
{
ca1 = true;
test = true;
alert("it's a hit")
}
if(ca1 && ca2 && ca3 && ca4 && ca5)
{
alert("You've sunk Carrier")
ship++;
insultship();
}
}
if(!ca2)
{
if(v==Carrier2)
{
ca2 = true;
test = true;
alert("it's a hit")
}
if(ca1 && ca2 && ca3 && ca4 && ca5)
{
alert("You've sunk Carrier")
ship++;
insultship();
}
}
if(!ca3)
{
if(v==Carrier3)
{
ca3 = true;
test = true;
alert("it's a hit")
}
if(ca1 && ca2 && ca3 && ca4 && ca5)
{
alert("You've sunk Carrier")
ship++;
insultship();
}
}
if(!ca4)
{
if(v==Carrier4)
{
ca4 = true;
test = true;
alert("it's a hit")
}
if(ca1 && ca2 && ca3 && ca4 && ca5)
{
alert("You've sunk Carrier")
ship++;
insultship();
}
}
if(!ca5)
{
if(v==Carrier5)
{
ca5 = true;
test = true;
alert("it's a hit")
}
if(ca1 && ca2 && ca3 && ca4 && ca5)
{
alert("You've sunk Carrier")
ship++;
insultship();
}
}
return test;
}
<!-- end of player's ship functions --!>
<!-- functions that test if a portion of computer's choice match player's ship coordinate, and if a ship is fully destoryed or not --!>
function cminesweeper(v)
{
var test=false; //ship demage initialized
if(!cms1) //if it's true, do not change it's data
{
if(v==cMinesweeper1) //if coordinates are matching
{
cms1 = true; //a portion of ship is destroyed
test = true; //ship got hit
alert("Computer hit")
}
if(cms1 && cms2) //if all portions of ship is destroyed
{
alert("Computer sunk Minesweeper")
sunk=true; //player's full ship is destroyed
cship++; //computer ship count increment
insultship();
}
else
sunk=false; //player's full ship is not destroyed
}
if(!cms2) //if it's true, do not change it's data
{
if(v==cMinesweeper2) //if coordinates are matching
{
cms2 = true; //a portion of ship is destoryed
test = true; //ship got hit
alert("Computer hit")
}
if(cms1 && cms2) //if all portions of ship is destroyed
{
alert("Computer sunk Minesweeper")
cship++; //increment ship counter
sunk=true; //player's full ship is destroyed
insultship();
}
else
sunk=false; //player's full ship is not destroyed
}
return test; //return test result
}
function csubmarine(v)
{
var test=false;
if(!csb1)
{
if(v==cSubmarine1)
{
csb1 = true;
test = true;
alert("computer hit")
}
if(csb1 && csb2 && csb3)
{
alert("computer sunk Submarine")
cship++;
sunk=true;
insultship();
}
else
sunk=false;
}
if(!csb2)
{
if(v==cSubmarine2)
{
csb2 = true;
test = true;
alert("Computer hit")
}
if(csb1 && csb2 && csb3)
{
alert("computer sunk Submarine")
cship++;
sunk=true;
insultship();
}
else
sunk=false;
}
if(!csb3)
{
if(v==cSubmarine3)
{
csb3 = true;
test = true;
alert("computer hit")
}
if(csb1 && csb2 && csb3)
{
alert("computer sunk Submarine")
cship++;
sunk=true;
insultship();
}
else
sunk=false;
}
return test;
}
function cfrigate(v)
{
var test=false;
if(!cfr1)
{
if(v==cFrigate1)
{
cfr1 = true;
test = true;
alert("Computer hit")
}
if(cfr1 && cfr2 && cfr3)
{
alert("Computer sunk Frigate")
cship++;
sunk=true;
insultship();
}
else
sunk=false;
}
if(!cfr2)
{
if(v==cFrigate2)
{
cfr2 = true;
test = true;
alert("Computer hit")
}
if(cfr1 && cfr2 && cfr3)
{
alert("Computer sunk Frigate")
cship++;
sunk=true;
insultship();
}
else
sunk=false;
}
if(!cfr3)
{
if(v==cFrigate3)
{
cfr3 = true;
test = true;
alert("Computer hit")
}
if(cfr1 && cfr2 && cfr3)
{
alert("Computer sunk Frigate")
cship++;
sunk=true;
insultship();
}
else
sunk=false;
}
return test;
}
function cbattleship(v)
{
var test=false;
if(!cbs1)
{
if(v==cBattleship1)
{
cbs1 = true;
test = true;
alert("Computer hit")
}
if(cbs1 && cbs2 && cbs3 && cbs4)
{
alert("Computer sunk battleship")
cship++;
sunk=true;
insultship();
}
else
sunk=false;
}
if(!cbs2)
{
if(v==cBattleship2)
{
cbs2 = true;
test = true;
alert("Computer hit")
}
if(cbs1 && cbs2 && cbs3 && cbs4)
{
alert("Computer sunk battleship")
cship++;
insultship();
sunk=true;
}
else
sunk=false;
}
if(!cbs3)
{
if(v==cBattleship3)
{
cbs3 = true;
test = true;
alert("Computer hit")
}
if(cbs1 && cbs2 && cbs3 && cbs4)
{
alert("Computer sunk battleship")
cship++;
sunk=true;
insultship();
}
else
sunk=false;
}
if(!cbs4)
{
if(v==cBattleship4)
{
cbs4 = true;
test = true;
alert("computer hit")
}
if(cbs1 && cbs2 && cbs3 && cbs4)
{
alert("Computer sunk battleship")
cship++;
sunk=true;
insultship();
}
else
sunk=false;
}
return test;
}
function ccarrier(v)
{
var test=false;
if(!cca1)
{
if(v==cCarrier1)
{
cca1 = true;
test = true;
alert("Computer hit")
}
if(cca1 && cca2 && cca3 && cca4 && cca5)
{
alert("Computer sunk Carrier")
cship++;
sunk=true;
insultship();
}
else
sunk=false;
}
if(!cca2)
{
if(v==cCarrier2)
{
cca2 = true;
test = true;
alert("Computer hit")
}
if(cca1 && cca2 && cca3 && cca4 && cca5)
{
alert("Computer sunk Carrier")
cship++;
sunk=true;
insultship();
}
else
sunk=false;
}
if(!cca3)
{
if(v==cCarrier3)
{
cca3 = true;
test = true;
alert("Computer hit")
}
if(cca1 && cca2 && cca3 && cca4 && cca5)
{
alert("Computer sunk Carrier")
cship++;
sunk=true;
insultship();
}
else
sunk=false;
}
if(!cca4)
{
if(v==cCarrier4)
{
cca4 = true;
test = true;
alert("Computer hit")
}
if(cca1 && cca2 && cca3 && cca4 && cca5)
{
alert("Computer sunk Carrier")
cship++;
sunk=true;
insultship();
}
else
sunk=false;
}
if(!cca5)
{
if(v==cCarrier5)
{
cca5 = true;
test = true;
alert("Computer hit")
}
if(cca1 && cca2 && cca3 && cca4 && cca5)
{
alert("Computer sunk Carrier")
cship++;
insultship();
sunk=true;
}
else
sunk=false;
}
return test;
}
<!-- end of computer's ship functions--!>
<!-- Insulting functiong --!>
function insult()
{
var rand = Math.round(Math.random()*2); //generate 3 random numbers
if (ship > cship)
{
if(shot > miss)
{
if(rand==0)
alert("I think you are cheating");
if(rand==1)
alert("I bet you 3 bytes that I win.....");
if(rand==2)
alert("I will show you my ship coordinate if you press ctrl + R");
}
else
{
if(rand==0)
alert("I think you will need more than luck to win...");
if(rand==1)
alert("Out of 100 possible selections you just had to pick that one....");
if(rand==2)
alert("So.... I hear there is ship in I9....");
}
}
if (cship > ship)
{
if(miss > shot)
{
if(rand==0)
alert("I guess you are made by Microsoft.... You make to many mistakes....");
if(rand==1)
alert("Don't worry mate, mistakes happen.... ");
if(rand==2)
alert("Who taught you how to aim? Dick Cheny?? ");
}
else
{
if(rand==0)
alert("I bet blind men have better aim than you");
if(rand==1)
alert("Oy mate, are you durnk?");
if(rand==2)
alert("Because of people like you, we spend hundreds of millions of dollars to improve our military technology");
}
}
if (ship == cship)
{
alert("Thank god you are not on my side");
}
}
<!-- end --!>
<!-- insult as ship is sunk --!>
function insultship()
{
if (ship == cship)
{
alert("We are tied now... But I have sunk more strategic ships... I'm still ahead of you")
}
else if((cship - ship) == 4)
{
alert("ERROR, User is stupid..... ")
}
else if((ship - cship) == 4 && ship != 5)
{
alert("So.... have you pressed Alt + f4 yet?? I wonder what happens...")
}
else if((cship - ship) == 3)
{
alert("I don't even have a phrase for the humiliation you are facing....")
}
else if((ship - cship) == 3)
{
alert("Whatever you do.... Don't press Alt + f4")
}
else if((cship - ship) == 2)
{
alert("It's about time to start questioning your genetic make up....")
}
else if((ship - cship) == 2)
{
alert("I really do think you are cheating....")
}
else if((cship - ship) == 1)
{
alert("I have a feeling this is going to be easy")
}
else if((ship - cship) == 1)
{
alert("Eh.... It don't matter.... ")
}
else if(ship == 5)
{
alert("I wish my developer didn't have a CSI test to study for so he could spend more time on this game.... ")
}
}
<!-- end --!>
<!-- function that change color of a cell chosen by player --!>
function colorcell(v)
{
var r=v.split('')[0]*1; //first digit is row
var c=v.split('')[1]*1; //2nd digit is colum
if (submarine(v)||minesweeper(v)||frigate(v)||battleship(v)||carrier(v)) //test if coordinate match any coordinates of ships
{
shot++; //increment on player hit
document.getElementById('player').rows[r].cells[c].style.backgroundColor="#FF0000"; //color that cell to red
}
else
{
miss++; //increment on player miss
document.getElementById('player').rows[r].cells[c].style.backgroundColor="00066"; //color that cell to blue
insult();
computerAI(); //fire computerAI function
}
if(ship==5) //if all 5 ships are destroyed
{
alert("You've won the game!!")
}
}
<!-- end of player's color functions--!>
<!-- computer's AI. --!>
<!-- if it's previously hit before and didn't fully destroyed a ship, fire function --!>
<!-- that recalls saved coordinate, else draw randomnumber array numerically. --!>
function computerAI()
{
var unique = true //initialize uniqueness of number to true
if(hit == true) //if previously destroyed a portion of ship but yet to destroy full ship
{
chit(save); //call chit function with saved coordinate
}
if(hit == false) //if previously fail to hit target
{
v = number[n++] //draw a random number and increment array's sub number
for(x = 0; x<1000; x++) //duplicate eliminator
{
if(used[x]==v) //if any used data match selected coordinate
{
computerAI(); //recall itself (since array's sub number is incremented previously it select next number)
unique = false; //when found duplicate, uniqueness is false
}
}
if(v >= 100 || v <= 0) //when chit or momentum function over load.
{
computerAI();
unique = false;
}
if (unique) //when number is unquie
{
if(ccolorcell(v)) //if computer successfully hit a portion of ship
{
chit(v); //call chit function with coordinate
}
}
}
if(cship==5) //if computer fully destroyed all 5 ships
{
alert("Computer won the game!!")
}
}
<!-- end of computer's AI functions--!>
<!-- computer's coloring functions--!>
<!-- if there is ship present in coordinate, color red, else, color blue --!>
<!-- return true if there is ship present in coordinate --!>
function ccolorcell(v)
{
var test=false //test variable initialize to false
used[m++] = v; //save coordinate to used number array
if(v<10) //if coordinate is < 10 (because coordinates are stored as '01', '04', or ect)
{
r = 0; //row is 0
c = v / 1; //colum is coordinate / 1
}
else
{
r = (v - (v % 10)) / 10; //row is (coordinate - 2nd digit) / 10
c = v % 10; //colum is remainder of coordinate / 10
}
if (csubmarine(v)||cminesweeper(v)||cfrigate(v)||cbattleship(v)||ccarrier(v)) //if there is any matching coordinate
{
document.getElementById('computer').rows[r].cells[c].style.backgroundColor="#FF0000"; //color it to red
test=true; //computer hit a target
}
else
{
document.getElementById('computer').rows[r].cells[c].style.backgroundColor="00066"; //color it to blue
}
return test; //return it's test result
}
<!-- end computer's color function --!>
<!-- computer's hit function --!>
<!-- when previously destroyed a portion of ship and yet to destroy full ship --!>
function chit(v)
{
var unique = true //initialize uniqueness of number to true
save=v; //save successfull coordinate
var ran= Math.round(Math.random()*3); //randomly pick 0 ~ 3
if(ran==0)
{
v++; //select right cell
if(v % 10 == 0) //when selcted coordinate increment row
{
unique = false;
chit(save);
}
}
if(ran==1)
{
v--; //select left cell
if(v % 10 == 9) //when selected coordinate decrement row
{
unique = false;
chit(save);
}
}
if(ran==2)
v+=10; //select upper cell
if(ran==3)
v-=10; //select lower cell
for(x = 0; x<1000; x++) //dupicate eliminator
{
if(used[x]==v) //if any of used data match current coordinate
{
chit(save); //recall it self with saved data
unique = false; //when duplicate found, uniqueness is false
}
}
if(v >= 100 || v <= 0)
{
chit(save);
unique = false;
}
if (unique)
{
if(ccolorcell(v))//if there is a ship in coordinate
{
if(sunk) //if ship is fully destroyed
{
hit=false; //initialize partialy destoryed ship since that ship is fully destroyed
computerAI(); //call computerAI function so it can randomly select a cell
exit;
}
else
{
momentum(v,ran)
}
}
else
hit=true; //if miss it is true that there is partially destroyed but yet to fully destroyed ship
}
}
<!-- end computer's hit function --!>
<!-- momentum function --!>
<!-- if computer found ship in a cell next to previously selected cell, --!>
<!-- target cell in same direction --!>
function momentum(v,ran)
{
var safe = v; //safe coordinate before modification of coordinate that possibly a bad choice
var unique = true //initialize uniqueness of number to true
if(ran==0)
{
v++; //select right cell, which is same direction as before
if(v % 10 == 0) //when coordinate is 9 and selcted coordinate increment row
{
unique = false;
chit(save);
}
}
if(ran==1)
{
v--; //select left cell, which is same direction as before
if(v % 10 == 9) //wheen coordinate is 0 and selected coordinate decrment row
{
unique = false;
chit(save);
}
}
if(ran==2)
v+=10; //select upper cell, which is same direction as before
if(ran==3)
v-=10; //select lower cell, which is same direction as before
for(x = 0; x<1000; x++) //duplicate eliminator
{
if(used[x]==v) //if any used data match selected coordinate
{
chit(save); //call chit with saved data
unique = false //when found duplicate, uniqueness become false
}
}
if(v >= 100 || v <= 0) // when computer selct cell that is out of table
{
momentum(safe,ran);
unique = false;
}
if (unique)
{
if(!ccolorcell(v))
{
hit = true; //if miss it is true that there is partially destroyed but yet to fully destroyed ship
}
else
{
if(sunk) //if a ship is fully destroyed
{
hit=false; //partialy destroyed ship is non existance
computerAI(); //call computer AI function
exit;
}
if(!sunk)
{
momentum(v,ran); //if a ship isn't fully destroyed recall it self with current coordinate and direction
}
}
}
}
<!-- end of momentum function --!>
</script>
</body>
<body onload="load()" background="ship.jpg">
<center>
<table border="1" width = "650" cellpadding = "10">
<tr>
<td><center>
<h1>Battleship game</h1><br>
Author : Alka_Vulcan<br>
Contact: yake_2002 (at) hotmail (dot) com<br>
</center>
Rulez : <br>
<b>Player's turn <br></b>
Player make a guess on which cell may have a portion of ship present in computer's table<br>
if a portion of computer's ship is present in player's guessed cell, it will turn red, and player go again.<br>
else cell will be blue and computer's turn.<br>
<br>
<b>Computer's turn <br></b>
computer will randomly guess any cell in table<br>
if a portion of player's ship is present in computer's guessed cell, it will turn red and computer will go again,<br>
else cell will e blue and players turn.<br>
<font color = "white"> variable "ship" is a variable that determines player's victory, if ship == numbers of ships on table don't you think you wil win?
<br><br>
</td>
</tr>
</table>
</center>
<h1>Computer's map</h1>
<table id="player" width="300" border="1" bgcolor='0077ff'>
<tr>
<td onClick="colorcell('00'); this.disabled=true"> </td>
<td onClick="colorcell('01'); this.disabled=true"> </td>
<td onClick="colorcell('02'); this.disabled=true"> </td>
<td onClick="colorcell('03'); this.disabled=true"> </td>
<td onClick="colorcell('04'); this.disabled=true"> </td>
<td onClick="colorcell('05'); this.disabled=true"> </td>
<td onClick="colorcell('06'); this.disabled=true"> </td>
<td onClick="colorcell('07'); this.disabled=true"> </td>
<td onClick="colorcell('08'); this.disabled=true"> </td>
<td onClick="colorcell('09'); this.disabled=true"> </td>
<td>a</td>
</tr>
<tr>
<td onClick="colorcell('10'); this.disabled=true"> </td>
<td onClick="colorcell('11'); this.disabled=true"> </td>
<td onClick="colorcell('12'); this.disabled=true"> </td>
<td onClick="colorcell('13'); this.disabled=true"> </td>
<td onClick="colorcell('14'); this.disabled=true"> </td>
<td onClick="colorcell('15'); this.disabled=true"> </td>
<td onClick="colorcell('16'); this.disabled=true"> </td>
<td onClick="colorcell('17'); this.disabled=true"> </td>
<td onClick="colorcell('18'); this.disabled=true"> </td>
<td onClick="colorcell('19'); this.disabled=true"> </td>
<td>b</td>
</tr>
<tr>
<td onClick="colorcell('20'); this.disabled=true"> </td>
<td onClick="colorcell('21'); this.disabled=true"> </td>
<td onClick="colorcell('22'); this.disabled=true"> </td>
<td onClick="colorcell('23'); this.disabled=true"> </td>
<td onClick="colorcell('24'); this.disabled=true"> </td>
<td onClick="colorcell('25'); this.disabled=true"> </td>
<td onClick="colorcell('26'); this.disabled=true"> </td>
<td onClick="colorcell('27'); this.disabled=true"> </td>
<td onClick="colorcell('28'); this.disabled=true"> </td>
<td onClick="colorcell('29'); this.disabled=true"> </td>
<td>c</td>
</tr>
<tr>
<td onClick="colorcell('30'); this.disabled=true"> </td>
<td onClick="colorcell('31'); this.disabled=true"> </td>
<td onClick="colorcell('32'); this.disabled=true"> </td>
<td onClick="colorcell('33'); this.disabled=true"> </td>
<td onClick="colorcell('34'); this.disabled=true"> </td>
<td onClick="colorcell('35'); this.disabled=true"> </td>
<td onClick="colorcell('36'); this.disabled=true"> </td>
<td onClick="colorcell('37'); this.disabled=true"> </td>
<td onClick="colorcell('38'); this.disabled=true"> </td>
<td onClick="colorcell('39'); this.disabled=true"> </td>
<td>d</td>
</tr>
<tr>
<td onClick="colorcell('40'); this.disabled=true"> </td>
<td onClick="colorcell('41'); this.disabled=true"> </td>
<td onClick="colorcell('42'); this.disabled=true"> </td>
<td onClick="colorcell('43'); this.disabled=true"> </td>
<td onClick="colorcell('44'); this.disabled=true"> </td>
<td onClick="colorcell('45'); this.disabled=true"> </td>
<td onClick="colorcell('46'); this.disabled=true"> </td>
<td onClick="colorcell('47'); this.disabled=true"> </td>
<td onClick="colorcell('48'); this.disabled=true"> </td>
<td onClick="colorcell('49'); this.disabled=true"> </td>
<td>e</td>
</tr>
<tr>
<td onClick="colorcell('50'); this.disabled=true"> </td>
<td onClick="colorcell('51'); this.disabled=true"> </td>
<td onClick="colorcell('52'); this.disabled=true"> </td>
<td onClick="colorcell('53'); this.disabled=true"> </td>
<td onClick="colorcell('54'); this.disabled=true"> </td>
<td onClick="colorcell('55'); this.disabled=true"> </td>
<td onClick="colorcell('56'); this.disabled=true"> </td>
<td onClick="colorcell('57'); this.disabled=true"> </td>
<td onClick="colorcell('58'); this.disabled=true"> </td>
<td onClick="colorcell('59'); this.disabled=true"> </td>
<td>f</td>
</tr>
<tr>
<td onClick="colorcell('60'); this.disabled=true"> </td>
<td onClick="colorcell('61'); this.disabled=true"> </td>
<td onClick="colorcell('62'); this.disabled=true"> </td>
<td onClick="colorcell('63'); this.disabled=true"> </td>
<td onClick="colorcell('64'); this.disabled=true"> </td>
<td onClick="colorcell('65'); this.disabled=true"> </td>
<td onClick="colorcell('66'); this.disabled=true"> </td>
<td onClick="colorcell('67'); this.disabled=true"> </td>
<td onClick="colorcell('68'); this.disabled=true"> </td>
<td onClick="colorcell('69'); this.disabled=true"> </td>
<td>g</td>
</tr>
<tr>
<td onClick="colorcell('70'); this.disabled=true"> </td>
<td onClick="colorcell('71'); this.disabled=true"> </td>
<td onClick="colorcell('72'); this.disabled=true"> </td>
<td onClick="colorcell('73'); this.disabled=true"> </td>
<td onClick="colorcell('74'); this.disabled=true"> </td>
<td onClick="colorcell('75'); this.disabled=true"> </td>
<td onClick="colorcell('76'); this.disabled=true"> </td>
<td onClick="colorcell('77'); this.disabled=true"> </td>
<td onClick="colorcell('78'); this.disabled=true"> </td>
<td onClick="colorcell('79'); this.disabled=true"> </td>
<td>h</td>
</tr>
<tr>
<td onClick="colorcell('80'); this.disabled=true"> </td>
<td onClick="colorcell('81'); this.disabled=true"> </td>
<td onClick="colorcell('82'); this.disabled=true"> </td>
<td onClick="colorcell('83'); this.disabled=true"> </td>
<td onClick="colorcell('84'); this.disabled=true"> </td>
<td onClick="colorcell('85'); this.disabled=true"> </td>
<td onClick="colorcell('86'); this.disabled=true"> </td>
<td onClick="colorcell('87'); this.disabled=true"> </td>
<td onClick="colorcell('88'); this.disabled=true"> </td>
<td onClick="colorcell('89'); this.disabled=true"> </td>
<td>i</td>
</tr>
<tr>
<td onClick="colorcell('90'); this.disabled=true"> </td>
<td onClick="colorcell('91'); this.disabled=true"> </td>
<td onClick="colorcell('92'); this.disabled=true"> </td>
<td onClick="colorcell('93'); this.disabled=true"> </td>
<td onClick="colorcell('94'); this.disabled=true"> </td>
<td onClick="colorcell('95'); this.disabled=true"> </td>
<td onClick="colorcell('96'); this.disabled=true"> </td>
<td onClick="colorcell('97'); this.disabled=true"> </td>
<td onClick="colorcell('98'); this.disabled=true"> </td>
<td onClick="colorcell('99'); this.disabled=true"> </td>
<td>j</td>
</tr>
<tr>
<td>01</td>
<td>02</td>
<td>03</td>
<td>04</td>
<td>05</td>
<td>06</td>
<td>07</td>
<td>08</td>
<td>09</td>
<td>10</td>
<td></td>
</tr>
</table>
</body>
<body>
<br>
<h1>Players's map</h1>
<table id="computer" width="300" border="1" bgcolor="0077ff">
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>a</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>b</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>c</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>d</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>e</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>f</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>g</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>h</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>i</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td>j</td>
</tr>
<tr>
<td>01</td>
<td>02</td>
<td>03</td>
<td>04</td>
<td>05</td>
<td>06</td>
<td>07</td>
<td>08</td>
<td>09</td>
<td>10</td>
<td></td>
</tr>
</body>
</html>