Author Topic: 60 lines Binary Puzzle in php and ncurses  (Read 6762 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
60 lines Binary Puzzle in php and ncurses
« on: July 29, 2013, 12:42:20 am »
I decided to try implementing a binary puzzle as well, but in php and ncurses and, to my amazement, I managed to do it under 75 lines of code. Paste this in a php file, uncomment the ncurses and gmp extentions in php.ini and run this in command line. Supports color, multiple grid sizes, multiple puzzles and all the conditions to win! Also optionally takes a puzzle set filename and puzzle number as argument.

puzzle.php:
Code: [Select]
<?php // 75 lines Binary Puzzle by Juju v1.0 | http://juju2143.ca | Licensed under GPLv3
$puzzles explode("\n"trim(file_get_contents(isset($argv[1])?$argv[1]:"puzzles.dat"), "\n\r"));
function 
initpuzzle($no)
{
        global 
$puzzles,$puzzle,$field,$x,$y,$size;
        
$puzzle explode("#"trim($puzzles[$no-1], "\n\r"), 2);
        
$field $puzzle[0];
        
$size sqrt(strlen($puzzle[0]));
        if(
$size != round($size) || $size%2==1)
        {
                
ncurses_end();
                die(
"Error: Puzzle #".$no." is invalid\n");
        }
        
$size = (int)$size;
        
$x $y 0;
        
ncurses_color_set(0);
        
ncurses_erase();
        
ncurses_addstr("Puzzle #".$no.(isset($puzzle[1])?(" - ".$puzzle[1]):"")."\n");
        for(
$i=0;$i<$size*$size;$i+=$size)
                
ncurses_addstr(substr($puzzle[0],$i,$size)."\n");
        
ncurses_move(1,0);
        
ncurses_color_set(1);
}
function 
checkline($str)
{
        if(
strstr($str" ")!==false) return false;
        if(
strstr($str"000")!==false) return false;
        if(
strstr($str"111")!==false) return false;
        return 
gmp_popcount(gmp_init($str2)) == strlen($str)/2;
}
$continue true;
$puzzleno = (isset($argv[2])&&$argv[2]>=1&&$argv[2]<=count($puzzles))?$argv[2]:1;
ncurses_init();
ncurses_start_color();
ncurses_init_pair(1NCURSES_COLOR_REDNCURSES_COLOR_BLACK);
ncurses_noecho();
initpuzzle($puzzleno);
while(
$continue)
{
        
$char ncurses_getch();
        if(
$char == 32)
        {
                
$pos=$y*$size+$x;
                if(
$puzzle[0][$pos] == " ")
                {
                        switch(
$field[$pos])
                        {
                                case 
"0"$field[$pos] = "1"; break;
                                case 
"1"$field[$pos] = " "; break;
                                case 
" "$field[$pos] = "0"; break;
                        }
                        
ncurses_addstr($field[$pos]);
                        
$points 0;
                        
$valh $valv = Array();
                        for(
$i=0;$i<$size;$i++)
                        {
                                
$vline ""; for($j=0;$j<$size;$j++) $vline .= $field[$j*$size+$i];
                                
$points += checkline(substr($field,$size*$i,$size)) + checkline($vline);
                                
$valh[] = bindec(substr($field,$size*$i,$size));
                                
$valv[] = bindec($vline);
                        }
                        if(
$points==2*$size && count($valh)==count(array_unique($valh)) && count($valv)==count(array_unique($valv)))
                        {
                                
ncurses_move($size+1,0);
                                
ncurses_addstr("You won!");
                        }
                }
        }
        if(
$char == ord("p") && $puzzleno 1initpuzzle(--$puzzleno);
        if(
$char == ord("n") && $puzzleno count($puzzles)) initpuzzle(++$puzzleno);
        if(
$char == ord("r")) initpuzzle($puzzleno);
        if(
$char == ord("q")) $continue false;
        
ncurses_move(($y=$y+($char==258&&$y<$size-1)-($char==259&&$y>0))+1,$x=$x+($char==261&&$x<$size-1)-($char==260&&$x>0));
        
ncurses_refresh(null);
}
ncurses_end();

puzzles.dat:
Code: [Select]
1  0    00 1 00  1      00 1   1  00#6x6 Easy 1
  0  11 00   1        1   1  000 1  #6x6 Easy 2
    00       1    01 1  0   0   1   #6x6 Medium 1
 0    00       0        1 11  1  1  #6x6 Medium 2
       0 00  1   0   1 0  1     00 1  1     1   11   0 1 1     1#8x8 Easy 1
 1 1  0 0 0  1 10  1   1 0         0   11  0 0 1    1   1  0  1 #8x8 Easy 2
    0 0 0 0 0 0             1 1 0  0    0  00          1 1 1    #8x8 Medium 1
   11    1   10       0 0 11   0 11          10     1    0 0   1#8x8 Medium 2

Controls:
Arrow keys: Move
Space: Set value
p: Previous puzzle
n: Next puzzle
r: Restart
q: Quit
« Last Edit: July 29, 2013, 08:22:43 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 blue_bear_94

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 801
  • Rating: +25/-35
  • Touhou Enthusiast / Former Troll / 68k Programmer
    • View Profile
Re: 75 lines Binary Puzzle in php and ncurses
« Reply #1 on: July 29, 2013, 12:55:26 am »
How'd you get the funky syntax coloring?
Due to dissatisfaction, I will be inactive on Omnimaga until further notice. (?? THP hasn't been much success and there's also the CE. I might possibly be here for a while.)
If you want to implore me to come back, or otherwise contact me, I can be found on GitHub (bluebear94), Twitter (@melranosF_), Reddit (/u/Fluffy8x), or e-mail (if you know my address). As a last resort, send me a PM on Cemetech (bluebear94) or join Touhou Prono (don't be fooled by the name). I've also enabled notifications for PMs on Omnimaga, but I don't advise using that since I might be banned.
Elvyna (Sunrise) 4 5%
TI-84+SE User (2.30 2.55 MP 2.43)
TI-89 Titanium User (3.10)
Casio Prizm User? (1.02)
Bag  東方ぷろの

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: 75 lines Binary Puzzle in php and ncurses
« Reply #2 on: July 29, 2013, 01:41:58 am »
Blame the code tag plugin I guess?

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 Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: 75 lines Binary Puzzle in php and ncurses
« Reply #3 on: July 29, 2013, 05:00:47 am »
Why is everypony suddenly making binary puzzels ???
Also, sounds awesome, i should try it :P

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Streetwalrus

  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3821
  • Rating: +80/-8
    • View Profile
Re: 75 lines Binary Puzzle in php and ncurses
« Reply #4 on: July 29, 2013, 05:13:33 am »
In Firefox I can't see the content of your code tags. It just says "Array". Though it works fine in Konqueror. Weird. ???

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: 75 lines Binary Puzzle in php and ncurses
« Reply #5 on: July 29, 2013, 10:26:45 am »
Now it should work. And yeah everyone is doing those lol. But in my case I wanted to see how optimized I could do this.

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 stevon8ter

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 663
  • Rating: +10/-0
    • View Profile
Re: 75 lines Binary Puzzle in php and ncurses
« Reply #6 on: July 29, 2013, 01:13:20 pm »
omg that's small :o

i already have 600 lines...

but this includes a start screen... stage select... level select.. and a custom button class (there is no standard button :/ )
None of my posts are meant offending... I you feel hurt by one of my posts, tell me ... So i can appoligise me and/or explain why i posted it


Hi there, I'm the allmighty (read as: stupid, weak...) STEVON8TER

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: 75 lines Binary Puzzle in php and ncurses
« Reply #7 on: July 29, 2013, 01:20:41 pm »
Yeah, I kept it to the strict minimum, so drawing all the stuff takes like 2 lines. I have a level select though.

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 stevon8ter

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 663
  • Rating: +10/-0
    • View Profile
Re: 75 lines Binary Puzzle in php and ncurses
« Reply #8 on: July 29, 2013, 02:35:23 pm »
well in lua there are a lot of functions i had to write myself (my ipad app works with lua 5.1 ...)

like all the bitmath functions... i had to write the myself
None of my posts are meant offending... I you feel hurt by one of my posts, tell me ... So i can appoligise me and/or explain why i posted it


Hi there, I'm the allmighty (read as: stupid, weak...) STEVON8TER

Offline harold

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 226
  • Rating: +41/-3
    • View Profile
Re: 75 lines Binary Puzzle in php and ncurses
« Reply #9 on: July 29, 2013, 02:47:04 pm »
Lol I have 2900 lines of C#, 500 lines of C++ and 60 lines of assembly, and that's not counting anything that has to do with user interaction (there's not a lot of code there, maybe 200 lines).
About 1300 lines have to do with tutorial mode, but those are also used in puzzle generation (to make sure that lower-level puzzles don't require hard steps, as well as to ensure that higher-level puzzles do require hard steps).
Most of the rest is puzzle generation.
« Last Edit: July 29, 2013, 02:47:51 pm by harold »
Blog about bitmath: bitmath.blogspot.nl
Check the haroldbot thread for the supported commands and syntax.
You can use haroldbot from this website.

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: 60 lines Binary Puzzle in php and ncurses
« Reply #10 on: July 29, 2013, 08:17:40 pm »
Slimmed it down to less than 60 lines while adding mouse support. Now it's just damn crazy >:D

Code: [Select]
<?php // 60 lines Binary Puzzle v1.1 by Juju | http://juju2143.ca | Licensed under GPLv3
$puzzles explode("\n"trim(file_get_contents(isset($argv[1])?$argv[1]:"puzzles.dat"), "\n\r"));
function 
initpuzzle($no){
        global 
$puzzles,$puzzle,$field,$x,$y,$size;
        
$puzzle explode("#"trim($puzzles[$no-1], "\n\r"), 2);
        
$field $puzzle[0];
        
$size sqrt(strlen($puzzle[0]));
        if(
$size != round($size) || $size%2==1){ncurses_end(); die("Error: Puzzle #".$no." is invalid\n");}
        
$size = (int)$size$x $y 0;
        
ncurses_color_set(0);
        
ncurses_erase();
        
ncurses_addstr("Puzzle #".$no." - ".$size."x".$size.(isset($puzzle[1])?(" - ".$puzzle[1]):"")."\n");
        for(
$i=0;$i<$size*$size;$i+=$sizencurses_addstr(substr($puzzle[0],$i,$size)."\n");
        
ncurses_move(1,0);
        
ncurses_color_set(1);
}
function 
checkline($str){
        if(
strstr($str" ")!==false||strstr($str"000")!==false||strstr($str"111")!==false) return false;
        return 
gmp_popcount(gmp_init($str2)) == strlen($str)/2;
}
$continue true;
$puzzleno = (isset($argv[2])&&$argv[2]>=1&&$argv[2]<=count($puzzles))?$argv[2]:1;
ncurses_init();
ncurses_start_color();
ncurses_init_pair(1NCURSES_COLOR_REDNCURSES_COLOR_BLACK);
ncurses_noecho();
ncurses_mousemask(NCURSES_BUTTON1_CLICKED$oldmask);
initpuzzle($puzzleno);
while(
$continue){
        
$char ncurses_getch(); $clicked false;
        if(
$char == NCURSES_KEY_MOUSE){
                
ncurses_getmouse($e);
                if(
$e["mmask"]&NCURSES_BUTTON1_CLICKED&&$e["x"]>=0&&$e["x"]<$size&&$e["y"]>0&&$e["y"]<=$size){
                        
ncurses_move(($y=$e["y"]-1)+1$x=$e["x"]); $clicked=true;
                }
        }
        if((
$char == 32 || $clicked) && $puzzle[0][$pos=$y*$size+$x] == " "){
                switch(
$field[$pos]){
                        case 
"0"$field[$pos] = "1"; break;
                        case 
"1"$field[$pos] = " "; break;
                        case 
" "$field[$pos] = "0"; break;
                }
                
ncurses_addstr($field[$pos]);
                
$points 0$valh $valv = Array();
                for(
$i=0;$i<$size;$i++){
                        
$valv[$i]=""; for($j=0;$j<$size;$j++) $valv[$i] .= $field[$j*$size+$i];
                        
$points += checkline($valh[$i]=substr($field,$size*$i,$size)) + checkline($valv[$i]);
                }
                if(
$points==2*$size&&count($valh)==count(array_unique($valh))&&count($valv)==count(array_unique($valv))){
                        
ncurses_move($size+1,0); ncurses_addstr("You won!");
                }
        }
        if(
$char == ord("p") && $puzzleno 1initpuzzle(--$puzzleno);
        if(
$char == ord("n") && $puzzleno count($puzzles)) initpuzzle(++$puzzleno);
        if(
$char == ord("r")) initpuzzle($puzzleno);
        if(
$char == ord("q")) $continue false;
        
ncurses_move(($y=$y+($char==258&&$y<$size-1)-($char==259&&$y>0))+1,$x=$x+($char==261&&$x<$size-1)-($char==260&&$x>0));
        
ncurses_refresh(null);
}
ncurses_end();

EDIT: DJ Omnimaga, just for you I condensed this in one line:
Code: [Select]
<?php $puzzles=explode("\n",trim(file_get_contents(isset($argv[1])?$argv[1]:"puzzles.dat"),"\n\r"));function initpuzzle($no){global $puzzles,$puzzle,$field,$x,$y,$size;$puzzle=explode("#",trim($puzzles[$no-1],"\n\r"),2);$field=$puzzle[0];$size=sqrt(strlen($puzzle[0]));if($size!=round($size)||$size%2==1){ncurses_end();die("Error: Puzzle #".$no." is invalid\n");}$size=(int)$size;$x=$y=0;ncurses_color_set(0);ncurses_erase();ncurses_addstr("Puzzle #".$no." - ".$size."x".$size.(isset($puzzle[1])?(" - ".$puzzle[1]):"")."\n");for($i=0;$i<$size*$size;$i+=$size)ncurses_addstr(substr($puzzle[0],$i,$size)."\n");ncurses_move(1,0);ncurses_color_set(1);}function checkline($str){if(strstr($str," ")!==false||strstr($str,"000")!==false||strstr($str,"111")!==false)return false;return gmp_popcount(gmp_init($str,2))==strlen($str)/2;}$continue=true;$puzzleno=(isset($argv[2])&&$argv[2]>=1&&$argv[2]<=count($puzzles))?$argv[2]:1;ncurses_init();ncurses_start_color();ncurses_init_pair(1,NCURSES_COLOR_RED,NCURSES_COLOR_BLACK);ncurses_noecho();ncurses_mousemask(NCURSES_BUTTON1_CLICKED,$oldmask);initpuzzle($puzzleno);while($continue){$char=ncurses_getch();$clicked=false;if($char==NCURSES_KEY_MOUSE){ncurses_getmouse($e);if($e["mmask"]&NCURSES_BUTTON1_CLICKED&&$e["x"]>=0&&$e["x"]<$size&&$e["y"]>0&&$e["y"]<=$size){ncurses_move(($y=$e["y"]-1)+1,$x=$e["x"]);$clicked=true;}}if(($char==32||$clicked)&&$puzzle[0][$pos=$y*$size+$x]==" "){switch($field[$pos]){case "0":$field[$pos]="1";break;case "1":$field[$pos]=" ";break;case" ":$field[$pos]="0";break;}ncurses_addstr($field[$pos]);$points=0;$valh=$valv=Array();for($i=0;$i<$size;$i++){$valv[$i]="";for($j=0;$j<$size;$j++)$valv[$i].=$field[$j*$size+$i];$points+=checkline($valh[$i]=substr($field,$size*$i,$size))+checkline($valv[$i]);}if($points==2*$size&&count($valh)==count(array_unique($valh))&&count($valv)==count(array_unique($valv))){ncurses_move($size+1,0);ncurses_addstr("You won!");}}if($char==ord("p")&&$puzzleno>1)initpuzzle(--$puzzleno);if($char==ord("n")&&$puzzleno<count($puzzles))initpuzzle(++$puzzleno);if($char==ord("r"))initpuzzle($puzzleno);if($char==ord("q"))$continue=false;ncurses_move(($y=$y+($char==258&&$y<$size-1)-($char==259&&$y>0))+1,$x=$x+($char==261&&$x<$size-1)-($char==260&&$x>0));ncurses_refresh(null);}ncurses_end();
« Last Edit: July 29, 2013, 09:31:33 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 DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: 60 lines Binary Puzzle in php and ncurses
« Reply #11 on: July 29, 2013, 09:27:49 pm »
Lol nice one juju. :P I really meant making the code 60 times smaller, though. But of course I was kidding, since I'm not even sure if that's possible. :P

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: 60 lines Binary Puzzle in php and ncurses
« Reply #12 on: July 30, 2013, 01:05:35 am »
I think a better comparison would be how many bytes does it use up because it could be written in one gigantic line.
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline stevon8ter

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 663
  • Rating: +10/-0
    • View Profile
Re: 60 lines Binary Puzzle in php and ncurses
« Reply #13 on: July 30, 2013, 03:40:21 am »
lolol, nice one juju xd
None of my posts are meant offending... I you feel hurt by one of my posts, tell me ... So i can appoligise me and/or explain why i posted it


Hi there, I'm the allmighty (read as: stupid, weak...) STEVON8TER