Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: nrkn/SimpleRLGolf
base: b2de6a0926
...
head fork: nrkn/SimpleRLGolf
compare: e6c6bbf7ca
Checking mergeability… Don't worry, you can still create the pull request.
  • 6 commits
  • 2 files changed
  • 0 commit comments
  • 2 contributors
Commits on Mar 21, 2011
@chron chron Can use % on the keycode instead of subtracting Curses::KEY_DOWN to s…
…ave 2 more characters.
aab1689
Commits on Mar 01, 2012
@chron chron Update ruby/SimpleRLGolf.rb 3e31c71
@chron chron Replaced ==' ' with <?# to save 2 characters. 8ddadec
Commits on Mar 02, 2012
@chron chron Assorted changes, bytecount now at 243:
* replaced while ... end with loop{}
* replaced d() method with a 1.9 style "stabby lambda"
* because of d being in scope now, changed $p to local var
* implicit receiver for getch
* use << instead of addstr and chain it off the initialization
* replaced inline if-statement with a ternary expression
* moved the clear (d[32]) call just after the getch to get fix redraw timing
9b975be
@chron chron Updated to match the latest version of the golfed code. f4eb8a6
@nrkn Merge pull request #6 from chron/master
Updated ruby version, 243 bytes
e6c6bbf
Showing with 41 additions and 39 deletions.
  1. +4 −7 ruby/SimpleRLGolf.rb
  2. +37 −32 ruby/SimpleRLGolfPretty.rb
View
11 ruby/SimpleRLGolf.rb
@@ -1,8 +1,5 @@
-require 'curses';include Curses
-def d t=?@;setpos $p/10,$p%10;addch t;end
-s=init_screen;noecho;curs_set 0;s.keypad 1
+require 'curses';include Curses;noecho;curs_set 0
b=0xf3e798070340902c0e019e7cf.to_s(2).tr'01',' #'
-addstr b.scan(/.{10}/)*$/;$p=22;d
-while v=$p+[10,-10,-1,1][s.getch-258]
-d 32;$p=v if b[v]==' ';d
-end
+s=init_screen<<b.scan(/.{10}/)*$/;s.keypad 1
+p=22;d=->t=?@{setpos p/10,p%10;addch t;6};d[]
+loop{p=b[v=p+[10,-10,-1,1][getch%d[32]]]<?#?v:p;d[]}
View
69 ruby/SimpleRLGolfPretty.rb
@@ -1,24 +1,9 @@
# Load up the curses library. Using #include allows us to use the module's
-# methods without the Curses:: prefix.
+# methods without the Curses:: prefix. We also do some basic curses init.
require 'curses'
include Curses
-
-# This method draws a single character at the current cursor position. The
-# cursor position is represented by a single integer, you can think of the
-# tens digit representing y-position while the ones digit represents x-position.
-#
-# The character to be drawn defaults to an '@', but another can be specified
-# by either a single-character string or an ascii code.
-def d t=?@
- setpos $p/10, $p%10
- addch t
-end
-
-# Basic curses initialization
-s = init_screen
noecho
curs_set 0
-s.keypad 1
# b is the game board in a one-dimensional array. It is created by taking a
# constant hex value and converting that into its binary representation in
@@ -26,24 +11,44 @@ def d t=?@
# and every 1 is replaced with a hash.
b = 0xf3e798070340902c0e019e7cf.to_s(2).tr '01',' #'
-# Draw the board to the screen, assume rows of 10 characters.
-addstr b.scan(/.{10}/)*$/
+# We create a curses screen and draw the game board to it, by splitting the
+# b string into chunks of 10 characters and joining them together again with
+# a newline character.
+s = init_screen << b.scan(/.{10}/)*$/
-# Set the initial player position to {2,2}, counting from the top-left.
-$p=22
+# We enable the use of the keypad for the newly-created screen.
+s.keypad 1
+
+# We set the initial player position to {2,2}, counting from the top-left.
+p=22
+
+# This lambda draws a character at the Player's current location, which is
+# represented by a single integer (p). You can think of the tens digit
+# representing y-position while the ones digit represents x-position.
+#
+# The character to be drawn defaults to an '@', but another can be specified
+# by either a single-character string or an ascii code.
+#
+# The method returns 6 because it is used as an argument to the modulus call
+# in the main game loop.
+d = -> t=?@ {
+ setpos p/10, p%10
+ addch t
+ 6
+}
-# Draw the player to the screen.
-d
+# Draw the player to his initial starting position.
+d[]
# Main game loop:
+# * Draw a space (ascii 32) over the current player's position using d[32].
# * Get a keypress from curses as an integer keycode.
-# * Subtract 258 (KEY_DOWN) from it and use the result as an index into an
-# array of movement options
-while v=$p+[10,-10,-1,1][s.getch-258]
- # Clear the tile the player is standing on (32 is ascii space)
- d 32
- # If the spot the player wants to move to (v) is clear, update $p
- $p=v if b[v]==' '
- # Redraw the player in the new spot (or old spot if he did not move)
- d
-end
+# * Take that value mod 6 (so KEY_DOWN is , KEY_UP is 1, etc) and use the result
+# as an index into an array of movement options.
+# * Use this movement option to find out the desired target position. Call this v.
+# * If v is accessible (if it is less than '#', so not a # character) update p to it.
+# * Draw the player in whatever position p points to now.
+loop {
+ p = b[v=p+[10,-10,-1,1][getch%d[32]]] < ?# ? v : p
+ d[]
+}

No commit comments for this range

Something went wrong with that request. Please try again.