Author Topic: Random Script Snippets  (Read 3874 times)

0 Members and 1 Guest are viewing this topic.

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Random Script Snippets
« on: April 26, 2013, 06:01:32 pm »
Post random interesting PHP, Python, Javascript or whatever piece of code here.

Code: [Select]
<?php
function uchr($u){return mb_convert_encoding(&#39;&#38;#&#39;.intval($u).&#39;;&#39;,&#39;UTF-8&#39;,&#39;HTML-ENTITIES&#39;);}
$text trim($argv[1],"\0..\x2f\x3a..\x40\x47..\x60\x67..\xff");
if(
strlen($text)%2==1)$text="0".$text;
if(
strlen($text)%4==2)$text.="00";
$char = Array(uchr(0x2000),uchr(0x2597),uchr(0x2596),uchr(0x2584),
                
uchr(0x259D),uchr(0x2590),uchr(0x259E),uchr(0x259F),
                
uchr(0x2598),uchr(0x259A),uchr(0x258C),uchr(0x2599),
                
uchr(0x2580),uchr(0x259C),uchr(0x259B),uchr(0x2588));
for(
$i=0;$i<strlen($text);$i+=4){
        
$byte hexdec(substr($text,$i,2));
        
$byte2 hexdec(substr($text,$i+2,2));
        echo 
$char[(($byte&192)>>4)|(($byte2&192)>>6)].$char[(($byte&48)>>2)|(($byte2&48)>>4)];
        echo 
$char[($byte&12)|(($byte2&12)>>2)].$char[(($byte&3)<<2)|($byte2&3)]."\n";
}

Input: An 8x8 Axe sprite
Output: The sprite drawn using 4x4 characters.
Example:
Code: [Select]
[julien@haruhi ~]$ php axesprite.php [AA55AA55aa55aa55]
▚▚▚▚
▚▚▚▚
▚▚▚▚
▚▚▚▚
Why it's interesting: It uses bitwise operations! Also 4 pixels in a single character.

EDIT: Ignore the &#38; at line 2 it's supposed to be a &.
« Last Edit: April 26, 2013, 06:06:15 pm by Juju »

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline aeTIos

  • Nonbinary computing specialist
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3915
  • Rating: +184/-32
    • View Profile
    • wank.party
Re: Random Script Snippets
« Reply #1 on: April 27, 2013, 12:10:44 am »
Lol, nice one. Will post one myself soon.
I'm not a nerd but I pretend:

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Random Script Snippets
« Reply #2 on: May 09, 2013, 01:31:12 pm »
Code: [Select]
<?php
ob_start
();
echo 
$num="1"," ";
$c=0;
for(
$a=0;$a<$argv[1]-1;$a++)
{
        
$newnum="";
        for(
$i=0;$i<strlen($num);$i++)
        {
                
$c++;
                if(
$num[$i] != (isset($num[$i+1])?$num[$i+1]:""))
                {
                        
$newnum .= $c.$num[$i];
                        
$c=0;
                }
        }
        echo 
$num=$newnum," ";
}
$out ob_get_contents();
ob_end_clean();
echo 
str_replace(Array("1""2""3"" "),
Array(
chr(27)."[44m1",
        
chr(27)."[42m2",
        
chr(27)."[41m3",
        
chr(27)."[0m "),
        
chunk_split($outtrim(`tput cols`), chr(27)."[0m\n"));

This one, running from CLI, makes use of output buffers to add some pretty colors :) Takes one parameter, its the number of numbers to generate.

Example:
« Last Edit: May 09, 2013, 01:35:32 pm by Juju »

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline Joshuasm32

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 142
  • Rating: +19/-7
    • View Profile
    • Network
Re: Random Script Snippets
« Reply #3 on: May 09, 2013, 07:01:17 pm »
Ahhhh...  My eyes are burning...   *.*  Colors!
My name is Josh and I a developer at Moonzean. I enjoy Radiohead, web development, Java, and cryptograms.
Spoiler For No Surprises, by Radiohead:
A heart that's full up like a landfill
A job that slowly kills you
Bruises that won't heal

You look so tired unhappy
Bring down the government
They don't, they don't speak for us

I'll take a quiet life
A handshake of carbon monoxide

And no alarms and no surprises
No alarms and no surprises
No alarms and no surprises
Silent, silent

This is my final fit
My final bellyache

With no alarms and no surprises
No alarms and no surprises
No alarms and no surprises please

Such a pretty house
And such a pretty garden

No alarms and no surprises
No alarms and no surprises
No alarms and no surprises please

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: Random Script Snippets
« Reply #4 on: May 10, 2013, 12:09:12 pm »
...
Input: An 8x8 Axe sprite
Output: The sprite drawn using 4x4 characters.
Example:
Code: [Select]
[julien@haruhi ~]$ php axesprite.php [AA55AA55aa55aa55]
▚▚▚▚
▚▚▚▚
▚▚▚▚
▚▚▚▚
Why it's interesting: It uses bitwise operations! Also 4 pixels in a single character.

EDIT: Ignore the &#38; at line 2 it's supposed to be a &.
The 'sprite' doesn't look like a sprite to me. It just looks like 16 squares, in a square.
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Random Script Snippets
« Reply #5 on: May 10, 2013, 12:12:28 pm »
...
Input: An 8x8 Axe sprite
Output: The sprite drawn using 4x4 characters.
Example:
Code: [Select]
[julien@haruhi ~]$ php axesprite.php [AA55AA55aa55aa55]
▚▚▚▚
▚▚▚▚
▚▚▚▚
▚▚▚▚
Why it's interesting: It uses bitwise operations! Also 4 pixels in a single character.

EDIT: Ignore the &#38; at line 2 it's supposed to be a &.
The 'sprite' doesn't look like a sprite to me. It just looks like 16 squares, in a square.
You must lack the necessary fonts to show this character up. Which OS are you using? If Linux, try installing the Microsoft fonts, probably.

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: Random Script Snippets
« Reply #6 on: May 10, 2013, 12:16:17 pm »
I'm using windows 7.

This is a screenshot of how it shows up to me:
« Last Edit: May 10, 2013, 12:16:25 pm by ben_g »
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline lkj

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +58/-1
    • View Profile
Re: Random Script Snippets
« Reply #7 on: May 10, 2013, 12:55:53 pm »
I'm also using Windows 7, but for me it shows up more or less correctly:
« Last Edit: May 10, 2013, 12:56:07 pm by lkj »

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Random Script Snippets
« Reply #8 on: May 10, 2013, 01:26:08 pm »
Here is something I did in my spare time in C#. It's not really a snippit, but fun anyway^^

Main.cs - Main program code
Code: [Select]
using System;

namespace Fractal
{
class MainClass
{


public static void TriLine(double distance, Turtle myTurtle)
{
Console.ForegroundColor = ConsoleColor.Magenta;
if (distance < 1)
{
myTurtle.Forward(distance);
}
else
{
TriLine (distance/3, myTurtle);
myTurtle.Left (60);
TriLine (distance/3, myTurtle);
myTurtle.Right (120);
TriLine (distance/3, myTurtle);
myTurtle.Left (60);
TriLine (distance/3, myTurtle);
}
}

public static void Main (string[] args)
{
Console.WriteLine ("Press any key to start");
Console.ReadLine ();

Display myDisplay = new Display ();
Turtle myTurtle = new Turtle (myDisplay);



//myTurtle.HomeXPos = 10;
myTurtle.HomeYPos = 10;

for (int i = 0; i < 3; i++) {
TriLine(300, myTurtle);
myTurtle.Right(120);
}

myDisplay.UpdateDisplay ();

}
}
}

Turtle.cs - Turtle 'engine'
Code: [Select]
using System;

namespace Fractal
{
public class Turtle
{
public double XPos  {get; set;}
public double YPos  {get; set;}
public double Angle {get; set;}

public bool Pen {get; set;}

public double HomeXPos  {get; set;}
public double HomeYPos  {get; set;}

private Display myDisplay;

public Turtle(Display myDisplay)
{
XPos = 0;
YPos = 0;
Angle = 0;

Pen = true;

this.myDisplay = myDisplay;

HomeXPos = myDisplay.Width / 2;
HomeYPos = myDisplay.Height / 2;
}


public static double DegToRad(double deg)
{
return deg * Math.PI / 180;
}



public void Forward(double distance)
{
double oldX = XPos;
double oldY = YPos;
double angle = DegToRad(this.Angle);

YPos = oldY + distance * Math.Cos(angle);
XPos = oldX + distance * Math.Sin(angle);

if (Pen)
{
myDisplay.DrawLine (
(int) Math.Round(HomeYPos + oldY),
(int) Math.Round(HomeXPos + oldX),
(int) Math.Round(HomeYPos + YPos),
(int) Math.Round(HomeXPos + XPos)
);

//m
}

}

public void Left(double angleDelta)
{
Angle -= angleDelta;
}

public void Right(double angleDelta)
{
Angle += angleDelta;
}

public override string ToString ()
{
return string.Format ("[Turtle: XPos={0}, YPos={1}, Angle={2}]", XPos, YPos, Angle);
}
}
}


Display.cs - Allows you to draw to your console
Code: [Select]
using System;

namespace Fractal
{
public class Display
{
public const string PIXEL_ON = "\u2588\u2588";
public const string PIXEL_OFF = "  ";
public const int PIXEL_CHARS = 2;

public int Height { get; set; }
public int Width { get; set; }

public bool[,] DisplayBuffer;

public Display()
{
SetDisplay ();
}

public void SetDisplay ()
{
Width = Console.BufferWidth / PIXEL_CHARS;
Height = Console.BufferHeight - 2;

DisplayBuffer = new bool[Height, Width];

for (int i = 0; i < DisplayBuffer.Length; i++) {
DisplayBuffer [(int) i/Width, i%Width] = false;
}

}

public void SetPixel (int y, int x, bool state)
{
if (x > 0 && y > 0 && x < Width && y < Height)
DisplayBuffer [y, x] = state;
}

public void DrawLine(int x1, int y1, int x2, int y2) {
int w = x2 - x1;
int h = y2 - y1;

int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;

if (w<0) dx1 = -1 ; else if (w>0) dx1 = 1 ;
if (h<0) dy1 = -1 ; else if (h>0) dy1 = 1 ;
if (w<0) dx2 = -1 ; else if (w>0) dx2 = 1 ;

int longest  = Math.Abs(w) ;
int shortest = Math.Abs(h) ;

if (!(longest>shortest)) {
longest = Math.Abs(h) ;
shortest = Math.Abs(w) ;
if (h<0) dy2 = -1 ; else if (h>0) dy2 = 1 ;
dx2 = 0 ;           
}

int numerator = longest >> 1;

for (int i=0; i<=longest; i++) {
SetPixel(x1 , y1, true) ;
numerator += shortest ;
if (!(numerator<longest)) {
numerator -= longest ;
x1 += dx1 ;
y1 += dy1 ;
} else {
x1 += dx2 ;
y1 += dy2 ;
}
}
}

public void UpdateDisplay()
{
Console.SetCursorPosition (0, 0);

for (int y = 0; y < Height; y++) {
for (int x = 0; x < Width; x++) {
string pixel = DisplayBuffer[y, x] ? PIXEL_ON : PIXEL_OFF;
Console.Write (pixel);
}
Console.WriteLine ();
}
}

}
}


Now, here is what it looks like:

Of course, I set my terminal to have very very many rows and columns so I can simulate pixels. (however you can change the code to work on a 'lower' resolution)