Skip to content

Instantly share code, notes, and snippets.

@OneCent01
OneCent01 / C: read_input_char
Last active November 15, 2019 08:16
Reads character input from the user; handles arrow keys, converting them to their equivalent WASD character
char read_input_char()
{
char c;
c = getch();
if (c == '\033') { // if the first value is esc
c = getch(); // skip the [
switch(c = getch()) { // the real value
case 'A':
// code for arrow up
return 'w';
@OneCent01
OneCent01 / Obsidian-Express
Last active October 7, 2019 23:42
Secure NodeJS server with add and auth users; salted and hashed passes, token authentication, and apply secure response headers
// This should be an external database in real development. This is just for simplified demonstration.
const memoryDB = {
users: {},
usersCount: 0
}
const express = require('express')
const obsidian = require('obsidian-js')
const bodyParser = require('body-parser')
var delegationObjects = {
Employee: (name) => Object.assign({
name,
hello: () => console.log(`Hi, I'm ${name}`)
}),
Fulltime: (name, role) => Object.assign(delegationObjects.Employee(name), {
role,
weeklyHours: 40
}),
@OneCent01
OneCent01 / JS Employee Class Inheritance
Created February 9, 2018 18:34
Inheritance model implemented in JavaScript, a prototype-based language without classes
function Employee() {
this.name = '';
this.dept = 'general';
}
function Manager() {
Employee.call(this);
this.reports = [];
}
Manager.prototype = Object.create(Employee.prototype);
@OneCent01
OneCent01 / JS Factory Method Example
Last active February 10, 2018 17:24
Employee Objects - modified code originally found at: http://www.dofactory.com/javascript/factory-method-design-pattern
var subclasses = {
Employee: function(name, role) {
this.name = name;
this.sayHi = function() {
console.log(this.name + ": I work " + this.weeklyHours + "hrs/week");
};
if(role === "manager" || role === "salesman") return subclasses.Fulltime(name, role);
else if(role === "temporary" || role === "traveling") return subclasses.Parttime(name, role);
},
Fulltime: function(name, role) {
@OneCent01
OneCent01 / battleship.py
Created January 29, 2018 19:56
final battleship game from the CodeAcademy intro tutorial
from random import randint
board = []
for x in range(5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print " ".join(row)
var findLeft = function(grid, row, col) {
if(col > 0) {
return grid[row][col - 1];
} else {
return undefined;
}
};
var findRight = function(grid, row, col) {
if(col < grid[row].length-1) {
var getCurrentRow = function(board, row, col) {
return board[row];
};
var getCurrentCol = function(board, row, col) {
var flatCol = [];
board.forEach(function(row) {
flatCol.push(row[col]);
return row;
});
var sortedIndex = function (array, value) {
var low = 0,
high = array.length;
while (low < high) {
var mid = (low + high) >>> 1;
if (array[mid] < value) low = mid + 1;
else high = mid;
}
return low;
};
function Person() {
if(typeof Person.instance === 'object')
return Person.instance;
Person.instance = this;
return this;
}