node-marshal 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,68 @@
1
+ require_relative '../lib/node-marshal.rb'
2
+ require 'test/unit'
3
+
4
+ # Set of tests for Convay's life game running and compilation
5
+ # (includes tests with binary Ruby files)
6
+ class TestLifeGame < Test::Unit::TestCase
7
+ # Compile and launch life game with different
8
+ # compiler options (requires zlib library)
9
+
10
+ def test_compile
11
+ compile_with_opts(nil)
12
+ compile_with_opts(:compress=>true)
13
+ end
14
+ # Test life game with glider gun configuration
15
+ def test_glider_gun
16
+ # Compile the class to the instuction sequence
17
+ bin = NodeMarshal.new(:srcfile, 'lifegame.rb')
18
+ puts bin.inspect
19
+ bin.compile.eval
20
+ # Calculate the life game
21
+ res_node = run_game
22
+ # Play the game of life without NodeMarshal
23
+ Object.send(:remove_const, :LifeGame)
24
+ load('lifegame.rb')
25
+ res_load = run_game
26
+ # Compare the results
27
+ assert_equal(res_node, res_load)
28
+ Object.send(:remove_const, :LifeGame)
29
+ puts res_load
30
+ end
31
+
32
+ # Runs Life game initialized with "glider gun" configuration
33
+ # and makes 75 turns (changes of generations)
34
+ def run_game
35
+ g = LifeGame::Grid.new(25, 80)
36
+ g.cfg_glider!
37
+ g.cfg_glider_gun!
38
+ 75.times {g.make_step!}
39
+ g.to_ascii
40
+ end
41
+
42
+ # Compiles Convay's life game using proposed options
43
+ # (opts is a Hash, see NodeMarshal#compile_rb_file method)
44
+ def compile_with_opts(opts)
45
+ stub = <<-EOS
46
+ g = LifeGame::Grid.new(25, 80)
47
+ g.cfg_glider_gun!
48
+ 75.times {g.make_step!}
49
+ File.open('life.res', 'w') {|fp| fp << g.to_ascii }
50
+ EOS
51
+ # Compile Life game to the file and obtain the result
52
+ if opts == nil
53
+ NodeMarshal.compile_rb_file('lifegame_bin.rb', 'lifegame.rb')
54
+ else
55
+ NodeMarshal.compile_rb_file('lifegame_bin.rb', 'lifegame.rb', opts)
56
+ end
57
+ txt = File.read('lifegame_bin.rb') + stub
58
+ File.open('lifegame_bin.rb', 'w') {|fp| fp << txt }
59
+ `ruby lifegame_bin.rb`
60
+ res_file = File.read('life.res')
61
+ `rm life.res`
62
+ # Run the life game without compilation to the file
63
+ load('lifegame.rb')
64
+ res_load = run_game
65
+ Object.send(:remove_const, :LifeGame)
66
+ assert_equal(res_file, res_load)
67
+ end
68
+ end
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/ruby
2
+ # Simple TETRIS game for Ruby 1.9.x that uses CURSES library
3
+ # (C) 2013 Alexey Voskov
4
+ require "curses"
5
+ include Curses
6
+ # TETRIS figure
7
+ class Figure
8
+ attr_accessor :mat, :x, :y, :rot
9
+ FIGURES = [0x0F00,0x0660,0x0270,0x0170,0x0470,0x0360,0x0C60] # Line, square, T, L, L, Z, Z
10
+ def initialize
11
+ fig = FIGURES[rand(6).round]
12
+ rf = ->bf{Array.new(4) {|y| Array.new(4) {|x| (1 << bf.(x,y)) & fig }}}
13
+ @mat = [rf.(->x,y{3-x+y*4}), rf.(->x,y{3-y+(3-x)*4}),
14
+ rf.(->x,y{x+(3-y)*4}), rf.(->x,y{y+x*4})]
15
+ @x, @y, @rot = 5, 0, 0 # Figure position
16
+ end
17
+ def each_pos &block
18
+ (0..3).each {|y| (0..3).each {|x| block.(y,x) if @mat[@rot][y][x] > 0}}
19
+ end
20
+ # Test the possibility of move and move if possible
21
+ def move!(op, unop, scr)
22
+ self.each_pos {|y,x| scr.brick!(y+@y,x+@x,GameField::COL_EMPTY) }
23
+ op.(self)
24
+ unop.(self) if !(ans = scr.place?self)
25
+ self.each_pos {|y,x| scr.brick!(y+@y,x+@x,GameField::COL_BRICK) }
26
+ refresh
27
+ ans
28
+ end
29
+ end
30
+ # TETRIS game fields
31
+ class GameField
32
+ WIDTH, HEIGHT, BRICK_WIDTH, COL_EMPTY, COL_WALL, COL_BRICK = 16, 26, 2, 0, 1, 2
33
+ def initialize # Initialize and show the game fields
34
+ @m = Array.new(HEIGHT+2) {[0,0,1,Array.new(WIDTH-6){0},1,0,0].flatten}
35
+ (2..WIDTH-3).each {|i| @m[HEIGHT-1][i] = COL_WALL}
36
+ lines!
37
+ end
38
+ def brick!(y,x,c) # Show the brick on the screen
39
+ @m[y][x] = c.to_i
40
+ return nil if y-3 <= 0
41
+ setpos(y-3,x*BRICK_WIDTH)
42
+ addstr([". ", "**", "[]"][c])
43
+ end
44
+ def place?(fig) # Check if the figure can be placed
45
+ fig.each_pos {|y,x| return false if @m[y+fig.y][x+fig.x] > 0}
46
+ return true
47
+ end
48
+ def lines! # Erase full lines from the screen
49
+ while (ind = @m[0..HEIGHT-2].index {|s| s[3..WIDTH-4].index(COL_EMPTY) == nil}) != nil
50
+ (ind-1).step(0,-1) {|i| @m[i+1] = @m[i]}
51
+ @m[0] = @m[HEIGHT+1].dup
52
+ end
53
+ (0..HEIGHT-1).each {|y| (2..WIDTH-3).each {|x| brick!(y,x,@m[y][x])}} # Update the screen (field + borders)
54
+ end
55
+ end
56
+ # Initialize the console and program data
57
+ init_screen; clear; noecho; stdscr.keypad(true)
58
+ fig, scr, speed = Figure.new, GameField.new, 1
59
+ # Keyboard control thread
60
+ keythread = Thread.new { loop {
61
+ case getch
62
+ when Key::LEFT then fig.move!(->f{f.x -=1}, ->f{f.x +=1}, scr)
63
+ when Key::RIGHT then fig.move!(->f{f.x +=1}, ->f{f.x -=1}, scr)
64
+ when Key::UP then fig.move!(->f{f.rot = (f.rot+1)%4}, ->f{f.rot = (f.rot+3)%4},scr)
65
+ when Key::DOWN then speed = 0.05 # Delay for fast falling
66
+ end
67
+ }}
68
+ # Game main loop (new figure creation + falling)
69
+ begin
70
+ fig, speed = Figure.new, 0.5 # Figure and delay for its normal falling
71
+ sleep(speed) while fig.move!(->f{f.y +=1}, ->f{f.y -=1}, scr)
72
+ scr.lines!
73
+ end until fig.y == 0
74
+ # Finish the game
75
+ keythread.kill
76
+ setpos(GameField::HEIGHT/2,GameField::WIDTH-4)
77
+ addstr("GAME OVER!")
78
+ getch
79
+ close_screen
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: node-marshal
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexey Voskov
8
+ autorequire:
9
+ bindir:
10
+ - bin
11
+ cert_chain: []
12
+ date: 2015-05-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: "This gem is designed for transformation of Ruby source code (eiher in
15
+ the form of files or strings) to the \nRuby nodes (syntax trees) used by Ruby MRI
16
+ internals. Obtained nodes can be serialized to the platform-dependent\nbinary or
17
+ ASCII strings and restored and launched from serialized format. Such kind of transformation
18
+ is\nirreversible and can be used for source code protection; the similar principle
19
+ is used by RubyEncoder commercial\nsoftware.\n"
20
+ email: alvoskov@gmail.com
21
+ executables:
22
+ - noderbc
23
+ extensions:
24
+ - ext/node-marshal/extconf.rb
25
+ extra_rdoc_files:
26
+ - README.rdoc
27
+ files:
28
+ - README.rdoc
29
+ - bin/noderbc
30
+ - bin/noderbc.bat
31
+ - ext/node-marshal/COPYING
32
+ - ext/node-marshal/base85r.c
33
+ - ext/node-marshal/extconf.rb
34
+ - ext/node-marshal/libobj/readme.txt
35
+ - ext/node-marshal/node193.h
36
+ - ext/node-marshal/node220.h
37
+ - ext/node-marshal/nodedump.c
38
+ - ext/node-marshal/nodedump.h
39
+ - ext/node-marshal/nodeinfo.c
40
+ - lib/node-marshal.rb
41
+ - test/lifegame.rb
42
+ - test/test_base.rb
43
+ - test/test_complex.rb
44
+ - test/test_lifegame.rb
45
+ - test/tinytet.rb
46
+ homepage:
47
+ licenses:
48
+ - 2-clause BSD
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options:
52
+ - "--main"
53
+ - README.rdoc
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 1.9.3
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.4.5
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Transforms Ruby sources to binary nodes (trees) that can be saved and loaded
72
+ test_files: []