战舰游戏
/Js教程作业/
感觉就是个低级的扫雷
初始版
相当于一个猜数游戏,0到6(范围)的数中随机产生3个数字,猜中就相当于命中目标,全部猜中即获胜,游戏结束后计算击中概率。
简易页面
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Zhanjian</title>
</head>
<body>
<h1>打击战舰</h1>
</body>
<script src="zhanjian.js">
</script>
</html>
简易Js
var randomLoc=Math.floor(Math.random()*5);
var location1=randomLoc;
var location2=randomLoc+1;
var location3=randomLoc+2;
var location11=location1;
var location21=location2;
var location31=location3;
var guess;
var hits=0;
var guesses=0;
var isSunk=false;
while(isSunk==false){
guess=prompt("预备瞄准发射(输入一个0-6的数字):");
if(guess<0||guess>6){
alert("输入数字超出了范围");
}
else{
guesses=guesses+1;
if(guess==location1&&guess==location11){location11=7;hits=hits+1;alert("您击中了!")}
else if(guess==location2&&guess==location21){location21=7;hits=hits+1;alert("您击中了!")}
else if(guess==location3&&guess==location31){location31=7;hits=hits+1;alert("您击中了!")}
else if(guess==location1||guess==location2||guess==location3){alert("您已击中该目标");}
else{alert("未击中");}
if(hits==3){isSunk=true;
alert("你已经全部击沉");}
}
}
var stats="你猜测了"+guesses+"次"+"命中率为:"+(3/guesses);
alert(stats);
完整版
利用给到的图片在页面上划分有效信息区,简单说就是对着图片格子建立一个表格,每个格子对应一个位置信息即数学中的横纵坐标,击中原理与简易版相同
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Battleship</title>
<style>
body {
background-color: black;
}
div#board {
position: relative;
width: 1024px;
height: 863px;
margin: auto;
background: url("board.jpg") no-repeat;
}
div#messageArea {
position: absolute;
top: 0px;
left: 0px;
color: rgb(83, 175, 19);
}
.hit {
background: url("ship.png") no-repeat center center;
}
.miss {
background: url("miss.png") no-repeat center center;
}
table {
border-spacing: 0px;
/* could use border-collapse instead */
/* border-collapse: collapse; */
position: absolute;
left: 173px;
top: 98px;
}
td {
width: 94px;
height: 94px;
}
form {
position: absolute;
bottom: 0px;
right: 0px;
padding: 15px;
background-color: rgb(83, 175, 19);
}
form input {
background-color: rgb(152, 207, 113);
border-color: rgb(83, 175, 19);
font-size: 1em;
}
</style>
</head>
<body>
<div id="board">
<div id="messageArea"></div>
<table>
<tr>
<td id="00"></td> <td id="01"></td> <td id="02"></td> <td id="03"></td>
<td id="04"></td> <td id="05"></td> <td id="06"></td>
</tr>
<tr>
<td id="10"></td> <td id="11"></td> <td id="12"></td> <td id="13"></td>
<td id="14"></td> <td id="15"></td> <td id="16"></td>
</tr>
<tr>
<td id="20"></td> <td id="21"></td> <td id="22"></td> <td id="23"></td>
<td id="24"></td> <td id="25"></td> <td id="26"></td>
</tr>
<tr>
<td id="30"></td> <td id="31"></td> <td id="32"></td> <td id="33"></td>
<td id="34"></td> <td id="35"></td> <td id="36"></td>
</tr>
<tr>
<td id="40"></td> <td id="41"></td> <td id="42"></td> <td id="43"></td>
<td id="44"></td> <td id="45"></td> <td id="46"></td>
</tr>
<tr>
<td id="50"></td> <td id="51"></td> <td id="52"></td> <td id="53"></td>
<td id="54"></td> <td id="55"></td> <td id="56"></td>
</tr>
<tr>
<td id="60"></td> <td id="61"></td> <td id="62"></td> <td id="63"></td>
<td id="64"></td> <td id="65"></td> <td id="66"></td>
</tr>
</table>
<form>
<input type="text" id="guessInput" placeholder="A0">
<input type="button" id="fireButton" value="Fire!">
</form>
</div>
<script src="battleship.js">
</script>
</body>
</html>
Js如下
var model = {
boardSize: 7,
numShips: 3,
shipLength: 3,
shipsSunk: 0,
ships: [
{ locations: [0, 0, 0], hits: ["", "", ""] },
{ locations: [0, 0, 0], hits: ["", "", ""] },
{ locations: [0, 0, 0], hits: ["", "", ""] }
],
/*
ships: [
{ locations: ["06", "16", "26"], hits: ["", "", ""] },
{ locations: ["24", "34", "44"], hits: ["", "", ""] },
{ locations: ["10", "11", "12"], hits: ["", "", ""] }
],
*/
fire: function(guess) {
for (var i = 0; i < this.numShips; i++) {
var ship = this.ships[i];
var index = ship.locations.indexOf(guess);
if (ship.hits[index] === "hit") {
view.displayMessage("Oops, you already hit that location!");
return true;
} else if (index >= 0) {
ship.hits[index] = "hit";
view.displayHit(guess);
view.displayMessage("HIT!");
if (this.isSunk(ship)) {
view.displayMessage("You sank my battleship!");
this.shipsSunk++;
}
return true;
}
}
view.displayMiss(guess);
view.displayMessage("You missed.");
return false;
},
isSunk: function(ship) {
for (var i = 0; i < this.shipLength; i++) {
if (ship.hits[i] !== "hit") {
return false;
}
}
return true;
},
generateShipLocations: function() {
var locations;
for (var i = 0; i < this.numShips; i++) {
do {
locations = this.generateShip();
} while (this.collision(locations));
this.ships[i].locations = locations;
}
console.log("Ships array: ");
console.log(this.ships);
},
generateShip: function() {
var direction = Math.floor(Math.random() * 2);
var row, col;
if (direction === 1) {
row = Math.floor(Math.random() * this.boardSize);
col = Math.floor(Math.random() * (this.boardSize - this.shipLength + 1));
} else {
row = Math.floor(Math.random() * (this.boardSize - this.shipLength + 1));
col = Math.floor(Math.random() * this.boardSize);
}
var newShipLocations = [];
for (var i = 0; i < this.shipLength; i++) {
if (direction === 1) {
newShipLocations.push(row + "" + (col + i));
} else {
newShipLocations.push((row + i) + "" + col);
}
}
return newShipLocations;
},
collision: function(locations) {
for (var i = 0; i < this.numShips; i++) {
var ship = this.ships[i];
for (var j = 0; j < locations.length; j++) {
if (ship.locations.indexOf(locations[j]) >= 0) {
return true;
}
}
}
return false;
}
};
var view = {
displayMessage: function(msg) {
var messageArea = document.getElementById("messageArea");
messageArea.innerHTML = msg;
},
displayHit: function(location) {
var cell = document.getElementById(location);
cell.setAttribute("class", "hit");
},
displayMiss: function(location) {
var cell = document.getElementById(location);
cell.setAttribute("class", "miss");
}
};
var controller = {
guesses: 0,
processGuess: function(guess) {
var location = parseGuess(guess);
if (location) {
this.guesses++;
var hit = model.fire(location);
if (hit && model.shipsSunk === model.numShips) {
view.displayMessage("You sank all my battleships, in " + this.guesses + " guesses");
}
}
}
}
// helper function to parse a guess from the user
function parseGuess(guess) {
var alphabet = ["A", "B", "C", "D", "E", "F", "G"];
if (guess === null || guess.length !== 2) {
alert("Oops, please enter a letter and a number on the board.");
} else {
var firstChar = guess.charAt(0);
var row = alphabet.indexOf(firstChar);
var column = guess.charAt(1);
if (isNaN(row) || isNaN(column)) {
alert("Oops, that isn't on the board.");
} else if (row < 0 || row >= model.boardSize ||
column < 0 || column >= model.boardSize) {
alert("Oops, that's off the board!");
} else {
return row + column;
}
}
return null;
}
function handleFireButton() {
var guessInput = document.getElementById("guessInput");
var guess = guessInput.value.toUpperCase();
controller.processGuess(guess);
guessInput.value = "";
}
function handleKeyPress(e) {
var fireButton = document.getElementById("fireButton");
e = e || window.event;
if (e.keyCode === 13) {
fireButton.click();
return false;
}
}
window.onload = init;
function init() {
var fireButton = document.getElementById("fireButton");
fireButton.onclick = handleFireButton;
var guessInput = document.getElementById("guessInput");
guessInput.onkeypress = handleKeyPress;
// place the ships on the game board
model.generateShipLocations();
}