Code:
arrOpenentGrid = [];
arrPlayerGrid = [];
arrShipLengths = [2,3,3,4,5];
arrAiCrossArrow = [];
arrAiShipNames = [
{Piece: "Patrol Boat", CoOrdinates: [], Destroyed: 0, Total: 0},
{Piece: "Destroyer", CoOrdinates: [], Destroyed: 0},
{Piece: "Submarine", CoOrdinates: [], Destroyed: 0},
{Piece: "Battleship", CoOrdinates: [], Destroyed: 0},
{Piece: "Aircraft Carrier", CoOrdinates: [], Destroyed: 0}
];
arrPlayerShipNames = [
{Piece: "Patrol Boat", CoOrdinates: [], Destroyed: 0, Total: 0},
{Piece: "Destroyer", CoOrdinates: [], Destroyed: 0},
{Piece: "Submarine", CoOrdinates: [], Destroyed: 0},
{Piece: "Battleship", CoOrdinates: [], Destroyed: 0},
{Piece: "Aircraft Carrier", CoOrdinates: [], Destroyed: 0}
];
intGameLevel = 1;
elPlayerTile = "";
blAiIsActive = false;
blOnline = false;
blGameActive = true;
blAiOrientationSet = false;
blAiReverse = false;
blAiPlaying = true;
intAi_LastHit = -1;
intAiAttackedNum = 0;
intAiShipHit = 0;
for (i = 0; i<=99; i++){
arrOpenentGrid.push (i);
arrPlayerGrid.push(i);
}
if (intGameLevel > 1){
for (i = 0; i <=99; i = i +2){
arrPlayerGrid.splice(i, 1);
}
}
for (i = 0; i<=arrShipLengths.length-1; i++){
funAiShips();
funPlayerShips();
}
console.log ("Game: AI co-ordinates are set (" + arrAiShipNames[0].Total + ")");
console.log ("Game: Player co-ordinates are set (" + arrPlayerShipNames[0].Total + ")");
intTotalShipTiles = 0;
for (a = 0; a < arrShipLengths.length; a++){
intTotalShipTiles += arrShipLengths[a];
}
function funGridMaker(){
for (p = 1; p <= 10; p++){
gridOpenent = document.getElementById("Openent" + p);
gridPlayer = document.getElementById("Player" + p);
for (i = 0; i <10; i++){
gridOpenent.innerHTML += "<div id='openentTile" + (p-1) + "" + i + "' class='grid' onclick='funPlayersTurn("+ (p-1) + "" + i + ", this)'><div class='cubeno'>" + (p-1) + "" + i +"</div></div>";
gridPlayer.innerHTML += "<div id='playerTile" + (p-1) + "" + i + "' class='grid'><div class='cubeno'>" + (p-1) + "" + i +"</div></div>";
}
}
funShowPlayerPieces(true);
}
function funPlayerShips(blHuman = false){
arrShipCoOrdinates = funCreateShipCoOrdinates(i);
funValidateShip(arrShipCoOrdinates, blHuman);
}
function funAiShips(blHuman = true){
arrShipCoOrdinates = funCreateShipCoOrdinates(i);
funValidateShip(arrShipCoOrdinates, blHuman);
}
function funCreateShipCoOrdinates(intShipNo){
blOrientation = parseInt(Math.random()* 100) % 2;
intBoardPossition = parseInt(Math.random() * 50);
intShipSize = arrShipLengths[intShipNo];
arrShip =[];
if (blOrientation == 1){
intIncrement = 10;
}else{
intIncrement = 1;
}
if (blOrientation ==0){
intSpareVerticalTiles = 0;
intSpareVerticalTiles = 9 - (intBoardPossition % 10);
if (intSpareVerticalTiles < intShipSize){
console.log ("Vertical ship spans 2 columns, redrawing new co-ordinates");
funCreateShipCoOrdinates(intShipNo);
return;
}
}
for (Q = 1; Q <= intShipSize; Q++){
if(intIncrement ==0){
if (9 - (intBoardPossition % 10) < intShipSize){
arrShip = funCreateShipCoOrdinates();
return;
}
}
arrShip.push (intBoardPossition + (Q * intIncrement));
}
intTestValue = arrShip[0];
if (typeof(intTestValue) == "undefined"){
arrShip = funCreateShipCoOrdinates(intShipNo);
return arrShip;
}
return arrShip;
}
function funValidateShip(arrShipCoOrdinates, blHuman = true){
console.log ("Validating ship co-ordinates: " + arrShipCoOrdinates);
if (Math.max.apply(null, arrShipCoOrdinates) > 100){
if (blHuman){
funPlayerShips();
}else{
funAiShips();
}
console.log ("Game: Ship co-ordinates are out of range");
return;
}
if (Math.min.apply(null, arrShipCoOrdinates) < 0){
if (blHuman){
funPlayerShips();
}else{
funAiShips();
}
console.log ("Game: Ship co-ordinates are out of range");
return;
}
while(typeof(arrShipCoOrdinates)=="undefined"){
arrShipCoOrdinates = funCreateShipCoOrdinates(i);
}
for (h = 0; h <= arrShipCoOrdinates.length-1; h++){
if (blHuman){
for (z = 0; z < arrShipLengths.length; z++){
if (arrAiShipNames[z].CoOrdinates.indexOf(arrShipCoOrdinates[h]) > -1){
console.log ("Game: Ship generated overlaps a (" + arrAiShipNames[z].Piece + ") for the AI, redrawing new co-ordinates");
funAiShips();
return;
}
}
}else{
for (z = 0; z < arrShipLengths.length; z++){
if (arrPlayerShipNames[z].CoOrdinates.indexOf(arrShipCoOrdinates[h]) > -1){
console.log ("Game: Ship generated overlaps a (" + arrPlayerShipNames[z].Piece + ") for the player, redrawing new co-ordinates");
funPlayerShips();
return;
}
}
}
}
console.log ("Game: Assigning Ship ("+arrAiShipNames[i].Piece + ") co-ordinates");
for (Q = 0; Q <= arrShipCoOrdinates.length-1; Q++){
if (blHuman){
arrAiShipNames[i].CoOrdinates = arrShipCoOrdinates;
}else{
arrPlayerShipNames[i].CoOrdinates = arrShipCoOrdinates;
}
}
if (blHuman){
arrAiShipNames[0].Total += arrShipCoOrdinates.length;
}else{
arrPlayerShipNames[0].Total += arrShipCoOrdinates.length;
}
}
function funAttemptAIMove(){
if (blAiIsActive){
if (blAiOrientationSet){
intAiAttackedNum = intLastAIMove - intAiAttackToken;
if (intAiAttackedNum > 100 || intAiAttackedNum < 0 || funAiMoveCheck(intAiAttackedNum) == false){
blAiOrientationSet = false;
if (arrAICrossArrow.length == 0){
funDisableAI();
}
funAttemptAIMove();
return;
}
console.log ("AI: To check the other side of ship next");
}else{
blCrossArrow = true;
intAiCrossArrowIndex = parseInt(Math.random() * (arrAICrossArrow.length -1));
intAiAttackedNum = arrAICrossArrow[intAiCrossArrowIndex];
arrAICrossArrow.splice(intAiCrossArrowIndex,1);
if (typeof(intAiAttackedNum) == "undefined"){
funDisableAI();
funAttemptAIMove();
return;
}
}
}else{
blCrossArrow = false;
intAiAttackedNum = parseInt(Math.random() * (arrPlayerGrid.length -1));
intAiAttackedNum = arrPlayerGrid[intAiAttackedNum];
}
console.log ("AI: Attacking tile " + intAiAttackedNum);
if (intAiAttackedNum < 10){
stBoardTile = "0" + intAiAttackedNum;
}else{
stBoardTile = intAiAttackedNum;
}
elPlayerTile = document.getElementById("playerTile" + stBoardTile);
intAiShipHit = -1;
intAiShipHitArrRef = funHitCheck();
if (intAiShipHit > -1){
funAiHit(intAiAttackedNum, intAiShipHitArrRef);
}else{
funAiMiss(intAiAttackedNum);
}
if (arrPlayerShipNames[0].Total == 0){
funGameOver("AI");
}
return;
}
function funHitCheck(){
for (z = 0; z < arrShipLengths.length; z++){
if (arrPlayerShipNames[z].CoOrdinates.indexOf(intAiAttackedNum) > -1){
intAiShipHit = arrPlayerShipNames[z].CoOrdinates.indexOf(intAiAttackedNum);
return z;
}
}
return -1;
}
function funAiHit(intHitNum, intAiShipHitArrRef){
elPlayerTile.className = "grid hit";
console.log ("AI: Hit");
// Remove played tile
console.log ("AI: Calculated moves activated");
if (blAiIsActive){
if(blAiOrientationSet == false){
intAiAttackToken = intLastAIMove - intAiAttackedNum;
for (i = 0; i < arrAICrossArrow.length; i++){
if (arrAICrossArrow[i] != (intLastAIMove + intAiAttackToken)){
arrAICrossArrow.splice(i,1);
}
}
if (intAiAttackToken == -1 || intAiAttackToken==1){
console.log ("AI: Orientation determined as vertical");
}else{
console.log ("AI: Orientation determined as horizontal");
}
intTestValue = arrAICrossArrow[0];
if (typeof(intTestValue) != "undefined" ){
blAiOrientationSet = true;
}
}
intLastAIMove = intAiAttackedNum;
}else{
arrAICrossArrow = [];
intLastAIMove = intAiAttackedNum;
if (arrPlayerGrid.indexOf(intAiAttackedNum -1) > -1){
if ((intAiAttackedNum % 10) != 0){
arrAICrossArrow.push(intAiAttackedNum - 1);
}
}
if (arrPlayerGrid.indexOf(intAiAttackedNum + 1) > -1){
if(((intAiAttackedNum +1) % 10) !=0){
arrAICrossArrow.push(intAiAttackedNum + 1);
}
}
if (arrPlayerGrid.indexOf(intAiAttackedNum - 10) > -1){
arrAICrossArrow.push(intAiAttackedNum - 10);
}
if (arrPlayerGrid.indexOf(intAiAttackedNum + 10) > -1){
arrAICrossArrow.push(intAiAttackedNum + 10);
}
console.log ("AI: All possible neighboring tiles configured");
blAiIsActive = true;
}
intHitIndex = arrPlayerGrid.indexOf(intHitNum);
arrPlayerGrid.splice(intHitIndex,1);
intHitIndex = arrPlayerShipNames[intAiShipHitArrRef].CoOrdinates.indexOf(intHitNum);
arrPlayerShipNames[intAiShipHitArrRef].CoOrdinates.splice(intHitIndex, 1);
if (arrPlayerShipNames[intAiShipHitArrRef].CoOrdinates.length == 0){
arrPlayerShipNames[intAiShipHitArrRef].Destroyed = 1;
console.log ("Ai: has destroyed the Player's " + arrPlayerShipNames[intAiShipHitArrRef].Piece);
}
arrPlayerShipNames[0].Total -= 1;
setTimeout(funAisTurn, 1000);
return;
}
function funAiMiss(intMissNum){
elPlayerTile.className = "grid miss";
console.log ("AI: Missed");
console.log ("Player: Waiting for player to select a tile");
intMissIndex = arrPlayerGrid.indexOf(intMissNum);
arrPlayerGrid.splice(intMissIndex,1);
if (blAiIsActive && blAiOrientationSet){
blAiReverse = true;
}else{
blAiReverse = false;
}
blGameActive = true;
blAiOrientationSet = false;
return;
}
function funAiMoveCheck(intBoardNumber){
intCheckBoardNumber = arrPlayerGrid.indexOf(intBoardNumber);
if(intCheckBoardNumber == -1){
return false;
}else{
return true;
}
}
function funAisTurn(){
coords = funAttemptAIMove()
}
function funPlayersTurn(coords, el){
if (blGameActive == false){
console.log ("Player: Move rejected, reason: Not players turn or game has ended");
return;
}
missedCheck = arrOpenentGrid.indexOf(coords);
if (missedCheck == -1){
console.log ("Player: Selected a played tile, waiting for player to choose another tile");
return;
}
blHit = false;
for (x=0; x< arrShipLengths.length; x++){
ShipsCheck = arrAiShipNames[x].CoOrdinates.indexOf(coords);
if ( ShipsCheck > -1){
el.className = "grid hit";
console.log ("Player: opponent ship hit");
arrAiShipNames[x].CoOrdinates.splice(ShipsCheck, 1)
arrAiShipNames[0].Total -= 1;
if (arrAiShipNames[x].CoOrdinates.length == 0){
arrAiShipNames[x].Destroyed = 1;
console.log ("Player: has destroyed the AI's " + arrAiShipNames[x].Piece);
}
blHit = true;
el = document.getElementById("PlayerCompleted");
el.style.width = 100 - ((arrAiShipNames[0].Total / intTotalShipTiles)* 100) + "%";
}
}
if (missedCheck > -1 && blHit == false){
el.className = "grid miss";
console.log ("Player: Missed");
if (blAiPlaying) {
blGameActive = false;
console.log ("Game: AIs Turn");
setTimeout(funAisTurn, 1000);
}
}
arrOpenentGrid.splice(missedCheck,1);
if (arrAiShipNames[0].Total ==0){
funGameOver("Player");
}
}
function funDisableAI(){
blAiIsActive = false;
blAiOrientationSet = false;
console.log ("AI: Calculated moves deactivated");
}
function funShowPlayerPieces(blDisplay = false){
if (blDisplay == false){return;}
for (i = 0; i < arrPlayerShipNames.length; i++){
for (x = 0; x < arrPlayerShipNames[i].CoOrdinates.length; x++){
intTileNum = arrPlayerShipNames[i].CoOrdinates[x];
if (intTileNum < 10){stTileNum = "0" + intTileNum;}else{stTileNum = intTileNum;}
el = document.getElementById('playerTile' + stTileNum);
if (typeof(el) != undefined){
el.className = "grid playerShip";
}
}
}
return;
}
function funGameOver(stPlayer){
alert ("Game Over: " + stPlayer + " Won!");
blGameActive = false;
console.log (stPlayer + ": has won the game");
return;
}
document.addEventListener("DOMContentLoaded", funGridMaker);
/*
Code developer: Adam MacKay
Available for free as posted on Dynamic Drive
*/
Bookmarks