Last active
August 3, 2016 20:01
-
-
Save JoshCheek/e2935c81e1477868178a1db517d99b2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Tweetable version: https://twitter.com/josh_cheek/status/760926396403310596 | |
# Move with vim keybindings | |
# Press `d` to quit | |
# If your terminal gets horked (like you can't see what you're typing, or the cursor or whatever) | |
# Then press return to clear the buffer, and type `stty cooked`, return once again to submit it | |
stty raw;ruby -e'b=[2,?_,8,5,3,4,i=1,7,6];loop{$><<"\e[H\e[2J"+"%s%s%s\r\n"*3%b;o=[$1,3,-3,1,-1][$<.getc.ord%5];b[i],b[i+=o]=b[i+o],b[i]}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby -rio/console -e' | |
print "\e[?25l" # hide cursor | |
at_exit { print "\e[?25h\n" } # show cursor | |
square_format = "\e[48;5;145m%2s\e[47m" | |
space = square_format % " " | |
tiles = (1..15).map { |n| square_format % n } | |
solved = [*tiles, space] | |
board = solved.shuffle | |
loop do | |
rows, cols = $stdin.winsize | |
blank_row = [" ", " ", " ", " "] | |
print "\e[H\e[2J\e[#{rows/2-3};#{cols/2-7}H", # clear the scren, center the board | |
[blank_row, board[0, 4], blank_row, board[4, 4], blank_row, board[8, 4], blank_row, board[12, 4], blank_row] | |
.map { |slice| "\e[30;47m %s %s %s %s \e[0m\e[B\e[18D" % slice } | |
.join | |
break if board == solved | |
index = board.index(space) | |
swap = -> i1, i2 { board[i1], board[i2] = board[i2], board[i1] } | |
row, col = index/4, index%4 | |
# Arrow keys | vim (hjkl) | readline/emacs (C-p C-n C-f C-b) | Gamer (wasd) | |
case $stdin.raw { |s| s.readpartial 1024 } | |
when"\e[A", "k", 16.chr, "w" then 0 < row && swap[index, index-4] | |
when"\e[B", "j", 14.chr, "s" then row < 3 && swap[index, index+4] | |
when"\e[C", "l", 6.chr, "d" then col < 3 && swap[index, index+1] | |
when"\e[D", "h", 2.chr, "a" then 0 < col && swap[index, index-1] | |
when 3.chr, 4.chr then break # C-c (interrupt) and C-d (eof) | |
end | |
end' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby -rio/console -e'e=?\e+?[;m=e+"40m \e[47m";E=[" "]*3;R=?1..?8;b=[*R,m].shuffle;loop{i=b.index m; print"\e[H\e[2J",[*E,*b,*E].each_slice(3).map{|s|e+"30;47m #{s.map{|c|" #{c} "}.join} \e[0m"}.join("\r\n");b==[*R,m]&&break;k=->o{b[i],b[i+o]=b[i+o],b[i]};case$stdin.raw{|s|s.readpartial 3}when"\e[A";2<i&&k[-3];when"\e[B";i<6&&k[3];when"\e[C";i%3<2&&k[1];when"\e[D";0<i%3&&k[-1];when 3.chr;break;end}' | |
ruby -rio/console -e'm="\e[40m \e[47m";E=[" "]*3;R=?1..?8;b=[*R,m].shuffle;loop{i=b.index m; print"\e[H\e[2J",[*E,*b,*E].each_slice(3).map{|s|"\e[30;47m #{s.map{|c|" #{c} "}.join} \e[0m"}.join("\n");b==[*R,m]&&break;k=->o{b[i],b[i+o]=b[i+o],b[i]};case$stdin.raw{|s|s.readpartial 3}when"\e[A";2<i&&k[-3];when"\e[B";i<6&&k[3];when"\e[C";i%3<2&&k[1];when"\e[D";0<i%3&&k[-1];when 3.chr;break;end}' | |
ruby -rio/console -e'm=?_;R=?1..?8;b=[*R,m].shuffle;loop{i=b.index m; print"\e[H\e[2J",b.each_slice(3).map{|s|s.map{|c|" #{c} "}.join}.join("\n");b==[*R,m]&&break;k=->o{b[i],b[i+o]=b[i+o],b[i]};case$stdin.raw{|s|s.readpartial 3}when"\e[A";2<i&&k[-3];when"\e[B";i<6&&k[3];when"\e[C";i%3<2&&k[1];when"\e[D";0<i%3&&k[-1];when 3.chr;break;end}' | |
ruby -rio/console -e'm=?_;b=[*1..8,m].shuffle;until print "\e[H\e[2J",b.each_slice(3).map{|s|s.map{|c|" #{c} "}.join}.join("\n") or b==[*1..8,m]; i=b.index m;k=->o{b[i],b[i+o]=b[i+o],b[i]};case$stdin.raw{|s|s.readpartial 3}when"\e[A";2<i&&k[-3];when"\e[B";i<6&&k[3];when"\e[C";i%3<2&&k[1];when"\e[D";0<i%3&&k[-1];when 3.chr;break;end;end' | |
ruby -e'`stty raw`;m=?_;b=[*1..8,m].shuffle;until print"\e[H\e[2J",b.each_slice(3).map{|s|s.join" "}.join("\r\n")or[*1..8,m]==b;i=b.index m;k=->o{b[i],b[i+o]=b[i+o],b[i]};case$stdin.getc;when"k";2<i&&k[-3];when"j";i<6&&k[3];when"l";i%3<2&&k[1];when"h";0<i%3&&k[-1];when 3.chr;break;end;end' | |
stty raw;ruby -e'b=[*1..8,?_].shuffle;until print"\e[H\e[2J%s%s%s\r\n%s%s%s\r\n%s%s%s\r\n"%b or[*1..8,?_]==b;i=b.index ?_;k=->o{b[i],b[i+o]=b[i+o],b[i]};case$stdin.getc;when"k";2<i&&k[-3];when"j";i<6&&k[3];when"l";i%3<2&&k[1];when"h";0<i%3&&k[-1];when 3.chr;break;end;end' | |
stty raw;ruby -e'b=[*1..8,?_].shuffle;until print"\e[H\e[2J"+"%s%s%s\r\n"*3%b or[*1..8,?_]==b;i=b.index ?_;k=->o{b[i],b[i+o]=b[i+o],b[i]};case$stdin.getc;when"k";2<i&&k[-3];when"j";i<6&&k[3];when"l";i%3<2&&k[1];when"h";0<i%3&&k[-1];when 3.chr;break;end;end' | |
stty raw;ruby -e'b=[*1..8,?_].shuffle;until print"\e[H\e[2J"+"%s%s%s\r\n"*3%b or[*1..8,?_]==b;i=b.index ?_;c=$stdin.getc;k=->d,o{b[i],b[i+o]=b[i+o],b[i]if c==d};2<i&&k[?k,-3];i<6&&k[?j,3];i%3<2&&k[?l,1];0<i%3&&k[?h,-1];break if c==3.chr;end' | |
stty raw;ruby -e'b=[*1..8,?_].shuffle;until print"\e[H\e[2J"+"%s%s%s\r\n"*3%b or[*1..8,?_]==b;i=b.index ?_;c=$stdin.getc;k=->d,o{b[i],b[i+o]=b[i+o],b[i]if c==d};2<i&&k[?k,-3];i<6&&k[?j,3];i%3<2&&k[?l,1];0<i%3&&k[?h,-1];c==3.chr&&break;end' | |
stty raw;ruby -e'b=[*1..8,?_].shuffle;until$><<"\e[H\e[2J"+"%s%s%s\r\n"*3%b&&[*1..8,?_]==b;i=b.index ?_;c=$stdin.getc;k=->d,o{b[i],b[i+o]=b[i+o],b[i]if c==d};2<i&&k[?k,-3];i<6&&k[?j,3];i%3<2&&k[?l,1];0<i%3&&k[?h,-1];c==3.chr&&break;end' | |
stty raw;ruby -e'b=[*1..8,?_].shuffle;loop{$><<"\e[H\e[2J"+"%s%s%s\r\n"*3%b;[*1..8,?_]!=b&&(c=$stdin.getc)!=3.chr||break;i=b.index ?_;k=->d,o{b[i],b[i+o]=b[i+o],b[i]if c==d};2<i&&k[?k,-3];i<6&&k[?j,3];i%3<2&&k[?l,1];0<i%3&&k[?h,-1]}' | |
stty raw;ruby -e'i=8;b=[*1..8].shuffle<<?_;loop{$><<"\e[H\e[2J"+"%s%s%s\r\n"*3%b;o={k:-3,j:3,l:1,h:-1}[$stdin.getc.intern]or break;b[i],b[i+=o]=b[i+o],b[i]}' | |
stty raw;ruby -e'i=8;b=[*1..8].shuffle<<?_;loop{$><<"\e[H\e[2J"+"%s%s%s\r\n"*3%b;o={k:-3,j:3,l:1,h:-1}[STDIN.getc.intern]or break;b[i],b[i+=o]=b[i+o],b[i]}' | |
stty raw;ruby -e'b=[?_,2,8,5,3,4,1,7,6];loop{$><<"\e[H\e[2J"+"%s%s%s\r\n"*3%b;o={k:-3,j:3,l:1,h:-1}[$<.getc.intern]or break;b[$.],b[$.+=o]=b[$.+o],b[$.]}' | |
stty raw;ruby -e'b=[2,?_,8,5,3,4,i=1,7,6];loop{$><<"\e[H\e[2J"+"%s%s%s\r\n"*3%b;o={k:-3,j:3,l:1,h:-1}[$<.getc.intern];b[i],b[i+=o]=b[i+o],b[i]}' | |
stty raw;ruby -e'b=[2,?_,8,5,3,4,i=1,7,6];loop{$><<"\e[H\e[2J"+"%s%s%s\r\n"*3%b;o=[3,-3,1,-1,$1][$<.getc.ord%5-1];b[i],b[i+=o]=b[i+o],b[i]}' | |
stty raw;ruby -e'b=[2,?_,8,5,3,4,i=1,7,6];loop{$><<"\e[H\e[2J"+"%s%s%s\r\n"*3%b;o=[$1,3,-3,1,-1][$<.getc.ord%5];b[i],b[i+=o]=b[i+o],b[i]}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment