Friday, January 2, 2015

Math Object Challenge: Update #2.

I have made substantial progress; using Taylor series, I approximated the sin, cos, sec, and csc functions. I also added summation and product and a few others. I now, however, have to lean on the use of ln and exp functions from the math object. I added a new function 'truncIf' that truncates a number if and only if the other parameter is an integer (that is, equal it itself when truncated). It is used to give more accurate results when using the pow command.

Code:
var atan = Math.atan;
var tan = Math.tan;
var pi = Math.PI;
var ln = Math.log;
var exp = Math.exp;

function ceil(x){
return (sgn(x))*(abs(x)+.5+div(atan(-tan(pi*(abs(x)+.5))),pi))+(x<0?1:0);
}

function floor(x){
return -ceil(-x);
}

function abs(x){
return sgn(x)*x;
}

function sgn(x){
return (x<0)?-1:(x>0)?1:0;
}

function div(x,y){
return x/y;
}

function sin(y){
var sign = sgn(y);
var x = abs(y);
while(x>2*pi){
x-=2*pi;
}
return (x - (pow(x,3)/fac(3)) + (pow(x,5)/fac(5)) - (pow(x,7)/fac(7)) + (pow(x,9)/fac(9)) - (pow(x,11)/fac(11)) + (pow(x,13)/fac(13)) - (pow(x,15)/fac(15)) + (pow(x,17)/fac(17)))*sign;
}

function cos(y){
var sign = sgn(y);
var x = abs(y);
while(x>2*pi){
x-=2*pi;
}
return (1 - (pow(x,2)/fac(2)) + (pow(x,4)/fac(4)) - (pow(x,6)/fac(6)) + (pow(x,8)/fac(8)) - (pow(x,10)/fac(10)) + (pow(x,12)/fac(12)) - (pow(x,14)/fac(14)) + (pow(x,16)/fac(16)))*sign;
}

function sec(x){
return 1/cos(x);
}

function csc(x){
return 1/sin(x);
}

function fac(x){
return product(1,x,"#");
}

function product(start,end,transform,inst){
var inst = inst || "#";
var out = 1;
transform = transform.split("");
for(i=0;i<transform.length;i++){
transform[i] = transform[i].replace(inst,"i");
}
transform = transform.join("");
for(i=start;i<=end;i++){
out*=eval(transform);
}
return out;
}

function sum(start,end,transform,inst){
var inst = inst || "#";
var out = 1;
transform = transform.split("");
for(i=0;i<transform.length;i++){
transform[i] = transform[i].replace(inst,"i");
}
transform = transform.join("");
for(i=start;i<=end;i++){
out+=eval(transform);
}
return out;
}

function pow(x,y){
return truncIf(x+y,exp(y*ln(x)));
}

function trunc(x){
return +(x+"").split(".")[0];
}

function truncIf(x,y){
return (trunc(x)==x)?trunc(y):y;
}

Thursday, January 1, 2015

Challenge initiated: Let's re-create the Math object.

First, a public apology: I'm moving away from the syntax highlighter. Sorry, :(, but I don't have the time and energy to use it. So, just a grey-and-white PRE. So, right now, I'm attempting at re-creating all of the functions in the Math object. Typically, this wouldn't be to hard, however; I am trying to define as much of the functions mathematically, that is, with equations, leaning only on the functions I define myself. Currently, I have very few functions covered; I am also currently leaning slightly on the Math object for two functions and a variable: Math.atan, Math.tan, and Math.PI; I can easily define the last one, and possibly the second with a Taylor sequence, but I haven't gotten around to doing this.
var atan  = Math.atan;
var tan   = Math.tan;
var pi    = Math.PI;

function ceil(x){
 return (sgn(x))*(abs(x)+.5+div(atan(-tan(pi*(abs(x)+.5))),pi))+(x<0?1:0);
}
function floor(x){
 return -ceil(-x);
}
function abs(x){
 return sgn(x)*x;
}
function sgn(x){
 return (x<0)?-1:(x>0)?1:0;
}
That's all for now, I hope to have more for you tomorrow.

Saturday, October 25, 2014

MOLOTOV

My friend has no idea what this is. *sighs*
               `:+shdmNMMMMNmdhs+:`               
           `/smMMNdyo+/::::/+oydNMMmy/`           
         :yMMms/.       +/       ./smMMy:         
       +mMNs-          +M.           .oNMm+       
     :mMm/             mMh`             /mMm/     
   `yMMo        .      NMMN-              +NMy`   
  `mMm-         y+.    sMMMN               .mMm.  
 `mMm`          `yMMd+ `MMMM:  :            `dMm` 
 yMN.             .dMMo/MMMM/  hd`           `NMh 
:MM+         o`    :MMMMMMMM/  yMh            /MM/
yMN          Nd.   /MMMMMMMMd/oMMN  :d`        NMh
NMy          oMMdyyMMMMMymMMMMMMN- `dMy        yMM
MMs           sMMMMMMMMN`:NMMMMMs-+NMMs        oMM
NMy     //`    sMMMMMMMN. .NMMMMMMMMMy`        yMM
hMN     `yMd+  `MMMMMMomm  oMMMMMMMMo  h+      mMd
:MM+      sMMmsdMMMNM: sh. yMMMMMMMM.:dM/     /MM/
 hMN.     `NMMMMMM/yM:    yMdMMmMMMMMMMN     `NMd 
 `mMd`     yMMMMMm +MM-   ://MN.MMMMMMM+    `dMm` 
  .mMm.    /MMMMMM+ :+`     `/.:MMMMMMy    .mMm.  
   `yMN+    sMMMMdyo           hMMMMMs    +NMh`   
     /mMm/   -hMMMm-         `hMMMNy-   /mMN/     
       +mMNo.  `/shmms`    .hNmho:   .omMNo`      
         /hMMms/`                `:smMMh/         
           `/ymMMNhso+/::::/+oshNMMmy/`           
               `:+yhmNMMMMMMNmhyo:`               
No code this time, sorry!

Wednesday, October 1, 2014

Javascript Random Text (i.e. ZALGONATION)

What this does

This is a little thing I invented whilst reading about the String.fromCodePoint and the respective identity getter. By generating a random number (thanks to Mozilla.org's developer wiki for the code of generating random integer) between the range of the normal text's points (30 - 126), and repeating that a random number of times.
  1. window.setInterval(function(){
  2.         title = document.getElementsByTagName("title")[0]
  3.         title.innerHTML = ""
  4.         var m = gI(2,20)
  5.         for(i=0;i<m;i++){
  6.                 title.innerHTML += String.fromCodePoint(gI(32,126))
  7.         }
  8. },20)

Final Notes

I'm on the Mozilla Developer wiki! Nothing big, duy, but I might be!

Links

Thursday, August 21, 2014

Alphabet to numbers + CODE

Put this on your page to encode, scramble, and decode strings! It's amazing! :D

Usage

When using this, you must give credit, as this is under a Creative Commons License (see sidebar for more information)

Notes

  1. Does not retain capitalization or punctuation
  2. Spaces are replaced with a '<space>' Sub-string after the initial encoding. You would have to replace this manually if you don't like it.

Try it out!

String:

Offset:

Code

  1. /*
  2.         ccccc ooooo dddd   eeeee
  3.         C     O   O D   D  E
  4.         C     O   O D    D Eee
  5.         C     O   O D   D  E
  6.         ccccc ooooo dddd   eeeee
  7.        
  8.         Copyright 2014 Conor O'Brien under a Creative Commons Attribution 4.0 International License.
  9. */
  10. var alphabet = {
  11.         lower: ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"],
  12.         upper: ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"],
  13.         number: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]
  14. }
  15. String.prototype.encode = function(){
  16.         var str = this.valueOf();
  17.         var returnStr = [];
  18.         var notAny = false;
  19.         str = str.toLowerCase()
  20.         str = str.split("")
  21.         for(i = 0; i < str.length; i++){
  22.                 for(h = 0; h < alphabet.lower.length; h++){
  23.                         if(str[i]==alphabet.lower[h]){                 
  24.                                 returnStr.push(h+1)
  25.                         } else {
  26.                                 notAny = true;
  27.                         }
  28.                 }
  29.                 if(str[i]==" "){
  30.                         returnStr.push("<space>")
  31.                 } else if(!notAny){
  32.                         returnStr.push(str[i])
  33.                 }
  34.                 notAny = false;
  35.         }
  36.         returnStr = returnStr.toString();
  37.         while (returnStr.search(",") !== -1){
  38.                 returnStr = returnStr.replace(","," ");
  39.         }
  40.         return returnStr;
  41. }
  42.  
  43. String.prototype.scramble = function(number){
  44.         var str = this.valueOf();
  45.         var returnStr = [];
  46.         var notAny = false;
  47.         str = str.toLowerCase()
  48.         str = str.split(" ")
  49.         for(i = 0; i < str.length; i++){
  50.                 for(h = 0; h < alphabet.number.length; h++){
  51.                         if(str[i]==alphabet.number[h]){                
  52.                                 returnStr.push(alphabet.number[h] + number)
  53.                         }
  54.                 }
  55.         }
  56.         returnStr = returnStr.toString();
  57.         while (returnStr.search(",") !== -1){
  58.                 returnStr = returnStr.replace(","," ");
  59.         }
  60.         return returnStr;
  61.        
  62. }
  63. String.prototype.decode = function(){
  64.         var str = this.valueOf();
  65.         var returnStr = [];
  66.         str = str.toLowerCase()
  67.         str = str.split(" ")
  68.         for(i = 0; i < str.length; i++){
  69.                 for(h = 0; h < alphabet.number.length; h++){
  70.                         if(str[i]==alphabet.number[h]){                
  71.                                 returnStr.push(alphabet.lower[h])
  72.                         }
  73.                 }
  74.                 if (str[i]=="<space>"){
  75.                         returnStr.push(" ");
  76.                 }
  77.         }
  78.         returnStr = returnStr.toString();
  79.         while (returnStr.search(",") !== -1){
  80.                 returnStr = returnStr.replace(",","");
  81.         }
  82.         return returnStr;
  83. }
  84.  

Tuesday, August 19, 2014

The year is 2048!

First things first! I am, yes, apart of the 2048 craze. It's just one of those games that make you want to punch the living everything out of it, yes? After playing for WHO KNOWS HOW LONG, I decided to play around with the Javascript.

Observations

One: This Is a JavaScript Job

Fiddling with the CSS, I made a 'Night' Theme (which I use, as it's preferable to the default). Anyone who has made a texture for 2048 would know that the JS 'pushes' (like you would do for an array item) a class to the tile upon it's creation. The CSS identifiers for this are:
  1. .tile-<value>
    • <value> is either 2, 4, 8, 16...1024, or 2048
  2. .tile
    • Describes the animation of the tile.
If one changes a two-tile (.tile-2) to a 2048 tile (.tile-2048), it will change upon moving the board, and will not retain its style.

Two: The JavaScript code is Complicated

With 10 JS Game-related files, one may be overwhelmed. There are 7 non-pollyfil files, which are listed below:
  1. application.js
  2. game_manager.js
  3. grid.js
  4. html_actuator.js
  5. keyboard_input_manager.js
  6. local_storage.js
  7. tile.js

Three: All of the tiles are powers of two

..And, as a friend tells me, most are Mojang's Most Popular Game's resolution for Resource/Texture packs. 2 = 21
4 = 22
8 = 23
16 = 24
32 = 25
64 = 26
128 = 27
256 = 28
256 = 28
512 = 29
1024 = 210
2048 = 211

What this does

This is a code that will add a tile whose value can be anything, not limited to the powers of 2.
Under application.js, one finds (on line 3) the program making a GameManager (found under game_manager.js), which takes the parameters of a grid size (4), an InputManager (KeyboardInputManager), an Actuator (HTMLActuator), and a StorageManager (LocalStorageManager). A new one is presumably created with each iteration of the loop (as it's found under "window.requestAnimationFrame"), but you can create one by simply typing into the console/command line on your 2048 page a variable as found here.
Of course, you have to create a prototype function (or a function that is applied to a certain object, like 'String.prototype.search = function(...' would be 'String.search(...')
Before doing what's stated above, you must do what is stated below (that is, here.)
And enjoy!

Codes

The Tile Manager Prototype

  1. GameManager.prototype.addTile = function (value) {
  2.   if (this.grid.cellsAvailable()) {
  3.     var tile = new Tile(this.grid.randomAvailableCell(), value);
  4.  
  5.     this.grid.insertTile(tile);
  6.   }
  7. };

The New Game Manager

  1. var Gamer = new GameManager(4, KeyboardInputManager, HTMLActuator, LocalStorageManager);

Syntax and Usage

All possible (legitimate) tiles.
  1. Gamer.addTile(2);
  2. Gamer.addTile(4);
  3. Gamer.addTile(8);
  4. Gamer.addTile(16);
  5. Gamer.addTile(32);
  6. Gamer.addTile(64);
  7. Gamer.addTile(128);
  8. Gamer.addTile(256);
  9. Gamer.addTile(512);
  10. Gamer.addTile(1024);
  11. Gamer.addTile(2048);

A final note

This is not a developers tactic--it has many glitches, and is not guaranteed to be cross-browser. Glitches can also occur due to the separate Game Managers trying to manage the same game, and can result in two separate respective games going on at once. Use with caution, and, by using this, you accept the Terms and Conditions of Service listed below.

Terms and Conditions of Service

Article I - Definitions

  1. The term 'Blog post' is defined herein as an article in which is written a paragraph or more.
  2. The term '2048 Game' or '2048 puzzle' or '2048' is defined herein as the original game hosted on the url "https://gabrielecirulli.github.io/2048/" (Link: here)
  3. The term 'Usage' is defined herein as taking the codes provided within this Blog Post and applying them to the 2048 game.
  4. The term 'Code' or 'Codes' refers to any of the Ordered List Item Groups (<ol>) that contain(s) a programming language (of which could be JavaScript, CSS, or HTML on this page).
  5. The term 'User' or 'The User' is defined as the person or persons using the Codes provided on this page.

Article II - Main Requirements

  1. Usage of the contents within the Blog Post can only be used for testing or for gratification of beating the game. You may not claim any results of using this code as anything besides that of which it is (which is the results of an edit of the JavaScripting). All screenshots of a game edited with the code provided in this blog post (or any alterations of said code) are strictly prohibited. The code can and will be taken down if multiple persons fail to comply to the Terms and Conditions of Service.
  2. These Terms of Service may and will be updated if necessary. Failure to comply with the alterations of the Terms of Service will be an infraction like any other. Ignorance of the Terms is the fault of the User
  3. All Content on the page is under a Creative Commons Attribution 4.0 international license.