Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- var BOARD_WIDTH = 10;
- var BOARD_HEIGHT = 10;
- // Vector2D "class"
- function Vector2D(x, y) {
- return { x: x,
- y: y,
- add: function (other) {
- return new Vector2D(x + other.x, y + other.y)
- },
- toString: function() {
- return "(" + x + "," + y + ")";
- }
- };
- }
- // Constants
- var HORIZONTAL = Vector2D(1, 0);
- var VERTICAL = Vector2D(0, 1);
- // Extend Array class with pickRandom method
- Array.prototype.pickRandom = function () {
- var idx = Math.floor( Math.random() * this.length );
- return this[idx];
- };
- // Returns a random orientation
- function randomOrientation() {
- return [ HORIZONTAL, VERTICAL ].pickRandom();
- }
- // Generates random integers
- function randomInteger(maximum) {
- return Math.floor( Math.random() * maximum);
- }
- // Generates random positions
- function randomPosition() {
- return Vector2D( randomInteger(BOARD_WIDTH), randomInteger(BOARD_HEIGHT) );
- }
- // Boatclass
- function Boat(position, orientation, length) {
- return { position: position,
- orientation: orientation,
- length: length,
- hits: (function () {
- var result = [];
- for(var i = 0; i !== length; i++){
- result.push(0);
- }
- return result;
- })(),
- usedPositions: function() {
- var result = [];
- var current = position;
- for ( var i = 0; i != length; i++ ) {
- result.push(current);
- current = current.add(orientation);
- }
- return result;
- }
- };
- }
- // Checks if x is a memeber of the Array
- Array.prototype.member = function (x) {
- for(var i = 0; i !== this.length; i++)
- {
- if(x === this[i]){
- return true;
- }
- }
- return false;
- }
- // Checks sf xs overlaps with the Array
- Array.prototype.overlaps = function (xs) {
- for(var i= 0; i !== xs.length; i++)
- {
- if(this.member(xs[i])){
- return true;
- }
- }
- return false;
- }
- // Generates a random boat with a set length
- function generateRandomBoat(size) {
- return Boat(randomPosition(), randomOrientation(), size);
- }
- // Checks if all elements of xs equal x
- function allEquals(xs, x) {
- var all = true;
- for (var i = 0; i != xs.length; i++) {
- if(xs[i] !== x)
- all = false;
- }
- return all;
- }
- // Generates an Array with random boats
- function randomBoats(sizes) {
- var occupiedPositions = [];
- var boats = [];
- sizes = sizes.slice(); // kopieren
- while ( sizes.length > 0 ) {
- var currentSize = sizes.pop();
- var candidate = generateRandomBoat(currentSize);
- var candidateSpaces = candidate.usedPositions();
- if ( candidateSpaces.overlaps(occupiedPositions) ) {
- sizes.push(currentSize);
- }
- else {
- occupiedPositions = occupiedPositions.concat(candidateSpaces);
- boats.push(candidate);
- }
- }
- for(var i = 0; i !== boats.length; i++) {
- document.write(boats[i].usedPositions());
- document.write(" ||| ");
- }
- return boats;
- }
- // The game itself
- function game(){
- inputArray = [4,3,2];
- var boats = randomBoats(inputArray);
- var currentBoat = 0;
- var sunkenBoat = 0;
- var numberOfTurns = 0;
- while(sunkenBoat !== inputArray.length ) {
- var raak= false;
- var target = "(" + prompt("Enter targetcoordinate (x,y)") + ")";
- var targetString = target.replace(/\s+/g, '');
- for(var i = 0; i !== inputArray.length; i++) {
- for(var j = 0; j !== boats[i].usedPositions().length; j++) {
- console.log(targetString)
- if(targetString === boats[i].usedPositions()[j].toString()) {
- raak = true;
- boats[i].hits[j] = 1;
- console.log(boats[i].hits);
- currentBoat = boats[i];
- }
- else {
- currentBoat = boats[i];
- }
- }
- }
- console.log(currentBoat.hits);
- console.log(allEquals(currentBoat.hits, 1));
- if(raak)
- alert ("Hit!");
- else
- alert ("Miss!");
- if(allEquals(currentBoat.hits, 1)) {
- alert("Boat with length " + currentBoat.hits.length + " has sunken!");
- sunkenBoat++;
- }
- numberOfTurns++
- }
- alert("You've won! You did it in " + numberOfTurns + " turns.")
- }
- game();
- </script>
- </head>
- <body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement