Commit 6efda8bf authored by Andrew Stephen Brown's avatar Andrew Stephen Brown
Browse files

added more stuff

parent 83f598aa
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -14,7 +14,7 @@
<body>
<div class="container">
<h1>Assignment 2</h1>
<h1>COSC360 Assignment</h1>
<p>
This is a simple Battleship game.
</p>
......@@ -33,9 +33,13 @@
<button type="button" class="shipButton" id='battleship'>Battleship</button><br>
<button type="button" class="shipButton" id='carrier'>Carrier</button><br><br>
</div>
<div class="defend" id="defend">
<svg xmlns="http://www.w3.org/2000/svg" id="defendBoard" width="300" height="300">
</svg>
<svg xmlns="http://www.w3.org/2000/svg" id="defendBoard" width="300" height="300">
</svg>
</div>
<div class="chatBox" id="chatBox">
</div>
</div>
......
84444110426f949bd683231f949d662e62ce0855 {"key":"{\"uglify-es\":\"3.3.9\",\"uglifyjs-webpack-plugin\":\"1.2.7\",\"uglifyjs-webpack-plugin-options\":{\"test\":/\\.js(\\?.*)?$/i,\"warningsFilter\":function () {\n return true;\n },\"extractComments\":false,\"sourceMap\":false,\"cache\":true,\"parallel\":true,\"uglifyOptions\":{\"compress\":{\"inline\":1},\"output\":{\"comments\":/^\\**!|@preserve|@license|@cc_on/}}},\"path\":\"\\u002Fhome\\u002Fabrown95\\u002F2019\\u002Ft2\\u002Fcosc360\\u002Fa2\\u002Fdist\\u002Fbundle.js\",\"hash\":\"2d27cf7809526653a7bfc81927efd9b3\"}","integrity":"sha512-ua5hyA8MOIMune9oydofHQcnZ7WvKkzZ8S8eBu/BGj19rG6yquiwz36aDGZHnbphlElS/riLkt8bjV0TT163NA==","time":1567148642568,"size":126379}
\ No newline at end of file
3a3b8bb62be918581ad8022d9ea276cc714b2497 {"key":"{\"uglify-es\":\"3.3.9\",\"uglifyjs-webpack-plugin\":\"1.2.7\",\"uglifyjs-webpack-plugin-options\":{\"test\":/\\.js(\\?.*)?$/i,\"warningsFilter\":function () {\n return true;\n },\"extractComments\":false,\"sourceMap\":false,\"cache\":true,\"parallel\":true,\"uglifyOptions\":{\"compress\":{\"inline\":1},\"output\":{\"comments\":/^\\**!|@preserve|@license|@cc_on/}}},\"path\":\"\\u002Fhome\\u002Fabrown95\\u002F2019\\u002Ft2\\u002Fcosc360\\u002Fa2\\u002Fdist\\u002Fbundle.js\",\"hash\":\"601aea51d325e0b5495b0b3a172557e7\"}","integrity":"sha512-6GIVgG65kRjBN14Wcn2qmOtQazuxfYEHYvcws0iNOr1zP7hIPuAhHDvh80CA2wR2wVNJ5s+WwKQ+d5QKYgzD3w==","time":1567148087003,"size":126391}
\ No newline at end of file
f9a4158bf1fa811ddb59951b1bd64c24b6e8c9cf {"key":"{\"uglify-es\":\"3.3.9\",\"uglifyjs-webpack-plugin\":\"1.2.7\",\"uglifyjs-webpack-plugin-options\":{\"test\":/\\.js(\\?.*)?$/i,\"warningsFilter\":function () {\n return true;\n },\"extractComments\":false,\"sourceMap\":false,\"cache\":true,\"parallel\":true,\"uglifyOptions\":{\"compress\":{\"inline\":1},\"output\":{\"comments\":/^\\**!|@preserve|@license|@cc_on/}}},\"path\":\"\\u002Fhome\\u002Fabrown95\\u002F2019\\u002Ft2\\u002Fcosc360\\u002Fa2\\u002Fdist\\u002Fbundle.js\",\"hash\":\"3767ca027b636ec973eadc76598bd10e\"}","integrity":"sha512-ua5hyA8MOIMune9oydofHQcnZ7WvKkzZ8S8eBu/BGj19rG6yquiwz36aDGZHnbphlElS/riLkt8bjV0TT163NA==","time":1567150066643,"size":126379}
\ No newline at end of file
export {
battleship as battleship,
ship as ship
battleship as battleship
}
class battleship {
constructor(public w:number = 10, public h:number = 10) {
this.board = this.emptyBoard()
}
// create board and ships
board: Array<number>
destroyer = new ship(2, 0, "Destroyer")
cruiser = new ship(3, 0, "Cruiser")
......@@ -18,8 +13,15 @@ class battleship {
battle = new ship(4, 0, "Battleship")
carrier = new ship(5, 0, "Carrier")
// array to hold ships
ships = [this.destroyer, this.cruiser, this.submarine, this.battle, this.carrier]
// constructor initialised board
constructor(public w:number = 10, public h:number = 10) {
this.board = this.emptyBoard()
}
// creates board of 0s
emptyBoard():Array<number> {
let arr = <number[]>[]
for (let i = 0; i < this.h * this.w; i++) {
......@@ -28,28 +30,34 @@ class battleship {
return arr
}
// returns index of coordinate in single dimension array
boardIndex(x:number, y:number):number {
return y * this.w + x
}
// return x index
x(index:number) {
return index % this.w
}
// return y index
y(index:number) {
return Math.floor(index / this.w)
}
// sets coordinate to 1 which is hit
setHit(x:number, y:number):void {
let b = this.boardIndex(x,y)
this.board[b] = 1
}
// set coordinate to 2 which is miss
setMiss(x:number, y:number):void {
let b = this.boardIndex(x,y)
this.board[b] = 2
}
// place ship
setShip(x:number, y:number, ship:ship):void {
let b = this.boardIndex(x,y)
if (this.isValidPlacement(x,y,ship.size,ship.rotation) == 1) {
......@@ -64,9 +72,11 @@ class battleship {
this.board[by] = 2
}
}
ship.placed = true
}
}
// check if placement of ship is valid
isValidPlacement(x:number, y:number, ship:number, rotation:number):number {
let b = this.boardIndex(x,y)
let flag = 0
......@@ -83,7 +93,6 @@ class battleship {
}
}
else {
console.log("insideb")
if (this.h - y >= ship) {
flag = 1
}
......@@ -104,11 +113,13 @@ class ship {
rotation:number
name:string
placed:boolean
health:number
constructor(s:number, r:number, n:string) {
this.size = s
this.rotation = r
this.name = n
this.placed = false
this.health = s
}
}
\ No newline at end of file
import { render, bs, bs2 } from "./render";
import { render } from "./render";
import * as React from "react";
import * as ReactDOM from "react-dom";
render()
......@@ -2,38 +2,45 @@ import { battleship } from "./battleship"
import * as d3 from "d3"
export {
bs as bs,
bs2 as bs2,
render as render
}
// cell size for board
const cellSize = 30
const smallCell = 20
// create two boards
const bs = new battleship()
const bs2 = new battleship()
const selBoard = new battleship(5,5)
// select the board divs
const board1 = d3.select("#attackBoard").append("g")
const board2 = d3.select("#defendBoard").append("g")
// logic for placing ships and for game start
let shipIndex = 0
let gameStart = false
// render the game to the screen
function render() {
// if the game has started, both boards will be rendered
// if not the ship buttons will render
if (gameStart)
renderAttackBoard()
else
renderButtons()
// this board will render depening on the game state
renderDefendBoard()
}
function renderAttackBoard() {
// clear the svg before rendering, removing defend board
board1.selectAll("*").remove();
let attack = board1.selectAll("rect").data(bs.board)
attack.attr("class", (d) => {
// render colour according to number in array
switch(d) {
case 0: {
return "cell"
......@@ -86,6 +93,8 @@ function renderAttackBoard() {
.on("click", (d,i) => {
let x = bs.x(i)
let y = bs.y(i)
// test for hit or miss
if(fire(bs2,x,y)) {
bs.setHit(x,y)
}
......@@ -98,6 +107,7 @@ function renderAttackBoard() {
function renderDefendBoard() {
// this will choose which side to render to board on depending on the gamestate
let defend = gameStart ? board2.selectAll("rect").data(bs2.board) : board1.selectAll("rect").data(bs2.board)
defend.attr("class", (d) => {
......@@ -120,9 +130,9 @@ function renderDefendBoard() {
.on("click", (d,i) => {
let x = bs2.x(i)
let y = bs2.y(i)
if(!bs2.ships[shipIndex].placed) {
// if at start screen, place ship
if(!gameStart && !bs2.ships[shipIndex].placed) {
bs2.setShip(x, y, bs2.ships[shipIndex])
bs2.ships[shipIndex].placed = true
}
renderDefendBoard()
})
......
......@@ -10,10 +10,6 @@
text-align: left;
}
.selectBoard {
text-align: center;
}
.shipButton {
width: 100px;
box-shadow: 2px 2px;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment