mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2024-12-26 22:30:44 -05:00
feat: ship class and test
This commit is contained in:
parent
36bf0c07d6
commit
685020dc94
14 changed files with 9884 additions and 0 deletions
21
battleship/.eslintrc.js
Normal file
21
battleship/.eslintrc.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
module.exports = {
|
||||
env: {
|
||||
browser: true,
|
||||
es2021: true,
|
||||
node: true,
|
||||
},
|
||||
extends: ['eslint:recommended', 'prettierd'],
|
||||
parserOptions: {
|
||||
ecmaVersion: 'latest',
|
||||
sourceType: 'module',
|
||||
},
|
||||
overrides: [
|
||||
{
|
||||
files: ['tests/**/*'],
|
||||
plugins: ['jest'],
|
||||
env: {
|
||||
'jest/globals': true,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
132
battleship/.gitignore
vendored
Normal file
132
battleship/.gitignore
vendored
Normal file
|
@ -0,0 +1,132 @@
|
|||
# ---> Node
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
.pnpm-debug.log*
|
||||
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
*.lcov
|
||||
|
||||
# nyc test coverage
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# Bower dependency directory (https://bower.io/)
|
||||
bower_components
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# Snowpack dependency directory (https://snowpack.dev/)
|
||||
web_modules/
|
||||
|
||||
# TypeScript cache
|
||||
*.tsbuildinfo
|
||||
|
||||
# Optional npm cache directory
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
.eslintcache
|
||||
|
||||
# Optional stylelint cache
|
||||
.stylelintcache
|
||||
|
||||
# Microbundle cache
|
||||
.rpt2_cache/
|
||||
.rts2_cache_cjs/
|
||||
.rts2_cache_es/
|
||||
.rts2_cache_umd/
|
||||
|
||||
# Optional REPL history
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
.yarn-integrity
|
||||
|
||||
# dotenv environment variable files
|
||||
.env
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.env.local
|
||||
|
||||
# parcel-bundler cache (https://parceljs.org/)
|
||||
.cache
|
||||
.parcel-cache
|
||||
|
||||
# Next.js build output
|
||||
.next
|
||||
out
|
||||
|
||||
# Nuxt.js build / generate output
|
||||
.nuxt
|
||||
dist
|
||||
|
||||
# Gatsby files
|
||||
.cache/
|
||||
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||
# public
|
||||
|
||||
# vuepress build output
|
||||
.vuepress/dist
|
||||
|
||||
# vuepress v2.x temp and cache directory
|
||||
.temp
|
||||
.cache
|
||||
|
||||
# Docusaurus cache and generated files
|
||||
.docusaurus
|
||||
|
||||
# Serverless directories
|
||||
.serverless/
|
||||
|
||||
# FuseBox cache
|
||||
.fusebox/
|
||||
|
||||
# DynamoDB Local files
|
||||
.dynamodb/
|
||||
|
||||
# TernJS port file
|
||||
.tern-port
|
||||
|
||||
# Stores VSCode versions used for testing VSCode extensions
|
||||
.vscode-test
|
||||
|
||||
# yarn v2
|
||||
.yarn/cache
|
||||
.yarn/unplugged
|
||||
.yarn/build-state.yml
|
||||
.yarn/install-state.gz
|
||||
.pnp.*
|
||||
|
4
battleship/.prettierrc
Normal file
4
battleship/.prettierrc
Normal file
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"tabWidth": 4,
|
||||
"singleQuote": true
|
||||
}
|
6
battleship/babel.config.js
Normal file
6
battleship/babel.config.js
Normal file
|
@ -0,0 +1,6 @@
|
|||
module.exports = {
|
||||
presets: [
|
||||
['@babel/preset-env', { targets: { node: 'current' } }],
|
||||
'@babel/preset-typescript',
|
||||
],
|
||||
};
|
9589
battleship/package-lock.json
generated
Normal file
9589
battleship/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
33
battleship/package.json
Normal file
33
battleship/package.json
Normal file
|
@ -0,0 +1,33 @@
|
|||
{
|
||||
"name": "webpack-template",
|
||||
"version": "1.0.0",
|
||||
"description": "Vanilla template for webpack",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "jest",
|
||||
"start": "webpack serve --open --config webpack.dev.js",
|
||||
"build": "webpack --config webpack.prod.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.23.9",
|
||||
"@babel/preset-env": "^7.23.9",
|
||||
"@babel/preset-typescript": "^7.23.3",
|
||||
"@types/jest": "^29.5.12",
|
||||
"babel-jest": "^29.7.0",
|
||||
"css-loader": "^6.8.1",
|
||||
"eslint": "^8.56.0",
|
||||
"eslint-config-prettier": "^9.1.0",
|
||||
"eslint-plugin-jest": "^27.9.0",
|
||||
"html-webpack-plugin": "^5.6.0",
|
||||
"jest": "^29.7.0",
|
||||
"style-loader": "^3.3.3",
|
||||
"ts-jest": "^29.1.2",
|
||||
"webpack": "^5.89.0",
|
||||
"webpack-cli": "^5.1.4",
|
||||
"webpack-dev-server": "^4.15.1",
|
||||
"webpack-merge": "^5.10.0"
|
||||
}
|
||||
}
|
0
battleship/src/components/gameboard.js
Normal file
0
battleship/src/components/gameboard.js
Normal file
16
battleship/src/components/ship.js
Normal file
16
battleship/src/components/ship.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
class Ship {
|
||||
constructor(length) {
|
||||
this.length = length;
|
||||
this.hits = 0;
|
||||
}
|
||||
|
||||
hit() {
|
||||
this.hits += 1
|
||||
}
|
||||
|
||||
isSunk() {
|
||||
return this.hits === this.length
|
||||
}
|
||||
}
|
||||
|
||||
export { Ship }
|
0
battleship/src/index.js
Normal file
0
battleship/src/index.js
Normal file
4
battleship/tests/gameboard.test.js
Normal file
4
battleship/tests/gameboard.test.js
Normal file
|
@ -0,0 +1,4 @@
|
|||
it('should place ships at random coordinates', () => {
|
||||
const gb = new Gameboard();
|
||||
|
||||
})
|
29
battleship/tests/ship.test.js
Normal file
29
battleship/tests/ship.test.js
Normal file
|
@ -0,0 +1,29 @@
|
|||
import { Ship } from "../src/components/ship";
|
||||
|
||||
|
||||
it('should have length, number of times hit, and if sunk', () => {
|
||||
// Small ship
|
||||
const _ship = new Ship(2)
|
||||
expect(_ship).toEqual({
|
||||
length: 2,
|
||||
hits: 0,
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
it('if hit, hit count should increase', () => {
|
||||
const _ship = new Ship(2);
|
||||
_ship.hit();
|
||||
expect(_ship.hits).toBe(1);
|
||||
})
|
||||
|
||||
it('is not sunk', () => {
|
||||
const _ship = new Ship(2);
|
||||
expect(_ship.isSunk()).toBeFalsy()
|
||||
})
|
||||
|
||||
it('is sunk', () => {
|
||||
const _ship = new Ship(2);
|
||||
_ship.hits = 2;
|
||||
expect(_ship.isSunk()).toBeTruthy()
|
||||
})
|
34
battleship/webpack.common.js
Normal file
34
battleship/webpack.common.js
Normal file
|
@ -0,0 +1,34 @@
|
|||
const path = require('path');
|
||||
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
||||
|
||||
module.exports = {
|
||||
entry: {
|
||||
index: './src/index.js',
|
||||
},
|
||||
plugins: [
|
||||
new HtmlWebpackPlugin({
|
||||
title: 'Battleship',
|
||||
}),
|
||||
],
|
||||
output: {
|
||||
filename: 'bundle.js',
|
||||
path: path.resolve(__dirname, 'dist'),
|
||||
clean: true,
|
||||
},
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.css$/i,
|
||||
use: ['style-loader', 'css-loader'],
|
||||
},
|
||||
{
|
||||
test: /\.(png|svg|jpg|jpeg|gif)$/i,
|
||||
type: 'asset/resource',
|
||||
},
|
||||
{
|
||||
test: /\.(woff|woff2|eot|ttf|otf)$/i,
|
||||
type: 'asset/resource',
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
10
battleship/webpack.dev.js
Normal file
10
battleship/webpack.dev.js
Normal file
|
@ -0,0 +1,10 @@
|
|||
const { merge } = require("webpack-merge");
|
||||
const common = require("./webpack.common.js");
|
||||
|
||||
module.exports = merge(common, {
|
||||
mode: "development",
|
||||
devtool: "inline-source-map",
|
||||
devServer: {
|
||||
static: "./dist",
|
||||
},
|
||||
});
|
6
battleship/webpack.prod.js
Normal file
6
battleship/webpack.prod.js
Normal file
|
@ -0,0 +1,6 @@
|
|||
const { merge } = require("webpack-merge");
|
||||
const common = require("./webpack.common.js");
|
||||
|
||||
module.exports = merge(common, {
|
||||
mode: "production",
|
||||
});
|
Loading…
Reference in a new issue